MyBatis备忘
0
平时我都使用JPA,虽然用过mybatis,但是没有搭建过,今天公司项目里面把JPA和mybatis整合在一起了。
mybatis不能使用JPA的@column进行属性映射,但是提供了一个驼峰属性映射:
mybatis配置:
mybatis:
configuration:
# 驼峰属性映射
map-underscore-to-camel-case: true
mapper-locations:
- classpath*:mybatis/mapper/**/*.xml
# 如果没配没有结果
pagehelper:
params: count=countSql
reasonable: true
helperDialect: mysql
supportMethodsArguments: true
查询
final Pageable pageable = PageRequest.of(0, 10);
//final long count = PageHelper.count(() -> this.accountMapper.page());
final PageInfo<Account> pageInfo = PageHelper
.startPage(pageable.getPageNumber(), pageable.getPageSize())
.doSelectPageInfo(() -> this.accountMapper.page());
final Page<Account> page = new PageImpl<Account>(pageInfo.getList(), pageable, pageInfo.getTotal());
assertNotNull(page);
assertFalse(pageInfo.getList().isEmpty());
maven配置:
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
</dependency>
resultType配置返回实体对象类型,不要配置List,resultMap自定义属性映射。
可以使用association和collection标签映射复杂类型。