java 的sigola orm 的开发,第一次学写java,可以用在play上面
当然还是开源:https://github.com/xiaose1205/sigola 初学者有用,高手可以给点建议,勿喷啊。net转java,有些思想还没有那么快转。希望得到大家的支持啊
使用了dbutils1.5,数据库暂时支持mysql.
使用说明: Dao继承于BaseDao,Dto继承于BaseDto,IDao继承于IRepository(方便使用google.guicy); Dto需要实现BaseDto中的toMap()与getTbName();
1 public class demoModel extends BaseDto { 2 public demoModel() 3 { 4 this.setTbName("demo"); 5 } 6 @Override 7 public Map toMap() 8 { 9 Map map = new HashMap(); 10 ...... 11 return map; 12 } 13 }
Dao的实现比较简单:
1 public class demoDao extends BaseDao {}
以下为实现的一些逻辑。
1 demoModel model = new demoModel(); 2 demoDao dao = new demoDao(); 3 /* 新增 */ 4 Random random = new Random(); 5 model.setUserName("demo" + random.nextInt()); 6 model.setUserPwd("123456"); 7 int reslut = dao.Add(model); 8 System.out.println(reslut); 9 /* 修改 */ 10 Random random = new Random(); 11 model.setUserName("update" + random.nextInt()); 12 model.setId(10); 13 dao.Save(model); 14 /* 删除 */ 15 model.setId(1); 16 dao.Remove(model);
以往分页比较麻烦,现加入了PageList,内部实现了List与count的。
pageList的源码:可以当作普通的list使用。
public class PageList<E> extends ArrayList<E> { private Long totalCount = (long) 0; public Long getTotalCount() { return totalCount; } public void setTotalCount(Long totalCount) { this.totalCount = totalCount; } }
PageList plist = null; List list = null;demoDao login=new demoDao(); { DataAction action = new DataAction(); action.setTable(model).setfileds("*"); /* 对in进行适当的优化,一条数据的时候自动会进入= */ action.where("userName", "12", RelationEnum.In); action.where("userName", "wan", RelationEnum.LikeLeft); action.where("userName", "an", RelationEnum.Like); action.order("id", OrderByEnum.Desc); /* list */ list = action.getList(demoModel.class); showlist(list); /* list with count 对count查找的结果适当的优化,减少一次查询 */ plist = action.getPageList(demoModel.class); showlist(plist); System.out.println(plist.getTotalCount()); } { /* list */ list = logic.FindList(0, 10); showlist(list); /* list with count */ plist = logic.FindPageList(0, 10); showlist(plist); /* * select count(1) from demo where name='wangjun' order by id desc; * 自动转换 'id,userName' 至'count(1)' */ System.out.println(logic.Cast().setfileds("id,userName") .order("id").where("userName", "wangjun") .getCount()); /* 执行count的结果 */ System.out.println(logic.Cast().setfileds("count(id)") .order("id").where("userName", "wangjun") .getCount()); /*自动判断是否加入and的条件 */ System.out.println(logic.Cast().setfileds("count(id)") .order("id").where("userName", "wangjun") .where("and LENGTH(id)>=2") .where("LENGTH(id)>=2").getCount()); }
如果需要执行简单的sql
/* execute with nomarl sql */ DataAction action = new DataAction(); /* select into List */ list = action.getList(demoModel.class, "select * from demo limit 2,3"); showlist(list); /* delete without params */ action.excute("delete from demo where id =1"); /* insert with params */ Object[] obj = new Object[2]; obj[0] = 1; obj[1] = "123jdhfjh"; action.excute( "insert into demo (id,username)values(?,?)", obj);
怎么用到play上面呢,使用插件的形式,添加sigolaPlugin(在你自己的项目中,名字可以自己定义)
public class sigolaPlugin extends PlayPlugin { /*内置一个class继承IdbBase,是想getConnecion的方法,内部数据会主动获取这个操作*/ class dbBase implements IdbBase { @Override public Connection getConnection() { // TODO Auto-generated method stub return DB.getConnection(); } } public static boolean IsReady = false; /*启动后赋值,基于play 1.2.4*/ @Override public void onApplicationReady() { if (!IsReady) { MysqlHelper.idbBase = new dbBase(); System.out.println("onApplicationReady loading mysqlhelper.DBConnection"); IsReady = true; } } }
我的Net版的HelloData http://www.cnblogs.com/xiaose1205/archive/2013/04/02/2995256.html#2649218