转 [Android 快速开发系列 ORMLite 框架最佳实践] (http://blog.csdn.net/lmj623565791/article/details/39122981#)
1. AndroidStudio gradle 依赖引入
1 2
| compile 'com.j256.ormlite:ormlite-android:5.0' compile 'com.j256.ormlite:ormlite-core:5.0'
|
2. 数据库管理操作Java对象类
- 继承 OrmLiteSqliteOpenHelper
在构造方法中创建数据库名称和数据库版本号
1 2 3 4 5 6 7 8 9 10
| public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
private final static String DB_NAME = "ormlite_test.db"
private DatabaseHelper(Context context) { super(context, DB_NAME, null, 1); } ... }
|
1
| TableUtils.createTable(connectionSource, User.class);
|
1
| TableUtils.dropTable(connectionSource, User.class, true);
|
3. 创建数据表和映射对象类
1 2 3 4 5
| //使用注解 @DatabaseTable 声明创建表 @DatabaseTable(tableName = "tb_user") public class user { ... }
|
1 2 3 4
| //使用注解 @DatabaseField 创建字段 // generatedId 声明自增ID @DatabaseField(generatedId = true) private int id;
|
1 2 3
| //name 字段 @DatabaseField(columnName = “name”) private String name;
|
4. 数据表操作DAO类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| /** * 更新用户姓名 */ public int updateTestName(String testName) { int updateResult = -1; try { UpdateBuilder updateBuilder = this.realDao.updateBuilder(); updateBuilder.where().like("test_name", testName); updateResult = updateBuilder.update(); LogUtil.getInstance().info(LogLevel.INFO, TAG + " updateTestName: updateResult" + updateResult); } catch (SQLException e) { e.printStackTrace(); } finally { return updateResult; } }
|
1 2 3 4 5
| public interface IDao<T, ID> { public T queryForId(ID id) throws SQLException ; public List<T> queryForAll() throws SQLException ; ... }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
| /** * Author: WeiQin * Date: 2017/2/28 * Time: 上午11:45 * Description: 测试数据库管理对象 */ public class TestDao implements IDao<TestObjectVo, Integer> {
private final static String TAG = "TestDao";
private Context context; private OrmLiteDBHelper helper; private Dao<TestObjectVo, Integer> realDao;
private static TestDao instance = null;
public static TestDao getInstance(Context context) { if (instance == null) { synchronized (TestDao.class) { if (instance == null) { instance = new TestDao(context); } } } return instance; }
private TestDao(Context context) { try { this.context = context; this.helper = OrmLiteDBHelper.getHelper(context); this.realDao = helper.getDao(TestObjectVo.class); } catch (SQLException e) { e.printStackTrace(); } } /** * 增加一个用户 * * @param testObjectVo */ public void add(TestObjectVo testObjectVo) { try { LogUtil.getInstance().info(LogLevel.INFO, TAG + " add " + testObjectVo.toString()); this.realDao.create(testObjectVo); } catch (SQLException e) { e.printStackTrace(); } }
/** * 删除一个用户 * * @param testObjectVo */ public void delete(TestObjectVo testObjectVo) { try { LogUtil.getInstance().info(LogLevel.INFO, TAG + " delete: " + testObjectVo.toString()); this.realDao.delete(testObjectVo); } catch (SQLException e) { e.printStackTrace(); }
} @Override public TestObjectVo queryForId(Integer id) throws SQLException { LogUtil.getInstance().info(LogLevel.INFO, TAG + " queryForId: " + id); return this.realDao.queryForId(id); }
@Override public List<TestObjectVo> queryForAll() throws SQLException { LogUtil.getInstance().info(LogLevel.INFO, TAG + " queryForAll: " + this.realDao.queryForAll()); return this.realDao.queryForAll(); }
|