android項目裏面不少都會有使用sqlite來保存數據。原生api真心很差使啊,要寫超多超多的代碼,還要寫顧慮不少細節問題。因而乎就想偷懶了,乾脆去網上找個orm框架吧!
Ok,google it。篩選一下,就鎖定了ormlite和greendao。簡單看了一下,ormlite簡單好用,比較符合JavaEE開發者使用習慣,註解真的很好用啊!再去greendao官網逛逛,他說咱們的目標是: java
咱們能夠在官網上直接下來,也可去github項目主頁上下載源碼。建議去下載github哈,由於有源碼有列子,比較直觀易懂。源碼使用gradle構建,須要安裝gradle插件。其實真正也只有依賴一個freemaker.jar,直接網上下載一個就好。下面新建一個java工程,注意是java工程不是android工程。導入freemaker.jar和greendao-generator.jar,加入到build path。建一個以下的類:
android
public class DaoGenerator { public static void main(String[] args) throws Exception { // first parameter for version, second for default generate package Schema schema = new Schema(1, "com.xckevin.example.model"); addNote(schema); addCustomerOrder(schema); addUser(schema); // set dao class generate package schema.setDefaultJavaPackageDao("com.xckevin.example.dao"); // keep custom code block schema.enableKeepSectionsByDefault(); new DaoGenerator().generateAll(schema, "../GreenDaoExample/src"); } private static void addNote(Schema schema) { Entity note = schema.addEntity("Note"); note.addIdProperty(); note.addStringProperty("text").notNull(); note.addStringProperty("comment"); note.addDateProperty("date"); } private static void addUser(Schema schema) { Entity user = schema.addEntity("User"); user.setTableName("t_user"); user.addIdProperty(); user.addStringProperty("account").unique(); user.addStringProperty("password"); user.addDateProperty("birthday"); user.addShortProperty("gender"); user.addIntProperty("height"); user.addFloatProperty("weight"); user.addDateProperty("registerTime"); user.implementsInterface("Jsonable<User>"); } private static void addCustomerOrder(Schema schema) { Entity customer = schema.addEntity("Customer"); customer.addIdProperty(); customer.addStringProperty("name").notNull(); Entity order = schema.addEntity("Order"); order.setTableName("ORDERS"); // "ORDER" is a reserved keyword order.addIdProperty(); Property orderDate = order.addDateProperty("date").getProperty(); Property customerId = order.addLongProperty("customerId").notNull().getProperty(); order.addToOne(customer, customerId); ToMany customerToOrders = customer.addToMany(order, customerId); customerToOrders.setName("orders"); customerToOrders.orderAsc(orderDate); } }
代碼號簡單的話,看名字就知道是什麼意思了。greendao支持各類類型的哇,還支持一對1、一對多、多對多的關係,很強悍!直接運行,代碼生成
自動生成model和dao,倍兒爽!隨便看一個model類: git
package com.xckevin.example.model; // THIS CODE IS GENERATED BY greenDAO, EDIT ONLY INSIDE THE "KEEP"-SECTIONS // KEEP INCLUDES - put your custom includes here import org.json.JSONException; import org.json.JSONObject; // KEEP INCLUDES END /** * Entity mapped to table t_user. */ public class User implements Jsonable<User> { private Long id; private String account; private String password; private java.util.Date birthday; private Short gender; private Integer height; private Float weight; private java.util.Date registerTime; // KEEP FIELDS - put your custom fields here // KEEP FIELDS END public User() { } public User(Long id) { this.id = id; } public User(Long id, String account, String password, java.util.Date birthday, Short gender, Integer height, Float weight, java.util.Date registerTime) { this.id = id; this.account = account; this.password = password; this.birthday = birthday; this.gender = gender; this.height = height; this.weight = weight; this.registerTime = registerTime; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getAccount() { return account; } public void setAccount(String account) { this.account = account; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public java.util.Date getBirthday() { return birthday; } public void setBirthday(java.util.Date birthday) { this.birthday = birthday; } public Short getGender() { return gender; } public void setGender(Short gender) { this.gender = gender; } public Integer getHeight() { return height; } public void setHeight(Integer height) { this.height = height; } public Float getWeight() { return weight; } public void setWeight(Float weight) { this.weight = weight; } public java.util.Date getRegisterTime() { return registerTime; } public void setRegisterTime(java.util.Date registerTime) { this.registerTime = registerTime; } // KEEP METHODS - put your custom methods here @Override public User parse(JSONObject jsonObj) { // TODO Auto-generated method stub try { id = jsonObj.getLong("id"); account = jsonObj.getString("account"); return this; } catch (JSONException e) { e.printStackTrace(); } return null; } // KEEP METHODS END }
注意上面的// KEEP代碼塊中是手動加入了,當設置了 github
schema.enableKeepSectionsByDefault
後,該部分代碼塊在下次更新的時候會保留下來。 sql
dao類中也有各類基本的方法,如insert,update,delete等等。基本可能完成大部分需求了,終於不用寫那麼繁瑣的數據庫操做啦! 數據庫
再看看怎麼在client獲取到dao,注意client要加入greendao.jar哦。有了dao就能夠對數據庫各類操做了! json
DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "notes-db", null); db = helper.getWritableDatabase(); daoMaster = new DaoMaster(db); daoSession = daoMaster.newSession(); userDao = daoSession.getUserDao();
整體來講,ormlite使用簡單,學習成本低,容易上手,效率比greendao偏慢一點。greendao耦合性高,使用時要另外使用一個java工程建立,開始環境搭建比較麻煩,可是一旦上手仍是十分容易使用的,而且效率最好。我的仍是推薦使用greendao。 api