Commit 79a9f635 authored by fengruiyu's avatar fengruiyu

创建项目的room数据已完成

创建景区的表结构已完成
parent 1ea0bdb7
...@@ -188,5 +188,13 @@ dependencies { ...@@ -188,5 +188,13 @@ dependencies {
implementation(name: 'BaiduLBS_AndroidSDK_Lib', ext: 'aar') implementation(name: 'BaiduLBS_AndroidSDK_Lib', ext: 'aar')
//room数据库
def room_version = "2.3.0"
implementation("androidx.room:room-runtime:$room_version")
annotationProcessor "androidx.room:room-compiler:$room_version"
// optional - Kotlin Extensions and Coroutines support for Room
implementation("androidx.room:room-ktx:$room_version")
// optional - Test helpers
testImplementation("androidx.room:room-testing:$room_version")
} }
\ No newline at end of file
package db
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import me.goldze.mvvmhabit.utils.Utils
/**
* Created by 冯瑞雨 on 2021/7/20.
*/
@Database(entities = [ScenicSpotBean::class], version = 1)
abstract class AppDatabase : RoomDatabase() {
abstract fun userDao(): ScenicSpotDao
companion object {
private var _instance: AppDatabase? = null
@JvmStatic
fun instance(): AppDatabase {
if (_instance == null) {
synchronized(AppDatabase::class.java) {
if (_instance == null) {
_instance = Room.databaseBuilder(
Utils.getContext()
, AppDatabase::class.java
, "mints_room.db")
.allowMainThreadQueries()//设置是否允许在主线程做查询操作
//.fallbackToDestructiveMigration()//设置迁移数据库如果发生错误,将会重新创建数据库,而不是发生崩溃
.build()
}
}
}
return _instance!!
}
}
}
\ No newline at end of file
package db
import androidx.room.Dao
import androidx.room.Query
import java.util.*
/**
* Created by 冯瑞雨 on 2021/7/20.
*/
@Dao
interface ScenicSpotDao {
@Query("SELECT * FROM scenic_spot")
fun getAll(): List<ScenicSpotBean>
}
\ No newline at end of file
package db
import androidx.room.Entity
import androidx.room.PrimaryKey
import java.util.*
/**
* Created by 冯瑞雨 on 2021/7/20.
* 景区表
*/
@Entity(tableName = "scenic_spot")
class ScenicSpotBean(
//主键 自增
@PrimaryKey(autoGenerate = true) var id: Int = 0,
//景区 类型 1=境内热门景区 2=境内免费专区 3=首页的景区
var type :Int = 0,
//景区 经度
var longitude :Double?=null,
//景区 纬度
var latitude :Double?=null,
//景区 名称
var name :String? = null,
//景区 图片
var images : List<String>? = null
)
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment