在使用Nutz时,需要用到一些自定义SQL,这里记一下已经运行通过的一些例子。
public boolean existUser(String userId) {
Sql sql = Sqls.create("SELECT 1 FROM $table WHERE userId=@userId LIMIT 1");
sql.setCallback(new SqlCallback() {
public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
while (rs.next())
return true;
return false;
}
});
sql.vars().set("table", dao.getEntity(User.class).getTableName());
sql.params().set("userId", userId);
dao.execute(sql);
return sql.getBoolean();
}
public long getMaxLastUpdateTime() {
Sql sql = Sqls.create("SELECT max(lastUpdateTime) AS maxLastUpdateTime FROM $table");
sql.setCallback(new SqlCallback() {
public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
while (rs.next())
return Long.valueOf(rs.getString("maxLastUpdateTime"));
return 0;
}
});
sql.vars().set("table", dao.getEntity(PageStatistics.class).getTableName());
dao.execute(sql);
return sql.getLong();
}