MyBatis Common Mapper

SpringBoot集成

springBoot version: 1.5.10

1
2
3
4
5
6
7
8
9
10
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper</artifactId>
<version>3.5.3</version>
</dependency>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>1.2.4</version>
</dependency>

properties

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 数据源基础配置
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
#数据库
spring.datasource.url=jdbc:mysql://localhost:3306/test?characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
# mybatis 配置
mybatis.type-aliases-package=com.mos.quote.model
mybatis.mapper-locations=classpath:mapper/*.xml
# 通用 Mapper 配置
mapper.mappers=com.mos.quote.common.MyMapper
mapper.not-empty=false
mapper.identity=MYSQL
# 分页插件配置
pagehelper.helperDialect=mysql
pagehelper.reasonable=true
pagehelper.supportMethodsArguments=true
pagehelper.params=count=countSql

PS:此处的坑,pagehelper.reasonable,启用合理化时候,如果pageNo<1,则会返回第一页数据,
如果pageNo>pages会查询最后一页,作为接口程序,如果传入的pageNo一直大于pages,则一直会有数据返回,前端还需要校验页码问题。
建议:如果普通的分页查询,建议开启该功能,如果作为前后端分离或者提供接口之类的,建议禁用该功能

Transactional

1
2
3
4
@EnableTransactionManagement
class AccountApplication

@Transactional(timeout = TransConfig.TIMEOUT, rollbackFor = Exception.class, propagation = Propagation.REQUIRED)