0


整合SpringBoot Mybatis-Puls-join 多表联查及分页(保姆级详细教程)

前言

  • MyBatis-Plus-Join (简称 MPJ)是一个 MyBatis-Plus的增强工具,在 MyBatis-Plus 的基础上只做增强不做改变,为简化开发、提高效率而生。
  • 无侵入:只做增强不做改变,引入它不会对现有工程产生影响,如丝般顺滑
  • 无感引入, 支持MP风格的查询, 您会MP就会MPJ, 无需额外的学习成本
  • 兼容MP的别名、逻辑删除、枚举列、TypeHandle列等特性
  • 支持注解形式一对一、一对多和连表查询形式的一对一和一对多

官网:mybatis-plus-join官网

一、新建SpringBoot项目

1.1、自选依赖

二、 mybatis-plus-join

2.1、导入依赖

  1. <dependency>
  2. <groupId>org.springframework.cloud</groupId>
  3. <artifactId>spring-cloud-starter-bootstrap</artifactId>
  4. <version>3.1.7</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>com.baomidou</groupId>
  8. <artifactId>mybatis-plus-boot-starter</artifactId>
  9. <version>3.5.5</version>
  10. </dependency>
  11. <dependency>
  12. <groupId>com.github.yulichang</groupId>
  13. <artifactId>mybatis-plus-join-boot-starter</artifactId>
  14. <version>1.4.11</version>
  15. </dependency>

2.2、修改配置文件

mybatis-plus-join配置文件如下:

  1. # mybatis-plus配置
  2. mybatis-plus:
  3. # 启动检查MyBatis配置文件
  4. check-config-location: false
  5. # MyBatis配置文件位置
  6. config-location:
  7. # MyBaits别名包扫描路径
  8. type-aliases-package: com.ui.springbootmybatisplusjoin.mybatis.entity
  9. # Mapper所对应的XML文件位置 默认【classpath*:/mapper/**/*.xml】
  10. mapper-locations: classpath:mappers/*xml
  11. # TypeHandler扫描路径
  12. type-handlers-package:
  13. configuration:
  14. # 日志打印
  15. log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  16. # 是否开启自动驼峰命名规则
  17. map-underscore-to-camel-case: true
  18. # 开启Mybatis二级缓存,默认为true
  19. cache-enabled: true
  20. global-config:
  21. # 控制台mybatis-plus的logo
  22. banner: true
  23. db-config:
  24. # 全局默认主键类型
  25. id-type: auto
  26. # 逻辑删除配置
  27. logic-delete-field: deleted
  28. logic-delete-value: 1
  29. logic-not-delete-value: 0

2.3、实体类

  1. import com.baomidou.mybatisplus.annotation.IdType;
  2. import com.baomidou.mybatisplus.annotation.TableField;
  3. import com.baomidou.mybatisplus.annotation.TableId;
  4. import com.baomidou.mybatisplus.annotation.TableName;
  5. import lombok.AllArgsConstructor;
  6. import lombok.Data;
  7. import lombok.NoArgsConstructor;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. /**
  10. * @ClassName User
  11. * @Description 描述
  12. * @Author 轸...
  13. * @Date 2024/9/28 20:15
  14. */
  15. @Data
  16. @NoArgsConstructor
  17. @AllArgsConstructor
  18. @TableName("user")
  19. public class User {
  20. @TableId(value = "user_id",type = IdType.AUTO)
  21. private Long userId;
  22. @TableField("user_name")
  23. private String userName;
  24. @TableField("user_account")
  25. private String userAccount;
  26. @TableField("user_pwd")
  27. private String userPwd;
  28. @TableField("user_tel")
  29. private String userTel;
  30. @TableField("clazz_id")
  31. private Integer clazzId;
  32. @TableField(exist = false)
  33. private String clazzName;
  34. }

  1. import com.baomidou.mybatisplus.annotation.IdType;
  2. import com.baomidou.mybatisplus.annotation.TableField;
  3. import com.baomidou.mybatisplus.annotation.TableId;
  4. import com.baomidou.mybatisplus.annotation.TableName;
  5. import lombok.AllArgsConstructor;
  6. import lombok.Data;
  7. import lombok.NoArgsConstructor;
  8. /**
  9. * @ClassName Log
  10. * @Description 描述
  11. * @Author 轸...
  12. * @Date 2024/9/28 20:15
  13. */
  14. @Data
  15. @NoArgsConstructor
  16. @AllArgsConstructor
  17. @TableName("clazz")
  18. public class Clazz {
  19. @TableId(value = "clazz_id",type = IdType.AUTO)
  20. private Long clazzId;
  21. @TableField("clazz_name")
  22. private String clazzName;
  23. }

