JFinal保存对象后可以取出主键

JFinal保存对象后可以取出主键,即使这个主键是数据库自增长的。

今天无意中发现,JFinal保存对象后可以把主键取出来,我的数据库表主键都是自增的。比如

  Blog blog = getModel(Blog.class);//这里没有存放id
  blog.save();
  System.out.println(blog.getInt("id"));//这里居然可以取出来。


今天研究的半天,最后在大家的帮助下终于明白是怎么实现的了。Model类的源码如下:

 public boolean save() {
  Config config = getConfig();
  Table table = getTable();
  
  StringBuilder sql = new StringBuilder();
  List<Object> paras = new ArrayList<Object>();
  config.dialect.forModelSave(table, attrs, sql, paras);
  // if (paras.size() == 0) return false; // The sql "insert into tableName() values()" works fine, so delete this line
  
  // --------
  Connection conn = null;
  PreparedStatement pst = null;
  int result = 0;
  try {
   conn = config.getConnection();
   if (config.dialect.isOracle())
    pst = conn.prepareStatement(sql.toString(), new String[]{table.getPrimaryKey()});
   else
    pst = conn.prepareStatement(sql.toString(), Statement.RETURN_GENERATED_KEYS);
   
   config.dialect.fillStatement(pst, paras);
   result = pst.executeUpdate();
   getGeneratedKey(pst, table);
   getModifyFlag().clear();
   return result >= 1;
  } catch (Exception e) {
   throw new ActiveRecordException(e);
  } finally {
   config.close(pst, conn);
  }
 }


根据波总的指导,红色加粗的代码才是保存后取出主键的关键,而绿色加粗部分是具体实现过程。经过查看API,这个方法是为了创建一个默认 PreparedStatement 对象,该对象能获取自动生成的键。给定常量告知驱动程序是否可以获取自动生成的键。如果 SQL 语句不是一条 INSERT 语句,或者 SQL 语句能够返回自动生成的键(这类语句的列表是特定于供应商的),则忽略此参数【JDK中文文档的原话】。


绿色加粗部分的源代码:

/**
  * Get id after save method.
  */
 private void getGeneratedKey(PreparedStatement pst, Table table) throws SQLException {
  String pKey = table.getPrimaryKey();
  if (get(pKey) == null || getConfig().dialect.isOracle()) {
   ResultSet rs = pst.getGeneratedKeys();
   if (rs.next()) {
    Class colType = table.getColumnType(pKey);
    if (colType == Integer.class || colType == int.class)
     set(pKey, rs.getInt(1));
    else if (colType == Long.class || colType == long.class)
     set(pKey, rs.getLong(1));
    else
     set(pKey, rs.getObject(1));  // It returns Long object for int colType
    rs.close();
   }
  }
 }








转载于:https://my.oschina.net/u/1444945/blog/395400

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值