mybatis plus实现多表分页条件查询
生活随笔
收集整理的這篇文章主要介紹了
mybatis plus实现多表分页条件查询
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 前言
- 一、
- 二、如何實現
- 1 配置mybatis plus分頁插件
- 2.準備三張表
- 2.1 學生表
- 2.2 老師表
- 2.3 老師和學生Vo表
- 3. 編寫SQL語句
- 4. 編寫mapper層
- 5. service層
- 6.controller層
- 演示效果
- 【補充】 XML 自定義分頁
- 總結
前言
沒啥可說的
一、
沒啥可說的
二、如何實現
1 配置mybatis plus分頁插件
@Configuration @MapperScan("com.breez.vote.mapper") public class MybatispluConfig {/*** 配置分頁插件*/@Beanpublic MybatisPlusInterceptor mybatisPlusInterceptor() {MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));return interceptor;} }2.準備三張表
2.1 學生表
@Data @TableName("student") public class Student implements Serializable {private Integer id;private String studentName;private Integer studentAge; }2.2 老師表
@Data @TableName("teacher") public class Teacher implements Serializable {private Integer id;private String teacherName;private Integer teacherAge; }2.3 老師和學生Vo表
💡這張表的數據來自老師和學生,用來保存來自多張表的數據。
/*** 學生和老師*/ @Data public class QueryVo implements Serializable {/*** 學生名字*/private String studentName;/*** 學生年齡*/private Integer studentAge;/*** 老師姓名*/private String teacherName;/*** 老師年齡*/private Integer teacherAge; }3. 編寫SQL語句
💡這里需要注意:寫完SQL語句后,需要在結尾加上${ew.customSqlSegment},查詢參數,由mybatis plus自動拼裝。
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.breez.vote.mapper.StudentMapper"><select id="findByPage" resultType="com.breez.vote.domain.vo.QueryVo">select * from student,teacher ${ew.customSqlSegment}</select></mapper>4. 編寫mapper層
public interface StudentMapper extends BaseMapper<Student> {/***自定義分頁條件查詢* @param page 分頁參數* @param wrapper 查詢參數* @return*/IPage<QueryVo> findByPage(IPage<QueryVo> page, @Param(Constants.WRAPPER) QueryWrapper<QueryVo> wrapper); }5. service層
public interface StudentService extends IService<Student> {/**** @param page 分頁參數* @param queryWrapper 查詢條件* @return 分頁后的數據*/public IPage<QueryVo> findByPage(Page<QueryVo> page, QueryWrapper<QueryVo> queryWrapper);} @Service public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {@Resourceprivate StudentMapper queryMapper;/*** @param page 分頁參數* @param queryWrapper 查詢條件* @return 分頁后的數據*/@Overridepublic IPage<QueryVo> findByPage(Page<QueryVo> page, QueryWrapper<QueryVo> queryWrapper) {return queryMapper.findByPage(page, queryWrapper);} }6.controller層
@RestController @RequestMapping("data") public class StudentController {@Autowiredprivate StudentService studentService;@GetMapping("query")public Object query() {Page<QueryVo> page = new Page<>(1, 2);IPage<QueryVo> queryVoPage = studentService.findByPage(page, new QueryWrapper<>());return queryVoPage;} }演示效果
??學生表【4條數據】
?? 老師表【3條數據】
💡這里出現這個結果原因(出現了12條數據【3*4=12】)是我沒有在SQL語句中寫多表查詢條件,出現了笛卡爾積,加上多表查詢條件后就不會出現相同的了(李剛)。
【補充】 XML 自定義分頁
- UserMapper.java 方法內容
- UserMapper.xml 等同于編寫一個普通 list 查詢,mybatis-plus 自動替你分頁
- UserServiceImpl.java 調用分頁方法
總結
📖
一個字: 巴適
總結
以上是生活随笔為你收集整理的mybatis plus实现多表分页条件查询的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JavaScript-获得和设置表单的值
- 下一篇: JavaScript-Map和Set