一、概述
日常工作中经常会涉及到一对一级联更新的问题,例如:更新员工信息时也需要更新员工所在的部门信息,那么这种情况下就涉及到了MyBatis的级联更新问题。
二、代码实战
2.1、pom
<dependencies><!-- springboot --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId></dependency><!-- 数据源 --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.26</version></dependency><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.3.1</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-starter</artifactId><version>1.1.10</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency><!-- 工具 --><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.30</version></dependency><dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>5.8.21</version></dependency><dependency><groupId>org.apache.commons</groupId><artifactId>commons-lang3</artifactId></dependency><dependency><groupId>org.apache.commons</groupId><artifactId>commons-collections4</artifactId><version>4.4</version></dependency><dependency><groupId>com.alibaba.fastjson2</groupId><artifactId>fastjson2</artifactId><version>2.0.25</version></dependency></dependencies>
2.2、yml
server:port: 9999spring:datasource:type: com.alibaba.druid.pool.DruidDataSourcedriver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/20231018_redis?useSSL=false&useUnicode=true&characterEncoding=UTF8&serverTimezone=GMTusername: rootpassword: 123456mybatis:mapper-locations: classpath:mapper/*.xmltype-aliases-package: org.stat.entity.modelconfiguration:map-underscore-to-camel-case: truelogging:level:org:star:mapper: debug
2.3、主启动
/*** @Author : 一叶浮萍归大海* @Date: 2023/12/10 12:44* @Description:**/
@MapperScan(basePackages = "org.star.mapper")
@SpringBootApplication
public class SpringbootMybatisApplication {public static void main(String[] args) {SpringApplication.run(SpringbootMybatisApplication.class, args);}}
2.4、DepartmentDO
/*** @Author : 一叶浮萍归大海* @Date: 2023/12/10 12:48* @Description:*/
@Data
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
@ToString(callSuper = true)
public class DepartmentDO implements Serializable {/*** 编号*/private Integer id;/*** 部门名称*/private String departmentName;}
2.5、EmployeeDO
/*** @Author : 一叶浮萍归大海* @Date: 2023/12/10 15:38* @Description:*/
@Data
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
@ToString(callSuper = true)
public class EmployeeDO implements Serializable {/*** 员工编号*/private Integer id;/*** 姓名*/private String name;/*** 年龄*/private Integer age;/*** 部门*/private DepartmentDO department;}
2.6、EmployeeMapper
/*** @Author : 一叶浮萍归大海* @Date: 2023/12/10 15:40* @Description:*/
public interface EmployeeMapper {/*** 级联更新员工信息(更新员工信息 & 部门信息)* @param param*/void updateEmployeeCascade(EmployeeDO param);}
2.7、EmployeeMapper.xml
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="org.star.mapper.EmployeeMapper"><update id="updateEmployeeCascade">update employee e left join department don e.department_id = d.id<set><if test="name != null and name != ''">e.name = #{name},</if><if test="age != null">e.age = #{age},</if><if test="department.departmentName != null and department.departmentName != ''">d.department_name = #{department.departmentName}</if></set>where e.id = #{id}</update></mapper>
2.8、EmployeeMapperTest
/*** @Author : 一叶浮萍归大海* @Date: 2023/12/10 15:42* @Description:*/
@SpringBootTest
public class EmployeeMapperTest {@Autowiredprivate EmployeeMapper employeeMapper;@Testpublic void updateEmployeeTest() {EmployeeDO employeeDO = new EmployeeDO().setId(2).setName("刘亦菲").setAge(18).setDepartment(new DepartmentDO().setId(2).setDepartmentName("市场部222"));employeeMapper.updateEmployeeCascade(employeeDO);}}