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