当前位置: 首页 > news >正文

制造业小程序网站开发php是做网站的吗

制造业小程序网站开发,php是做网站的吗,商丘seo快速排名,河北网站seo外包文章目录 前言集成Room结合协程的使用总结 一、前言, 现在kotlin 是趋势#xff0c;那必然就要用到协程#xff0c;还有就是随着jetpack 的发力#xff0c;带来了很多好用的库#xff0c;比如今天提到Room#xff0c;是一个类似greenDao的数据库。它不但支持kotlin协程…文章目录 前言集成Room结合协程的使用总结 一、前言, 现在kotlin 是趋势那必然就要用到协程还有就是随着jetpack 的发力带来了很多好用的库比如今天提到Room是一个类似greenDao的数据库。它不但支持kotlin协程/RxJava还具备编译期检查是非常友好的库。我们一起来看下在项目中怎么使用。 二、集成Room 1、创建一个kotlin项目然后在app里面的build.gradle添加依赖 plugins { ...id kotlin-android-extensionsid kotlin-kapt } dependencies { ...//room数据库implementation androidx.room:room-runtime:2.4.2kapt androidx.room:room-compiler:2.4.2 // Kotlin 使用 kaptimplementation androidx.room:room-ktx:2.4.2//Coroutines support for Room 协程操作库//lifecycleimplementation androidx.lifecycle:lifecycle-extensions:2.2.0implementation androidx.lifecycle:lifecycle-runtime-ktx:2.2.0}此时我们同步一下开始运行项目。会报错 Android Gradle plugin requires Java 11 to run. You are currently using Java 1.8解决将项目的jdk1.8 改成 jdk11。 接下来还有报错 Cant determine type for tag macro namem3_comp_assist_chip_container_shape?attr/shapeAppearanceCornerSmall/macro原因是material 的版本高了的问题。 implementation com.google.android.material:material:1.8.0解决我们将material改成1.6.0。项目就能正常运行了。 2、相关的类的创建 2.1创建数据库的实体类 通过在实体类上加注解Entity,这样实体类就相当于是一张表 import android.os.Parcelable import androidx.room.Entity import androidx.room.PrimaryKey import kotlinx.android.parcel.ParcelizeParcelize Entity(tableName Student) data class Student(PrimaryKeyvar id: String,var name: String ) : Parcelable2.2创建实体类的Dao 通过在Dao接口上加注解Dao,就可以让dao轻松地完成增删改查。 另外可以将 suspend Kotlin 关键字添加到 DAO 方法中用 Kotlin 协程功能使这些方法成为异步方法。这样可确保不会在主线程上执行这些方法。 Dao interface StudentDao {//通过Insert 注解的onConflict 解决冲突如果有老的数据存在则会进行替换,如果没有就插入Insert(onConflict OnConflictStrategy.REPLACE)suspend fun putStudent(cacheBean: Student)Query(select * from Student where id :id)suspend fun getStudent(id: String): Student?Query(select * from Student)suspend fun getAllStudent(): ListStudent?Deletesuspend fun delete(student: Student)Update(onConflict OnConflictStrategy.REPLACE)suspend fun update(student: Student)}2.3 创建数据库的类 创建一个类继承RoomDatabase,加注解Database,轻松地建数据库和建表 import android.util.Log import androidx.room.Database import androidx.room.Room import androidx.room.RoomDatabase import androidx.sqlite.db.SupportSQLiteDatabase//后续的数据库升级通过这个version来控制exportSchema是否导出数据库的配置信息 Database(entities [Student::class], version 2, exportSchema false) abstract class StudentDatabase : RoomDatabase() {companion object {var dataBase: StudentDatabaseval TAG StudentDatabase::class.java.simpleNameinit {//如果databaseBuilder改为inMemoryDatabaseBuilder则创建一个内存数据库进程销毁后数据丢失dataBase Room.databaseBuilder(MyApplication.getApplicationContext(), StudentDatabase::class.java, db_user)//数据库的操作是否允许在主线程中执行.allowMainThreadQueries()//数据库创建和打开后的回调可以重写其中的方法.addCallback(object : Callback() {override fun onCreate(db: SupportSQLiteDatabase) {super.onCreate(db)Log.d(TAG, onCreate: db_student)}})//数据库升级异常之后的回滚.fallbackToDestructiveMigration().build()}}abstract fun getStudentDao(): StudentDao 2.4 创建一个MyApplication 通过它我们来获取context。 class MyApplication : Application() {init {instance this}companion object {private var instance: MyApplication? nullfun getApplicationContext() : Context {return instance!!.applicationContext}}override fun onCreate() {super.onCreate()}}三、接下来就是使用了 通过点击按钮进行增删改查的操作。 增加数据 btn_add.setOnClickListener {lifecycleScope.launch {val studentDao StudentDatabase.dataBase.getStudentDao()studentDao.putStudent(Student(101,小李));studentDao.putStudent(Student(102,小王))}}更改数据 btn_update.setOnClickListener {lifecycleScope.launch {val studentDao StudentDatabase.dataBase.getStudentDao()val student studentDao.update(Student(101,小陈))Log.d(TAG,student.toString());}}删除数据 btn_delete.setOnClickListener {lifecycleScope.launch {val studentDao StudentDatabase.dataBase.getStudentDao()val student studentDao.delete(Student(101,小陈))Log.d(TAG,student.toString());val students studentDao.getAllStudent()Log.d(TAG, students: $students)}}查询数据 lifecycleScope.launch {btn_query.setOnClickListener {lifecycleScope.launch {val studentDao StudentDatabase.dataBase.getStudentDao()val students studentDao.getAllStudent()Log.d(TAG, students: $students)}}备注数据库的操作一定要放到子线程中切不可在主线程中操作虽然可以通过allowMainThreadQueries强制开启允许这么做但这个是测试时用实际项目中还是不要在主线程中操作数据库避免遇到ANR问题 源码地址https://github.com/shenshizhong/KotlinRoomDemo 总结 1 、导入依赖, 同步 2 、创建一个实体类Student,加注解Entity就相当于一张表 3 、创建一个接口StudentDao,加注解Dao就可以完成增删改查 4 、创建抽象类继承RoomDatabase,加注解Database轻松地建数据库和建表 5 、通过数据库的实例获取dao调用方法 如果对你有一点点帮助那是值得高兴的事情。) 我的csdnhttp://blog.csdn.net/shenshizhong 我的简书http://www.jianshu.com/u/345daf0211ad
http://www.zqtcl.cn/news/547750/

