Spring boot mybatis整合

spring boot这个框架,可以不用配置任何xml,运行web应用程序。里面内嵌了web服务器,tomcat或jetty

最近准备做一个远程部署redis的程序,其中有个模块需要web和数据库,这里选择了spring boot和mybatis

预期效果:

1. 能够通过/index访问到index.jsp

2. 通过spring ioc注入dao对象,通过mybatis操作数据库

解决方案

1. spring mvc实现访问index.jsp

2. 创建instance_config表

3. 写好InstanceConfig实体类

4. 写好instanceConfigDao接口

5. 写好instanceConfigMapper文件

6. 配置好相关的配置文件

7. 运行程序

 

instance_config.sql

CREATE TABLE `instance_config` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `config_key` varchar(128) NOT NULL ,
  `config_value` varchar(512) NOT NULL ,
  `info` varchar(512) NOT NULL ,
  `update_time` datetime NOT NULL ,
  `type` mediumint(9) NOT NULL ,
  `status` tinyint(4) NOT NULL ,
  PRIMARY KEY (`id`),
  UNIQUE KEY `config_key_type` (`config_key`,`type`)
) ENGINE=InnoDB AUTO_INCREMENT=78 DEFAULT CHARSET=utf8;
View Code

InstanceConfig实体类

import java.util.Date;

/**
 * Created by gxf on 2017/7/15.
 */
public class InstanceConfig {

    private long id;

    private String configKey;

    private String configValue;

    private String info;

    private Date updateTime;

    private int type;

    private int status;

    @Override
    public String toString() {
        return "InstanceConfig{" +
                "id=" + id +
                ", configKey='" + configKey + '\'' +
                ", configValue='" + configValue + '\'' +
                ", info='" + info + '\'' +
                ", updateTime=" + updateTime +
                ", type=" + type +
                ", status=" + status +
                '}';
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getConfigKey() {
        return configKey;
    }

    public void setConfigKey(String configKey) {
        this.configKey = configKey;
    }

    public String getConfigValue() {
        return configValue;
    }

    public void setConfigValue(String configValue) {
        this.configValue = configValue;
    }

    public String getInfo() {
        return info;
    }

    public void setInfo(String info) {
        this.info = info;
    }

    public Date getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(Date updateTime) {
        this.updateTime = updateTime;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }
}
View Code

instanceDao接口

import com.gxf.entity.InstanceConfig;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

/**
 * Created by gxf on 2017/7/16.
 */
@Mapper
public interface InstanceConfigDao {

    /**
     * 查询所有记录
     * */
//    @Select("select * from instance_config")
    List<InstanceConfig> queryAll();
}
View Code

mapper文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.gxf.dao.InstanceConfigDao">
    <!--<resultMap id="instanceConfigMap" type="com.gxf.entity.InstanceConfig">-->
        <!--<id property="id" column="id"/>-->
        <!--<result property="configKey" column="config_key"/>-->
        <!--<result property="configValue" column="config_value"/>-->
        <!--<result property="info" column="info"/>-->
        <!--<result property="updateTime" column="update_time"/>-->
        <!--<result property="type" column="type"/>-->
        <!--<result property="status" column="status"/>-->
    <!--</resultMap>-->

    <select id="queryAll" resultType="com.gxf.entity.InstanceConfig">
        select * from instance_config
    </select>
</mapper>
View Code

application.properties

# 配置jsp文件的位置,默认位置为:src/main/webapp
spring.mvc.view.prefix:/WEB-INF/jsp/

# 配置jsp文件的后缀
spring.mvc.view.suffix:.jsp

#mybatis
spring.datasource.url=jdbc:mysql://localhost:3306/db_cloud_redis?serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#mybatis.mapperLocations=classpath:mapper/*.xml
mybatis.config-location=classpath:mybatis-config
View Code

mybatis-config

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <settings>
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>

    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/db_cloud_redis?serverTimezone=UTC"/>
                <property name="username" value="root"/>
                <property name="password" value=""/>
            </dataSource>
        </environment>
    </environments>

    <mappers>
        <mapper resource="mapper/InstanceConfigDao.xml"/>
    </mappers>
</configuration>
View Code

controller

import com.gxf.dao.InstanceConfigDao;
import com.gxf.entity.InstanceConfig;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@SpringBootApplication
@MapperScan("com.gxf.dao")
public class SampleController {

    @Autowired
    private InstanceConfigDao instanceConfigDao;

    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index() {
        List<InstanceConfig> listOfConfig = instanceConfigDao.queryAll();
        for(int i = 0; i < listOfConfig.size(); i++){
            System.out.println(listOfConfig.get(i).getConfigKey());
        }
        return "index";
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(SampleController.class, args);
    }
}
View Code

maven依赖

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.4.RELEASE</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud_web</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>

        <!-- servlet 依赖 -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <scope>provided</scope>
        </dependency>

        <!-- 添加 JSTL 支持 -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.1</version>
        </dependency>
        <!--mybatis-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.1.1</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>6.0.6</version>
        </dependency>
        <dependency>
            <groupId>c3p0</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.1.2</version>
        </dependency>
    </dependencies>
View Code

 

这里spring和mybatis都可以选择,使用xml或者注解进行配置。配置主要目的:

1. spring能够把bean注入到容器中

2. 能够初始化datasource连接数据库

3.能够找对应的dao和mapper配置

 

posted on 2017-07-16 20:18  luckygxf  阅读(306)  评论(0编辑  收藏  举报

导航