1、引入依赖
<!-- mybatis依赖 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<!-- mybatis分页 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>${pagehelper.version}</version>
</dependency>
2、在application.yml添加mybatis相关配置
#mybatis配置
mybatis:
#指定实体所在的包名
typeAliasesPackage: com.gsww.scjzfp.open.entity
#指定mapper.xml文件位置
mapperLocations: classpath:mapper/*.xml
configuration:
#开启驼峰命名
map-underscore-to-camel-case: true
#开启二级缓存
cache-enabled: true
#pagehelper分页插件
pagehelper:
helperDialect: mysql
reasonable: true
supportMethodsArguments: true
params: count=countSql
3、主启动类添加注解@MapperScan(value = "com.atguigu.entity.mapper")
即可。这样就完成了SpringBoot和Mybatis的整合。
4、分页:https://github.com/pagehelper/MybatisPageHelper/blob/master/wikis/zh/HowToUse.md
//pageNo为1,pageSize为10
PageHelper.startPage(1, 10);
//查询结果,返回值必须是List
List<Country> list = countryMapper.selectAll();
//用PageInfo对结果进行包装
PageInfo page = new PageInfo(list);