1.添加依赖
<dependency> <groupId>org.nutz</groupId> <artifactId>nutz</artifactId> <version>1.r.68.v20191031</version> </dependency>
2.查一条数据
Record fetch = dao.fetch('表名', Cnd.where("id", "=", 1));//整条数据
String sql = fetch.getString("字段名");//这条数据的某个字段值
3.更新某条数据的所有字段
Record headers_record = new Record(); headers_record.put(".table",HEADER_TASK); headers_record.put("create_time",new Date()); headers_record.put("id",1); headers_record.put("user_agent",user_agent); dao.update(headers_record);
4.更新某条数据的部分字段,多个字段在后面add即可
dao.update(TASK_TABLE_NAME, Chain.make("check_code",check_code).add("cookie",cookie).add("status",102), Cnd.where("id","=",id)); dao.update("elong_jack_headers",Chain.make("cookie",cookie),Cnd.where("id","=",1));
5.根据一条SQL语句查询所有符合条件的记录
/** * 运行sql语句 返回到一个nutz的model list里 * @param sql sql语句 * @param classOfT 模型类名 * @return 一个pojo列表 */ private NutDao dao; public static List<Record> excuteSql(String sql,Dao dao){ Sql sq = Sqls.create(sql); sq.setCallback(Sqls.callback.records()); sq.setEntity(dao.getEntity(Record.class)); dao.execute(sq); return sq.getList(Record.class); }
6.遍历查询出来的所有记录
public class Util { public static NutDao getBiDao() throws ClassNotFoundException { DruidDataSource dataSource = new DruidDataSource(); dataSource.setDriverClassName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); dataSource.setUrl(""); dataSource.setUsername(""); dataSource.setPassword(""); NutDao dao = new NutDao(dataSource); return dao; } } NutDao dao = Util.getBiDao(); List<XdOrderPageJob> list = dao.query(XdOrderPageJob.class, Cnd.where("page", "=", 1)); if(CollectionUtils.isEmpty(list)) { logger.info("暂无数据,睡眠五分钟"); Thread.sleep(5*60*1000);//睡眠五分钟 } for(XdOrderPageJob job : list) { //线程名称 XdOrderThread art = new XdOrderThread(job); art.dao = dao; art.run(); }