标签:
package com.example.jekin.greendao.manager; import android.app.Application; import android.content.Context; import com.example.jekin.greendao.dao.DaoMaster; import com.example.jekin.greendao.dao.DaoSession; /** * Created by JeKin on 2016/4/8. */ public class App extends Application{ public static App mInstance; public static DaoMaster daoMaster; public static DaoSession daoSession; public static DaoManager daoManager; @Override public void onCreate() { super.onCreate(); mInstance = this; daoManager = DaoManager.getInstance(getApplicationContext()); } /** * 取得DaoMaster * * @param context * @return daoMaster */ public static DaoMaster getDaoMaster(Context context) { DaoMaster.OpenHelper helper = new DaoMaster.DevOpenHelper(context, "person.db", null); daoMaster = new DaoMaster(helper.getWritableDatabase()); return daoMaster; } /** * 取得DaoSession * * @param context * @return daoSession */ public static DaoSession getDaoSession(Context context) { if (daoSession == null) { if (daoMaster == null) { daoMaster = getDaoMaster(context); } daoSession = daoMaster.newSession(); } return daoSession; } }DaoManager类:
package com.example.jekin.greendao.manager; import android.content.Context; import com.example.jekin.greendao.dao.DaoSession; import com.example.jekin.greendao.dao.Person; import com.example.jekin.greendao.dao.PersonDao; import java.util.List; import de.greenrobot.mydao.query.DeleteQuery; import de.greenrobot.mydao.query.QueryBuilder; import de.greenrobot.mydao.query.WhereCondition; /** * Created by JeKin on 2016/4/8. * 功能实现类 */ public class DaoManager { private static DaoManager instance; private static Context appContext; private DaoSession mDaoSession; private PersonDao personDao; public DaoManager(){ } public static DaoManager getInstance(Context context){ if (instance == null){ instance = new DaoManager(); if (appContext == null) { appContext = context.getApplicationContext(); } instance.mDaoSession = App.getDaoSession(context); instance.personDao = instance.mDaoSession.getPersonDao(); } return instance; } /** * ================Person====================* */ public List<Person> orderAscPerson() { return personDao.queryBuilder().orderAsc(PersonDao.Properties.Id).list(); } /** * Person插入功能 * * @return * @param:album */ public void insertPerson(Person person) { personDao.insert(person); } public void insertOrReplacePerson(Person person) { personDao.insertOrReplaceInTx(person); } public void updatePerson(Person person) { personDao.update(person); } /** * Person查找功能 * //查找条件 * @param arg0 * @param conditions * @return:albumList */ public List<Person> queryPerson(WhereCondition arg0, WhereCondition... conditions) { QueryBuilder<Person> qb = personDao.queryBuilder(); qb.where(arg0, conditions); List<Person> personList = qb.list(); return personList; } /** * Person删除所有功能 * * @param * @return */ public void deleteAllPerson() { personDao.deleteAll(); } /** * Person删除功能 * * @return * @param:album */ public void deletePerson(Person person) { personDao.delete(person); } public void deletePersonByName(String name) { QueryBuilder<Person> qb = personDao.queryBuilder(); DeleteQuery<Person> bd = qb.where(PersonDao.Properties.Name.eq(name)) .buildDelete(); bd.executeDeleteWithoutDetachingEntities(); } }utils文件夹下只有DaoUtils封装类,易于后期扩展,懂点单词的都很容易看懂
package com.example.jekin.greendao.utils; import com.example.jekin.greendao.dao.Person; import com.example.jekin.greendao.dao.PersonDao; import com.example.jekin.greendao.dao.JsonCode; import com.example.jekin.greendao.manager.App; import java.util.List; /** * Created by JeKin on 2016/4/8. * 对数据库的实现进行封装,隐藏实现细节 */ public class DaoUtils { //===================getPersonDao======================== public static List<Person> getPersonByName(String name) { List<Person> list = null; list = App.daoManager.queryPerson(PersonDao.Properties.Name.eq(name)); return list; } // 查找排序 public static List<Person> getPerson() { List<Person> personList = null; personList = App.daoManager.orderAscPerson(); return personList; } //==============================insertDao==================================== public static void insertPersonDao(JsonCode jsonCode) { // 添加数据 for (int i = 0; i < jsonCode.getPerson().size(); i++) { App.daoManager.insertPerson(jsonCode.getPerson().get(i)); } } public static boolean checkPersonExistAndUpdate(String name) { List<Person> personList = App.daoManager.queryPerson(PersonDao.Properties.Name.eq(name)); if (personList.size() > 0) { for (int i = 0; i < personList.size(); i++) { Person person = new Person(personList.get(i).getId(), personList.get(i).getName(), personList.get(i).getHigh(), personList.get(i).getAge()); App.daoManager.insertOrReplacePerson(person); } return true; } return false; } }最后就是我们的老大出场了,主要代码就四句
package com.example.jekin.greendao; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; import android.widget.ListView; import android.widget.SimpleAdapter; import com.example.jekin.greendao.dao.Person; import com.example.jekin.greendao.dao.JsonCode; import com.example.jekin.greendao.manager.App; import com.example.jekin.greendao.utils.DaoUtils; import com.google.gson.Gson; import java.util.ArrayList; /** * Created by JeKin on 2016/04/12 * 数据操作类 */ public class MainActivity extends AppCompatActivity { private static final String TAG1 = "addData"; private static final String TAG2 = "deleteData"; private static final String TAG3 = "changeData"; private static final String TAG4 = "queryData"; // 模拟JSON数据 private String jsonString = "{'code':'200','success':'true','Person':[{'name':'jekin','high':'173','age':'23'},{'name':'mike','high':'178','age':'24'}]}"; // 查找数据的条件 private String name = "mike"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); JsonCode jsonCode = new Gson().fromJson(jsonString, JsonCode.class); // 添加数据 // 检查是否已经存在该Person对象,不存在则插入 boolean isExist = DaoUtils.checkPersonExistAndUpdate(name); if (!isExist) { DaoUtils.insertPersonDao(jsonCode); } // 增加之后查找数据 for (Person person : DaoUtils.getPerson()) { Log.e(TAG1, person.getId().toString()); Log.e(TAG1, person.getName().toString()); Log.e(TAG1, person.getHigh().toString()); Log.e(TAG1, person.getAge().toString()); } // 条件删除 App.daoManager.deletePersonByName(name); for (Person person : DaoUtils.getPerson()) { Log.e(TAG2, person.getId().toString()); Log.e(TAG2, person.getName().toString()); Log.e(TAG2, person.getHigh().toString()); Log.e(TAG2, person.getAge().toString()); } // 条件修改 if (DaoUtils.checkPersonExistAndUpdate(name)) { for (Person person : DaoUtils.getPersonByName(name)) { Log.e(TAG3, person.getId().toString()); Log.e(TAG3, person.getName().toString()); Log.e(TAG3, person.getHigh().toString()); Log.e(TAG3, person.getAge().toString()); } } // 修改之后查询语句 for (Person person : DaoUtils.getPerson()) { Log.e(TAG4, person.getId().toString()); Log.e(TAG4, person.getName().toString()); Log.e(TAG4, person.getHigh().toString()); Log.e(TAG4, person.getAge().toString()); } } }
Android数据库框架GreenDao封装使用,易理解、易扩展
标签:
原文地址:http://blog.csdn.net/fjekin/article/details/51142056