MyBatis一對一級聯(lián)更新問題小結(jié)
一、概述
日常工作中經(jīng)常會涉及到一對一級聯(lián)更新的問題,例如:更新員工信息時也需要更新員工所在的部門信息,那么這種情況下就涉及到了MyBatis的級聯(lián)更新問題。
二、代碼實戰(zhàn)
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> <!-- 數(shù)據(jù)源 --> <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: 9999 spring: datasource: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/20231018_redis?useSSL=false&useUnicode=true&characterEncoding=UTF8&serverTimezone=GMT username: root password: 123456 mybatis: mapper-locations: classpath:mapper/*.xml type-aliases-package: org.stat.entity.model configuration: map-underscore-to-camel-case: true logging: 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 { /** * 級聯(lián)更新員工信息(更新員工信息 & 部門信息) * @param param */ void updateEmployeeCascade(EmployeeDO param); }
2.7、EmployeeMapper.xml
<!DOCTYPE mapper PUBLIC "-//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 d on 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 { @Autowired private EmployeeMapper employeeMapper; @Test public void updateEmployeeTest() { EmployeeDO employeeDO = new EmployeeDO() .setId(2) .setName("劉亦菲") .setAge(18) .setDepartment( new DepartmentDO() .setId(2) .setDepartmentName("市場部222") ); employeeMapper.updateEmployeeCascade(employeeDO); } }
到此這篇關(guān)于MyBatis一對一級聯(lián)更新問題小結(jié)的文章就介紹到這了,更多相關(guān)MyBatis一對一級聯(lián)更新內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java?Socket編程從零到實戰(zhàn)詳解(完整實戰(zhàn)案例)
這篇文章主要介紹了Java?Socket編程從零到實戰(zhàn)詳解,本文給大家介紹的非常詳細,感興趣的朋友一起看看吧2025-04-04RedisTemplate中opsForValue和opsForList方法的使用詳解
這篇文章主要介紹了RedisTemplate中opsForValue和opsForList方法的使用詳解,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08Java使用httpRequest+Jsoup爬取紅藍球號碼
本文將結(jié)合實例代碼,介紹Java使用httpRequest+Jsoup爬取紅藍球號碼,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-07-07SpringBoot解決數(shù)據(jù)庫時間和返回時間格式不一致的問題
這篇文章主要介紹了SpringBoot解決數(shù)據(jù)庫時間和返回時間格式不一致的問題,文章通過代碼示例和圖文結(jié)合的方式講解的非常詳細,對大家的學(xué)習(xí)和工作有一定的幫助,需要的朋友可以參考下2024-03-03