Configure plug-ins and dependencies
Under the project
-----------stay repositories Put the warehouse in the brackets of---- mavenCentral()
-------------Add plug-ins in parentheses of dependencies---------
classpath 'org.greenrobot:greendao-gradle-plugin:3.2.2'
---------------On the top of Build in app---------------------
apply plugin: 'org.greenrobot.greendao'
----------------Build plus dependency in app----------------------
implementation 'org.greenrobot:greendao:3.2.2'
----------------On top of dependencies-----------
greendao { schemaVersion 1 //Edition daoPackage 'Package name.greendao' // Generally, it is the app package name + the folder name of the generated file targetGenDir 'src/main/java' //Build file path }
Write a Bean
After the annotation is written, press Ctrl+Fn+F9 to automatically generate Dao
@Entity public class GBean { @Id(autoincrement = true)//Primary key self increment private Long id;
Generated files
Write a tool class for adding, deleting, modifying and querying
public class SqliteUtiles { private SQLiteDatabase db; private DaoMaster daoMaster; private DaoSession daoSession; //Single instance requires a private construct and a public static parameter private SqliteUtiles() { } private static SqliteUtiles sqliteUtiles; public static SqliteUtiles getSqliteUtiles() { //Judge whether it is empty if (sqliteUtiles == null) { sqliteUtiles = new SqliteUtiles(); } return sqliteUtiles; } //Initialize database transfer context public void init(Context context){ //The Dev method of new daomaster returns a help class by passing the context database name DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(context, "g1"); //Get db of write data db = helper.getWritableDatabase(); daoMaster = new DaoMaster(db);//control class daoSession = daoMaster.newSession();//Initialization protocol } //increase public void insert(User user){ //Directly use daoSession to get the insert in the userdao call daoSession.getUserDao().insert(user); } //Check all public List<User> chaAll(){ //Query all return daoSession.getUserDao().loadAll(); } //Single query public User chaid(String key){ //Transfer primary key Long object return daoSession.getUserDao().load(Long.parseLong(key)); } //Single deletion public void delid(String key){ daoSession.getUserDao().deleteByKey(Long.parseLong(key)); } //Delete all public void delAll(){ daoSession.getUserDao().deleteAll(); } //modify public void gai(User user){ daoSession.getUserDao().update(user); } }
Use this tool class
First of all, the page initialization tool class in use must
SqliteUtiles.getSqliteUtiles().init(this);
Here are a few buttons to show how to add, delete, modify and check
@Override public void onClick(View view) { switch (view.getId()) { case R.id.zeng://Directly call the add method in the tool class User user = new User(); user.setAge(18); user.setName("high XX"); user.setPsd("2452"); user.setSex("male"); SqliteUtiles.getSqliteUtiles().insert(user); break; case R.id.cha://query List<User> cha = SqliteUtiles.getSqliteUtiles().chaAll(); for (int i = 0; i < cha.size(); i++) { Log.i("MainActivity", cha.get(i).getName()+"id: "+cha.get(i).getId()+"Gender:"+cha.get(i).getSex()+"Password:"+cha.get(i).getPsd()+"Age:"+cha.get(i).getAge()); } break; case R.id.chaid: //key inside User chaid = SqliteUtiles.getSqliteUtiles().chaid("2"); Log.i("MainActivity", "id: " + chaid.getId() + "Name:" + chaid.getName()); break; case R.id.del: //Delete all SqliteUtiles.getSqliteUtiles().delAll(); break; case R.id.delid: //Delete single SqliteUtiles.getSqliteUtiles().delid("1"); break; case R.id.gai: //Query one before modifying User usergai = SqliteUtiles.getSqliteUtiles().chaid("19"); usergai.setId(Long.parseLong("19")); usergai.setSex("female"); usergai.setName("Horse XX"); usergai.setPsd("123456"); SqliteUtiles.getSqliteUtiles().gai(usergai); break; } }