当前位置:   article > 正文

SpringBoot整合MyBatis-Plus完整详细版(提供Gitee源码)

SpringBoot整合MyBatis-Plus完整详细版(提供Gitee源码)

前言:本篇文章完整详细介绍了SpringBoot整合MyBatis-Plus的完整过程,这边我的SpringBoot版本是2.4版本、JDK1.8和Maven3.8.1版本,跟着文章一步步走就可以了,MyBatis-Plus整合非常方便,也是现在企业开发中经常会用的一个持久层框架。

目录

一、导入依赖

二、yml配置文件

三、实现核心逻辑

3.1、新增实体类

3.2、新增持久层

3.3、实现服务层

四、实现增删查改 

4.1、新增数据

4.2、修改数据

4.3、删除数据

4.4、基础查询

4.5、自定义查询

五、Gitee源码 


一、导入依赖

在pom.xml文件中引入如下依赖:

  1. <!-- MyBatis-Plus -->
  2. <dependency>
  3. <groupId>com.baomidou</groupId>
  4. <artifactId>mybatis-plus-boot-starter</artifactId>
  5. <version>3.5.1</version>
  6. </dependency>
  7. <!-- 数据库驱动 -->
  8. <dependency>
  9. <groupId>mysql</groupId>
  10. <artifactId>mysql-connector-java</artifactId>
  11. <version>8.0.28</version>
  12. </dependency>
  13. <!-- Lombok (可选) -->
  14. <dependency>
  15. <groupId>org.projectlombok</groupId>
  16. <artifactId>lombok</artifactId>
  17. <version>1.18.24</version>
  18. <scope>provided</scope>
  19. </dependency>

二、yml配置文件

完整代码:

  1. server:
  2. port: 9090
  3. spring:
  4. datasource:
  5. username: 用户名
  6. password: 密码
  7. url: jdbc:mysql://ip地址:3306/数据库?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
  8. driver-class-name: com.mysql.cj.jdbc.Driver
  9. mybatis-plus:
  10. # 搜索指定包别名(改成自己的)
  11. typeAliasesPackage: com.example.mybatisplus.domain
  12. # 配置mapper的扫描,找到所有的mapper.xml映射文件
  13. mapperLocations: classpath*:mapper/**/*Mapper.xml
  14. configuration:
  15. # 其他 MyBatis 配置
  16. map-underscore-to-camel-case: true # 将数据库下划线转为驼峰命名
  17. cache-enabled: false # 是否开启二级缓存
  18. lazy-loading-enabled: true # 是否开启懒加载
  19. log-impl: org.apache.ibatis.logging.stdout.StdOutImpl # 设置Mybatis的日志实现类为控制台输出

三、实现核心逻辑

实现Service、Mapper和Xml三个核心文件,先看一下项目整体结构:

3.1、新增实体类

  1. package com.example.mybatisplus.domain;
  2. import com.baomidou.mybatisplus.annotation.IdType;
  3. import com.baomidou.mybatisplus.annotation.TableId;
  4. import lombok.AllArgsConstructor;
  5. import lombok.Data;
  6. import lombok.NoArgsConstructor;
  7. import java.util.Date;
  8. import com.fasterxml.jackson.annotation.JsonFormat;
  9. @Data
  10. @AllArgsConstructor
  11. @NoArgsConstructor
  12. public class Student {
  13. /**
  14. * 主键
  15. */
  16. @TableId(type = IdType.AUTO)
  17. private Integer id;
  18. /**
  19. * 姓名
  20. */
  21. private String name;
  22. /**
  23. * 性别
  24. */
  25. private String sex;
  26. /**
  27. * 电话
  28. */
  29. private String phone;
  30. /**
  31. * 地址
  32. */
  33. private String address;
  34. /**
  35. * 电子邮箱
  36. */
  37. private String email;
  38. /**
  39. * 入学时间
  40. */
  41. @JsonFormat(pattern = "yyyy-MM-dd")
  42. private Date registerTime;
  43. /**
  44. * 毕业时间
  45. */
  46. @JsonFormat(pattern = "yyyy-MM-dd")
  47. private Date graduateTime;
  48. /**
  49. * 学号
  50. */
  51. private String number;
  52. /**
  53. * 所属班级
  54. */
  55. private String className;
  56. /**
  57. * 用户ID
  58. */
  59. private Integer userId;
  60. }

