一、mybatisplus是什么?

MyBatis-Plus (opens new window)(简称 MP)是一个 MyBatis (opens new window)的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。

二、整合mybatisplus的步骤

1.引入相关jar包

  1. <dependency>
  2. <groupId>com.baomidou</groupId>
  3. <artifactId>mybatis-plus-boot-starter</artifactId>
  4. <version>3.5.1</version>
  5. </dependency>

此外还需要引入其他相关jar包

2.完整pom文件内容

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.6.3</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.example</groupId>
  12. <artifactId>demo</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>springboot-mybatisplus</name>
  15. <description>Demo project for Spring Boot</description>
  16. <properties>
  17. <java.version>1.8</java.version>
  18. </properties>
  19. <dependencies>
  20. <dependency>
  21. <groupId>org.springframework.boot</groupId>
  22. <artifactId>spring-boot-starter-data-jdbc</artifactId>
  23. </dependency>
  24. <dependency>
  25. <groupId>org.springframework.boot</groupId>
  26. <artifactId>spring-boot-starter-web</artifactId>
  27. </dependency>
  28. <dependency>
  29. <groupId>mysql</groupId>
  30. <artifactId>mysql-connector-java</artifactId>
  31. <scope>runtime</scope>
  32. </dependency>
  33. <dependency>
  34. <groupId>org.projectlombok</groupId>
  35. <artifactId>lombok</artifactId>
  36. <optional>true</optional>
  37. </dependency>
  38. <dependency>
  39. <groupId>org.springframework.boot</groupId>
  40. <artifactId>spring-boot-starter-test</artifactId>
  41. <scope>test</scope>
  42. </dependency>
  43. <dependency>
  44. <groupId>com.baomidou</groupId>
  45. <artifactId>mybatis-plus-boot-starter</artifactId>
  46. <version>3.5.1</version>
  47. </dependency>
  48. </dependencies>
  49. <build>
  50. <plugins>
  51. <plugin>
  52. <groupId>org.springframework.boot</groupId>
  53. <artifactId>spring-boot-maven-plugin</artifactId>
  54. <configuration>
  55. <excludes>
  56. <exclude>
  57. <groupId>org.projectlombok</groupId>
  58. <artifactId>lombok</artifactId>
  59. </exclude>
  60. </excludes>
  61. </configuration>
  62. </plugin>
  63. </plugins>
  64. </build>
  65. </project>

3.application配置文件内容

  1. spring:
  2. datasource:
  3. # 数据源基本配置
  4. username: root
  5. password: 123456
  6. driver-class-name: com.mysql.cj.jdbc.Driver
  7. url: jdbc:mysql://101.34.49.127:3306/springBootAll?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false
  8. # mysql5 不需要声明serverTimezone=GMT%2B8& 时区
  9. # mysql8 需要声明serverTimezone=GMT%2B8& 时区
  10. server:
  11. port: 8082 # 配置项目启动端口
  12. mybatis-plus:
  13. configuration:
  14. log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #控制台打印日志

4.项目主方法内容

  1. @SpringBootApplication
  2. @MapperScan("com.example.demo.dao") //dao接口位置
  3. public class SpringbootMybatisplusApplication {
  4. public static void main(String[] args) {
  5. SpringApplication.run(SpringbootMybatisplusApplication.class, args);
  6. }
  7. }

5.dao接口层内容

  1. //此处继承BaseMapper实现mybatisplus的基础操作
  2. public interface UserDao extends BaseMapper<User> {
  3. }

6.service服务处方法

  1. @Service
  2. public class UserService {
  3. @Resource
  4. UserDao userDao;
  5. public User findUserById(Integer userId){
  6. return userDao.selectById(userId);
  7. }
  8. public List<User> selectUserList(){
  9. return userDao.selectList(null);
  10. }
  11. public void insertUser(User user){
  12. userDao.insert(user);
  13. }
  14. public void updateUser(User user){
  15. userDao.updateById(user);
  16. }
  17. public List<User> findUserListByQuery(User user){
  18. //查询条件
  19. QueryWrapper<User> wrapper = new QueryWrapper<>();
  20. //模糊查询
  21. wrapper.like("user_name",user.getUserName());
  22. wrapper.like("idCardNumber",user.getIdCardNumber());
  23. return userDao.selectList(wrapper);
  24. }
  25. }

