通过record(map)的方式操作表数据,似orm非orm,半对像操作。
本类库基于jfinalV1.9改造,写一些简单的功能的时候特别适合使用,比如要转结数据,比如初始数据,相比orm不需要搞一堆对像,相比存sql不需要搞一些insertupdate。
通过sql语句查询某几个表的数据返回record,直接将该对像(不是写sql)保存或者更新到一个新表中。这种方式特别省事
apachedbutils对数据库的支持已足够好,但还是略显烦杂,当然也可以基于dbutils实现 jfinal的操作方式
相比SpringjdbcTemplate 那肯定是少了N多依赖
相比 jdbc操作那肯定方便很多
使用添加依赖
<dependency><groupId>com.liucf</groupId><artifactId>db.record</artifactId><version>0.0.4</version></dependency>示例1.初始化Db
//初始化数据连接Db.init("jdbc:mysql://host:port/test?characterEncoding=utf-8&autoReconnect=true&autoReconnectForPools=true&serverTimezone=GMT%2B8","root","xxx");//打印sql日志Db.use().setShowSql(true);2.查询数据
//简单查询List<Record>baskets=Db.find("select*frombase_basket");for(Recordrecord:baskets){System.out.println(record.getStr("id"));System.out.println(record.toJson());}//根据id查询Recordrecord=Db.findById("base_basket","001")//查询首条数据Db.findFirst("select*frombase_basketwhereid=?","001")//分页查询count参数决定是否统计总行数Page<Record>p=Db.paginate(1,2,"select*frombase_basketwhereid>?",false,"1000");p.getList();p.getPageNumber();p.getPageSize();p.getTotalPage();p.getTotalRow();3.新增
Recordr=newRecord();r.set("id","ddddd");Db.save("base_basket",r);4.更新
Recordr=newRecord();r.set("id","ddddd");Db.update("base_basket",r);//主键名称非id//Db.update("base_basket","id",r);4.删除
Db.deleteById("base_basket","001");//Db.update("deletefrombase_basket");5.事务
Db.tx(newTransactionWrap(){@Overridepublicbooleanrun()throwsSQLException{try{Recordr=newRecord();r.set("id","ddddd");Db.save("base_basket",r);r.set("id","ddddd");r.set("remarks","remarks");Db.update("base_basket","id",r);}catch(Exceptione){returnfalse;}returntrue;}});6.多数据源
Db.init("jdbc:mysql://host:port/test?characterEncoding=utf-8&autoReconnect=true&autoReconnectForPools=true&serverTimezone=GMT%2B8","root","xxx");Db.initAlias("toDb","jdbc:mysql://xxx:3306/test?characterEncoding=utf-8&autoReconnect=true&autoReconnectForPools=true&serverTimezone=GMT%2B8","user","password")//使用Db.use切换数据源Db.use().find();//默认数据源Db.use("toDb").find();//指定数据源
评论