spring-boot系列——与mybaits整合
2017-02-27 19:44
525 查看
原创文章,转载请注明
与mybatis的整合有两种方式,一种使用mybatis-spring-boot-starter,另一种是传统的使用mybatis-spring。
我这里就只说一下mybatis-spring-boot-starter的方式,因为更符合“约定大于配置”的理念。
一、依赖
首先,我们来看看依赖的jar包
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency> 很简洁,就这一个jar包就搞定。
如果你使用albaba的连接池,那么依赖的jar包如下
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>0.2.26</version>
</dependency> 当然,你还需要提供jdbc驱动,比如mysql的驱动
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.21</version>
</dependency>
链接池的配置
spring:
datasource:
url: jdbc:mysql://localhost:3306/vehicle?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&pinGlobalTxToPhysicalConnection=true&autoReconnect=true
username: root
password: nmamtf
driver-class-name: com.mysql.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
initialSize: 0
maxActive: 20
maxIdle: 20
minIdle: 0
maxWait: 60000
validationQuery: select 1
testOnBorrow: false
testOnReturn: false
testWhileIdle: true
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 25200000
removeAbandoned: true
removeAbandonedTimeout: 1800
logAbandoned: true mybatis的配置
mybatis. configLocation:该选项允许你通过xml来进行额外配置,比如我们可以通过它来制定xml配置interceptor
mybatis.typeAliasesPackage:该配置指定需要映射的entity所在的包,支持通配符
mybatis.mapperLocations:该配置指定sql所在的xml,支持通配符
唯一缺少的东西是MapperScannerConfigurer,通过它指定mapper所在的包,从而不必使用@Mapper进行注释。如果要使用该功能,只能通过mybatis-spring的方式了。
最后举一个例子
mybatis:
configLocation: classpath:com/zs/vehicle/bussiness/application_mysql.xml
typeAliasesPackage: com.zs.vehicle.service.*.model
mapperLocations: ["classpath:com/zs/vehicle/bussiness/sql/*/*.xml",
"classpath:com/zs/vehicle/bussiness/sql/*.xml"]
与mybatis的整合有两种方式,一种使用mybatis-spring-boot-starter,另一种是传统的使用mybatis-spring。
我这里就只说一下mybatis-spring-boot-starter的方式,因为更符合“约定大于配置”的理念。
一、依赖
首先,我们来看看依赖的jar包<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency> 很简洁,就这一个jar包就搞定。
如果你使用albaba的连接池,那么依赖的jar包如下
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>0.2.26</version>
</dependency> 当然,你还需要提供jdbc驱动,比如mysql的驱动
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.21</version>
</dependency>
二、配置
配置还是那几样,只不过,你可以把它们放在application.yml中链接池的配置
spring:
datasource:
url: jdbc:mysql://localhost:3306/vehicle?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&pinGlobalTxToPhysicalConnection=true&autoReconnect=true
username: root
password: nmamtf
driver-class-name: com.mysql.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
initialSize: 0
maxActive: 20
maxIdle: 20
minIdle: 0
maxWait: 60000
validationQuery: select 1
testOnBorrow: false
testOnReturn: false
testWhileIdle: true
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 25200000
removeAbandoned: true
removeAbandonedTimeout: 1800
logAbandoned: true mybatis的配置
mybatis. configLocation:该选项允许你通过xml来进行额外配置,比如我们可以通过它来制定xml配置interceptor
mybatis.typeAliasesPackage:该配置指定需要映射的entity所在的包,支持通配符
mybatis.mapperLocations:该配置指定sql所在的xml,支持通配符
唯一缺少的东西是MapperScannerConfigurer,通过它指定mapper所在的包,从而不必使用@Mapper进行注释。如果要使用该功能,只能通过mybatis-spring的方式了。
最后举一个例子
mybatis:
configLocation: classpath:com/zs/vehicle/bussiness/application_mysql.xml
typeAliasesPackage: com.zs.vehicle.service.*.model
mapperLocations: ["classpath:com/zs/vehicle/bussiness/sql/*/*.xml",
"classpath:com/zs/vehicle/bussiness/sql/*.xml"]
相关文章推荐
- Spring Boot系列之六 以注解方式整合MyBatis
- SpringBoot系列2—整合Mybatis-plus
- Springboot系列:Springboot与Thymeleaf模板引擎整合基础教程(附源码)
- Springboot系列:Springboot与Thymeleaf模板引擎整合基础教程(附源码)
- Springboot系列:Springboot与Thymeleaf模板引擎整合基础教程(附源码)
- Spring Boot系列之七 以xml整合MyBatis
- Springboot系列:Springboot与Thymeleaf模板引擎整合基础教程(附源码)
- Springboot系列:Springboot与Thymeleaf模板引擎整合基础教程(附源码)
- Springboot系列:Springboot与Thymeleaf模板引擎整合基础教程(附源码)
- Springboot系列:Springboot与Thymeleaf模板引擎整合基础教程(附源码)
- [Spring Boot实战系列] - No.2 Spring boot 整合Spring Security用户管理和用户权限管理
- Springboot系列:Springboot与Thymeleaf模板引擎整合基础教程(附源码)
- Springboot系列:Springboot与Thymeleaf模板引擎整合基础教程(附源码)
- Springboot系列:Springboot与Thymeleaf模板引擎整合基础教程(附源码)
- spring-boot系列——整合jersey
- 2.SpringBoot系列-SpringBoot整合MyBatis
- Springboot系列:Springboot与Thymeleaf模板引擎整合基础教程(附源码)
- Springboot系列:Springboot与Thymeleaf模板引擎整合基础教程(附源码)
- Springboot系列:Springboot与Thymeleaf模板引擎整合基础教程(附源码)
- Springboot系列:Springboot与Thymeleaf模板引擎整合基础教程(附源码)