快速&轻量级的 Android SQLite ORM 映射框架,尽可能的简化数据库操作。

easyDAO

项目地址: smuyyh/easyDAO
简介:快速&轻量级的 Android SQLite ORM 映射框架,尽可能的简化数据库操作。

easyDAO is a light-weight&fast ORM library for Android that maps objects to SQLite , it becomes much easier to operate the SQLite database.

中文文档

Features

  1. easyDAO makes the database CRUD easier for Android.
  2. maps objects to SQLite by annotation.

Download

dependencies {
    compile 'com.yuyh.easydao:easydao:1.1.1'
}

A Brief Guide

1. Declare a public entity class inherit from {@link com.yuyh.easydao.base.BaseEntity}.
2. Use notation '@{@link com.yuyh.easydao.annotation.Column}' to make an element a column in the database,
   the column name will be the same as the element.
3. Every element with the '@Column' anotation MUST have getter/setter.
4. Call {@link IDB#getDatabase(int, String, String, Class, IDBListener)} to create the database and table,
   and use the returned value to operate the database.

Notices(important!!!)

  1. The name of getter/setter method MUST strictly follow the rule: getElement, setElement, setOk, isOk. (i.e. get/set + element name with the first character in upper case), NOTE that the auto generated getters/setters of the Boolean/boolean element is following this rule. e.g.

     @Column
     private boolean isOk;
    
     public boolean isOk(){
         return isOk;
     }
    
     public void setOk(boolean isOk){
         this.isOk = isOk;
     }
    
  2. For elements with Boolean/boolean type, the internal type in database is INTEGER, 1 means true, 0 means false. You may notice this if you want to find with condition.

  3. By default, the value of the column can NOT be null, so that it would NOT save to database if the value is NULL. use (nullable = true) to allow this.

Quick Setup

1. Download module and add to the project or direct add dependencies

2. get DB singleton object

IDAO<UserBean> dao = DB.getInstance(mContext).getDatabase(1, database, listener);

there is no tableName and entityClass parameter, calledIDAO.initTable(String, Class) to init table.

or

IDAO<UserBean> dao = DB.getInstance(mContext)
                       .getDatabase(1, databaseName, tablename, UserBean.class, listener);

3. CRUD

IDAO  interface provides the following methods for CRUD

/**
 * get the sqlite database object
 *
 * @return
 */
SQLiteDatabase getDatabase();

/**
 * init table name and entityClass
 *
 * @param tableName
 * @param clazz
 */
void initTable(String tableName, Class<T> clazz);

/**
 * get count of entities
 *
 * @return
 * @throws DBException
 */
long getCount() throws DBException;

boolean isTableExist(String tableName) throws DBException;

/**
 * check table exists
 *
 * @return
 * @throws DBException
 */
boolean isTableExist() throws DBException;

/**
 * create table
 *
 * @throws DBException
 */
void createTable() throws DBException;

/**
 * create table
 *
 * @param tableName table name
 * @throws DBException
 */
<T> void createTable(Class<T> entityClass, String tableName) throws DBException;

/**
 * drop table
 *
 * @throws DBException
 */
void dropTable() throws DBException;

/**
 * drop all table
 *
 * @throws DBException
 */
void dropAllTable() throws DBException;

/**
 * save database entity
 *
 * @param entity
 * @throws DBException
 */
void save(T entity) throws DBException;

/**
 * delete database entity by id(primary key)
 *
 * @param id
 * @throws DBException
 */
void delete(int id) throws DBException;

/**
 * delete database entity by ids(primary key)
 *
 * @param ids
 * @throws DBException
 */
void delete(int[] ids) throws DBException;

/**
 * delete all data
 *
 * @throws DBException
 */
void deleteAll() throws DBException;

/**
 * update entity
 *
 * @param entity
 * @throws DBException
 */
void update(T entity) throws DBException;

/**
 * update entity by a condition string
 *
 * @param condition part of the update SQL command after keyword 'WHERE'
 *                  (i.e."UPDATE Person SET age = 35 WHERE condition")
 *                  (e.g. condition -- "name = 'Richard' OR name = 'Jefferson'")
 * @param entity
 * @throws DBException
 */
void updateByCondition(String condition, T entity) throws DBException;

/**
 * find entity by id
 *
 * @param id
 * @return
 * @throws DBException
 */
T find(int id) throws DBException;

/**
 * find last entity
 *
 * @return
 * @throws DBException
 */
T findLastEntity() throws DBException;

/**
 * find entities by a condition string
 *
 * @param condition part of the select SQL command after keyword 'WHERE'
 *                  (i.e."SELECT * FROM Person WHERE condition")
 *                  (e.g. condition -- "name = 'Richard' OR name = 'Jefferson'")
 * @return
 */
List<T> findByCondition(String condition) throws DBException;

/**
 * find all entities
 *
 * @return
 * @throws DBException
 */
List<T> findAll() throws DBException;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值