mybatis 添加一条新数据并返回此数据的ID(主键)

通常数据库中表的主键是‘自动递增(mysql)’或’序列(oracle)‘,但插入数据后又要取得些条数据的ID(将ID做为主键)


利用Mybatis 的 selectKey来获得:

[html]  view plain  copy
  1. <!-- 添加部门 返回部门ID -->  
  2. <insert id="addDept" parameterType="com.demo.model.Department" keyProperty="id">  
  3.     <selectKey keyProperty='id' resultType='int' order='AFTER'  >  
  4.         select LAST_INSERT_ID();  
  5.     </selectKey>  
  6.      insert into department(<include refid="departmentAllField"/>)   
  7.         values(#{departmentId},#{departmentName},#{departmentManagerName},#{companyId});  
  8.  </insert>  

[html]  view plain  copy
  1. <insert id="addDept" parameterType="com.demo.model.Department" useGeneratedKeys="true" keyProperty="id">  
  2.   insert into department(<include refid="departmentAllField"/>)   
  3.    values(#{departmentId},#{departmentName},#{departmentManagerName},#{companyId});  
  4. </insert><span style="font-family: Arial, Helvetica, sans-serif;"></span>  

注意:insert 标签中的 keyProperty  和  selectKey标签块中的 LAET_INSERT_ID() ,另外 order属性 对于 oracl为 BEFORE; mysql为AFTER


实体类:

[java]  view plain  copy
  1. public class Department {  
  2.     private int id;  
  3.     private int departmentId;  
  4.     private String departmentName;  
  5.     private String departmentManagerName;  
  6.     private int companyId;  
  7.     private List<Employee> employees;  
  8.     //...GET SET ...  
  9. }  

测试:

[java]  view plain  copy
  1. @Test  
  2. public void testDao(){  
  3.     deptDao = session.getMapper(DepartmentDao.class);  
  4.   
  5.     Department department = new Department();  
  6.     department.setDepartmentName("ares");  
  7.     department.setDepartmentManagerName("tom");  
  8.     department.setDepartmentId(32);  
  9.     department.setCompanyId(6201);  
  10.   
  11.     deptDao.addDept(department);  
  12.     System.out.println("新部门ID:"+department.getId());  
  13. }  


输出成功!
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MyBatis 中,插入一条数据成功后可以返回的值。通常有以下几种方式可以实现: 1. 使用 `useGeneratedKeys` 和 `keyProperty` 属性:在插入语句的 XML 配置中,可以通过设置 `useGeneratedKeys` 属性为 `true` 来启用自动生成的功能,然后使用 `keyProperty` 属性指定对应的实体类属性。这样,在插入数据成功后,MyBatis 会自动将生成的值赋给 `keyProperty` 指定的属性。 例如,在 XML 配置中: ```xml <insert id="insertUser" useGeneratedKeys="true" keyProperty="id"> INSERT INTO user (username, password) VALUES (#{username}, #{password}) </insert> ``` 在 Java 代码中,插入数据成功后可以获取值: ```java User user = new User(); user.setUsername("test"); user.setPassword("123456"); sqlSession.insert("insertUser", user); Integer id = user.getId(); ``` 2. 使用 `selectKey` 元素:另一种方式是使用 `selectKey` 元素将插入语句与查询的语句结合起来。`selectKey` 元素可以定义一个查询语句,用于获取值,并将值赋给指定的属性。 例如,在 XML 配置中: ```xml <insert id="insertUser"> <selectKey keyProperty="id" resultType="java.lang.Integer" order="AFTER"> SELECT LAST_INSERT_ID() </selectKey> INSERT INTO user (username, password) VALUES (#{username}, #{password}) </insert> ``` 在 Java 代码中,插入数据成功后可以通过 `id` 属性获取值: ```java User user = new User(); user.setUsername("test"); user.setPassword("123456"); sqlSession.insert("insertUser", user); Integer id = user.getId(); ``` 无论使用哪种方式,插入数据成功后,都可以通过获取实体类的属性或通过参数的方式,得到刚插入数据值。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值