2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

自動採番のIDを取得する

Posted at

#自動採番したIDを取得する



		// 自動採番取得用メソッド
		@PostConstruct
		public void init() {
			SimpleJdbcInsert simpleJdbcInsert = new SimpleJdbcInsert((JdbcTemplate) template.getJdbcOperations());
			SimpleJdbcInsert withTableName = simpleJdbcInsert.withTableName("orders");
			insert = withTableName.usingGeneratedKeyColumns("id");	
	}

インサートした後に自動採番で振られたIDを取得してreturnする


public Order insert(Order order) {

    // ドメインの名前とSQLの?部分があっていれば自動的に入っていく
     SqlParameterSource param = new BeanPropertySqlParameterSource(order);
   
    // executeAndReturnKeyが勝手にインサート文を実行してくれる
     Number key = insert.executeAndReturnKey(param);

     order.setId(key.intValue());

	return order;
}

備忘録として残します。
何かご指摘あればお願いします。

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?