使用方法:
1.在lib中引入jar库
在build.gradle中增加对orm框架的使用,通过使用本地框架的形式
//ORM框架
implementation files('libs/ormlite-android-5.0.jar')
implementation files('libs/ormlite-core-5.0.jar')
User.kt对bean文件进行数据库的表格操作
package com.example.takeout.beans
import com.j256.ormlite.field.DatabaseField
import com.j256.ormlite.table.DatabaseTable
import java.io.Serializable
@DatabaseTable(tableName = "t_user") class User {
@DatabaseField(id = true) var id: Int = 0 //使用指定id
@DatabaseField(columnName = "name") var name: String? = null
@DatabaseField(columnName = "balance") var balance: Float = 0.toFloat()
@DatabaseField(columnName = "discount") var discount: Int = 0
@DatabaseField(columnName = "integral") var integral: Int = 0
@DatabaseField(columnName = "phone") var phone: String? = null
}
//data class User(
// var id: Int = 0,
// var name: String? = null,
// var balance: Float = 0.toFloat(),
// var discount: Int = 0,
// var integral: Int = 0,
// var phone: String? = null
//) : Serializable
TakeoutOpenHelper.kt数据库操作的helper操作,继承框架
package com.example.takeout.model.dao
import android.content.Context
import android.database.sqlite.SQLiteDatabase
import com.example.takeout.beans.User
import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper
import com.j256.ormlite.support.ConnectionSource
import com.j256.ormlite.table.TableUtils
class TakeoutOpenHelper(val context: Context) : OrmLiteSqliteOpenHelper(context, "takeout_kotlin.db", null, 2) {
override fun onCreate(p0: SQLiteDatabase?, connectionSource: ConnectionSource?) {
TableUtils.createTable(connectionSource, User::class.java)
}
override fun onUpgrade(p0: SQLiteDatabase?, p1: ConnectionSource?, oldVersion: Int, newVersion: Int) {
//升级app的用户会执行此方法
// TableUtils.createTable(connectionSource, RecepitAddressBean::class.java)
}
}
LoginActivityPresenter.kt操作数据Dao
package com.example.takeout.presenter
import android.util.Log
import com.example.takeout.beans.User
import com.example.takeout.model.dao.TakeoutOpenHelper
import com.example.takeout.ui.activity.LoginActivity
import com.example.takeout.utils.TakeoutApp
import com.google.gson.Gson
import com.j256.ormlite.dao.Dao
//继承NetPresenter
class LoginActivityPresenter(val loginActivity: LoginActivity) : NetPresenter(){
//使用手机号码登录的业务
fun loginByPhone(phone:String) {
val homeCall = takeoutService.loginByPhone()
homeCall.enqueue(callback)
}
override fun parserJson(json: String) {
val user = Gson().fromJson(json, User::class.java)
if (user != null) {
//缓存到内存中
TakeoutApp.sUser = user
//缓存到本地数据库中,SqLiteOpenhelper,要使用SQL语句
//第三方ORM框架(ormlite,greendao),直接操纵javabean,
val takeoutOpenHelper = TakeoutOpenHelper(loginActivity)
val userDao: Dao<User, Int> = takeoutOpenHelper.getDao(User::class.java)
//保证如果已有此用户,就不需再创建了
// userDao.create(user)
// userDao.createOrUpdate(user)//无法根据id区分新老用户
Log.e("login", "创建新用户或者更新老用户信息")
//TODO:区分新用户和老用户,并且统计比率
//step1:查找以前是否有这个用户记录
val userList: List<User> = userDao.queryForAll()
var isOldUser: Boolean = false
//遍历确定是否是老用户
for (i in 0 until userList.size) {
val u = userList.get(i)
if (u.id == user.id) {
isOldUser = true
}
}
if (isOldUser){
userDao.update(user)
Log.e("login", "老用户更新信息")
} else{
userDao.create(user)
Log.e("login", "新用户登录")
}
loginActivity.onLoginSuccess()
} else{
loginActivity.onLoginFailed()
}
}
}