基于Springboot+Mybatis+Mysql的基本增删改查操作

基于Springboot+Mybatis+Mysql的基本增删改查操作 一、需求说明在Idea中基于SpringbootMybatisMysql进行基本的增删改查操作二、准备工作1. 新建项目配置Mybatis和Mysql在新建项目时勾选Mybatis FrameworkMySQL Driver和Lombok依赖配置Mybatis连接MySQL数据库在springboot项目中可以编写application.properties文件配置数据库连接信息。包括driver-class-name、url 、usernamepassword。#驱动类名称 spring.datasource.driver-class-namecom.mysql.cj.jdbc.Driver #数据库连接的url spring.datasource.urljdbc:mysql://localhost:3306/mybatis_test #连接数据库的用户名 spring.datasource.usernameroot #连接数据库的密码 spring.datasource.password1234562. 准备好需要用的数据库表部门表dept-- 部门管理 create table dept ( id int unsigned primary key auto_increment comment 主键ID, name varchar(10) not null unique comment 部门名称, create_time datetime not null comment 创建时间, update_time datetime not null comment 修改时间 ) comment 部门表 DEFAULT CHARSETutf8mb4;; -- 部门表测试数据 insert into dept (id, name, create_time, update_time) values (1, 学工部, now(), now()), (2, 教研部, now(), now()), (3, 咨询部, now(), now()), (4, 就业部, now(), now()), (5, 人事部, now(), now());员工表emp-- 员工管理 create table emp ( id int unsigned primary key auto_increment comment ID, username varchar(20) not null unique comment 用户名, password varchar(32) default 123456 comment 密码, name varchar(10) not null comment 姓名, gender tinyint unsigned not null comment 性别, 说明: 1 男, 2 女, image varchar(300) comment 图像, job tinyint unsigned comment 职位, 说明: 1 班主任,2 讲师, 3 学工主管, 4 教研主管, 5 咨询师, entrydate date comment 入职时间, dept_id int unsigned comment 部门ID, create_time datetime not null comment 创建时间, update_time datetime not null comment 修改时间 ) comment 员工表; -- 员工表测试数据 INSERT INTO emp (id, username, password, name, gender, image, job, entrydate, dept_id, create_time, update_time) VALUES (1, jinyong, 123456, 金庸, 1, 1.jpg, 4, 2000-01-01, 2, now(), now()), (2, zhangwuji, 123456, 张无忌, 1, 2.jpg, 2, 2015-01-01, 2, now(), now()), (3, yangxiao, 123456, 杨逍, 1, 3.jpg, 2, 2008-05-01, 2, now(), now()), (4, weiyixiao, 123456, 韦一笑, 1, 4.jpg, 2, 2007-01-01, 2, now(), now()), (5, changyuchun, 123456, 常遇春, 1, 5.jpg, 2, 2012-12-05, 2, now(), now()), (6, xiaozhao, 123456, 小昭, 2, 6.jpg, 3, 2013-09-05, 1, now(), now()), (7, jixiaofu, 123456, 纪晓芙, 2, 7.jpg, 1, 2005-08-01, 1, now(), now()), (8, zhouzhiruo, 123456, 周芷若, 2, 8.jpg, 1, 2014-11-09, 1, now(), now()), (9, dingminjun, 123456, 丁敏君, 2, 9.jpg, 1, 2011-03-11, 1, now(), now()), (10, zhaomin, 123456, 赵敏, 2, 10.jpg, 1, 2013-09-05, 1, now(), now()), (11, luzhangke, 123456, 鹿杖客, 1, 11.jpg, 5, 2007-02-01, 3, now(), now()), (12, hebiweng, 123456, 鹤笔翁, 1, 12.jpg, 5, 2008-08-18, 3, now(), now()), (13, fangdongbai, 123456, 方东白, 1, 13.jpg, 5, 2012-11-01, 3, now(), now()), (14, zhangsanfeng, 123456, 张三丰, 1, 14.jpg, 2, 2002-08-01, 2, now(), now()), (15, yulianzhou, 123456, 俞莲舟, 1, 15.jpg, 2, 2011-05-01, 2, now(), now()), (16, songyuanqiao, 123456, 宋远桥, 1, 16.jpg, 2, 2010-01-01, 2, now(), now()), (17, chenyouliang, 123456, 陈友谅, 1, 17.jpg, NULL, 2015-03-21, NULL, now(), now());3. 创建实体类Emp 准备Mapper接口创建实体类Emp.java实体类属性采用驼峰命名其中用到的Data NoArgsConstructor AllArgsConstructor注解均是创建项目时引入的Lombok所实现的。Lombok通过注解的形式自动生成构造器、getter/setter、equals、hashcode、toString等方法并可以自动化生成日志变量简化java开发、提高效率。注解作用Getter/Setter为所有的属性提供get/set方法ToString会给类自动生成易阅读的 toString 方法EqualsAndHashCode根据类所拥有的非静态字段自动重写 equals 方法和 hashCode 方法Data提供了更综合的生成代码功能Getter Setter ToString EqualsAndHashCodeNoArgsConstructor为实体类生成无参的构造器方法AllArgsConstructor为实体类生成除了static修饰的字段之外带有各参数的构造器方法。import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import java.time.LocalDate; import java.time.LocalDateTime; Data NoArgsConstructor AllArgsConstructor public class Emp { private Integer id; private String username; private String password; private String name; private Short gender; private String image; private Short job; private LocalDate entrydate; //LocalDate类型对应数据表中的date类型 private Integer deptId; private LocalDateTime createTime;//LocalDateTime类型对应数据表中的datetime类型 private LocalDateTime updateTime; }准备接口EmpMapper/*Mapper注解表示当前接口为mybatis中的Mapper接口 程序运行时会自动创建接口的实现类对象(代理对象)并交给Spring的IOC容器管理 */ Mapper public interface EmpMapper { }项目工程目录如下4. 开启日志输入在 Mybatis 当中可以借助日志查看到 sql 语句的执行、执行传递的参数以及执行结果。在application.properties 文件输入#指定mybatis输出日志的位置, 输出控制台 mybatis.configuration.log-implorg.apache.ibatis.logging.stdout.StdOutImpl开启日志之后运行单元测试可以在控制台中看到输出了SQL 语句的相关信息三、删除功能实现根据主键删除数据Delete 注解用于编写 delete 操作的 SQL 语句。如果 mapper 接口方法形参只有一个普通类型的参数#{…} 里面的属性名可以随便写如#{id}、#{value}。但是建议保持名字一致。接口方法Mapper public interface EmpMapper { //Delete(delete from emp where id 17) //public void delete(); //以上delete操作的SQL语句中的id值写成固定的17就表示只能删除id17的用户数据 //SQL语句中的id值不能写成固定数值需要变为动态的数值 //解决方案在delete方法中添加一个参数(用户id)将方法中的参数传给SQL语句 /** * 根据id删除数据 * param id 用户id */ Delete(delete from emp where id #{id})//使用#{key}方式获取方法中的参数值 void delete(Integer id);测试类SpringBootTest class SpringbootMybatisCrudApplicationTests { Autowired //从Spring的IOC容器中获取类型是EmpMapper的对象并注入 private EmpMapper empMapper; Test public void testDel(){ //调用删除方法 empMapper.delete(16); }测试结果在输出的 SQL 语句delete from emp where id ?输入的参数 16 并没有在后面拼接id 的值是使用 ? 进行占位这种 SQL 语句即预编译 SQL。预编译 SQL 有两个优势性能更高更安全(防止 SQL 注入)。四、新增功能实现1. 基本新增接口方法Mapper public interface EmpMapper { Insert(insert into emp(username, name, gender, image, job, entrydate, dept_id, create_time, update_time) values (#{username}, #{name}, #{gender}, #{image}, #{job}, #{entrydate}, #{deptId}, #{createTime}, #{updateTime})) public void insert(Emp emp); }测试类Test public void testInsert(){ //创建员工对象 Emp emp new Emp(); emp.setUsername(tom); emp.setName(汤姆); emp.setImage(1.jpg); emp.setGender((short)1); emp.setJob((short)1); emp.setEntrydate(LocalDate.of(2000,1,1)); emp.setCreateTime(LocalDateTime.now()); emp.setUpdateTime(LocalDateTime.now()); emp.setDeptId(1); //调用添加方法 empMapper.insert(emp); }2. 主键返回在数据添加成功后需要获取插入数据库数据的主键接口方法//Options会自动将生成的主键值赋值给emp对象的id属性 Options(useGeneratedKeys true,keyProperty id) Insert(insert into emp(username, name, gender, image, job, entrydate, dept_id, create_time, update_time) values (#{username}, #{name}, #{gender}, #{image}, #{job}, #{entrydate}, #{deptId}, #{createTime}, #{updateTime})) void insert(Emp emp);测试类Test public void testInsert(){ //创建员工对象 Emp emp new Emp(); emp.setUsername(tom4); emp.setName(汤姆4); emp.setImage(1.jpg); emp.setGender((short)1); emp.setJob((short)1); emp.setEntrydate(LocalDate.of(2000,1,1)); emp.setCreateTime(LocalDateTime.now()); emp.setUpdateTime(LocalDateTime.now()); emp.setDeptId(1); //调用添加方法 empMapper.insert(emp); System.out.println(emp.getDeptId()); }日志输出五、更新功能实现接口方法//根据ID更新 Update (update emp set username#{username}, name#{name}, gender#{gender}, image#{image}, job#{job}, entrydate#{entrydate}, dept_id#{deptId}, update_time#{updateTime} where id#{id}) void update(Emp emp);测试类Test public void testUpdate(){ 要修改的员工信息 Emp emp new Emp(); emp.setId(23); emp.setUsername(songdaxia); emp.setPassword(null); emp.setName(老宋); emp.setImage(2.jpg); emp.setGender((short)1); emp.setJob((short)2); emp.setEntrydate(LocalDate.of(2012,1,1)); emp.setCreateTime(null); emp.setUpdateTime(LocalDateTime.now()); emp.setDeptId(2); 调用方法修改员工数据 empMapper.update(emp); }日志输出六、 查询操作实现1. 根据ID查询接口方法//查询操作 Select(select id, username, password, name, gender, image, job, entrydate, dept_id, create_time, update_time from emp where id#{id}) public Emp getById(Integer id);测试类Test public void testGetById(){ //根据ID查询 Emp emp empMapper.getById(5); System.out.println(emp); }执行结果2. 条件查询需求描述要实现的查询姓名要求支持模糊匹配性别要求精确匹配入职时间要求进行范围查询根据最后修改时间进行降序排序接口方式使用 MySQL 提供的字符串拼接函数concat(% , 关键字 , %)解决SQL注入风险Mapper public interface EmpMapper { Select(select * from emp where name like concat(%,#{name},%) and gender #{gender} and entrydate between #{begin} and #{end} order by update_time desc) public ListEmp list ( Param(name) String name, Param(gender) Short gender, Param(begin) LocalDate begin, Param(end) LocalDate end); }测试类Test public void testList2() { // 设置查询参数 String name 张; Short gender 1; LocalDate begin LocalDate.of(2020, 1, 1); LocalDate end LocalDate.of(2023, 12, 31); // 执行查询 ListEmp empList empMapper.list(name, gender, begin, end); }日志结果在上面所编写的条件查询功能中需要保证接口中方法的形参名和 SQL 语句中的参数占位符名相同。参数名在不同的 SpringBoot 版本中处理方案还不同在 springBoot 的 2.x 版本中保证参数名一致在 springBoot 的 1.x 版本中/单独使用 mybatis时使用Param 注解来指定 SQL 语句中的参数名七、Mybatis的XML配置文件Mybatis 的开发有两种方式注解和XML。使用 Mybatis 的注解方式主要是来完成一些简单的增删改查功能。如果需要实现复杂的 SQL 功能建议使用 XML 来配置映射语句也就是将 SQL 语句写在 XML 配置文件中。在 Mybatis 中使用 XML 映射文件方式开发需要符合一定的规范XML 映射文件的名称与 Mapper 接口名称一致并且将 XML 映射文件和 Mapper 接口放置在相同包下同包同名XML 映射文件的 namespace 属性为 Mapper 接口全限定名一致XML 映射文件中 sql 语句的 id 与 Mapper 接口中的方法名一致并保持返回类型一致。select标签就是用于编写 select 查询语句的。resultType 属性指的是查询返回的单条记录所封装的类型。编写XML配置文件XML 映射文件的 namespace 属性为 Mapper 接口的全限定名?xml version1.0 encodingUTF-8 ? !DOCTYPE mapper PUBLIC -//mybatis.org//DTD Mapper 3.0//EN https://mybatis.org/dtd/mybatis-3-mapper.dtd mapper namespaceedu.wust.springbootmybatiscrud.mapper.EmpMapper /mapperXML 映射文件中 sql 语句的 id 与 Mapper 接口中的方法名一致并保持返回类型一致此处以前面的查询操作为例?xml version1.0 encodingUTF-8 ? !DOCTYPE mapper PUBLIC -//mybatis.org//DTD Mapper 3.0//EN https://mybatis.org/dtd/mybatis-3-mapper.dtd mapper namespaceedu.wust.springbootmybatiscrud.mapper.EmpMapper !--查询操作-- select idlist resultTypeedu.wust.springbootmybatiscrud.pojo.Emp select * from emp where name like concat(%,#{name},%) and gender #{gender} and entrydate between #{begin} and #{end} order by update_time desc /select /mapper日志结果八、Mybatis动态SQL在刚才编写的 SQL 语句中可以看到三个条件直接写死了。eg.如果页面只传递了参数姓名 name 字段其他两个字段“性别”和“入职时间”没有传递那么这两个参数的值就是 null。而正确的做法应该是传递了参数再组装这个查询条件如果没有传递参数就不应该组装这个查询条件。如此SQL 语句会随着用户的输入或外部条件的变化而变化称为动态 SQL。在Mybatis 中提供了很多实现动态 SQL 的标签。1. 动态 SQL-ifif用于判断条件是否成立。使用 test 属性进行条件判断如果条件为 true则拼接 SQL。基本样式if test条件表达式 要拼接的sql语句 /if通过if标签来改造之前的条件查询语句原语句见上面“编写XML配置文件”板块动态SQL语句如下其中使用where标签代替 SQL 语句中的 where 关键字where只会在子元素有内容的情况下才插入 where 子句而且会自动去除子句的开头的 AND 或 OR从而避免执行时多出关键字导致报错。!--动态SQL语句if/if -- !--使用where标签代替 SQL 语句中的 where 关键字,where只会在子元素有内容的情况下才插入 where 子句而且会自动去除子句的开头的 AND 或 OR-- select idlist resultTypeedu.wust.springbootmybatiscrud.pojo.Emp select * from emp where if testname ! null name like concat(%,#{name},%) /if if testgender ! null and gender #{gender} /if if testbegin ! null and end ! null and entrydate between #{begin} and #{end} /if /where order by update_time desc /select两种测试方法Test public void testList(){ //动态SQL语句进行查询性别数据为null、开始时间和结束时间也为null ListEmp list empMapper.list(张, null, null, null); for(Emp emp : list){ System.out.println(emp); } } Test public void testList1(){ //动态SQL语句进行查询姓名为null ListEmp list empMapper.list(null, (short)1, null, null); for(Emp emp : list){ System.out.println(emp); } }执行结果改造之前的更新操作。修改Mapper接口//删除Update注解编写的SQL语句 //update操作的SQL语句编写在Mapper映射文件中 public void update(Emp emp); }XML映射文件其中使用set标签代替 SQL 语句中的 set 关键字set动态的在 SQL 语句中插入 set 关键字并会删掉额外的逗号。用于 update 语句中!--更新操作-- !--使用set标签代替 SQL 语句中的 set 关键字,set动态的在 SQL 语句中插入 set 关键字并会删掉额外的逗号。用于 update 语句中)-- update idupdate update emp !-- 使用set标签代替update语句中的set关键字 -- set if testusername ! null username#{username}, /if if testname ! null name#{name}, /if if testgender ! null gender#{gender}, /if if testimage ! null image#{image}, /if if testjob ! null job#{job}, /if if testentrydate ! null entrydate#{entrydate}, /if if testdeptId ! null dept_id#{deptId}, /if if testupdateTime ! null update_time#{updateTime} /if /set where id#{id} /update测试类Test public void testUpdate2(){ //要修改的员工信息 Emp emp new Emp(); emp.setId(20); emp.setUsername(Tom222); //调用方法修改员工数据 empMapper.update(emp); }执行结果2. 动态 SQL-foreach使用foreach遍历 deleteByIds 方法中传递的参数 ids 集合foreach collection集合名称 item集合遍历出来的元素/项 separator每一次遍历使用的分隔符 open遍历开始前拼接的片段 close遍历结束后拼接的片段 /foreach实现数据的批量删除操作XML映射文件!--批量删除使用foreach遍历 deleteByIds 方法中传递的参数 ids 集合 格式 foreach collection集合名称 item集合遍历出来的元素/项 separator每一次遍历使用的分隔符 open遍历开始前拼接的片段 close遍历结束后拼接的片段 /foreach -- delete iddeleteByIds delete from emp where id in foreach collectionids itemid separator, open( close) #{id} /foreach /delete接口方法//批量删除 public void deleteByIds(Param(ids) ListInteger ids);测试类//批量删除 Test public void testDeleteByIds() { // 准备测试数据假设数据库中已有这些ID的记录 ListInteger idsToDelete Arrays.asList(11, 12, 13); // 执行删除操作 empMapper.deleteByIds(idsToDelete); }执行结果九、代码全览EmpMapper.javapackage edu.wust.springbootmybatiscrud.mapper; import edu.wust.springbootmybatiscrud.pojo.Emp; import org.apache.ibatis.annotations.*; import java.time.LocalDate; import java.util.List; /*Mapper注解表示当前接口为mybatis中的Mapper接口 程序运行时会自动创建接口的实现类对象(代理对象)并交给Spring的IOC容器管理 */ Mapper public interface EmpMapper { //Delete(delete from emp where id 17) //public void delete(); //以上delete操作的SQL语句中的id值写成固定的17就表示只能删除id17的用户数据 //SQL语句中的id值不能写成固定数值需要变为动态的数值 //解决方案在delete方法中添加一个参数(用户id)将方法中的参数传给SQL语句 /** * 根据id删除数据 * param id 用户id */ Delete(delete from emp where id #{id})//使用#{key}方式获取方法中的参数值 void delete(Integer id); //Options会自动将生成的主键值赋值给emp对象的id属性 Options(useGeneratedKeys true,keyProperty id) Insert(insert into emp(username, name, gender, image, job, entrydate, dept_id, create_time, update_time) values (#{username}, #{name}, #{gender}, #{image}, #{job}, #{entrydate}, #{deptId}, #{createTime}, #{updateTime})) void insert(Emp emp); //根据ID更新 //Update (update emp set username#{username}, name#{name}, gender#{gender}, image#{image}, job#{job}, entrydate#{entrydate}, dept_id#{deptId}, update_time#{updateTime} where id#{id}) //void update(Emp emp); //完善更新员工功能修改为动态更新员工数据信息 //动态更新员工信息如果更新时传递有值则更新如果更新时没有传递值则不更新。解决方案动态 SQL //删除Update注解编写的SQL语句 //update操作的SQL语句编写在Mapper映射文件中 public void update(Emp emp); //查询操作 Select(select id, username, password, name, gender, image, job, entrydate, dept_id, create_time, update_time from emp where id#{id}) public Emp getById(Integer id); //要实现的查询 //姓名要求支持模糊匹配 //性别要求精确匹配 //入职时间要求进行范围查询 //根据最后修改时间进行降序排序 public ListEmp list( Param(name) String name, Param(gender) Short gender, Param(begin) LocalDate begin, Param(end) LocalDate end); //批量删除 public void deleteByIds(Param(ids) ListInteger ids); }Emp.javapackage edu.wust.springbootmybatiscrud.pojo; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import java.time.LocalDate; import java.time.LocalDateTime; Data NoArgsConstructor AllArgsConstructor public class Emp { private Integer id; private String username; private String password; private String name; private Short gender; private String image; private Short job; private LocalDate entrydate; //LocalDate类型对应数据表中的date类型 private Integer deptId; private LocalDateTime createTime;//LocalDateTime类型对应数据表中的datetime类型 private LocalDateTime updateTime; }EmpMapper.xml?xml version1.0 encodingUTF-8 ? !DOCTYPE mapper PUBLIC -//mybatis.org//DTD Mapper 3.0//EN https://mybatis.org/dtd/mybatis-3-mapper.dtd mapper namespaceedu.wust.springbootmybatiscrud.mapper.EmpMapper !--查询操作-- !--原有的SQL语句 select idlist resultTypeedu.wust.springbootmybatiscrud.pojo.Emp select * from emp where name like concat(%,#{name},%) and gender #{gender} and entrydate between #{begin} and #{end} order by update_time desc /select -- !--动态SQL语句if/if -- !--使用where标签代替 SQL 语句中的 where 关键字,where只会在子元素有内容的情况下才插入 where 子句而且会自动去除子句的开头的 AND 或 OR-- select idlist resultTypeedu.wust.springbootmybatiscrud.pojo.Emp select * from emp where if testname ! null name like concat(%,#{name},%) /if if testgender ! null and gender #{gender} /if if testbegin ! null and end ! null and entrydate between #{begin} and #{end} /if /where order by update_time desc /select !--更新操作-- !--使用set标签代替 SQL 语句中的 set 关键字,set动态的在 SQL 语句中插入 set 关键字并会删掉额外的逗号。用于 update 语句中)-- update idupdate update emp !-- 使用set标签代替update语句中的set关键字 -- set if testusername ! null username#{username}, /if if testname ! null name#{name}, /if if testgender ! null gender#{gender}, /if if testimage ! null image#{image}, /if if testjob ! null job#{job}, /if if testentrydate ! null entrydate#{entrydate}, /if if testdeptId ! null dept_id#{deptId}, /if if testupdateTime ! null update_time#{updateTime} /if /set where id#{id} /update !--批量删除使用foreach遍历 deleteByIds 方法中传递的参数 ids 集合 格式 foreach collection集合名称 item集合遍历出来的元素/项 separator每一次遍历使用的分隔符 open遍历开始前拼接的片段 close遍历结束后拼接的片段 /foreach -- delete iddeleteByIds delete from emp where id in foreach collectionids itemid separator, open( close) #{id} /foreach /delete /mapperSpringbootMybatisCrudApplicationTests.javapackage edu.wust.springbootmybatiscrud; import edu.wust.springbootmybatiscrud.mapper.EmpMapper; import edu.wust.springbootmybatiscrud.pojo.Emp; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import java.time.LocalDate; import java.time.LocalDateTime; import java.util.Arrays; import java.util.List; SpringBootTest class SpringbootMybatisCrudApplicationTests { Autowired //从Spring的IOC容器中获取类型是EmpMapper的对象并注入 private EmpMapper empMapper; Test public void testDel(){ //调用删除方法 empMapper.delete(16); } //批量删除 Test public void testDeleteByIds() { // 准备测试数据假设数据库中已有这些ID的记录 ListInteger idsToDelete Arrays.asList(11, 12, 13); // 执行删除操作 empMapper.deleteByIds(idsToDelete); } Test public void testInsert(){ //创建员工对象 Emp emp new Emp(); emp.setUsername(tom4); emp.setName(汤姆4); emp.setImage(1.jpg); emp.setGender((short)1); emp.setJob((short)1); emp.setEntrydate(LocalDate.of(2000,1,1)); emp.setCreateTime(LocalDateTime.now()); emp.setUpdateTime(LocalDateTime.now()); emp.setDeptId(1); //调用添加方法 empMapper.insert(emp); System.out.println(emp.getDeptId()); } //原SQL语句执行更新员工的操作 Test public void testUpdate(){ //要修改的员工信息 Emp emp new Emp(); emp.setId(23); emp.setUsername(songdaxia); emp.setPassword(null); emp.setName(老宋); emp.setImage(2.jpg); emp.setGender((short)1); emp.setJob((short)2); emp.setEntrydate(LocalDate.of(2012,1,1)); emp.setCreateTime(null); emp.setUpdateTime(LocalDateTime.now()); emp.setDeptId(2); //调用方法修改员工数据 empMapper.update(emp); } //动态SQL语句执行更新员工操作 Test public void testUpdate1(){ //要修改的员工信息 Emp emp new Emp(); emp.setId(20); emp.setUsername(Tom111); emp.setName(汤姆111); emp.setUpdateTime(LocalDateTime.now()); //调用方法修改员工数据 empMapper.update(emp); } Test public void testUpdate2(){ //要修改的员工信息 Emp emp new Emp(); emp.setId(20); emp.setUsername(Tom222); //调用方法修改员工数据 empMapper.update(emp); } Test public void testGetById(){ //根据ID查询 Emp emp empMapper.getById(5); System.out.println(emp); } //原有的SQL语句进行条件查询 Test public void testList2() { // 设置查询参数 String name 鹿; Short gender 1; LocalDate begin LocalDate.of(2001, 1, 1); LocalDate end LocalDate.of(2023, 12, 31); // 执行查询 ListEmp empList empMapper.list(name, gender, begin, end); } Test public void testList(){ //动态SQL语句进行查询性别数据为null、开始时间和结束时间也为null ListEmp list empMapper.list(张, null, null, null); for(Emp emp : list){ System.out.println(emp); } } Test public void testList1(){ //动态SQL语句进行查询姓名为null ListEmp list empMapper.list(null, (short)1, null, null); for(Emp emp : list){ System.out.println(emp); } } }