获取插入记录的自增长字段值

MyBatis获取插入记录的自增长字段值

第一步:

    在Mybatis Mapper文件中添加属性“useGeneratedKeys”和“keyProperty”,其中keyProperty是Java对象的属性名!

[html]  view plain copy
  1. <insert id="insert" parameterType="Spares"   
  2.         useGeneratedKeys="true" keyProperty="id">  
  3.         insert into spares(spares_id,spares_name,  
  4.             spares_type_id,spares_spec)  
  5.         values(#{id},#{name},#{typeId},#{spec})  
  6.     </insert>  

第二步:

    Mybatis执行完插入语句后,自动将自增长值赋值给对象Spares的属性id。因此,可通过Spares对应的getter方法获取!

[java]  view plain copy
  1. /** 
  2.  * 新增备件 
  3.  * @author hellostory 
  4.  * @param spares 
  5.  * @return 
  6.  */  
  7. @RequestMapping(value = "/insert")  
  8. @ResponseBody  
  9. public JsonResponse insert(Spares spares) {  
  10.     int count = sparesService.insert(spares);  
  11.     System.out.println("共插入" + count + "条记录!"  
  12.             + "\n刚刚插入记录的主键自增长值为:" + spares.getId());  



JDBC 插入数据返
  1. package com.test;  
  2. import java.sql.Connection;  
  3. import java.sql.PreparedStatement;  
  4. import java.sql.ResultSet;  
  5. import java.sql.Statement;  
  6. import java.util.Date;  
  7. import java.util.Properties;  
  8. /** 
  9.  * 数据库连接对象管理类 
  10.  * @说明 
  11.  * @author cuisuqiang 
  12.  * @version 1.0 
  13.  * @since 
  14.  */  
  15. public class ConnectionManager {  
  16.     private static final String url = "jdbc:mysql://localhost:3306/test";  
  17.     private static final String username = "root";  
  18.     private static final String userpass = "root";  
  19.     @SuppressWarnings("deprecation")  
  20.     public static void main(String[] args) throws Exception{  
  21.         Connection conn = getConnection();  
  22.         if (null != conn) {  
  23.             String sql = "insert into common_user (name) values(?)";  
  24.             // 指定返回生成的主键  
  25.             PreparedStatement pstmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);   
  26.             // 如果使用静态的SQL,则不需要动态插入参数  
  27.             pstmt.setString(1new Date().toLocaleString());  
  28.             pstmt.executeUpdate();   
  29.             // 检索由于执行此 Statement 对象而创建的所有自动生成的键   
  30.             ResultSet rs = pstmt.getGeneratedKeys();   
  31.             if (rs.next()) {  
  32.                 Long id = rs.getLong(1);   
  33.                 System.out.println("数据主键:" + id);   
  34.             }  
  35.         }  
  36.     }  
  37.     public static Connection getConnection() {  
  38.         Connection conn = null;  
  39.         try {             
  40.             com.mysql.jdbc.Driver driver = new com.mysql.jdbc.Driver();  
  41.             Properties properties = new Properties();  
  42.             properties.put("user", username);  
  43.             properties.put("password", userpass);  
  44.             conn = driver.connect(url, properties);  
  45.         } catch (Exception e) {  
  46.             e.printStackTrace();  
  47.         }  
  48.         return conn;  
  49.     }  
  50. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值