当前位置:网站首页>Android: the kotlin language uses grendao3, a cross platform app development framework
Android: the kotlin language uses grendao3, a cross platform app development framework
2022-07-02 01:27:00 【m0_ sixty-six million two hundred and sixty-four thousand seven】
apply plugin: ‘org.greenrobot.greendao’
greendao {
// Database schema edition , It can also be understood as the database version number
schemaVersion 1
// Set up DaoMaster、DaoSession、Dao Package name , That is, the full path of the package where these classes are to be placed .
daoPackage ‘tsou.com.simple.greendaoforkotlin.greendao’
// Set up DaoMaster、DaoSession、Dao Catalog
targetGenDir ‘src/main/java’
}
dependencies
compile ‘org.greenrobot:greendao:3.2.0’
}
- In the project build.gradle in
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
}
}
Step two 、 establish Student Entity class
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 +
‘}’;
}
}
Step three 、 Fix the project , Automatic generation greendao Class under package ( Just click on the little hammer ), You will generate it automatically greendao The class in the package .
Step four 、 Start using , Create management class
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 {
// Initialize database information
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
}
}
/**
Get a readable database
@param context
@return
*/
fun getReadableDatabase(context: Context): SQLiteDatabase? {
if (null == mDevOpenHelper) {
getInstance(context)
}
return mDevOpenHelper?.getReadableDatabase()
}
/**
Get writable database
@param context
@return
*/
fun getWritableDatabase(context: Context): SQLiteDatabase? {
if (null == mDevOpenHelper) {
getInstance(context)
}
return mDevOpenHelper?.getWritableDatabase()
}
/**
obtain 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
}
/**
obtain DaoSession
@param context
@return
*/
fun getDaoSession(context: Context): DaoSession? {
if (null == mDaoSession) {
synchronized(DbManager::class.java) {
mDaoSession = getDaoMaster(context)?.newSession()
}
}
return mDaoSession
}
}
Step five 、 Additions and deletions ,
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
}
}
/**
Add data to database
@param context
@param stu
*/
fun insertData(context: Context?, stu: Student) {
DbManager.getInstance(context!!)?.getDaoSession(context)?.getStudentDao()?.insert(stu)
}
/**
Add data entities to the database through transactions
@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)
}
/**
Add data to database , If there is , Overwrite the original data
The internal code judges that if it exists update(entity); If it doesn't exist insert(entity);
@param context
@param student
*/
fun saveData(context: Context?, student: Student) {
DbManager.getInstance(context!!)?.getDaoSession(context)?.getStudentDao()?.save(student)
}
/**
Delete data to database
@param context
@param student Delete specific content
*/
fun deleteData(context: Context?, student: Student) {
DbManager.getInstance(context!!)?.getDaoSession(context)?.getStudentDao()?.delete(student)
}
/**
according to id Delete data to database
@param context
@param id Delete specific content
*/
fun deleteByKeyData(context: Context?, id: Long) {
DbManager.getInstance(context!!)?.getDaoSession(context)?.getStudentDao()?.deleteByKey(id)
}
/**
Delete all data
@param context
*/
fun deleteAllData(context: Context?) {
DbManager.getInstance(context!!)?.getDaoSession(context)?.getStudentDao()?.deleteAll()
}
/**
- Update the database
Last
May you one day , Love yourself , Treat yourself .
This article is on the open source project :Android Development doesn't do that ? How to get a high salary in an interview Has been included in , It contains different directions of self-learning programming route 、 Interview question set / Face the 、 And a series of technical articles , Resources are constantly updated …
边栏推荐
- Keepalived introduction and installation
- Architecture evolution from MVC to DDD
- [IVX junior engineer training course 10 papers] 02 numerical binding and adaptive website production
- Sql--- related transactions
- Study note 2 -- definition and value of high-precision map
- No converter found for return value of type: class
- Unity AssetBundle subcontracting
- 【疾病检测】基于BP神经网络实现肺癌检测系统含GUI界面
- 三分钟学会基础k线图知识
- 只是以消费互联网的方式和方法来落地和实践产业互联网,并不能够带来长久的发展
猜你喜欢

Edge computing accelerates live video scenes: clearer, smoother, and more real-time

Docker安装Oracle_11g

微信小程序中使用tabBar

"C zero foundation introduction hundred knowledge hundred examples" (73) anonymous function -- lambda expression

We should make clear the branch prediction

ACM tutorial - quick sort (regular + tail recursion + random benchmark)

Basic concepts of machine learning

【疾病检测】基于BP神经网络实现肺癌检测系统含GUI界面

Memorabilia of domestic database in June 2022

The first "mobile cloud Cup" empty publicity meeting, looking forward to working with developers to create a new world of computing!
随机推荐
机器学习基本概念
SAP ui5 beginner tutorial XXI - trial version of custom formatter of SAP ui5
Global and Chinese markets for food allergens and intolerance tests 2022-2028: Research Report on technology, participants, trends, market size and share
KS006基于SSM实现学生成绩管理系统
Global and Chinese markets of edge AI software 2022-2028: Research Report on technology, participants, trends, market size and share
We should make clear the branch prediction
What is commercial endowment insurance? Is commercial endowment insurance safe?
Appium inspector can directly locate the WebView page. Does anyone know the principle
Docker安装Oracle_11g
【图像增强】基于Frangi滤波器实现血管图像增强附matlab代码
Learning notes 25 - multi sensor front fusion technology
[IVX junior engineer training course 10 papers to get certificates] 0708 news page production
Brief description of grafana of # yyds dry goods inventory # Prometheus
Variables and constants of go language foundation
Part 29 supplement (XXIX) basis of ECMAScript
Leetcode 45 Jumping game II (2022.02.14)
Since I understand the idea of dynamic planning, I have opened the door to a new world
学习笔记2--高精度地图定义及价值
ECS project deployment
Day 13 of hcip (relevant contents of BGP agreement)