SpringBoot整合Mybatis(二)

前言:

    在我的上一篇文章中对SpringBoot整合Maybatis通过java注解的方式进行了记录,这篇文章说一下使用mybatis官方提供的Spring Boot整合包实现。

环境

  版本:SpringBoot1.5.9. RELEASE

整合工程下载

SpringBoot整合Mybatis
注:项目中除了整合Mybatis外,还整合了Redis-cluster集群、Swagger2接口说明

Maven依赖


        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starterartifactId>
        dependency>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-jdbcartifactId>
        dependency>
        <dependency>
            <groupId>org.mybatis.spring.bootgroupId>
            <artifactId>mybatis-spring-boot-starterartifactId>
            <version>1.3.0version>
        dependency>

        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>5.1.35version>
        dependency>
        
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>druidartifactId>
            <version>1.0.11version>
        dependency>
         
        <dependency>
            <groupId>com.github.pagehelpergroupId>
            <artifactId>pagehelper-spring-boot-starterartifactId>
            <version>1.1.2version>
        dependency>
        
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>druid-spring-boot-starterartifactId>
            <version>1.1.0version>
        dependency>

在application.properties中添加配置

########################################mybatis#####################
#mybatis配置文件
spring.datasource.name = test
spring.datasource.url = jdbc:mysql://127.0.0.1:3306/android
spring.datasource.username = root
spring.datasource.password = root
# 使用druid数据源
spring.datasource.type = com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name = com.mysql.jdbc.Driver
spring.datasource.filters = stat
spring.datasource.maxActive = 20
spring.datasource.initialSize: 1
spring.datasource.maxWait = 60000
spring.datasource.minIdle = 1
spring.datasource.timeBetweenEvictionRunsMillis=60000
spring.datasource.minEvictableIdleTimeMillis: 300000
spring.datasource.validationQuery = select 'x'
spring.datasource.testWhileIdle = true
spring.datasource.testOnBorrow = true
spring.datasource.testOnReturn = false
spring.datasource.poolPreparedStatements = true
spring.datasource.maxOpenPreparedStatements = 20
#mybatis
mybatis.mapper-locations=classpath:mapping/*.xml
type-aliases-package: com.lpl.springboot1.pojo
#pagehelper分页插件
pagehelper.helperDialect=mysql
pagehelper.reasonable=true
pagehelper.supportMethodsArguments=true
pagehelper.params=count=countSql
######################################################mybatis###################
#输出日志
#debug=true
logging.level.org.springframework.web=debug
#输入sql日志:logging.level.+mapper的包
logging.level.com.lpl.springboot1.dao=debug
logging.file=logs/spring-boot-logging.log 
#################################################日志####################################################  

使用逆向工程生成mybatis相关文件

    这里我使用的是从网上找的一个逆行工程代码生产的一个程序,其中就是配置generatorConfig.xml中需要逆向工程的数据库、表、并配置生成的mapper.xml和mapper的接口文件到指定包下。
工程下载地址:http://download.csdn.net/download/plei_yue/10173926
备注(需要按照自己的需求修改generatorConfig.xml文件,点击GeneratorSqlmap.java运行程序。)

配置mybatis

有两种方式进行配置:

1.在SpringBoot启动中配置: @MapperScan(“com.lpl.springboot1.dao”)

@SpringBootApplication
@MapperScan("com.lpl.springboot1.dao")
public class Springboot1Application {

    public static void main(String[] args) {
        SpringApplication.run(Springboot1Application.class, args);
    }
}

2.在接口文件中配置@Mapper

@Mapper
public interface StudentMapper {

    int insert(Student student);//增加数据

    int deleteByPrimaryKey(@Param("id")Long id);//删除数据

    int updateByPrimaryKey(Student student);//修改数据

    Student selectByPrimaryKey(Long id);//查询数据

    List selectList();//查询学生列表
}

测试

访问:http://localhost:8080/users/getStudentById.do?id=7

结果:
SpringBoot整合Mybatis(二)_第1张图片

你可能感兴趣的:(spring,mybatis,SpringBoot,java,SpringBoot,Mybatis)