2.4、mapper 配置 mapper 扫描路径

  1. import org.mybatis.spring.annotation.MapperScan;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. @SpringBootApplication
  5. @MapperScan("com.ui.springbootmybatisplusjoin.demos.mapper")
  6. public class SpringBootMybatisPlusJoinApplication {
  7. public static void main(String[] args) {
  8. SpringApplication.run(SpringBootMybatisPlusJoinApplication.class, args);
  9. }
  10. }

2.5、service 以及 serviceImpl

三、mybatis-plus-join 分页联查

3.1、增加Vo实体类 分页参数

3.2、分页列表及联查业务层

  1. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  2. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  3. import com.github.yulichang.base.MPJBaseServiceImpl;
  4. import com.github.yulichang.wrapper.MPJLambdaWrapper;
  5. import com.ui.springbootmybatisplusjoin.demos.mapper.UserMapper;
  6. import com.ui.springbootmybatisplusjoin.demos.pojo.Clazz;
  7. import com.ui.springbootmybatisplusjoin.demos.pojo.User;
  8. import com.ui.springbootmybatisplusjoin.demos.pojo.Vo.UserVo;
  9. import com.ui.springbootmybatisplusjoin.demos.service.UserService;
  10. import org.springframework.beans.factory.annotation.Autowired;
  11. import org.springframework.stereotype.Service;
  12. /**
  13. * @ClassName UserServiceImpl
  14. * @Description 描述
  15. * @Author 轸...
  16. * @Date 2024/9/28 20:22
  17. */
  18. @Service
  19. public class UserServiceImpl extends MPJBaseServiceImpl<UserMapper, User> implements UserService {
  20. @Autowired
  21. private UserMapper userMapper;
  22. /**
  23. * mybatis-plus-join 联查及分页
  24. * @param userVo
  25. * @return
  26. */
  27. @Override
  28. public Page<User> pageList(UserVo userVo) {
  29. // SELECT t.*,t1.clazz_name FROM user t LEFT JOIN clazz t1 ON t.clazz_id = t1.clazz_id LIMIT ?,?
  30. MPJLambdaWrapper<User> lambdaWrapper = new MPJLambdaWrapper<>();
  31. lambdaWrapper.selectAll(User.class)
  32. .select(Clazz::getClazzName)
  33. .leftJoin(Clazz.class,Clazz::getClazzId,User::getClazzId);
  34. Page<User> userPage = new Page<>(userVo.getPageNum(), userVo.getPageSize());
  35. return userMapper.selectJoinPage(userPage, User.class,lambdaWrapper);
  36. }
  37. }

3.3、控制层

四、以上步骤 启动执行方法时会报一个com.baomidou.mybatisplus.core.exceptions.MybatisPlusException: mapper not find by class <Clazz> , add mapper and extends BaseMapper<T> or MPJBaseMapper<T>这样的错误

这个错误是因为 找不到联查表的mapper

所以我们应该写一个 clazzMapper

  1. import com.github.yulichang.base.MPJBaseMapper;
  2. import com.ui.springbootmybatisplusjoin.demos.pojo.Clazz;
  3. /**
  4. * @ClassName ClazzMapper
  5. * @Description 描述
  6. * @Author 轸...
  7. * @Date 2024/9/28 20:53
  8. */
  9. public interface ClazzMapper extends MPJBaseMapper<Clazz> {
  10. }

六、测试


本文转载自: https://blog.csdn.net/X_xxxx7/article/details/142620774
版权归原作者 七七七7 所有, 如有侵权,请联系我们删除。

“整合SpringBoot Mybatis-Puls-join 多表联查及分页(保姆级详细教程)”的评论:

还没有评论