当前位置:网站首页>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 …
边栏推荐
- The pain of Xiao Sha
- Datawhale 社区黑板报(第1期)
- [dynamic planning] interval dp:p3205 Chorus
- ACM tutorial - quick sort (regular + tail recursion + random benchmark)
- [rust web rokcet Series 2] connect the database and add, delete, modify and check curd
- Global and Chinese market of picture archiving and communication system (PACS) 2022-2028: Research Report on technology, participants, trends, market size and share
- Design and implementation of radio energy transmission system
- Altium designer measure distance (ctrl+m)
- How can I batch produce the same title for the video?
- No converter found for return value of type: class
猜你喜欢
![[IVX junior engineer training course 10 papers to get certificates] 01 learn about IVX and complete the New Year greeting card](/img/99/53b0ae47bada8b0d4db30d0517fe3d.jpg)
[IVX junior engineer training course 10 papers to get certificates] 01 learn about IVX and complete the New Year greeting card

Infiltration records of CFS shooting range in the fourth phase of the western regions' Dadu Mansion
![[IVX junior engineer training course 10 papers] 06 database and services](/img/68/967566fc2f1d0b93ecd78bdb208b64.jpg)
[IVX junior engineer training course 10 papers] 06 database and services

学习笔记24--多传感器后融合技术

It's already 30. Can you learn programming from scratch?

Have you stepped on the nine common pits in the e-commerce system?

XMIND mind map

学习笔记3--高精度地图关键技术(上)

微信小程序中使用tabBar

Learning note 24 - multi sensor post fusion technology
随机推荐
Penser au jeu 15: penser au service complet et au sous - service
Basic number theory -- Gauss elimination
How to determine whether the current script is in the node environment or the browser environment?
【疾病检测】基于BP神经网络实现肺癌检测系统含GUI界面
[IVX junior engineer training course 10 papers to get certificates] 0708 news page production
KS006基于SSM实现学生成绩管理系统
Daily work and study notes
The first "mobile cloud Cup" empty publicity meeting, looking forward to working with developers to create a new world of computing!
Keepalived introduction and installation
Global and Chinese markets of edge AI software 2022-2028: Research Report on technology, participants, trends, market size and share
Liteos learning - first knowledge of development environment
Global and Chinese markets for context and location-based services 2022-2028: Research Report on technology, participants, trends, market size and share
[IVX junior engineer training course 10 papers] 05 canvas and aircraft war game production
Learn about servlets
学习笔记25--多传感器前融合技术
Basic usage of three JS high-order functions --filter---map---reduce
What are the differences between software testers with a monthly salary of 7K and 25K? Leaders look up to you when they master it
[Chongqing Guangdong education] Tianshui Normal University universe exploration reference
学习笔记24--多传感器后融合技术
Two TVs