最近理解设计模式有点累,转接来了解下开发框架。笔者对于数据库操作一直觉得觉得很繁琐,代码量太多,自己通过原生方式构建数据库,对数据的增删改查,效率不说,操作起来也是挺费劲的。这几天了解下数据库框架,发现一个性能较强大的数据库框架greenDAO,这里我引用官方网站一张与ORMlite框架的性能图。
apply plugin: 'java'
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'de.greenrobot:greendao-generator:2.0.0'
}
public class AppMyGenerator {
public static void main(String[] args) throws Exception {
Schema schema = new Schema(1, "cn.wsy.commom.bean");//创建一个操作Schema操作对象,这里两个形参意思是代表数据库版本号与数据表字段存储的对象包名
schema.setDefaultJavaPackageDao("cn.wsy.commom.dao");//这里是指定自动生成的数据表操作类包名
schema.enableActiveEntitiesByDefault();
schema.enableKeepSectionsByDefault();
initUserBean(schema);//初始化数据表字段
new DaoGenerator().generateAll(schema, "app/src/main/java");//指定存储的工作路径
}
private static void initUserBean(Schema schema) {
Entity userBean = schema.addEntity("UserBean");
userBean.setTableName("user");//表名字
userBean.addStringProperty("id").primaryKey().index();//字段,并且为主键
userBean.addStringProperty("phone");
userBean.addStringProperty("gender");
}
}
package cn.wsy.commom.bean;
import cn.wsy.commom.dao.DaoSession;
import de.greenrobot.dao.DaoException;
import cn.wsy.commom.dao.UserBeanDao;
// THIS CODE IS GENERATED BY greenDAO, EDIT ONLY INSIDE THE "KEEP"-SECTIONS
// KEEP INCLUDES - put your custom includes here
// KEEP INCLUDES END
/**
* Entity mapped to table "user".
*/
public class UserBean {
private String id;
private String phone;
private String profile_picture;
private String client_id;
private String name;
private String location;
private String gender;
/** Used to resolve relations */
private transient DaoSession daoSession;
/** Used for active entity operations. */
private transient UserBeanDao myDao;
// KEEP FIELDS - put your custom fields here
// KEEP FIELDS END
public UserBean() {
}
public UserBean(String id) {
this.id = id;
}
public UserBean(String id, String phone, String profile_picture, String client_id, String name, String location, String gender) {
this.id = id;
this.phone = phone;
this.profile_picture = profile_picture;
this.client_id = client_id;
this.name = name;
this.location = location;
this.gender = gender;
}
/** called by internal mechanisms, do not call yourself. */
public void __setDaoSession(DaoSession daoSession) {
this.daoSession = daoSession;
myDao = daoSession != null ? daoSession.getUserBeanDao() : null;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getProfile_picture() {
return profile_picture;
}
public void setProfile_picture(String profile_picture) {
this.profile_picture = profile_picture;
}
public String getClient_id() {
return client_id;
}
public void setClient_id(String client_id) {
this.client_id = client_id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
/** Convenient call for {@link AbstractDao#delete(Object)}. Entity must attached to an entity context. */
public void delete() {
if (myDao == null) {
throw new DaoException("Entity is detached from DAO context");
}
myDao.delete(this);
}
/** Convenient call for {@link AbstractDao#update(Object)}. Entity must attached to an entity context. */
public void update() {
if (myDao == null) {
throw new DaoException("Entity is detached from DAO context");
}
myDao.update(this);
}
/** Convenient call for {@link AbstractDao#refresh(Object)}. Entity must attached to an entity context. */
public void refresh() {
if (myDao == null) {
throw new DaoException("Entity is detached from DAO context");
}
myDao.refresh(this);
}
// KEEP METHODS - put your custom methods here
// KEEP METHODS END
}
package cn.wsy.commom.dao;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;
import de.greenrobot.dao.AbstractDao;
import de.greenrobot.dao.Property;
import de.greenrobot.dao.internal.DaoConfig;
import cn.wsy.commom.bean.UserBean;
// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
/**
* DAO for table "user".
*/
public class UserBeanDao extends AbstractDao<UserBean, String> {
public static final String TABLENAME = "user";
/**
* Properties of entity UserBean.<br/>
* Can be used for QueryBuilder and for referencing column names.
*/
public static class Properties {
public final static Property Id = new Property(0, String.class, "id", true, "ID");
public final static Property Phone = new Property(1, String.class, "phone", false, "PHONE");
public final static Property Profile_picture = new Property(2, String.class, "profile_picture", false, "PROFILE_PICTURE");
public final static Property Client_id = new Property(3, String.class, "client_id", false, "CLIENT_ID");
public final static Property Name = new Property(4, String.class, "name", false, "NAME");
public final static Property Location = new Property(5, String.class, "location", false, "LOCATION");
public final static Property Gender = new Property(6, String.class, "gender", false, "GENDER");
};
private DaoSession daoSession;
public UserBeanDao(DaoConfig config) {
super(config);
}
public UserBeanDao(DaoConfig config, DaoSession daoSession) {
super(config, daoSession);
this.daoSession = daoSession;
}
/** Creates the underlying database table. */
public static void createTable(SQLiteDatabase db, boolean ifNotExists) {
String constraint = ifNotExists? "IF NOT EXISTS ": "";
db.execSQL("CREATE TABLE " + constraint + "\"user\" (" + //
"\"ID\" TEXT PRIMARY KEY NOT NULL ," + // 0: id
"\"PHONE\" TEXT," + // 1: phone
"\"PROFILE_PICTURE\" TEXT," + // 2: profile_picture
"\"CLIENT_ID\" TEXT," + // 3: client_id
"\"NAME\" TEXT," + // 4: name
"\"LOCATION\" TEXT," + // 5: location
"\"GENDER\" TEXT);"); // 6: gender
// Add Indexes
db.execSQL("CREATE INDEX " + constraint + "IDX_user_ID ON user" +
" (\"ID\");");
}
/** Drops the underlying database table. */
public static void dropTable(SQLiteDatabase db, boolean ifExists) {
String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"user\"";
db.execSQL(sql);
}
/** @inheritdoc */
@Override
protected void bindValues(SQLiteStatement stmt, UserBean entity) {
stmt.clearBindings();
String id = entity.getId();
if (id != null) {
stmt.bindString(1, id);
}
String phone = entity.getPhone();
if (phone != null) {
stmt.bindString(2, phone);
}
String profile_picture = entity.getProfile_picture();
if (profile_picture != null) {
stmt.bindString(3, profile_picture);
}
String client_id = entity.getClient_id();
if (client_id != null) {
stmt.bindString(4, client_id);
}
String name = entity.getName();
if (name != null) {
stmt.bindString(5, name);
}
String location = entity.getLocation();
if (location != null) {
stmt.bindString(6, location);
}
String gender = entity.getGender();
if (gender != null) {
stmt.bindString(7, gender);
}
}
@Override
protected void attachEntity(UserBean entity) {
super.attachEntity(entity);
entity.__setDaoSession(daoSession);
}
/** @inheritdoc */
@Override
public String readKey(Cursor cursor, int offset) {
return cursor.isNull(offset + 0) ? null : cursor.getString(offset + 0);
}
/** @inheritdoc */
@Override
public UserBean readEntity(Cursor cursor, int offset) {
UserBean entity = new UserBean( //
cursor.isNull(offset + 0) ? null : cursor.getString(offset + 0), // id
cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1), // phone
cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2), // profile_picture
cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3), // client_id
cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4), // name
cursor.isNull(offset + 5) ? null : cursor.getString(offset + 5), // location
cursor.isNull(offset + 6) ? null : cursor.getString(offset + 6) // gender
);
return entity;
}
/** @inheritdoc */
@Override
public void readEntity(Cursor cursor, UserBean entity, int offset) {
entity.setId(cursor.isNull(offset + 0) ? null : cursor.getString(offset + 0));
entity.setPhone(cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1));
entity.setProfile_picture(cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2));
entity.setClient_id(cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3));
entity.setName(cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4));
entity.setLocation(cursor.isNull(offset + 5) ? null : cursor.getString(offset + 5));
entity.setGender(cursor.isNull(offset + 6) ? null : cursor.getString(offset + 6));
}
/** @inheritdoc */
@Override
protected String updateKeyAfterInsert(UserBean entity, long rowId) {
return entity.getId();
}
/** @inheritdoc */
@Override
public String getKey(UserBean entity) {
if(entity != null) {
return entity.getId();
} else {
return null;
}
}
/** @inheritdoc */
@Override
protected boolean isEntityUpdateable() {
return true;
}
}
compile 'de.greenrobot:greendao:2.1.0'
DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "wsy-db", null);
SQLiteDatabase db = helper.getWritableDatabase();
DaoMaster daoMaster = new DaoMaster(db);
DaoSession daoSession = daoMaster.newSession();
UserBeanDao userDao = daoSession.getUserBeanDao();
userDao.insert(new UserBean("1","1","1","1","1","1","1"));
DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "wsy-db", null);
DaoMaster daoMaster = new DaoMaster(db);
DaoSession daoSession = daoMaster.newSession();
UserBeanDao userDao = daoSession.getUserBeanDao();
userDao.insert(new UserBean("1","1","1","1","1","1","1"));//插入数据