赞
踩
前言:本篇文章完整详细介绍了SpringBoot整合MyBatis-Plus的完整过程,这边我的SpringBoot版本是2.4版本、JDK1.8和Maven3.8.1版本,跟着文章一步步走就可以了,MyBatis-Plus整合非常方便,也是现在企业开发中经常会用的一个持久层框架。
目录
在pom.xml文件中引入如下依赖:
- <!-- MyBatis-Plus -->
- <dependency>
- <groupId>com.baomidou</groupId>
- <artifactId>mybatis-plus-boot-starter</artifactId>
- <version>3.5.1</version>
- </dependency>
-
- <!-- 数据库驱动 -->
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <version>8.0.28</version>
- </dependency>
-
- <!-- Lombok (可选) -->
- <dependency>
- <groupId>org.projectlombok</groupId>
- <artifactId>lombok</artifactId>
- <version>1.18.24</version>
- <scope>provided</scope>
- </dependency>

完整代码:
server: port: 9090 spring: datasource: username: 用户名 password: 密码 url: jdbc:mysql://ip地址:3306/数据库?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC driver-class-name: com.mysql.cj.jdbc.Driver mybatis-plus: # 搜索指定包别名(改成自己的) typeAliasesPackage: com.example.mybatisplus.domain # 配置mapper的扫描,找到所有的mapper.xml映射文件 mapperLocations: classpath*:mapper/**/*Mapper.xml configuration: # 其他 MyBatis 配置 map-underscore-to-camel-case: true # 将数据库下划线转为驼峰命名 cache-enabled: false # 是否开启二级缓存 lazy-loading-enabled: true # 是否开启懒加载 log-impl: org.apache.ibatis.logging.stdout.StdOutImpl # 设置Mybatis的日志实现类为控制台输出
实现Service、Mapper和Xml三个核心文件,先看一下项目整体结构:
- package com.example.mybatisplus.domain;
-
- import com.baomidou.mybatisplus.annotation.IdType;
- import com.baomidou.mybatisplus.annotation.TableId;
- import lombok.AllArgsConstructor;
- import lombok.Data;
- import lombok.NoArgsConstructor;
- import java.util.Date;
- import com.fasterxml.jackson.annotation.JsonFormat;
-
- @Data
- @AllArgsConstructor
- @NoArgsConstructor
- public class Student {
-
- /**
- * 主键
- */
- @TableId(type = IdType.AUTO)
- private Integer id;
-
-
- /**
- * 姓名
- */
-
- private String name;
-
-
- /**
- * 性别
- */
-
- private String sex;
-
-
- /**
- * 电话
- */
-
- private String phone;
-
-
- /**
- * 地址
- */
-
- private String address;
-
-
- /**
- * 电子邮箱
- */
-
- private String email;
-
-
- /**
- * 入学时间
- */
- @JsonFormat(pattern = "yyyy-MM-dd")
- private Date registerTime;
-
-
- /**
- * 毕业时间
- */
- @JsonFormat(pattern = "yyyy-MM-dd")
- private Date graduateTime;
-
-
- /**
- * 学号
- */
-
- private String number;
-
-
- /**
- * 所属班级
- */
-
- private String className;
-
-
- /**
- * 用户ID
- */
-
- private Integer userId;
-
- }
-
-
-

- package com.example.mybatisplus.mapper;
-
- import java.util.List;
- import com.baomidou.mybatisplus.core.mapper.BaseMapper;
- import com.example.mybatisplus.domain.Student;
- import org.apache.ibatis.annotations.Mapper;
- import org.springframework.stereotype.Repository;
-
- @Mapper
- @Repository
- public interface StudentMapper extends BaseMapper<Student>
- {
-
- /**
- * 查询
- * @param student
- * @return
- */
- public List<Student> selectStudentList(Student student);
-
- }

- package com.example.mybatisplus.service;
-
- import javax.annotation.Resource;
- import java.util.List;
- import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
- import com.example.mybatisplus.domain.Student;
- import com.example.mybatisplus.mapper.StudentMapper;
- import org.springframework.stereotype.Service;
-
- @Service
- public class StudentService extends ServiceImpl<StudentMapper, Student> {
-
- @Resource
- private StudentMapper studentMapper;
-
- public List<Student> selectStudentList(Student student) {
- return studentMapper.selectStudentList(student);
- }
-
-
- }

4.4、XML文件
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE mapper
- PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
- "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
- <mapper namespace="com.example.mybatisplus.mapper.StudentMapper">
-
- <select id="selectStudentList" resultType="Student">
- select * from student
- <where>
- <if test="id != null "> and id = #{id} </if>
- <if test="name != null and name != ''"> and name like concat('%', #{name}, '%') </if>
- <if test="sex != null and sex != ''"> and sex like concat('%', #{sex}, '%') </if>
- <if test="phone != null and phone != ''"> and phone like concat('%', #{phone}, '%') </if>
- <if test="address != null and address != ''"> and address like concat('%', #{address}, '%') </if>
- <if test="email != null and email != ''"> and email like concat('%', #{email}, '%') </if>
- <if test="registerTime != null "> and register_time = #{registerTime} </if>
- <if test="graduateTime != null "> and graduate_time = #{graduateTime} </if>
- <if test="number != null and number != ''"> and number like concat('%', #{number}, '%') </if>
- <if test="className != null and className != ''"> and class_name like concat('%', #{className}, '%') </if>
- <if test="userId != null "> and user_id = #{userId} </if>
- </where>
- </select>
-
- </mapper>

以下是代码示例。
- @Resource
- private StudentService studentService;
-
- public void insert(){
- Student student = new Student();
- student.setName("张三");
- student.setSex("1");
- student.setPhone("18888888");
- student.setEmail("199999@qq.com");
- student.setRegisterTime(new Date());
- student.setGraduateTime(new Date());
- student.setNumber("187891124");
- student.setClassName("高三1班");
- student.setUserId(109);
- studentService.save(student);
- }

- @Resource
- private StudentService studentService;
-
- public void update(){
- //根据id获取
- Student student = studentService.getById(2);
- student.setName("李四");
- student.setSex("0");
- student.setPhone("15555555");
- student.setEmail("16556224@qq.com");
- studentService.updateById(student);
- }
- @Resource
- private StudentService studentService;
-
- public void delete(){
- studentService.removeById(1);
- }
- @Resource
- private StudentService studentService;
-
- public void select() {
- Student query = new Student();
- query.setName("张三");
- List<Student> studentList = studentService.selectStudentList(query);
- for(Student student : studentList){
- System.out.println(student);
- }
- }
- @Resource
- private StudentMapper studentMapper;
-
- public void query(){
- List<Integer> ids = new ArrayList<>();
- ids.add(9);
- ids.add(10);
- LambdaQueryWrapper<Student> lambdaQueryWrapper = new LambdaQueryWrapper<Student>();
- //小于当前时间
- lambdaQueryWrapper.lt(Student::getRegisterTime,new Date());
- //包含的id
- lambdaQueryWrapper.in(Student::getId,ids);
- List<Student> list = studentMapper.selectList(lambdaQueryWrapper);
- System.out.println(list);
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。