介绍
在使用mybatis-plus时,IPage对象可以方便的进行单表分页查询,但是进行多表分页查询及带查询条件则需要自己封装sql语句,自定义封装sql语句也可以使用mybatis-plus的IPage和QueryWrapper条件
先来看下官方介绍
步骤:
Mapper层
- Mapper层,使用mybatis映射xml文件写sql语句
此处的LendList是查询结果的VO对象
方法传参:IPage<VO> page,@Param(Constants.WRAPPER) QueryWrapper<VO>
方法返回:IPage<VO>
xml文件@Mapper public interface LendRecordMapper extends BaseMapper<LendRecord> { /** * 多表分页查询加条件 * @param page IPage * @param wrapper 查询条件 * @return 返回IPage对象 */ IPage<LendList> getLendList(IPage<LendList> page, @Param(Constants.WRAPPER) Wrapper<LendList> wrapper); }
<select id="getLendList" resultType="LendList"> select lend_record.id, book_name, user_name, lend_time from lend_record left join book b on b.id = lend_record.book_id left join user u on u.id = lend_record.user_id <where> ${ew.sqlSegment} </where> </select>
Service层
- Service层,这里的Service层是主表对象,继承了IService
/** * 获取借阅信息(多表查询) * * @param pageNo 当前页 * @param pageSize 页面展示数据大小 * @param lendSearch 查询条件 * @return 返回自定义VO对象 */ @Override public Result<MyPage<LendList>> getLendList(Integer pageNo, Integer pageSize, LendList lendSearch) { // 封装查询条件 HashMap<String, Object> map = new HashMap<>(5); map.put("book_name", lendSearch.getBookName()); map.put("user_name", lendSearch.getUserName()); QueryWrapper<LendList> wrapper = new QueryWrapper<>(); wrapper.allEq(map, false); // 分页查询数据 Page<LendList> page = new Page<>(pageNo, pageSize); // 对多表查询后的结果进行count(*) page.setOptimizeCountSql(false); IPage<LendList> lendList = lendRecordMapper.getLendList(page, wrapper); // 封装数据返回 MyPage<LendList> myPage = new MyPage<>( lendList.getRecords(), pageNo, pageSize, lendList.getTotal() ); return Result.success(myPage); }
Mapper层详情
- xml方法
这里Mapper主要用了Mybatis-plus自定义sql分页查询,查询条件整合
方法传参:IPage<VO> page,@Param(Constants.WRAPPER) QueryWrapper<VO>
方法返回:IPage<VO>
注意:/** * 多表分页查询加条件 * @param page IPage * @param wrapper 查询条件 * @return 返回IPage对象 */ IPage<LendList> getLendList(IPage<LendList> page, @Param(Constants.WRAPPER) Wrapper<LendList> wrapper);
这里的xml不需要parameterType
在<where>中写${ew.sqlSegment}<select id="getLendList" resultType="LendList"> select lend_record.id, book_name, user_name, lend_time from lend_record left join book b on b.id = lend_record.book_id left join user u on u.id = lend_record.user_id <where> ${ew.sqlSegment} </where> </select>
Service层详情
-
Service层详情介绍
封装查询条件
使用的是wrapper.allEq(Map(String,Object) param,boolean null2IsNull);
第二个参数为false是为了没有这个条件的时候查询语句没有xxx is null// 封装查询条件 HashMap<String, Object> map = new HashMap<>(5); map.put("book_name", lendSearch.getBookName()); map.put("user_name", lendSearch.getUserName()); QueryWrapper<LendList> wrapper = new QueryWrapper<>(); wrapper.allEq(map, false);
分页查询
注意 new Page<>(PageNo,pageSize);
第三个参数为false,因为在mybatisplus默认查询时会查询主表的count,如果有条件则会报错(条件的字段在主表中没有)
加上 page.setOptimizeCountSql(false);,他会对条件查询后的结果进行封装// 分页查询数据 Page<LendList> page = new Page<>(pageNo, pageSize); // 对多表查询后的结果进行count(*) page.setOptimizeCountSql(false); IPage<LendList> lendList = lendRecordMapper.getLendList(page, wrapper);
封装数据
根据自定义VO,封装数据返回,前台需要三个数据,列表,当前页,页面大小,总条数
count()方法是继承的IService中的方法// 封装数据返回 MyPage<LendList> myPage = new MyPage<>( lendList.getRecords(), pageNo, pageSize, lendList.getTotal() );