GreenDao

来源:互联网 发布:手机函数绘图软件 编辑:程序博客网 时间:2024/05/29 19:14

需要在工程(Project)的build.gradle中添加依赖classpath 'org.greenrobot:greendao-gradle-plugin:3.2.1'buildscript {    repositories {        jcenter()    }    dependencies {        classpath 'com.android.tools.build:gradle:2.3.1'       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    }}allprojects {    repositories {        jcenter()    }}task clean(type: Delete) {    delete rootProject.buildDir}在项目(Module)的build.gradle中添加依赖apply plugin: 'com.android.application'apply plugin: 'org.greenrobot.greendao'android {    compileSdkVersion 26    buildToolsVersion "26.0.1"    defaultConfig {        applicationId "com.bewei.greendao"        minSdkVersion 15        targetSdkVersion 26        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    daoPackage 'com.bwie.test.greendao32.gen'    targetGenDir 'src/main/java'}}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:26.+'    compile 'com.android.support.constraint:constraint-layout:1.0.2'    testCompile 'junit:junit:4.12'        compile 'org.greenrobot:greendao:3.2.0'    compile 'com.jakewharton:butterknife:7.0.1'}布局文件:<LinearLayout    android:orientation="vertical"    xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"   >    <Button        android:id="@+id/bt_add"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="添加数据"        />    <Button        android:id="@+id/bt_delete"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="删除数据"        />    <Button        android:id="@+id/bt_updata"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="更新数据"        />    <Button        android:id="@+id/bt_find"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="查询数据"        /></LinearLayout>//自定义一个MyApp类继承Application,并在清单文件中声明/** * 1. 类的用途  单例设计模式 */public class MyApp extends Application {    private static  MyApp mInstance;    private DaoSession daoSession;    @Override    public void onCreate() {        super.onCreate();        mInstance = this;        setDb();    }    public static MyApp getmInstance(){        return  mInstance;    }    private void setDb() {        DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "bw.db", null);        SQLiteDatabase database = helper.getWritableDatabase();        DaoMaster daoMaster = new DaoMaster(database);        daoSession = daoMaster.newSession();    }    public DaoSession getDaoSessio(){        return daoSession;    }}public class MainActivity extends AppCompatActivity implements View.OnClickListener {    private UserDao userDao;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        initView();        DaoSession daoSessio = MyApp.getmInstance().getDaoSessio();        userDao = daoSessio.getUserDao();    }    private void initView() {        Button bt_add = (Button) findViewById(R.id.bt_add);        Button bt_delete = (Button) findViewById(R.id.bt_delete);        Button bt_updata = (Button) findViewById(R.id.bt_updata);        Button bt_find = (Button) findViewById(R.id.bt_find);        bt_add.setOnClickListener(this);        bt_delete.setOnClickListener(this);        bt_updata.setOnClickListener(this);        bt_find.setOnClickListener(this);    }    @Override    public void onClick(View view) {        switch (view.getId()) {            case R.id.bt_add:                List<User> users = new ArrayList<>();                User user1 = new User(Long.valueOf(5), "张三");                User user2 = new User(Long.valueOf(2), "李四");                User user3 = new User(Long.valueOf(3), "王五");                User user4 = new User(Long.valueOf(4), "赵六");                users.add(user1);                users.add(user2);                users.add(user3);                users.add(user4);                for (int i=0;i<users.size();i++){                    userDao.insert(users.get(i));                }                Toast.makeText(this, "添加数据成功", Toast.LENGTH_SHORT).show();                break;            case R.id.bt_delete:                User useDelete = userDao.load(Long.valueOf(1));                userDao.delete(useDelete);//删除对象                Toast.makeText(this, "删除数据成功", Toast.LENGTH_SHORT).show();                break;            case R.id.bt_updata:                User useUpdata = userDao.load(Long.valueOf(1));                useUpdata.setName("马七");                userDao.update(useUpdata);//更新对象                break;            case R.id.bt_find:                User useFind = userDao.load(Long.valueOf(1));//查询对象                String name = useFind.getName();                Toast.makeText(this, "查询数据成功:name" + name, Toast.LENGTH_SHORT).show();                List<User> userList = userDao.loadAll();//查询集合                for (User userinfo : userList ) {                    String userinfoName = userinfo.getName();                    Toast.makeText(this, "查询数据成功:userinfoName:" + userinfoName, Toast.LENGTH_SHORT).show();                }                break;        }    }}



原创粉丝点击