GreenDao3.0
来源:互联网 发布:网络诈骗青少年案例 编辑:程序博客网 时间:2024/06/06 10:50
apply plugin: 'com.android.application'android { compileSdkVersion 25 buildToolsVersion "25.0.3" defaultConfig { applicationId "com.anye.greendao" minSdkVersion 15 targetSdkVersion 25 versionCode 1 versionName "1.0" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } }}apply plugin: 'org.greenrobot.greendao'buildscript { repositories { mavenCentral() }}greendao { schemaVersion 1 daoPackage 'com.anye.greendao.gen' targetGenDir 'src/main/java'}dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:25.0.0' compile 'org.greenrobot:greendao:3.0.1' compile 'org.greenrobot:greendao-generator:3.0.0'}============================================================================================================
属性介绍:
schemaVersion--> 指定数据库schema版本号,迁移等操作会用到;
daoPackage --> dao的包名,包名默认是entity所在的包;
targetGenDir --> 生成数据库文件的目录;
==================================================================================
// Top-level build file where you can add configuration options common to all sub-projects/modules.buildscript { repositories { jcenter() } dependencies { classpath 'com.android.tools.build:gradle:2.3.1' classpath 'org.greenrobot:greendao-gradle-plugin:3.0.0' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files }}allprojects { repositories { jcenter() }}task clean(type: Delete) { delete rootProject.buildDir}
创建一个User的实体类,MakeProject,实体类会自动编译,生成get、set方法并且会在com.anye.greendao.gen目录下生成三个文件;
============================================================================
greendao中的注解
(一) @Entity 定义实体
@nameInDb 在数据库中的名字,如不写则为实体中类名
@indexes 索引
@createInDb 是否创建表,默认为true,false时不创建
@schema 指定架构名称为实体
@active 无论是更新生成都刷新
(二) @Id
(三) @NotNull 不为null
(四) @Unique 唯一约束
(五) @ToMany 一对多
(六) @OrderBy 排序
(七) @ToOne 一对一
(八) @Transient 不存储在数据库中
(九) @generated 由greendao产生的构造函数或方法
===============================================================================
package com.anye.greendao.db;import android.content.Context;import com.anye.greendao.gen.DaoMaster;import com.anye.greendao.gen.DaoSession;/** * Created by anye0 on 2016/7/24. */public class DBHelper { private static DBHelper instance; private static Context mContext; private static DaoMaster daoMaster; private static DaoSession daoSession; /** * 取得DaoMaster * * @param context * @return */ public static DaoMaster getDaoMaster(Context context) { if (daoMaster == null) { DaoMaster.OpenHelper helper = new DaoMaster.DevOpenHelper(context, "notes.db", null); daoMaster = new DaoMaster(helper.getWritableDatabase()); } return daoMaster; } /** * 取得DaoSession * * @param context * @return */ public static DaoSession getDaoSession(Context context) { if (daoSession == null) { if (daoMaster == null) { daoMaster = getDaoMaster(context); } daoSession = daoMaster.newSession(); } return daoSession; } private DBHelper() { } public static void init(Context context) { mContext = context; instance = new DBHelper(); // 数据库对象 DaoSession daoSession = getDaoSession(mContext); } public static DBHelper getInstance() { return instance; }}
package com.anye.greendao.entity;import org.greenrobot.greendao.annotation.Entity;import org.greenrobot.greendao.annotation.Id;import org.greenrobot.greendao.annotation.Transient;import org.greenrobot.greendao.annotation.Generated;/** * Created by anye0 on 2016/7/24. */@Entitypublic class User { @Id private Long id; private String name; @Transient private int tempUsageCount; // not persisted public String getName() { return this.name; } public void setName(String name) { this.name = name; } public Long getId() { return this.id; } public void setId(Long id) { this.id = id; } @Generated(hash = 873297011) public User(Long id, String name) { this.id = id; this.name = name; } @Generated(hash = 586692638) public User() { }}
package com.anye.greendao;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.Button;import android.widget.TextView;import android.widget.Toast;import com.anye.greendao.entity.User;import com.anye.greendao.gen.UserDao;import java.util.List;public class MainActivity extends AppCompatActivity implements View.OnClickListener { private Button mAdd,mDelete,mUpdate,mFind; private TextView mContext; private User mUser,mUser1,mUser2,mUser3; private UserDao mUserDao; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); initEvent(); mUserDao = MyApplication.getInstances().getDaoSession().getUserDao(); } private void initEvent() { mAdd.setOnClickListener(this); mDelete.setOnClickListener(this); mUpdate.setOnClickListener(this); mFind.setOnClickListener(this); } private void initView() { mContext = (TextView) findViewById(R.id.textView); mAdd = (Button) findViewById(R.id.button); mDelete = (Button) findViewById(R.id.button2); mUpdate = (Button) findViewById(R.id.button3); mFind = (Button) findViewById(R.id.button4); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.button: addDate(); break; case R.id.button2: deleteDate(); break; case R.id.button3: updateDate(); break; case R.id.button4: findDate(); break; } } /** * 增加数据 */ private void addDate() { mUser1 = new User((long)1,"anye3"); mUser2 = new User((long)2,"anye3"); mUser3 = new User((long)3,"anye3"); mUserDao.insert(mUser1);//添加一个 mUserDao.insert(mUser2);//添加一个 mUserDao.insert(mUser3);//添加一个 //mContext.setText(mUser.getName()); Toast.makeText(MainActivity.this,"点击添加",Toast.LENGTH_SHORT).show(); } /** * 删除数据 */ private void deleteDate() { deleteUserById(2); } /** * 根据主键删除User * * @param id User的主键Id */ public void deleteUserById(long id) { mUserDao.deleteByKey(id); Toast.makeText(MainActivity.this,"点击删除 id--> "+id,Toast.LENGTH_SHORT).show(); } /** * 更改数据 */ private void updateDate() { mUser = new User((long)1,"anye0803"); mUserDao.update(mUser); Toast.makeText(MainActivity.this,"点击更改 id==1",Toast.LENGTH_SHORT).show(); } /** * 查找数据 */ private void findDate() { List<User> users = mUserDao.loadAll(); String userName = ""; for (int i = 0; i < users.size(); i++) { userName += users.get(i).getName()+","; } Toast.makeText(MainActivity.this,"点击查找",Toast.LENGTH_SHORT).show(); mContext.setText("查询全部数据==>"+userName); }}
package com.anye.greendao;import android.app.Application;import android.database.sqlite.SQLiteDatabase;import com.anye.greendao.gen.DaoMaster;import com.anye.greendao.gen.DaoSession;/** * Created by anye0 on 2016/7/24. */public class MyApplication extends Application { private DaoMaster.DevOpenHelper mHelper; private SQLiteDatabase db; private DaoMaster mDaoMaster; private DaoSession mDaoSession; public static MyApplication instances; @Override public void onCreate() { super.onCreate(); instances = this; setDatabase(); } public static MyApplication getInstances(){ return instances; } /** * 设置greenDao */ private void setDatabase() { // 通过 DaoMaster 的内部类 DevOpenHelper,你可以得到一个便利的 SQLiteOpenHelper 对象。 // 可能你已经注意到了,你并不需要去编写「CREATE TABLE」这样的 SQL 语句,因为 greenDAO 已经帮你做了。 // 注意:默认的 DaoMaster.DevOpenHelper 会在数据库升级时,删除所有的表,意味着这将导致数据的丢失。 // 所以,在正式的项目中,你还应该做一层封装,来实现数据库的安全升级。 mHelper = new DaoMaster.DevOpenHelper(this, "notes-db", null); db = mHelper.getWritableDatabase(); // 注意:该数据库连接属于 DaoMaster,所以多个 Session 指的是相同的数据库连接。 mDaoMaster = new DaoMaster(db); mDaoSession = mDaoMaster.newSession(); } public DaoSession getDaoSession() { return mDaoSession; } public SQLiteDatabase getDb() { return db; }}
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.anye.greendao.MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" android:id="@+id/textView" android:layout_centerVertical="true" android:layout_alignLeft="@+id/button4" android:layout_alignStart="@+id/button4" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="增" android:id="@+id/button" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="删" android:id="@+id/button2" android:layout_below="@+id/button" android:layout_toLeftOf="@+id/button4" android:layout_toStartOf="@+id/button4" android:layout_marginRight="19dp" android:layout_marginEnd="19dp" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="改" android:id="@+id/button3" android:layout_below="@+id/button2" android:layout_alignLeft="@+id/button2" android:layout_alignStart="@+id/button2" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="查" android:id="@+id/button4" android:layout_alignBaseline="@+id/button3" android:layout_alignBottom="@+id/button3" android:layout_centerHorizontal="true" /></RelativeLayout>
- GreenDao3.0
- GreenDao3.0学习(一)
- GreenDao3.0学习(二)
- GreenDao3.0学习(三)
- GreenDao3.0简单使用
- GreenDao3.0简单使用
- greenDAO3.0使用
- GreenDao3.0升级数据库
- greendao3.0的引入
- GreenDao3.0使用详解
- GreenDao3.0简单使用
- GreenDao3.0简单使用
- GreenDao3.0简单使用
- GreenDao3.0简单使用
- GreenDao3.0的使用
- GreenDao3.0简单使用
- 数据库框架GreenDao3.0
- GreenDao3.0简单使用
- LLVIRTUAL 博客引言
- HashMap结构分析
- zabbix官网下载地址:https://sourceforge.net/projects/zabbix/files/ZABBIX Latest Stable/
- android 命令行settings进行相关设置项的修改
- PHP 连接MAMP中 MySQL失败问题的解决
- GreenDao3.0
- HDU
- iterm2 使用rzsz
- Android开发关于MD5和Base64加密的代码帮助类
- neural networks学习笔记(一)
- flask 流内容
- C# 处理多媒体的公共类
- JAVA多线程编程之判断终止状态Interrupted和isInterrupted的区别
- mysql查询主键