当前位置:网站首页>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 …
边栏推荐
- uTools
- Based on Simulink and FlightGear, the dynamic control of multi rotor UAV in equilibrium is modeled and simulated
- No converter found for return value of type: class
- Learning note 24 - multi sensor post fusion technology
- Bubble Sort Graph
- Global and Chinese market of picture archiving and communication system (PACS) 2022-2028: Research Report on technology, participants, trends, market size and share
- Basic usage of three JS high-order functions --filter---map---reduce
- Docker installing Oracle_ 11g
- ECS project deployment
- Using tabbar in wechat applet
猜你喜欢
Datawhale 社区黑板报(第1期)
Based on Simulink and FlightGear, the dynamic control of multi rotor UAV in equilibrium is modeled and simulated
Unity AssetBundle subcontracting
Introduction to ffmpeg Lib
Sql--- related transactions
Since I understand the idea of dynamic planning, I have opened the door to a new world
机器学习基本概念
Datawhale community blackboard newspaper (issue 1)
Learning note 24 - multi sensor post fusion technology
Principle of finding combinatorial number and template code
随机推荐
Penser au jeu 15: penser au service complet et au sous - service
Global and Chinese markets of edge AI software 2022-2028: Research Report on technology, participants, trends, market size and share
Basic usage of shell script
Datawhale community blackboard newspaper (issue 1)
学习笔记3--高精度地图关键技术(上)
微信小程序中使用tabBar
Global and Chinese market of safety detection systems 2022-2028: Research Report on technology, participants, trends, market size and share
遊戲思考15:全區全服和分區分服的思考
How does schedulerx help users solve the problem of distributed task scheduling?
首场“移动云杯”空宣会,期待与开发者一起共创算网新世界!
Private project practice sharing [Yugong series] February 2022 U3D full stack class 009 unity object creation
error: . repo/manifests/: contains uncommitted changes
Laravel artisan 常用命令
I Brief introduction of radio energy transmission technology
"C zero foundation introduction hundred knowledge hundred examples" (73) anonymous function -- lambda expression
Minimize the error
ACM tutorial - quick sort (regular + tail recursion + random benchmark)
Brief introduction to the development of mobile network
ES6 new method of string
CEPH buffer yyds dry inventory