时间:2021-07-01 10:21:17 帮助过:21人阅读
getSingleResult(); 获得第一条数据
关于事物:
insert update 都是一样的方式
try (Connection con = sql2o.beginTransaction()) {
con.createQuery(sql1).addParameter("id", idVariable1).executeUpdate();
con.createQuery(sql2).addParameter("id", idVariable2).executeUpdate();
con.commit();
}
关于批量插入:
public void insertABunchOfRows(){
final String sql = "INSERT INTO SomeTable(id, value) VALUES (:id, :value)";
try (Connection con = sql2o.beginTransaction()) {
Query query = con.createQuery(sql);
for (int i = 0; i < 100; i++){
query.addParameter("id", i).addParameter("value", "foo" + i)
.addToBatch();
}
query.executeBatch(); // executes entire batch
con.commit(); // remember to call commit(), else sql2o will automatically rollback.
}
官方文档:
http://www.sql2o.org/docs/spring/
sql2o的使用(转)
标签:object style ace var ble final com sdn 事物