apply plugin: ‘org.greenrobot.greendao’
greendao {
//数据库的schema版本,也可以理解为数据库版本号
schemaVersion 1
//设置DaoMaster、DaoSession、Dao包名,也就是要放置这些类的包的全路径。
daoPackage ‘tsou.com.simple.greendaoforkotlin.greendao’
//设置DaoMaster、DaoSession、Dao目录
targetGenDir ‘src/main/java’
}
dependencies
compile ‘org.greenrobot:greendao:3.2.0’
}
- 在项目的build.gradle中
buildscript {
ext.kotlin_version = ‘1.1.50’
repositories {
mavenCentral()
google()
jcenter()
}
dependencies {
classpath ‘com.android.tools.build:gradle:3.0.0-beta7’
classpath “org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version”
classpath ‘org.greenrobot:greendao-gradle-plugin:3.2.0’
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
步骤二、创建Student实体类
package tsou.com.simple.greendaoforkotlin.bean;
import android.support.annotation.Keep;
import org.greenrobot.greendao.annotation.Entity;
import org.greenrobot.greendao.annotation.Id;
import org.greenrobot.greendao.annotation.Generated;
@Entity(generateConstructors = false)
public class Student {
@Id
private Long id;
private String name;
private int age;
public Student() {
}
@Keep
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public Student(Long id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
@Keep
public Long getId() {
return id;
}
@Keep
public void setId(Long id) {
this.id = id;
}
@Keep
public String getName() {
return name;
}
@Keep
public void setName(String name) {
this.name = name;
}
@Keep
public int getAge() {
return age;
}
@Keep
public void setAge(int age) {
this.age = age;
}
@Keep
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Student)) return false;
Student student = (Student) o;
return name.equals(student.name);
}
@Keep
@Override
public int hashCode() {
return (int) (id ^ (id >>> 32));
}
@Keep
@Override
public String toString() {
return “Student{” +
“id=” + id +
“, name=’” + name + ‘’’ +
“, age=” + age +
‘}’;
}
}
步骤三、修复一下工程,自动生成greendao包下的类(就是点击一下小锤子),你会自动生成greendao包中的类。
步骤四、开始使用,创建管理类
package tsou.com.simple.greendaoforkotlin.manager
import android.content.Context
import android.database.sqlite.SQLiteDatabase
import tsou.com.simple.greendaoforkotlin.greendao.DaoMaster
import tsou.com.simple.greendaoforkotlin.greendao.DaoSession
class DbManager private constructor(mContext: Context) {
private val DB_NAME = “test.db”
private var mDevOpenHelper: DaoMaster.DevOpenHelper? = null
private var mDaoMaster: DaoMaster? = null
private var mDaoSession: DaoSession? = null
init {
// 初始化数据库信息
mDevOpenHelper = DaoMaster.DevOpenHelper(mContext, DB_NAME)
getDaoMaster(mContext)
getDaoSession(mContext)
}
companion object {
@Volatile
var instance: DbManager? = null
fun getInstance(mContext: Context): DbManager? {
if (instance == null) {
synchronized(DbManager::class) {
if (instance == null) {
instance = DbManager(mContext)
}
}
}
return instance
}
}
/**
获取可读数据库
@param context
@return
*/
fun getReadableDatabase(context: Context): SQLiteDatabase? {
if (null == mDevOpenHelper) {
getInstance(context)
}
return mDevOpenHelper?.getReadableDatabase()
}
/**
获取可写数据库
@param context
@return
*/
fun getWritableDatabase(context: Context): SQLiteDatabase? {
if (null == mDevOpenHelper) {
getInstance(context)
}
return mDevOpenHelper?.getWritableDatabase()
}
/**
获取DaoMaster
@param context
@return
*/
fun getDaoMaster(context: Context): DaoMaster? {
if (null == mDaoMaster) {
synchronized(DbManager::class.java) {
if (null == mDaoMaster) {
mDaoMaster = DaoMaster(getWritableDatabase(context))
}
}
}
return mDaoMaster
}
/**
获取DaoSession
@param context
@return
*/
fun getDaoSession(context: Context): DaoSession? {
if (null == mDaoSession) {
synchronized(DbManager::class.java) {
mDaoSession = getDaoMaster(context)?.newSession()
}
}
return mDaoSession
}
}
步骤五、增删改查,
package tsou.com.simple.greendaoforkotlin.dao
import android.content.Context
import tsou.com.simple.greendaoforkotlin.bean.Student
import tsou.com.simple.greendaoforkotlin.greendao.StudentDao
import tsou.com.simple.greendaoforkotlin.manager.DbManager
class StudentDaoOpe private constructor() {
private object mHolder {
val instance = StudentDaoOpe()
}
companion object {
fun getInstance(): StudentDaoOpe {
return mHolder.instance
}
}
/**
添加数据至数据库
@param context
@param stu
*/
fun insertData(context: Context?, stu: Student) {
DbManager.getInstance(context!!)?.getDaoSession(context)?.getStudentDao()?.insert(stu)
}
/**
将数据实体通过事务添加至数据库
@param context
@param list
*/
fun insertData(context: Context?, list: List?) {
if (null == list || list.size <= 0) {
return
}
DbManager.getInstance(context!!)?.getDaoSession(context)?.getStudentDao()?.insertInTx(list)
}
/**
添加数据至数据库,如果存在,将原来的数据覆盖
内部代码判断了如果存在就update(entity);不存在就insert(entity);
@param context
@param student
*/
fun saveData(context: Context?, student: Student) {
DbManager.getInstance(context!!)?.getDaoSession(context)?.getStudentDao()?.save(student)
}
/**
删除数据至数据库
@param context
@param student 删除具体内容
*/
fun deleteData(context: Context?, student: Student) {
DbManager.getInstance(context!!)?.getDaoSession(context)?.getStudentDao()?.delete(student)
}
/**
根据id删除数据至数据库
@param context
@param id 删除具体内容
*/
fun deleteByKeyData(context: Context?, id: Long) {
DbManager.getInstance(context!!)?.getDaoSession(context)?.getStudentDao()?.deleteByKey(id)
}
/**
删除全部数据
@param context
*/
fun deleteAllData(context: Context?) {
DbManager.getInstance(context!!)?.getDaoSession(context)?.getStudentDao()?.deleteAll()
}
/**
- 更新数据库
最后
愿你有一天,真爱自己,善待自己。
本文在开源项目:Android开发不会这些?如何面试拿高薪中已收录,里面包含不同方向的自学编程路线、面试题集合/面经、及系列技术文章等,资源持续更新中…