说明:
使用了快两年的Nutz.Dao,聊一下Nutz.Dao 的一些使用技巧
如果你觉得 Hibernate 控制比较繁琐,iBatis 编写SQL又比较麻烦,Nutz.Dao 专为你设计
1.分页:
Pager pager = dao.createPager(page,pageSize);
pager.setRecordCount(count);
sql.setPager(pager);
2.事务:
无返回值:
Trans.exec(Connection.TRANSACTION_READ_COMMITTED,new Atom() {
@Override
public void run() {
......
}
})
有返回值:
NutMap nutMap = Trans.exec(new Molecule<NutMap>() {
public void run() {
......
setObj(NutMap.NEW());
}
})
3.获取更新条数:
dao.execute(sql)
int count = sql.getUpdateCount()
4.获取单条
Record record = dao.fetch("table",cnd.where("usid","=","hints"))
5.获取多条
List<Record> recordlist = dao.query("table",cnd.where("usid","=","hints"))
6.数据库函数使用:
int num = dao.func("table","max","number",cnd.where("usid","=","hints"))
7:删除
int num = dao.clear("table",cnd.where("usid","=","hints"))
8.更新
int num = dao.update("table",chain.make("password","123456").add("name","hints1"),cnd.where("usid","=","hints"))
9.对象转条件
Cnd cnd = Cnd.from(dao,elasticJobExecLog);
10.拼接条件
Sql sql = Sqls.create("select * from table $condition");
Cnd cnd = Cnd.where("1","=","1");
sql.setCondition(cnd);
sql.setCallback(Sqls.callback.entities());
sql.setEntity(dao.getEntity(Record.class));
List<Record> list = dao.execute(sql).getList(Record.class);
11.orm蛇形变驼峰
@Column(hump = true)
private Long maxSiteNum;
12.不建对象,ddl操作
list.get(0).put(".table", "user");
dao.insert(list);