使用greenDAO进行开发总结
来源:互联网 发布:盐城seo 编辑:程序博客网 时间:2024/06/05 13:20
网上有很多介绍使用greenDAO的方法,看完之后不是很清楚,因为所有的讲解都是围绕其自身附带的例子,java文件也使用的是生成的,没有针对性,比如当接入第三方进行开发时,第三方的model是提供好的,而且可能不是普通的getter和setter,而是使用的public属性,还有就是项目移植,不用它的代码来升级框架层。所以我就没能使用它生成的文件来开发。
我的开发过程(前提是有自己的model,我以我的model:Status为例):
1、引入DaoCore库工程,加入到你的项目库里面。
2、创建自己的DAO文件,继承AbstractDao<Status, Long> ,
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);}@Overridepublic 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);}@Overridepublic 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<Class<? extends AbstractDao<?, ?>>, 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、使用它来建表
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();
4.2官方推荐的办法:
不用每次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;@Overridepublic 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,新的一次的版本号不能低于之前运行时的版本号,除非卸载重装。
- 使用greenDAO进行开发总结
- Greendao 使用总结
- greendao 3.2使用总结
- greenDao----使用greenDao进行查询与Session(3)
- 使用GreenDao对数据库进行操作
- GreenDao总结
- android开发 greendao学习及使用笔记
- GreenDao在Android项目开发中的使用
- Android开发之GreenDao的简单使用
- 使用AppFuse进行开发的总结 (摘)
- 使用AppFuse进行开发的总结
- 使用AppFuse进行开发的总结
- 使用AppFuse进行开发的总结
- 使用AppFuse进行开发的总结
- 使用gSOAP进行Webservice开发总结
- ORM对象关系映射之使用GreenDAO进行CRUD操作
- ORM对象关系映射之使用GreenDAO进行CRUD操作
- GreenDao使用
- AVL树与Hash Table
- 文本分类实验中用java实现取名词和去除停用词
- awk中next以及getline用法示例
- C++ 静态变量或成员初始化陷阱与缺陷
- UVA 401 Palindromes 回文词
- 使用greenDAO进行开发总结
- 黑马程序员IOS键盘的关闭以及通知中心的运用
- Fedora 17 修改GRUB启动菜单顺序
- NYOJ-49 开心的小明
- Titanium笔记---ScrollableView
- 设计模式之(Decorator)装饰者模式
- poj 1681 Painter's Problem 高斯消元
- IplImage和Mat的转换
- 二进制与java移位