RoomLite
GitHub链接:https://github.com/xiaolei123/RoomLite
Gitee链接:https://gitee.com/xcode_xiao/RoomLite
介绍
Android平台下,使用编译期注解生成Java文件,拒绝低性能。一个SQLite数据库ORM的船新版本。增删改查建表,排序正序倒序唯一性,表索引,多线程并发读写都不在话下。确定不了解一下?
获取
allprojects {
repositories {
...
maven { url 'https://www.jitpack.io' }
}
}
java:
dependencies {
annotationProcessor 'com.github.xiaolei123:compiler:+'
implementation 'com.github.xiaolei123:runtime:+'
}
kotlin:
dependencies {
kapt 'com.github.xiaolei123:compiler:+'
implementation 'com.github.xiaolei123:runtime:+'
}
使用说明
1.创建数据库
public static class DataBase extends RoomLiteDatabase
{
public DataBase()
{
// 数据库名称
super("school");
}
// 所有的表Entity
@Override
public Class<?>[] getEntities()
{
return new Class[]{User.class};
}
// 是否允许在主线程中执行
@Override
public boolean allowRunOnUIThread()
{
return true;
}
// 数据库升级
@Override
public void onUpgrade(@Nullable SQLiteDatabase db, int oldVersion, int newVersion)
{
}
// 数据库版本
@Override
public int version()
{
return 1;
}
}
2.在数据库里创建表
@Entity(name = "User")
public class User
{
@Column
@PrimaryKey(autoGenerate = true)
public int id;
public String name = "当前时间:" + System.currentTimeMillis();
@Ignore
public Bitmap bitmap;
}
2.1声明字段为主键(并且自增长),自增长只有在类型为数字类型的时候才会生效
@PrimaryKey(autoGenerate = true)
2.2字段 NOT NULL
@Column(notNull = true)
2.3字段唯一 UNIQUE
@Column(unique = true)
2.4默认值 DEFAULT
@Column( defaultValue = "0")
2.5忽略某个字段
@Ignore
public Bitmap bitmap;
2.6支持自定义字段
第一步:在表类里声明自定义类型
@Entity(name = "User")
public class User
{
public Date date;
}
第二步:继承对应的转换器
public class DateConvert extends ToLongConvert<Date>
{
public DateConvert()
{
super(Date.class);
}
@Override
public Long convertToLong(Date javaObj)
{
Date date = (Date) javaObj;
if (javaObj == null)
return null;
return date.getTime();
}
/**
* 从数据库的Cursor获取数据,并转换成对应 javaType 类型的数据
*
* @param value
*/
@Override
public Date cursorToJavaObject(long value)
{
return new Date(value);
}
}
第三步:向RoomLite注册转换器
RoomLite.addConvert(new DateConvert());
3.创建索引 方式一
@Entity(name = "User", indices = {
@Index(columnNames = {"id", "name"}),
@Index(name = "index2", columnNames = {"id", "name"}),
@Index(name = "index3", columnNames = {"id", "name"}, unique = true),
})
4.创建索引 方式二
@Column(index = true)
5.创建Dao
@Dao
public interface UserDao
{
@Insert
public int addUser(User user);
@Delete
public int deleteUser(User user);
@Update
public void update(User user);
@Query(entity = User.class, limit = "0,1")
public User query();
}
6.获取DataBase实例,获得Dao
DataBase dataBase = RoomLite.build(DataBase.class);
UserDao dao = dataBase.getDao(UserDao.class);
7.增删改查使用
7.1增
@Insert
public int addUser(User user);
@Insert
public void addUser(User[] users);
@Insert
public void addUserList(List<User> users);
7.2删除
@Delete
public int deleteUser(User user);
@Delete
public void deleteUser(User[] users);
@Delete
public void deleteUserList(List<User> users);
7.3改
@Update
public int updateUser(User user);
@Update
public void updateUser(User[] users);
@Update
public void updateUserList(List<User> users);
7.4查
// 查询所有
@Query(entity = User.class)
public List<User> queryAll();
// 查询第一个
@Query(entity = User.class, limit = "0,1")
public User query();
// 查询总数
@Query(what = "count(id)", entity = User.class)
public int queryCount();
// 查询所有的名字
@Query(what = "name", entity = User.class)
public String[] queryNames();
// 模糊查询
@Query(entity = User.class, whereClause = "name like ?")
public User[] querySearch(String name);
7.5查询分页
@Query(entity = User.class, whereClause = "name like ?",limit=@Limit(index = "0", maxLength = "30"))
7.6查询占位符
@Query(entity = User.class, whereClause = "name like ?",limit=@Limit(index = "0", maxLength = "?"))
7.7查询排序-正序
@Query(entity = User.class, orderBy = @OrderBy(columnNames = {"id"}, type = OrderBy.Type.ASC))
7.8查询排序-倒序
@Query(entity = User.class, orderBy = @OrderBy(columnNames = {"id"}, type = OrderBy.Type.DESC))
End.
感兴趣的+> 709287944加Q群交流