google推出了自己的数据库框架:Room 有点类似于greenDao的使用
官方介绍:The Room persistence library provides an abstraction layer over SQLite to allow fluent database access while harnessing the full power of SQLite
1、首先我们需要在项目的build.gradlle文件添加google的maven仓库
allprojects { repositories { google() jcenter() } }
2、接着在主module的build.gradle文件添加依赖
implementation 'android.arch.persistence.room:runtime:1.1.1' annotationProcessor 'android.arch.persistence.room:compiler:1.1.1' implementation 'android.arch.persistence.room:testing:1.1.1'
3、接下来建立一个实体类,类似于greendao,都i是通过实体类映射的方式来创建
@Entity(tableName = "article_type") public class ArticleType { //设置主键,并且定义自增增 @PrimaryKey(autoGenerate = true) private int id; //字段映射具体的数据表字段名 @ColumnInfo(name = "name") private String name; @ColumnInfo(name = "count") private int count; @ColumnInfo(name = "title") private String title; @ColumnInfo(name = "image") private String image; @ColumnInfo(name = "content") private String content; @Override public String toString() { return "ArticleType{" + "id=" + id + ", name='" + name + ''' + ", count=" + count + ", title='" + title + ''' + ", image='" + image + ''' + ", content='" + content + ''' + '}'; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getCount() { return count; } public void setCount(int count) { this.count = count; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getImage() { return image; } public void setImage(String image) { this.image = image; } public String getContent() { return content; } public void setContent(String content) { this.content = content; }
//必须指定一个构造方法,room框架需要。并且只能指定一个
//,如果有其他构造方法,则其他的构造方法必须添加@Ignore注解
public ArticleType() { } //其他构造方法要添加@Ignore注解 @Ignore public ArticleType(int id, String name, int count, String title, String image, String content) { this.id = id; this.name = name; this.count = count; this.title = title; this.image = image; this.content = content; } }
注意:(1)@Entity(tableName="表名称") 此注解放在实体类的类名上面,可以设置表的名称、主键、外键等信息
(2)@PrimaryKey(autoGenerate = true) 此注解放在主键变量上,并定义自增
(3)@ColumnInfo(name = "字段名称") 此注解放在普通变量上,字段映射具体的数据表字段名
(4)@Ignore 这个注解需要好好说一下,放在方法和变量上。Room定义必须有一个无参的构造方法,所以说如果你想有别的构造方法,需要在别的构造方法上添加上此注解。如果说你实体类中的字段不是表中的字段,这时也需要忽略下,需要在变量上添加此注解。
4、增删改查
(1)、增加 @Insert(onConflict = OnConflictStrategy.REPLACE)
OnConflictStrategy.REPLACE表如已有数据,就覆盖掉。数据的判断通过主键进行匹配,也就是id,非整个ArticleType对象。增加分为两种,一种是增加一条、另外一种是增加多条数据,
@Insert(onConflict = OnConflictStrategy.REPLACE) Long insert(ArticleType articleType); @Insert(onConflict = OnConflictStrategy.REPLACE) List<Long> insertAll(List<ArticleType> articleTypeList);
(2)、删除
@Delete() int delete(ArticleType articleType); @Delete() int deleteAll(List<ArticleType> articleTypeList);
(3)、修改 需要传入修改完的实体类,其中必须要有主键(通过主键来进行修改)
@Update() int update(ArticleType articleType); @Update() int updateAll(ArticleType...articleTypes);
(4)、查询
查询 @Query("SELECT * FROM article_type")
查询灵活性就很大了,可以直接里面写sql语句。很赞的是:Room可以直接判断你写的表名是否存在,还和参数值一一对应。
@Query("SELECT * FROM article_type") List<ArticleType> getAll(); @Query("SELECT * FROM article_type WHERE id = :memberId") LiveData<ArticleType> findById(int memberId);
5、数据库创建
表的实体类在上面已经写完,接下来创建一下数据库。需要继承自RoomDatabase。
@Database(entities = {ArticleType.class,FamilyMemberType.class}, version = 1,exportSchema = true) public abstract class AppDataBase extends RoomDatabase { public abstract ArticleTypeDao articleTypeDao(); public abstract FamilyMemberType familyMemberTypeDao(); public static final Migration MIGRATION_1_2 = new Migration(1, 2) { @Override public void migrate(@NonNull SupportSQLiteDatabase database) { database.execSQL("ALTER TABLE article_type ADD COLUMN age INTEGER NOT NULL DEFAULT 0"); } }; }
需要在创建的类的上面添加@Database注解,其中的entities是一个数组,可以添加多个类,相应的在下面可以创建多个抽象方法(多个表的情况下),以便使用。下面就是创建数据库
private AppDataBase db=null; private ArticleTypeDao dao=null; db = Room.databaseBuilder(this, AppDataBase.class, "room-database").build(); dao = db.articleTypeDao();
下面以查询为例:记得操作数据库,需要开启子线程 long result = dao.insert(new ArticleType(1, "李伟", 80, "今天搞活动", "http://www.image.com", "下班")); if (result > 0) { i++; Log.i("insert_data_success", "插入了一条数据成功"); } else { Log.i("insert_data_success", "插入失败、、、"); }
参考:原文链接:https://blog.csdn.net/haojiagou/article/details/103053792
相关知识
宠物商城+ssm框架+jsp页面+mysql数据库
宠物商店管理系统数据库设计
Airbnb机器学习框架Aerosolve
基于vue框架的宠物寄养系统3d388(程序+源码+数据库+调试部署+开发环境)系统界面在最后面。
基于vue框架的宠物交流平台1n2n3(程序+源码+数据库+调试部署+开发环境)系统界面在最后面。
基于vue框架的宠物领养系统tusw9(程序+源码+数据库+调试部署+开发环境)系统界面在最后面。
基于vue框架的宠物领养系统l3a76(程序+源码+数据库+调试部署+开发环境)系统界面在最后面。
基于vue框架的宠物店管理系统的设计与实现4czn0(程序+源码+数据库+调试部署+开发环境)系统界面在最后面。
基于vue框架的的流浪宠物救助系统25128(程序+源码+数据库+调试部署+开发环境)系统界面在最后面。
jsp宠物医院管理系统58rr2(程序+源码+数据库+调试部署+开发环境)
网址: Google Room 数据库框架详解 https://m.mcbbbk.com/newsview811537.html
上一篇: 探究泰迪宠物的颜色(从外貌美观) |
下一篇: 小狗的颜色大全,让你爱不释手(颜 |