系列十、MyBatis更新数据后返回更新后的数据

一、概述

        日常的开发中经常涉及到执行完更新操作后,返回更新后的数据,这种场景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: 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 SpringbootRedisDistributeCacheAnnotationApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootRedisDistributeCacheAnnotationApplication.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、DepartmentMapper

/**
 * @Author : 一叶浮萍归大海
 * @Date: 2023/12/10 12:50
 * @Description:
 */
public interface DepartmentMapper {

    /**
     * 更新Department
     * @param department
     * @return
     */
    int updateDepartment(DepartmentDO department);

}

2.6、DepartmentMapper.xml

<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="org.star.mapper.DepartmentMapper">

    <update id="updateDepartment" useGeneratedKeys="true" keyProperty="id">
        update department set department_name = #{departmentName} where id = #{id}
        <selectKey resultType="org.star.entity.model.DepartmentDO" order="AFTER" keyProperty="id">
            select id,department_name from department where id = #{id}
        </selectKey>
    </update>

</mapper>

2.7、DepartmentMapperTest

/**
 * @Author : 一叶浮萍归大海
 * @Date: 2023/12/10 20:07
 * @Description:
 */
@SpringBootTest
public class DepartmentServiceTest {

    @Resource
    private DepartmentService departmentService;

    @Test
    public void updateDepartmentTest() {
        DepartmentDO department = new DepartmentDO().setDepartmentName("研发部444").setId(1);
        DepartmentDO updatedDepartment = departmentService.updateDepartment(department);
        System.out.println("updatedDepartment = " + updatedDepartment);
    }
	
}

三、参考

https://www.5axxw.com/questions/simple/eyyqh0

  • 10
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Mybatis是一种流行的ORM(对象关系映射器)框架,它为Java开发者提供了许多便利的API和注解,以简化数据库编程。在Mybatis中,更新数据也是一项常见的任务,而有时我们需要在更新数据返回当前主键。这样做的好处是可以避免频繁地查询数据库,从而提高程序的性能。 那么,如何在Mybatis中实现更新数据返回当前主键呢?其实很简单,只需要在Mapper.xml文件中添加useGeneratedKeys和keyProperty属性即可。 useGeneratedKeys属性告诉Mybatis使用数据库自动生成的键作为主键,而keyProperty属性则指定了要返回键的名称。例如,如果我们要更新一个用户的信息并返回其主键,可以这样写: ```xml <update id="updateUser" parameterType="User"> update users set name=#{name}, email=#{email}, password=#{password} where id=#{id} </update> ``` ```xml <update id="updateUser" parameterType="User" useGeneratedKeys="true" keyProperty="id"> update users set name=#{name}, email=#{email}, password=#{password} where id=#{id} </update> ``` 这样做的好处是可以省去一次查询,在更新后直接返回主键。但需要注意的是,这种方法只适用于数据库自动生成的主键,如果我们要使用自定义的主键,就需要另外的处理方式。 以上就是在Mybatis更新数据返回当前主键的方法,它不仅可以提高程序的性能,还可以简化程序的逻辑。所以在实际项目中,大家可以尝试使用这种方式来处理更新数据的操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值