
MybatisPlus分页插件使用
配置分页
创建配置类MPConfiguration
,类名随意,第6行中需要传入数据库类型
@Configuration
public class MPConfiguration {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
}
分页插件使用
构造分页对象
分页对象包含了分页的各项信息,其核心属性如下:
属性名 类型 默认值 描述 records List emptyList 查询数据列表 total Long 0 查询列表总记录数 size Long 10 每页显示条数,默认 10
current Long 1 当前页 分页对象既作为分页查询的参数,也作为分页查询的返回结果,当作为查询参数时,通常只需提供
current
和size
属性,如下IPage<T> page = new Page<>(current, size);
注:
IPage
为分页接口,Page
为IPage
接口的一个实现类。分页查询
Mybatis Plus的
BaseMapper
和ServiceImpl
均提供了常用的分页查询的方法,例如:BaseMapper
的分页查询:IPage<T> selectPage(IPage<T> page,Wrapper<T> queryWrapper);
ServiceImpl
的分页查询:// 无条件分页查询 IPage<T> page(IPage<T> page); // 条件分页查询 IPage<T> page(IPage<T> page, Wrapper<T> queryWrapper);
自定义Mapper
对于自定义SQL,也可以十分方便的完成分页查询,如下
Mapper
接口:IPage<UserVo> selectPageVo(IPage<?> page, Integer state);
Mapper.xml
:<select id="selectPageVo" resultType="xxx.xxx.xxx.UserVo"> SELECT id,name FROM user WHERE state=#{state} </select>
注意:
Mapper.xml
中的SQL只需实现查询list
的逻辑即可,无需关注分页的逻辑。
案例实操
分页查询案例如下:
创建
PageTest
测试类,内容如下@SpringBootTest public class PageTest { @Autowired private UserService userService; @Autowired private UserMapper userMapper; //通用Service分页查询 @Test public void testPageService() { Page<User> page = new Page<>(2, 3); Page<User> userPage = userService.page(page); userPage.getRecords().forEach(System.out::println); } //通用Mapper分页查询 @Test public void testPageMapper() { IPage<User> page = new Page<>(2, 3); IPage<User> userPage = userMapper.selectPage(page, null); userPage.getRecords().forEach(System.out::println); } //自定义SQL分页查询 @Test public void testCustomMapper() { IPage<User> page = new Page<>(2, 3); IPage<User> userPage = userMapper.selectUserPage(page); userPage.getRecords().forEach(System.out::println); } }
在UserMapper中声明分页查询方法如下
IPage<User> selectUserPage(IPage<User> page);
创建
resources/mapper/UserMapper.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.atguigu.hellomp.mapper.UserMapper"> <select id="selectUserPage" resultType="com.atguigu.hellomp.entity.User"> select * from user </select> </mapper>
注意:
Mybatis-Plus中
Mapper.xml
文件路径默认为:classpath*:/mapper/**/*.xml
,可在application.yml
中配置以下参数进行修改mybatis-plus: mapper-locations: classpath*:/mapper/**/*.xml
MybatisX插件
MyBatis Plus提供了一个IDEA插件——MybatisX
,使用它可根据数据库快速生成Entity
、Mapper
、Mapper.xml
、Service
、ServiceImpl
等代码,使用户更专注于业务。
下面为具体用法
安装插件
在IDEA插件市场搜索
MyBatisX
,进行在线安装配置数据库连接
在IDEA中配置数据库连接
生成代码
配置实体类相关信息
配置代码模版信息
点击Finish然后查看生成的代码。
其他注意事项
xml文件
<
和>
的转义由于xml文件中的
<
和>
是特殊符号,需要转义处理。原符号 转义符号 <
<
>
>
Mybatis-Plus分页插件注意事项
使用Mybatis-Plus的分页插件进行分页查询时,如果结果需要使用
<collection>
进行映射,只能使用**嵌套查询(Nested Select for Collection),而不能使用嵌套结果映射(Nested Results for Collection)**。即对于
<collection>
的内容使用单独一个sql进行查询。嵌套查询和嵌套结果映射是Collection映射的两种方式,下面通过一个案例进行介绍
例如有
room_info
和graph_info
两张表,其关系为一对多现需要查询房间列表及其图片信息,期望返回的结果如下
[ { "id": 1, "number": 201, "rent": 2000, "graphList": [ { "id": 1, "url": "http://", "roomId": 1 }, { "id": 2, "url": "http://", "roomId": 1 } ] }, { "id": 2, "number": 202, "rent": 3000, "graphList": [ { "id": 3, "url": "http://", "roomId": 2 }, { "id": 4, "url": "http://", "roomId": 2 } ] } ]
为得到上述结果,可使用以下两种方式
嵌套结果映射
<select id="selectRoomPage" resultMap="RoomPageMap"> select ri.id room_id, ri.number, ri.rent, gi.id graph_id, gi.url, gi.room_id from room_info ri left join graph_info gi on ri.id=gi.room_id </select> <resultMap id="RoomPageMap" type="RoomInfoVo" autoMapping="true"> <id column="room_id" property="id"/> <collection property="graphInfoList" ofType="GraphInfo" autoMapping="true"> <id column="graph_id" property="id"/> </collection> </resultMap>
嵌套查询
<select id="selectRoomPage" resultMap="RoomPageMap"> select id, number, rent from room_info </select> <resultMap id="RoomPageMap" type="RoomInfoVo" autoMapping="true"> <id column="id" property="id"/> <collection property="graphInfoList" ofType="GraphInfo" select="selectGraphByRoomId" column="id"/> </resultMap> <select id="selectGraphByRoomId" resultType="GraphInfo"> select id, url, room_id from graph_info where room_id = #{id} </select>
这种方法使用两个独立的查询语句来获取一对多关系的数据。首先,Mybatis会执行主查询来获取
room_info
列表,然后对于每个room_info
,Mybatis都会执行一次子查询来获取其对应的graph_info
。
若现在使用MybatisPlus的分页插件进行分页查询,假如查询的内容是第1页,每页2条记录,显然嵌套结果映射的分页逻辑是存在问题的。
打印SQL语句
需要在
application.yml
文件中进行如下配置#用于打印框架生成的sql语句,便于调试 mybatis-plus: configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl