springboot整合mybatis-plus实现多表分页查询的示例代码

2025-05-29 0 88

1.新建一个springboot工程

2.需要导入mybatis和mybatis-plus的依赖文件

  1. <dependency>
  2. <groupId>com.baomidou</groupId>
  3. <artifactId>mybatis-plus-boot-starter</artifactId>
  4. <version>3.1.1</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.mybatis.spring.boot</groupId>
  8. <artifactId>mybatis-spring-boot-starter</artifactId>
  9. <version>2.0.1</version>
  10. </dependency>

3.application.yml配置文件

  1. server:
  2. port: 8080
  3. spring:
  4. datasource:
  5. url: jdbc:mysql://localhost:3306/mybatisplus?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=UTC
  6. username: root
  7. password: 数据库密码
  8. mybatis:
  9. mapperlocations: classpath*:mapper/*.xml
  10. mybatis-plus:
  11. mapper-locations: classpath:/mapper/*Mapper.xml
  12. logging:
  13. level:
  14. com.tuanzi.*: debug

4.首先我们需要写一个类来配置分页插件

  1. 省略import
  2. @EnableTransactionManagement
  3. @Configuration
  4. @MapperScan("com.tuanzi.*.mapper*")
  5. public class MybatisPlusConfig {
  6. /**
  7. * 分页插件
  8. */
  9. @Bean
  10. public PaginationInterceptor paginationInterceptor(){
  11. return new PaginationInterceptor();
  12. }
  13. }

5.controller类

  1. @RestController
  2. @RequestMapping("/user")
  3. public class UserController {
  4. @Autowired
  5. UserService userService;
  6. /**
  7. * 多表关联,分页查询(1对1)
  8. * @param page
  9. * @return
  10. */
  11. @RequestMapping("/findAll")
  12. public Result<IPage<User>> findAll(@RequestBody Page<User> page){
  13. return userService.pages(page);
  14. }
  15. /**
  16. * 多表关联,分页查询(1对多)
  17. * @param page
  18. * @return
  19. */
  20. @RequestMapping("/selectAll")
  21. public Result<IPage<User>> selectAll(@RequestBody Page<User> page){
  22. return userService.pageList(page);
  23. }
  24. }

6.service类

  1. public interface UserService extends IService<User> {
  2. Result<IPage<User>> pages(Page<User> page);
  3. Result<IPage<User>> pageList(Page<User> page);
  4. }

7.service实现类

  1. @Service
  2. public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
  3. @Autowired
  4. UserMapper userMapper;
  5. @Override
  6. public Result<IPage<User>> pages(Page<User> page) {
  7. IPage<User> userIPage = userMapper.Pages(page);
  8. return Result.getSuccess("分页查询成功",userIPage);
  9. }
  10. @Override
  11. public Result<IPage<User>> pageList(Page<User> page) {
  12. IPage<User> userIPage = userMapper.pageList(page);
  13. return Result.getSuccess("分页查询成功",userIPage);
  14. }
  15. }

8.mapper接口

注意!!: 如果入参是有多个,需要加注解指定参数名才能在xml中取值

  1. @Mapper
  2. @Repository
  3. public interface UserMapper extends BaseMapper<User> {
  4. IPage<User> Pages(@Param("page") Page<User> page);
  5. IPage<User> pageList(@Param("page") Page<User> page);
  6. }

9.xml文件

一对一关联

  1. <!– 一对一 通用查询映射结果 –>
  2. <resultMap id="BaseResultMap1" type="com.tuanzi.user.entity.User">
  3. <result column="id" property="id" />
  4. <result column="name" property="name" />
  5. <result column="age" property="age" />
  6. <result column="email" property="email" />
  7. <!–assocication 一对一关联查询
  8. 可以指定联合的JavaBean对象
  9. property="work"指定哪个属性是联合的对象
  10. javaType:指定这个属性对象的类型
  11. –>
  12. <association property="work" javaType="com.tuanzi.user.entity.Work">
  13. <result column="id" property="id" />
  14. <result column="position" property="position" />
  15. <result column="user_id" property="userId" />
  16. </association>
  17. </resultMap>

一对多关联

  1. <!– 一对多 通用查询映射结果 –>
  2. <resultMap id="BaseResultMap2" type="com.tuanzi.user.entity.User">
  3. <result column="id" property="id" />
  4. <result column="name" property="name" />
  5. <result column="age" property="age" />
  6. <result column="email" property="email" />
  7. <!–
  8. collection定义关联结合类型的属性的封装规则
  9. property="workList"指定哪个属性是联合的对象
  10. ofType:指定集合里面元素的类型
  11. –>
  12. <collection property="workList" ofType="com.tuanzi.user.entity.Work">
  13. <result column="id" property="id" />
  14. <result column="position" property="position" />
  15. <result column="user_id" property="userId" />
  16. </collection>
  17. </resultMap>

SQL语句:

  1. <select id="Pages" resultMap="BaseResultMap1">
  2. select a.id id,a.name name,a.age age,a.email email,b.position position,b.user_id user_id from user a LEFT JOIN work b on a.id=b.user_id
  3. </select>
  4. <select id="pageList" resultMap="BaseResultMap2">
  5. select a.id id,a.name name,a.age age,a.email email,b.position position,b.user_id user_id from user a LEFT JOIN work b on a.id=b.user_id
  6. </select>

10.这样就基本完成了!我这里省略了实体类

我们运行一下,用postman测试一下结果
这里我们需要传2个参数,当然我们也可以不用传,因为mybatis-plus有默认值
来看下mybatis-plus的page源码

springboot整合mybatis-plus实现多表分页查询的示例代码

效果图:

springboot整合mybatis-plus实现多表分页查询的示例代码

springboot整合mybatis-plus实现多表分页查询的示例代码

最后附赠源码地址:demo

到此这篇关于springboot整合mybatis-plus实现多表分页查询的示例代码的文章就介绍到这了,更多相关springboot整合mybatis-plus多表分页查询内容请搜索快网idc以前的文章或继续浏览下面的相关文章希望大家以后多多支持快网idc!

原文链接:https://blog.csdn.net/weixin_42370891/article/details/93061955

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 springboot整合mybatis-plus实现多表分页查询的示例代码 https://www.kuaiidc.com/108473.html

相关文章

发表评论
暂无评论