spring boot mybatis+分页配置
本文介绍springboot项目中,如何集成mybatis的分页插件PageHelper。
1.mybatis和分页插件的依赖配置
<!-- mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<!--mybatis分页插件-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.1.0</version>
</dependency>
2.application.yml配置
mybatis:
#mapper-locations: classpath:/mybatis/mysql/*Mapper.xml
#type-aliases-package: com.ybf.activity.web.entity
config-location: classpath:/mybatis/mybatis-config.xml
check-config-location: true
3.mybatis-config.xml配置
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<setting name="logImpl" value="LOG4J"/>
</settings>
<typeAliases>
<typeAlias type="com.ybf.activity.web.entity.Student" alias="Student" />
</typeAliases>
<plugins>
<!--mybatis分页插件-->
<plugin interceptor="com.github.pagehelper.PageHelper">
<property name="dialect" value="mysql"/>
<property name="offsetAsPageNum" value="false"/>
<property name="rowBoundsWithCount" value="false"/>
<property name="pageSizeZero" value="true"/>
<property name="reasonable" value="false"/>
<property name="supportMethodsArguments" value="false"/>
<property name="returnPageInfo" value="none"/>
</plugin>
</plugins>
<mappers>
<mapper resource="mybatis/mapper/mysql/StudentMapper.xml"/>
</mappers>
</configuration>
4.spring启动类增加Mapper扫描
/**
* Created by Administrator on 2017/7/6.
*/
@SpringBootApplication
// Mapper扫描
@MapperScan("com.ybf.activity.web.mapper")
public class WebApplication extends SpringBootServletInitializer {
private final static Logger logger = LoggerFactory.getLogger(WebApplication.class);
// 编码过滤器
@Bean
Filter characterEncodingFilter() {
logger.info("==========初始化编码过滤器=================");
CharacterEncodingFilter filter = new CharacterEncodingFilter();
filter.setEncoding("UTF-8");
filter.setForceEncoding(true);
return filter;
}
public static void main(String[] args) {
SpringApplication.run(WebApplication.class,args);
logger.info("Application [activity-web] started!");
}
}
5.Mapper接口
public interface StudentMapper extends BaseMapper{
Student getById(int id);
List<Student> sel();
}
6.StudentMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ybf.activity.web.mapper.StudentMapper">
<resultMap id="stuMap" type="Student">
<id property="id" column="id" />
<result property="name" column="name" />
<result property="sumScore" column="score_sum" />
<result property="avgScore" column="score_avg" />
<result property="age" column="age" />
</resultMap>
<select id="getById" resultMap="stuMap" resultType="Student">
SELECT *
FROM STUDENT a
WHERE ID = #{id}
</select>
<select id="sel" resultType="Student" resultMap="stuMap">
select * from student a
</select>
</mapper>
注:测试的spring boot版本为1.5.4.RELEASE.
7.用法
public List<Student> selectStudentByPage(@PathVariable int pageNo) {
if (pageNo > 0) {
PageHelper.startPage(pageNo,3); // 设置分页,参数1=页数,参数2=每页显示条数
}
return studentMapper.sel();
}
更多PageHelper用法,请自行搜索。