7.User实体类

  1. @TableName("user")
  2. @Data
  3. public class User {
  4. @TableId(type = IdType.ASSIGN_ID)
  5. private Integer id;
  6. @TableField("user_name")
  7. private String userName;
  8. @TableField("idCardNumber")
  9. private String idCardNumber;
  10. @TableField("phone_number")
  11. private String phoneNumber;
  12. @TableField("dept_id")
  13. private Integer deptId;
  14. /**
  15. * 新增时填入时间
  16. */
  17. @TableField(fill = FieldFill.INSERT)
  18. private Date createTime;
  19. /**
  20. * 新增或修改时填入数据
  21. */
  22. @TableField(fill = FieldFill.INSERT_UPDATE)
  23. private Date updateTime;
  24. }

注意:

  1. @KeySequence(value = "SEQ_TEMPLATE_PARAM") 注解中是当主键id的生成策略为序列时使用
  2. @TableFiled(exist = false) 说明此字段不为表中字段

springBoot学习笔记(2.6)——- 整合mybatisPlus - 图1

springBoot学习笔记(2.6)——- 整合mybatisPlus - 图2

8.插件配置类

  1. /**
  2. * @description:插件配置类
  3. * @author: xiaYZ
  4. * @createDate: 2022/2/3
  5. * @version: 1.0
  6. */
  7. @Component
  8. public class MyHandle implements MetaObjectHandler {
  9. /**
  10. * 新增时填充策略
  11. */
  12. @Override
  13. public void insertFill(MetaObject metaObject) {
  14. this.setFieldValByName("createTime",new Date(),metaObject);
  15. this.setFieldValByName("updateTime",new Date(),metaObject);
  16. }
  17. /**
  18. * 修改时填充策略
  19. */
  20. @Override
  21. public void updateFill(MetaObject metaObject) {
  22. this.setFieldValByName("updateTime",new Date(),metaObject);
  23. }
  24. }

9. Controller控制层内容

  1. /**
  2. * @description:
  3. * @author: xiaYZ
  4. * @createDate: 2022/2/2
  5. * @version: 1.0
  6. */
  7. @Controller
  8. @RequestMapping("userController")
  9. public class UserController {
  10. @Resource
  11. UserService userService;
  12. /***
  13. * description: findUserById
  14. * version: 1.0 ->
  15. * date: 2022/2/2 10:37
  16. * author: xiaYZ
  17. * iteration: 迭代说明
  18. * @param userId
  19. * @return java.lang.String
  20. */
  21. @ResponseBody
  22. @RequestMapping("findUserById")
  23. @Description("查询用户信息通过id")
  24. public String findUserById(Integer userId){
  25. User user = new User();
  26. try{
  27. user = userService.findUserById(userId);
  28. }catch (Exception e){
  29. e.printStackTrace();
  30. }
  31. return user.toString();
  32. }
  33. /***
  34. * description: insertUser
  35. * version: 1.0 ->
  36. * date: 2022/2/3 11:32
  37. * author: xiaYZ
  38. * iteration: 迭代说明
  39. * @param user
  40. * @return java.lang.String
  41. */
  42. @ResponseBody
  43. @PostMapping("insertUser")
  44. @Description("新增用户")
  45. public String insertUser(User user){
  46. try{
  47. userService.insertUser(user);
  48. }catch (Exception e){
  49. e.printStackTrace();
  50. return "新增用户失败";
  51. }
  52. return "新增用户成功";
  53. }
  54. /****
  55. * description: updateUser
  56. * version: 1.0 ->
  57. * date: 2022/2/3 11:54
  58. * author: xiaYZ
  59. * iteration: 迭代说明
  60. * @param user
  61. * @return java.lang.String
  62. */
  63. @ResponseBody
  64. @PutMapping("updateUser")
  65. @Description("修改用户")
  66. public String updateUser(User user){
  67. try{
  68. userService.updateUser(user);
  69. }catch (Exception e){
  70. e.printStackTrace();
  71. return "修改用户失败";
  72. }
  73. return "修改用户成功";
  74. }
  75. /*****
  76. * description: findUserListByQuery
  77. * version: 1.0 ->
  78. * date: 2022/2/7 14:01
  79. * author: xiaYZ
  80. * iteration: 迭代说明
  81. * @param user
  82. * @return java.util.List<com.example.demo.entity.User>
  83. */
  84. @ResponseBody
  85. @RequestMapping("findUserListByQuery")
  86. @Description("条件查询")
  87. public List<User> findUserListByQuery(User user){
  88. List<User> list = new ArrayList<>();
  89. try{
  90. list = userService.findUserListByQuery(user);
  91. }catch (Exception e){
  92. e.printStackTrace();
  93. }
  94. return list;
  95. }
  96. }

三、项目运行截图

springBoot学习笔记(2.6)——- 整合mybatisPlus - 图3

总结

  1. 此次项目只是简单的springBoo整合mybatisplus,只涉及简单的CRUD操作
  2. 更多详细功能操作请查询官网mybatisPlus

项目源码