3.2、新增持久层

  1. package com.example.mybatisplus.mapper;
  2. import java.util.List;
  3. import com.baomidou.mybatisplus.core.mapper.BaseMapper;
  4. import com.example.mybatisplus.domain.Student;
  5. import org.apache.ibatis.annotations.Mapper;
  6. import org.springframework.stereotype.Repository;
  7. @Mapper
  8. @Repository
  9. public interface StudentMapper extends BaseMapper<Student>
  10. {
  11. /**
  12. * 查询
  13. * @param student
  14. * @return
  15. */
  16. public List<Student> selectStudentList(Student student);
  17. }

3.3、实现服务层

  1. package com.example.mybatisplus.service;
  2. import javax.annotation.Resource;
  3. import java.util.List;
  4. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  5. import com.example.mybatisplus.domain.Student;
  6. import com.example.mybatisplus.mapper.StudentMapper;
  7. import org.springframework.stereotype.Service;
  8. @Service
  9. public class StudentService extends ServiceImpl<StudentMapper, Student> {
  10. @Resource
  11. private StudentMapper studentMapper;
  12. public List<Student> selectStudentList(Student student) {
  13. return studentMapper.selectStudentList(student);
  14. }
  15. }

 4.4、XML文件

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <mapper namespace="com.example.mybatisplus.mapper.StudentMapper">
  6. <select id="selectStudentList" resultType="Student">
  7. select * from student
  8. <where>
  9. <if test="id != null "> and id = #{id} </if>
  10. <if test="name != null and name != ''"> and name like concat('%', #{name}, '%') </if>
  11. <if test="sex != null and sex != ''"> and sex like concat('%', #{sex}, '%') </if>
  12. <if test="phone != null and phone != ''"> and phone like concat('%', #{phone}, '%') </if>
  13. <if test="address != null and address != ''"> and address like concat('%', #{address}, '%') </if>
  14. <if test="email != null and email != ''"> and email like concat('%', #{email}, '%') </if>
  15. <if test="registerTime != null "> and register_time = #{registerTime} </if>
  16. <if test="graduateTime != null "> and graduate_time = #{graduateTime} </if>
  17. <if test="number != null and number != ''"> and number like concat('%', #{number}, '%') </if>
  18. <if test="className != null and className != ''"> and class_name like concat('%', #{className}, '%') </if>
  19. <if test="userId != null "> and user_id = #{userId} </if>
  20. </where>
  21. </select>
  22. </mapper>

四、实现增删查改 

以下是代码示例。

4.1、新增数据

  1. @Resource
  2. private StudentService studentService;
  3. public void insert(){
  4. Student student = new Student();
  5. student.setName("张三");
  6. student.setSex("1");
  7. student.setPhone("18888888");
  8. student.setEmail("199999@qq.com");
  9. student.setRegisterTime(new Date());
  10. student.setGraduateTime(new Date());
  11. student.setNumber("187891124");
  12. student.setClassName("高三1班");
  13. student.setUserId(109);
  14. studentService.save(student);
  15. }

4.2、修改数据

  1. @Resource
  2. private StudentService studentService;
  3. public void update(){
  4. //根据id获取
  5. Student student = studentService.getById(2);
  6. student.setName("李四");
  7. student.setSex("0");
  8. student.setPhone("15555555");
  9. student.setEmail("16556224@qq.com");
  10. studentService.updateById(student);
  11. }

4.3、删除数据

  1. @Resource
  2. private StudentService studentService;
  3. public void delete(){
  4. studentService.removeById(1);
  5. }

4.4、基础查询

  1. @Resource
  2. private StudentService studentService;
  3. public void select() {
  4. Student query = new Student();
  5. query.setName("张三");
  6. List<Student> studentList = studentService.selectStudentList(query);
  7. for(Student student : studentList){
  8. System.out.println(student);
  9. }
  10. }

4.5、自定义查询

  1. @Resource
  2. private StudentMapper studentMapper;
  3. public void query(){
  4. List<Integer> ids = new ArrayList<>();
  5. ids.add(9);
  6. ids.add(10);
  7. LambdaQueryWrapper<Student> lambdaQueryWrapper = new LambdaQueryWrapper<Student>();
  8. //小于当前时间
  9. lambdaQueryWrapper.lt(Student::getRegisterTime,new Date());
  10. //包含的id
  11. lambdaQueryWrapper.in(Student::getId,ids);
  12. List<Student> list = studentMapper.selectList(lambdaQueryWrapper);
  13. System.out.println(list);
  14. }

五、Gitee源码 

源码地址:SpringBoot整合MyBatis-Plus完整详细版

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/987193
推荐阅读
相关标签
  

闽ICP备14008679号