网上有很多介绍使用greenDAO的方法,看完之后不是很清楚,因为所有的讲解都是围绕其自身附带的例子,java文件也使用的是生成的,没有针对性,比如当接入第三方进行开发时,第三方的model是提供好的,而且可能不是普通的getter和setter,而是使用的public属性,还有就是项目移植,不用它的代码来升级框架层。所以我就没能使用它生成的文件来开发。
我的开发过程(前提是有自己的model,我以我的model:Status为例):
1、引入DaoCore库工程,加入到你的项目库里面。
2、创建自己的DAO文件,继承AbstractDao
2.1复写其中的部分方法,同时创建一个内部类
public static class Properties {
...
}
这个类里面是进行列的参数设置,为每一列创建一个Property对象,实例化这个对象时里面的参数分别为:
(int ordinal, Class> type, String name, boolean primaryKey, String columnName)
ordinal:第几列
type:属于哪个model
name:属性名
primaryKey:是否主键
columnName:数据表中的列名
2.2建表和删除表的方法
/** Creates the underlying database table. */
public static void createTable(SQLiteDatabase db, boolean ifNotExists) {
String constraint = ifNotExists? "IF NOT EXISTS ": "";
db.execSQL("建表语句自己写");
}
/** Drops the underlying database table. */
public static void dropTable(SQLiteDatabase db, boolean ifExists) {
String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "'"+TABLENAME+"'";
db.execSQL(sql);
}
3、使用给的例子里面的两个文件,master和session,在这两个上进行修改。
贴出我的,仔细看的话会发现这些是有规律的,自己添加的时候要按照这个规律来。
package com.yang.DBUtil;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import com.yang.DBDAO.StatusDAO;
import com.yang.DBDAO.UsersDAO;
import de.greenrobot.dao.AbstractDaoMaster;
import de.greenrobot.dao.identityscope.IdentityScopeType;
// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
/**
* Master of DAO (schema version 1000): knows all DAOs.
*/
public class DaoMaster extends AbstractDaoMaster {
public static final int SCHEMA_VERSION = 1;
/** Creates underlying database table using DAOs. */
public static void createAllTables(SQLiteDatabase db, boolean ifNotExists) {
StatusDAO.createTable(db, ifNotExists);
UsersDAO.createTable(db, ifNotExists);
}
/** Drops underlying database table using DAOs. */
public static void dropAllTables(SQLiteDatabase db, boolean ifExists) {
StatusDAO.dropTable(db, ifExists);
UsersDAO.dropTable(db, ifExists);
}
public static abstract class OpenHelper extends SQLiteOpenHelper {
public OpenHelper(Context context, String name, CursorFactory factory) {
super(context, name, factory, SCHEMA_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
Log.i("greenDAO", "Creating tables for schema version "
+ SCHEMA_VERSION);
createAllTables(db, false);
}
}
/** WARNING: Drops all table on Upgrade! Use only during development. */
public static class DevOpenHelper extends OpenHelper {
public DevOpenHelper(Context context, String name, CursorFactory factory) {
super(context, name, factory);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.i("greenDAO", "Upgrading schema from version " + oldVersion
+ " to " + newVersion + " by dropping all tables");
dropAllTables(db, true);
onCreate(db);
}
}
public DaoMaster(SQLiteDatabase db) {
super(db, SCHEMA_VERSION);
registerDaoClass(StatusDAO.class);
registerDaoClass(UsersDAO.class);
}
public DaoSession newSession() {
return new DaoSession(db, IdentityScopeType.Session, daoConfigMap);
}
public DaoSession newSession(IdentityScopeType type) {
return new DaoSession(db, type, daoConfigMap);
}
}
package com.yang.DBUtil;
import java.util.Map;
import android.database.sqlite.SQLiteDatabase;
import com.sina.weibo.sdk.openapi.models.Status;
import com.sina.weibo.sdk.openapi.models.User;
import com.yang.DBDAO.StatusDAO;
import com.yang.DBDAO.UsersDAO;
import de.greenrobot.dao.AbstractDao;
import de.greenrobot.dao.AbstractDaoSession;
import de.greenrobot.dao.identityscope.IdentityScopeType;
import de.greenrobot.dao.internal.DaoConfig;
// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
/**
* {@inheritDoc}
*
* @see de.greenrobot.dao.AbstractDaoSession
*/
public class DaoSession extends AbstractDaoSession {
private final DaoConfig statusDaoConfig;
private final DaoConfig usersDaoConfig;
private final StatusDAO statusDAO;
private final UsersDAO usersDAO;
public DaoSession(SQLiteDatabase db, IdentityScopeType type,
Map>, DaoConfig> daoConfigMap) {
super(db);
statusDaoConfig = daoConfigMap.get(StatusDAO.class).clone();
statusDaoConfig.initIdentityScope(type);
usersDaoConfig = daoConfigMap.get(UsersDAO.class).clone();
usersDaoConfig.initIdentityScope(type);
statusDAO = new StatusDAO(statusDaoConfig, this);
usersDAO = new UsersDAO(usersDaoConfig, this);
registerDao(Status.class, statusDAO);
registerDao(User.class, usersDAO);
}
public void clear() {
statusDaoConfig.getIdentityScope().clear();
usersDaoConfig.getIdentityScope().clear();
}
public StatusDAO getStatusDAO() {
return statusDAO;
}
public UsersDAO getUsersDAO() {
return usersDAO;
}
}
4.1普通的办法:
private SQLiteDatabase db;
private DaoMaster daoMaster;
private DaoSession daoSession;
private NoteDao noteDao;
DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "notes-db", null);
db = helper.getWritableDatabase();
daoMaster = new DaoMaster(db);
daoSession = daoMaster.newSession();
noteDao = daoSession.getNoteDao();
不用每次new一个session
4.2.1新建一个类,继承Application
package com.sina.Demo;
import android.app.Application;
import android.content.Context;
import com.yang.DBUtil.DaoMaster;
import com.yang.DBUtil.DaoMaster.OpenHelper;
import com.yang.DBUtil.DaoSession;
public class BaseApplication extends Application {
private static BaseApplication mInstance;
private static DaoMaster daoMaster;
private static DaoSession daoSession;
@Override
public void onCreate() {
super.onCreate();
if (mInstance == null)
mInstance = this;
}
/**
* 取得DaoMaster
*
* @param context
* @return
*/
public static DaoMaster getDaoMaster(Context context) {
if (daoMaster == null) {
OpenHelper helper = new DaoMaster.DevOpenHelper(context,
Constants.DBNAME, null);
daoMaster = new DaoMaster(helper.getWritableDatabase());
}
return daoMaster;
}
/**
* 取得DaoSession
*
* @param context
* @return
*/
public static DaoSession getDaoSession(Context context) {
if (daoSession == null) {
if (daoMaster == null) {
daoMaster = getDaoMaster(context);
}
daoSession = daoMaster.newSession();
}
return daoSession;
}
}
4.2.2在AndroidManifest.xml中进行配置
application的属性name指向这个类。
4.2.3调用来生成数据库
application=(BaseApplication) getApplication();
daoMaster = application.getDaoMaster(MyHomePage.this);
daoSession = application.getDaoSession(MyHomePage.this);
statusDAO = daoSession.getStatusDAO();
usersDAO = daoSession.getUsersDAO();
拿到dao就可以进行数据操作了。
结束。
注意:它的数据库版本控制是在master类中,参数名叫:SCHEMA_VERSION,新的一次的版本号不能低于之前运行时的版本号,除非卸载重装。