1. Bulk Update Inserts
Jdbctemplate.batchupdate (String sql, list<object[]> Batchargs)
Object[] The length of the array is the number of arguments per record (i.e. "?" in the SQL statement). Number of)
List: How many records are inserted in total.
/** * BULK INSERT Order Details * */public void Addbatch (list<orderdetail> orderdetails) { list<object[]> params = New Arraylist<> (); for (OrderDetail orderdetail:orderdetails) { params.add (new Object[]{orderdetail.getproductname (), Orderdetail.getproductprice (), orderdetail.getproductcount (), Orderdetail.getparentid ()}); } Final String sql = "INSERT into Order_Detail (pro_name,pro_price,pro_count,parent_id) VALUES (?,?,?,?)"; Jdbctemplate.batchupdate (sql, params); }
2. Single record insert get primary key ID
/** * insert and get primary key */public int Addandgetid (order order) { final String sql = "INSERT into orders (name, Address,createt Ime,totalprice,status) VALUES (?,?,?,?,?) "; Keyholder keyholder = new generatedKeyHolder (); Jdbctemplate.update (New PreparedStatementCreator () { @Override public PreparedStatement Createpreparedstatement (Connection Connection) throws SQLException { PreparedStatement ps = Connection.preparestatement (SQL, Statement.return_generated_keys); Ps.setobject (1, Order.getname ()); Ps.setobject (2, order.getaddress ()); Ps.setobject (3, Order.getcreatetime ()); Ps.setobject (4, Order.gettotalprice ()); Ps.setobject (5, Order.getstatus ()); return PS; } }, Keyholder); Return Keyholder.getkey (). Intvalue ();}
Spring JdbcTemplate BULK INSERT and get ID on single insert