RoomLite

GitHub link: github.com/xiaolei123/… Gitee link: gitee.com/xcode_xiao/…

introduce

On Android, use compile-time annotations to generate Java files, avoiding low performance. A new version of SQLite database ORM ship. Add, delete, change, check and build tables, sort positive order reverse order uniqueness, table index, multithreading concurrent reading and writing are not a problem. Are you sure?

To obtain

allprojects {
	repositories{... maven { url'https://www.jitpack.io'}}}Copy the code
java:
dependencies {
    annotationProcessor 'com.github.xiaolei123:compiler:+'
    implementation 'com.github.xiaolei123:runtime:+'
}

kotlin:
dependencies {
    kapt 'com.github.xiaolei123:compiler:+'
    implementation 'com.github.xiaolei123:runtime:+'
}


Copy the code

Directions for use

1. Create a database
public static class DataBase extends RoomLiteDatabase
{
    public DataBase(a)
    {
        // Database name
        super("school");
    }
    
    // All table entities
    @Override
    publicClass<? >[] getEntities() {return new Class[]{User.class};
    }
    
    // Whether execution is allowed in the main thread
    @Override
    public boolean allowRunOnUIThread(a)
    {
        return true;
    }
    
    // Database upgrade
    @Override
    public void onUpgrade(@Nullable SQLiteDatabase db, int oldVersion, int newVersion)
    {}// Database version
    @Override
    public int version(a)
    {
        return 1; }}Copy the code
2. Create tables in the database
@Entity(name = "User")
public class User
{
    @Column
    @PrimaryKey(autoGenerate = true)
    public int id;

    public String name = "Current time :" + System.currentTimeMillis();

    @Ignore
    public Bitmap bitmap;
}
Copy the code
2.1 Declare the primary key of a field (and increment). Increment takes effect only when the type is numeric
@PrimaryKey(autoGenerate = true)
Copy the code
2.2 Field NOT NULL
@Column(notNull = true)
Copy the code
2.3 Field UNIQUE
@Column(unique = true)
Copy the code
2.4 DEFAULT Value DEFAULT
@Column( defaultValue = "0")
Copy the code
2.5 Ignoring a field
@Ignore
public Bitmap bitmap;
Copy the code
2.6 User-defined fields are supported

Step 1: Declare the custom type in the table class

@Entity(name = "User")
public class User
{
    public Date date;
}
Copy the code

Step 2: Inherit the corresponding converter

public class DateConvert extends ToLongConvert<Date>
{
    public DateConvert(a)
    {
        super(Date.class);
    }
    @Override
    public Long convertToLong(Date javaObj)
    {
        Date date = (Date) javaObj;
        if (javaObj == null) 
            return null;
        return date.getTime();
    }
    /** * retrieves data from a database Cursor and converts it to the corresponding javaType type **@param value
     */
    @Override
    public Date cursorToJavaObject(long value)
    {
        return newDate(value); }}Copy the code

Step 3: Register the converter with RoomLite

RoomLite.addConvert(new DateConvert());
Copy the code
3. Create indexes in method 1
@Entity(name = "User", indices = { @Index(columnNames = {"id", "name"}), @Index(name = "index2", columnNames = {"id", "name"}), @Index(name = "index3", columnNames = {"id", "name"}, unique = true), })
Copy the code
4. Create indexes in method 2
@Column(index = true)
Copy the code
5. Create a 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(a);
}
Copy the code
6. Obtain the DataBase instance and obtain the Dao
DataBase dataBase = RoomLite.build(DataBase.class);
UserDao dao = dataBase.getDao(UserDao.class);
Copy the code
7. Add, delete, revise, check and use
7.1 to add
@Insert
public int addUser(User user);
@Insert
public void addUser(User[] users);
@Insert
public void addUserList(List<User> users);
Copy the code
7.2 delete
@Delete
public int deleteUser(User user);
@Delete
public void deleteUser(User[] users);
@Delete
public void deleteUserList(List<User> users);
Copy the code
7.3 change
@Update
public int updateUser(User user);
@Update
public void updateUser(User[] users);
@Update
public void updateUserList(List<User> users);
Copy the code
7.4 check
// Query all
@Query(entity = User.class)
public List<User> queryAll(a);

// query the first one
@query (entity = user. class, limit = "0,1")
public User query(a);

// Query the total number
@Query(what = "count(id)", entity = User.class)
public int queryCount(a);

// Query all names
@Query(what = "name", entity = User.class)
public String[] queryNames();

// fuzzy query
@Query(entity = User.class, whereClause = "name like ?" )
public User[] querySearch(String name);
Copy the code
7.5 Querying Paging
@Query(entity = User.class, whereClause = "name like ?" ,limit=@Limit(index = "0", maxLength = "30"))
Copy the code
7.6 Querying placeholders
@Query(entity = User.class, whereClause = "name like ?" ,limit=@Limit(index = "0", maxLength = "?" ))
Copy the code
7.7 Query Sort in Positive Order
@Query(entity = User.class, orderBy = @OrderBy(columnNames = {"id"}, type = OrderBy.Type.ASC))
Copy the code
7.8 Query Sort in reverse order
@Query(entity = User.class, orderBy = @OrderBy(columnNames = {"id"}, type = OrderBy.Type.DESC))
Copy the code

End.