項目太多,沒時間優(yōu)化簡書昔驱,只是為了做個記錄,煩請見諒。有時間再做個布局美化什么的
1:工程的build.gradle
classpath'org.greenrobot:greendao-gradle-plugin:3.2.2'
2:項目的build.gradle
1) applyplugin:'org.greenrobot.greendao' // apply plugin
2)greendao {
? ?schemaVersion1
? ? daoPackage'com.example.mayn.greendao'
? ? targetGenDir'src/main/java'
? ? generateTestsfalse
}
3)implementation'org.greenrobot:greendao:3.2.2'
使用:
創(chuàng)建實體類
@Entity?
@id? ? ? 后面跟的是自增長的意思捅位,如果需要自增加,設(shè)置成Long,調(diào)用People的時候绿渣,參數(shù)填寫成Null就可以
@Generated 后面是自動生成的朝群,不用管
這三個是必須要設(shè)置的
然后get,set
最后中符,項目
就會發(fā)現(xiàn)你的項目多了一個文件夾greendao命名的姜胖,里面內(nèi)容不需要動
這時你的GreenDao就配置成功了,此時需要一個工具類
public class SQLHelper {
private static SQLHelperinstance;
? ? private DaoMastermDaoMaster;
? ? private DaoMastermDaoMaster1;
? ? private DaoMaster.DevOpenHelpermHelper;
? ? private DaoMaster.DevOpenHelpermHelper1;
? ? private DaoSessionmDaoSession;
? ? private DaoSessionmDaoSession1;
? ? private PeopleDaopeopleInforDao;
? ? private RecordListDaorecordListDao;
? ? private ContextmContext;
? ? private SQLHelper(){
this.mContext = Myapplication.getApplication().getApplicationContext();
? ? ? ? mHelper=new DaoMaster.DevOpenHelper(mContext,
? ? ? ? ? ? ? ? "people.db", null);
? ? ? ? mHelper1=new DaoMaster.DevOpenHelper(mContext,
? ? ? ? ? ? ? ? "record.db", null);
? ? ? ? mDaoMaster =new DaoMaster(getWritableDatabase());
? ? ? ? mDaoMaster1 =new DaoMaster(getWritableDatabase1());
? ? ? ? mDaoSession =mDaoMaster.newSession();
? ? ? ? mDaoSession1 =mDaoMaster1.newSession();
? ? ? ? peopleInforDao =mDaoSession.getPeopleDao();
? ? ? ? recordListDao =mDaoSession1.getRecordListDao();
? ? }
private SQLiteDatabasegetWritableDatabase() {
if (mHelper ==null) {
mHelper =new DaoMaster.DevOpenHelper(mContext, "people.db", null);
? ? ? ? }
return? mHelper.getWritableDatabase();
? ? }
private SQLiteDatabasegetWritableDatabase1() {
if (mHelper1 ==null) {
mHelper1 =new DaoMaster.DevOpenHelper(mContext, "record.db", null);
? ? ? ? }
return? mHelper1.getWritableDatabase();
? ? }
private SQLiteDatabasegetReadableDatabase(){
if(mHelper ==null){
mHelper =new DaoMaster.DevOpenHelper(mContext,"people.db",null);
? ? ? ? }
return mHelper.getReadableDatabase();
? ? }
public static SQLHelpergetInstance(){
if (instance ==null) {
synchronized (SQLHelper.class) {
if (instance ==null) {
instance =new SQLHelper();
? ? ? ? ? ? ? ? }
}
}
return instance;
? ? }
/**
* 返回相應(yīng)的行數(shù)
? ? * @param entity
? ? * @return
? ? */
? ? public long insert(People entity){
return peopleInforDao.insert(entity);
? ? }
public long insert1(RecordList entity){
return recordListDao.insert(entity);
? ? }
/**
*
? ? * @param id 刪除的id淀散,刪除此行數(shù)據(jù)
*/
? ? public void delete(long id){
peopleInforDao.queryBuilder()
.where(PeopleDao.Properties.Id.eq(id)).buildDelete()
.executeDeleteWithoutDetachingEntities();
? ? }
/**
* 查重
? ? * @param name
? ? * @return
? ? */
? ? public boolean isSave(String name){
QueryBuilder queryBuilder =peopleInforDao.queryBuilder().where(PeopleDao.Properties.Number.eq(name));
? ? ? ? Long size = queryBuilder.buildCount().count();
? ? ? ? return size >0;
? ? }
/**
* 更新
? ? * @param hand
? ? * @param id
? ? ? */
? ? public void update(Long id, String hand) {
People people =peopleInforDao.queryBuilder().where(PeopleDao.Properties.Id.eq(id)).unique();
? ? ? ? people.setHand(hand);
? ? ? ? peopleInforDao.update(people);
? ? }
/**
* 根據(jù)id添加
? ? * @param id
? ? * @return
? ? */
? ? public ListselectPeople(long id) {
return peopleInforDao.queryBuilder()
.where(PeopleDao.Properties.Id.eq(id))
.list();
? ? }
public ListqueryAll(){
return peopleInforDao.queryBuilder().list();
? ? }
public ListqueryAll1(){
return recordListDao.queryBuilder().list();
? ? }
/**
* 刪除
*/
? ? public void deleteAll() {
peopleInforDao.deleteAll();
? ? }
public void deleteAll1() {
recordListDao.deleteAll();
? ? }
}
這個工具類里有查重右莱,跟新,刪除 档插,等基礎(chǔ)使用慢蜓,如果需要其他功能自行添加
如只有一個實體類,
把紅框里的都刪除郭膛,下面報錯的也刪了晨抡,就可以了。懶得起名字则剃,都是一號二號這樣耘柱。所以見諒
使用中
獲取全部:
添加:
更新:
根據(jù)ID添加hand數(shù)據(jù),在Sql工具類中可以修改
SQLHelper.getInstance().update(people.getId(), strfea);
刪除:
根據(jù)ID刪除
SQLHelper.getInstance().delete(Long.parseLong(id));
全部刪除
SQLHelper.getInstance().deleteAll();
查重:
大概就是這些內(nèi)容