相关文章:

  • 怎么查网站备案的公司wordpress 无刷新评论
  • 学前心理学课程建设网站百度极速版下载
  • 佛山做营销型网站建设深圳宝安区租房
  • 做汽车团购的网站建设营销方案有哪些
  • 做设计的网站网络公关什么意思
  • 一般课程网站要怎么做做钓鱼网站软件下载
  • 济南网站建设92jzh收不到wordpress的邮件
  • 一键优化在哪里打开新手怎么入行seo
  • 网站建设的费用明细创建公司网站需要注意什么
  • 微网站怎么做的好宣传片拍摄服务
  • 抚州网站开发机构wordpress开源
  • 企业营销网站建设不属于网页制作工具
  • 呼和浩特网站建设信息建服装类网站需要考虑的因素
  • 百度站长平台工具南京开发app的公司
  • 济南如何挑选网站建设公司设计 网站 现状
  • 网站开发建设流程图wordpress 插件 简码
  • 信宜网站开发公司阿里指数app下载
  • AAP网站开发需要多少钱网站核验通知书
  • 网站续费模板wordpress安装到ESC
  • 网站网址大全做商品条形码的网站
  • php购物网站开发成品各大网站收录提交入口
  • 怎么办个人网站网络管理系统中故障管理的目标是
  • 想做网站的客户在哪找下载网站系统源码
  • 网站建设是固定资产还是列费用soho做网站
  • 学校建设评建工作网站应用中心软件
  • 网站建设公司如何拓宽业务跨境进口网站怎么做
  • 邢台企业建站速卖通网站怎样做店面的二维码
  • 网上竞价采购网站建设wordpress添加发布视频
  • 隐形眼镜网站开发的经济效益微企点自助建站系统
  • 延庆网站建设优化seo中山百度seo排名公司