Android中初步使用greenDAO3.2
来源:互联网 发布:淘宝保证金解冻步骤 编辑:程序博客网 时间:2024/06/04 18:16
greenDAO是一个将对象映射到SQLite数据库中的轻量且快速的ORM解决方案。
首先是在项目中导入相关依赖,与其它不同,greenDAO的依赖既需要在Module的gradle中添加,有需要在Project的gradle中添加。
在Project的gradle中的dependencies
中添加classpath 'org.greenrobot:greendao-gradle-plugin:3.2.1
buildscript { repositories { jcenter() } dependencies { classpath 'com.android.tools.build:gradle:2.2.3' classpath 'org.greenrobot:greendao-gradle-plugin:3.2.1 // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files }}
在Module的gradle需要添加三部分首先是 apply plugin: 'org.greenrobot.greendao'
然后需要在 android
中添加
greendao{ schemaVersion 1 targetGenDir 'src/main/java/'}
最后再加入compile 'org.greenrobot:greendao:3.2.0'
的依赖。
添加后的gradle代码如下:
apply plugin: 'com.android.application'apply plugin: 'org.greenrobot.greendao'android { compileSdkVersion 25 buildToolsVersion "25.0.2" defaultConfig { applicationId "com.example.daodemo" minSdkVersion 15 targetSdkVersion 25 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } greendao{ schemaVersion 1 targetGenDir 'src/main/java/' //为greendao代码自动生成的路径 }}dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) compile 'com.android.support:appcompat-v7:25.1.0' testCompile 'junit:junit:4.12' compile 'org.greenrobot:greendao:3.2.0'}
Rebuild后AS就会下载相关jar包,与greenDAO的插件。接着需要建立一个实体类以一个简单的User类为例(需要在class前加上@Entity
注解):
@Entity//将该类变为一个能被greenDAO识别的实体类public class User { @Id(autoincrement = true) //通过该属性标识的必须是Long型,在数据库中作为主键,并默认自增 //autoincrement = true 表示id自增,false 使用旧值 private Long userId; @Unique //该属性标识的必须是数据库中的唯一值 private String name; private String userPassword; }
greenDAO的其它常用注解有 @NotNull
:属性不能为空。 @Transien
:临时储存的数据,不需持久化存入数据库中。
之后使用Build中的Make Project,greenDAO的插件便自动为我们补充了User类,并且添加了 DaoMaster
DaoSession
UserDao
三个类,
接下来是配置greenDAO,建立一个app
类继承自Application
public class App extends Application{ private static DaoSession daoSession; @Override public void onCreate() { super.onCreate(); setupUserDb(); } /** *配置数据库 */ private void setupUserDb(){ //创建user.db DaoMaster.DevOpenHelper openHelper = new DaoMaster.DevOpenHelper(this,"user.db",null); SQLiteDatabase db = openHelper.getWritableDatabase(); //获取数据库对象 DaoMaster daoMaster = new DaoMaster(db); //获取greenDAO的对象管理者 daoSession = daoMaster.newSession(); } public static DaoSession getDaoSession() { return daoSession; }}
下面在创建一个UserDAOUtils类来实现增,删,改,的查相关方法
public class UserDAOUtils { /** * 增加数据 */ public static void insertUser(User user){ App.getDaoSession().getUserDao().insertOrReplace(user); } /** * 删除数据 */ public static void deleteUser(Long id){ App.getDaoSession().getUserDao().deleteByKey(id); } public static void deleteUser(User user){ App.getDaoSession().getUserDao().delete(user); } /** * 更新数据 */ public static void updateUser(User user){ App.getDaoSession().getUserDao().update(user); } /** *查询全部 */ public static List<User> queryAll(){ return App.getDaoSession().getUserDao().loadAll(); } /** * 按条件查询 */ public static List<User> queryAs(WhereCondition... whereCondition){ List<User> users = new ArrayList<User>(); for (WhereCondition w :whereCondition){ users.addAll(App.getDaoSession().getUserDao().queryBuilder() .where(w).list()); } return users; }}
最后,试验一下,greenDAO的相关使用,布局文件
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="@dimen/activity_horizontal_margin" tools:context="com.example.daodemo.Acctivity.MainActivity" android:weightSum="1"> <TextView android:layout_width="match_parent" android:layout_height="200dp" android:id="@+id/output_data" android:textSize="25sp" android:padding="10dp" android:layout_weight="0.70" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="添加数据" android:id="@+id/insert_data"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="查询数据" android:id="@+id/query_all"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/query_as" android:text="查询隔壁老王"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/query_as_more" android:text="查询老王,小黑"/></LinearLayout>
MainActivity中:
public class MainActivity extends AppCompatActivity implements View.OnClickListener{ private TextView textView; private Button queryAll; private Button queryAs; private Button queryAsMore; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init();//初始化 } /** * 初始化 */ private void init(){ textView = (TextView)findViewById(R.id.output_data); queryAll = (Button)findViewById(R.id.query_all); queryAs = (Button)findViewById(R.id.query_as); queryAsMore = (Button)findViewById(R.id.query_as_more); queryAll.setOnClickListener(this); queryAs.setOnClickListener(this); queryAsMore.setOnClickListener(this); initUser();//初始化User } @Override public void onClick(View view) { int id = view.getId(); switch (id){ case R.id.query_all: { List<User> users = UserDAOUtils.queryAll(); textView.setText(getUserData(users)); break; } case R.id.query_as: { List<User> users = UserDAOUtils.queryAs(UserDao.Properties.Name.eq("隔壁老王")); textView.setText(getUserData(users)); break; } case R.id.query_as_more: { List<User> users = UserDAOUtils.queryAs(UserDao.Properties.Name.eq("隔壁老王"), UserDao.Properties.Name.eq("楼下小黑")); textView.setText(getUserData(users)); break; } } } /** * 初始化User */ private void initUser(){ User user1 = new User((long) 0, "隔壁老王", "laowang"); User user2 = new User((long) 1, "楼下小黑", "xiaohei"); User user3 = new User((long) 2, "门口大黄", "dahuang"); UserDAOUtils.insertUser(user1); UserDAOUtils.insertUser(user2); UserDAOUtils.insertUser(user3); } /** *获取User的数据 */ private String getUserData(List<User> users){ StringBuilder sb = new StringBuilder(); for (int i = 0; i < users.size(); i++) { User user = users.get(i); sb.append(user.getName() + "\n" + user.getUserPassword() + "\n"); } return sb.toString(); }}
Demo的演示
- Android中初步使用greenDAO3.2
- GreenDao3的初步使用
- android studio中使用GreenDao3.2.0
- Android 中GreenDao3.0简单使用
- GreenDao3在Android Studio中使用实例
- GreenDao3.2在AndroidStudio中的配置和初步使用
- Android GreenDao3.2配置及使用详解
- android stdudio 数据库框架greendao3.2使用
- Android 笔记:GreenDao3.2的使用,爱不释手
- Android GreenDao3.2配置及使用详解
- Android GreenDao3.2配置及使用详解
- Android GreenDao3.2.0使用
- greenDAO3 中使用关系
- greenDAO3.2的使用
- GreenDao3.2的使用
- Greendao3.2使用
- GreenDao3.2使用
- GreenDao3.2使用宝典
- 王幼军:拉普拉斯概率理论的历史研究
- Qt Model-View (1)
- [BZOJ2732][HNOI2012]射箭(二分+半平面交)
- Android ContentProvider的介绍(很详细)
- ubuntu下安装Spark(单机环境)
- Android中初步使用greenDAO3.2
- Wireless Network 并查集
- 热的解析理论
- mysql复制
- java 常见需要死记硬背的
- ZCMU-1828-十六进制转八进制
- BZOJ1005: [HNOI2008]明明的烦恼
- OpenMP 中的线程任务调度
- Spring Boot 菜鸟教程 20 elasticsearch