private static void sessionDemo(DataSource ds) {
Entity entity = Entity.create(TABLE_NAME).set("字段1", "值").set("字段2", 2);
Entity where = Entity.create(TABLE_NAME).set("条件1", "条件值");
Session session = Session.create(ds);
try {
session.beginTransaction();
// 增,生成SQL为 INSERT INTO `table_name` SET(`字段1`, `字段2`) VALUES(?,?)
session.insert(entity);
// 删,生成SQL为 DELETE FROM `table_name` WHERE `条件1` = ?
session.del(where);
// 改,生成SQL为 UPDATE `table_name` SET `字段1` = ?, `字段2` = ? WHERE `条件1` = ?
session.update(entity, where);
// 查,生成SQL为 SELECT * FROM `table_name` WHERE WHERE `条件1` = ? 第一个参数为返回的字段列表,如果null则返回所有字段
List<Entity> entityList = session.find(null, where, new EntityHandler());
log.info("{}", entityList);
// 分页,注意,ANSI SQL中不支持分页!
List<Entity> pagedEntityList = session.page(null, where, 0, 20, new EntityHandler());
log.info("{}", pagedEntityList);
session.commit();
} catch (Exception e) {
session.quietRollback();
} finally {
session.close();
}
}