java 的sigola orm 的開發,第一次學寫java,能夠用在play上面

固然仍是開源:https://github.com/xiaose1205/sigola     初學者有用,高手能夠給點建議,勿噴啊。net轉java,有些思想尚未那麼快轉。但願獲得你們的支持啊html

 

使用了dbutils1.5,數據庫暫時支持mysql.java

使用說明: Dao繼承於BaseDao,Dto繼承於BaseDto,IDao繼承於IRepository(方便使用google.guicy); Dto須要實現BaseDto中的toMap()與getTbName();mysql

 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的實現比較簡單:git

1         public class demoDao extends BaseDao {}

 

如下爲實現的一些邏輯。github

 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的。sql

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());
        }
 

若是須要執行簡單的sqldom

 

  /* 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#2649218ide

相關文章
相關標籤/搜索