Android GreenDao簡單使用
阿新 • • 發佈:2018-11-16
一些簡單的配置都在上篇部落格中 定義工具類 package soexample.umeng.com.day03greendao; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import java.util.List; import soexample.umeng.com.day03greendao.greendao.DaoMaster; import soexample.umeng.com.day03greendao.greendao.DaoSession; public class SqlLiteUtils { private SQLiteDatabase db; private DaoMaster mDaoMaster; private DaoSession mSession; //單例模式 public SqlLiteUtils() { } private static SqlLiteUtils mSqlLiteUtilis; public static SqlLiteUtils getmSqlLiteUtilis(){ if (mSqlLiteUtilis == null){ mSqlLiteUtilis = new SqlLiteUtils(); } return mSqlLiteUtilis; } public void initData(Context context){ DaoMaster.DevOpenHelper qiang = new DaoMaster.DevOpenHelper( context, "jin" ); db = qiang.getWritableDatabase(); mDaoMaster = new DaoMaster( db ); mSession= mDaoMaster.newSession(); } //簡單的增刪查改 public void insert(User user){ mSession.getUserDao().insert( user ); } public List<User> queryAll(){ return mSession.getUserDao().loadAll(); } public User query(Long key){ return mSession.getUserDao().load( key ); } public void deleteAll(){ mSession.getUserDao().deleteAll(); } public void delete(String key){ mSession.getUserDao().deleteByKey( Long.parseLong( key ) ); } public void update(User user){ mSession.getUserDao().update( user ); } } 在MainActivity佈局裡面定義按鈕簡單使用
package soexample.umeng.com.day03greendao; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Toast; import java.util.List; public class MainActivity extends AppCompatActivity implements View.OnClickListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate( savedInstanceState ); setContentView( R.layout.activity_main ); findViewById( R.id.addUser ).setOnClickListener( this ); findViewById( R.id.delUser ).setOnClickListener( this ); findViewById( R.id.updUser ).setOnClickListener( this ); findViewById( R.id.queUser ).setOnClickListener( this ); findViewById( R.id.queUserOne ).setOnClickListener( this ); findViewById( R.id.delUserOne ).setOnClickListener( this ); SqlLiteUtils.getmSqlLiteUtilis().initData( this ); } @Override public void onClick(View view) { switch (view.getId()) { case R.id.addUser: User user = new User(); user.setUsername( "17801130950" ); user.setPassword( "1111111" ); user.setAge( 18 ); user.setSex( "男" ); SqlLiteUtils.getmSqlLiteUtilis().insert( user ); break; case R.id.delUser: SqlLiteUtils.getmSqlLiteUtilis().deleteAll(); Log.i( "TAG", "刪除成功" + "AAA" ); break; case R.id.updUser: User two = SqlLiteUtils.getmSqlLiteUtilis().query( Long.parseLong( "24" ) ); two.setUsername( "123456789" ); SqlLiteUtils.getmSqlLiteUtilis().update( two ); break; case R.id.queUser: List<User> list = SqlLiteUtils.getmSqlLiteUtilis().queryAll(); for (int i = 0; i < list.size(); i++) { if (list == null) { Log.i( "TAG", "沒有資料" ); } else { Log.i( "TAG", "" + list.get( i ).getUsername() + "====" + list.get( i ).getId() ); } } break; case R.id.queUserOne: User query = SqlLiteUtils.getmSqlLiteUtilis().query( Long.parseLong( "1" ) ); if (query == null) { Log.i( "TAG", "沒有資料" ); } else { Log.i( "TAG", query.getUsername() + query.getId() ); } break; case R.id.delUserOne: SqlLiteUtils.getmSqlLiteUtilis().delete( "1" ); Log.i( "TAG", "成功刪除一條" ); break; } } }
定義一個實體類 @Entity //設定主鍵 @Id(autoincrement = true) package soexample.umeng.com.day03greendao; import org.greenrobot.greendao.annotation.Entity; import org.greenrobot.greendao.annotation.Id; import org.greenrobot.greendao.annotation.Generated; @Entity public class User { @Id(autoincrement = true) private Long id; private String username; private String password; private int age; private String sex; Ctrl+F9快捷鍵生成只需定義及格成員屬性就可以了 @Generated(hash = 1351765912) public User(Long id, String username, String password, int age, String sex) { this.id = id; this.username = username; this.password = password; this.age = age; this.sex = sex; } @Generated(hash = 586692638) public User() { } public Long getId() { return this.id; } public void setId(Long id) { this.id = id; } public String getUsername() { return this.username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return this.password; } public void setPassword(String password) { this.password = password; } public int getAge() { return this.age; } public void setAge(int age) { this.age = age; } public String getSex() { return this.sex; } public void setSex(String sex) { this.sex = sex; } }