项目太多,没时间优化,只是为了做个记录,烦请见谅。有时间再做个布局美化什么的
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'
使用:
创建实体类
@Entity
@id 后面跟的是自增长的意思,如果需要自增加,设置成Long,调用People的时候,参数填写成Null就可以
@Generated 后面是自动生成的,不用管
这三个是必须要设置的
然后get,set
最后,项目
就会发现你的项目多了一个文件夹greendao命名的,里面内容不需要动
这时你的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;
}
/**
* 返回相应的行数
* @param entity
* @return
*/
public long insert(People entity){
return peopleInforDao.insert(entity);
}
public long insert1(RecordList entity){
return recordListDao.insert(entity);
}
/**
*
* @param id 删除的id,删除此行数据
*/
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);
}
/**
* 根据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();
}
}
这个工具类里有查重,跟新,删除 ,等基础使用,如果需要其他功能自行添加
如只有一个实体类,
把红框里的都删除,下面报错的也删了,就可以了。懒得起名字,都是一号二号这样。所以见谅
使用中
获取全部:
添加:
更新:
根据ID添加hand数据,在Sql工具类中可以修改
SQLHelper.getInstance().update(people.getId(), strfea);
删除:
根据ID删除
SQLHelper.getInstance().delete(Long.parseLong(id));
全部删除
SQLHelper.getInstance().deleteAll();
查重:
大概就是这些内容