[关闭]
@linux1s1s 2016-12-02T13:11:41.000000Z 字数 4732 阅读 1964

Android ORM - OrmLite

AndroidRefine 2016-11


系列博文
Android ORM - GreenDAO
Android ORM - OrmLite

先普及一下数据库的关联关系,可以直接参看博文数据库设计(一对一、一对多、多对多)

OrmLite是什么

相信大家在android开发过程中大多数项目都需要使用android的数据库sqllite,但是每次直接利用android提供的api来增删改查数据表有点繁琐,做javaweb的都知道有一款数据库框架叫Hibernateandroid端同样有类似这样的框架OrmLite,

Demo

这里通过一个简单的栗子直观感受一下ORM框架数据持久化提供的便利

配置文件

看一下gradle配置文件

gradle.build
  1. //ORMLite
  2. compile group: 'com.j256.ormlite', name: 'ormlite-android', version: '5.0'
  3. compile 'com.j256.ormlite:ormlite-core:5.0'

获取ORMLite的maven地址可供参考

源码文件

首先是POJO类

LiteInfo.java
  1. package okhttp.mutex.com.orm_demo.ormlite;
  2. import com.j256.ormlite.field.DatabaseField;
  3. import com.j256.ormlite.table.DatabaseTable;
  4. @DatabaseTable(tableName = "lite_info")
  5. public class LiteInfo {
  6. @DatabaseField(id = true)
  7. private long id;
  8. @DatabaseField
  9. private String name;
  10. @DatabaseField
  11. private int age;
  12. public LiteInfo() {
  13. }
  14. public LiteInfo(long id, String name, int age) {
  15. this.id = id;
  16. this.name = name;
  17. this.age = age;
  18. }
  19. public long getId() {
  20. return id;
  21. }
  22. public void setId(long id) {
  23. this.id = id;
  24. }
  25. public String getName() {
  26. return name;
  27. }
  28. public void setName(String namel) {
  29. this.name = namel;
  30. }
  31. public int getAge() {
  32. return age;
  33. }
  34. public void setAge(int age) {
  35. this.age = age;
  36. }
  37. @Override
  38. public String toString() {
  39. return "LiteInfo{" +
  40. "id=" + id +
  41. ", namel='" + name + '\'' +
  42. ", age=" + age +
  43. '}';
  44. }
  45. }

简要介绍一下上面的注解

  • @DatabaseTable:表示定义了一个数据表,如果不指定名字,在Android中会以类名作为表名,如packageInfo就是SQLite数据库中的表名,我们也可以指定表名,@DatabaseTable(tableName = "tb_package") 。
  • @DatabaseField:表示定义了数据中的一个字段,id表示数据中的一个主键,如果指定为generatedId,表示自动增长id,我们不需要给它赋值。其他字段,可以使用columnName来指定字段名,canBeNull表示是否为空,这些赋值可以按照以下来指定
    -(id = true, canBeNull = false)

然后定义OrmLiteHelper继承自OrmLiteSqliteOpenHelper

OrmLiteHelper.java
  1. package okhttp.mutex.com.orm_demo.ormlite;
  2. import android.content.Context;
  3. import android.database.sqlite.SQLiteDatabase;
  4. import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
  5. import com.j256.ormlite.dao.Dao;
  6. import com.j256.ormlite.support.ConnectionSource;
  7. import com.j256.ormlite.table.TableUtils;
  8. import java.sql.SQLException;
  9. import java.util.HashMap;
  10. import java.util.Map;
  11. public class OrmLiteHelper extends OrmLiteSqliteOpenHelper {
  12. private static final String TAB_NAME = "orm_lite_table_name";
  13. private static OrmLiteHelper sORMLITEHELPER;
  14. private Map<String, Dao> mDaoMap = new HashMap<>();
  15. private OrmLiteHelper(Context context) {
  16. super(context, TAB_NAME, null, 1);
  17. }
  18. public static void createInstance(Context context) {
  19. if (sORMLITEHELPER == null) {
  20. synchronized (OrmLiteHelper.class) {
  21. if (sORMLITEHELPER == null) {
  22. sORMLITEHELPER = new OrmLiteHelper(context);
  23. }
  24. }
  25. }
  26. }
  27. public static OrmLiteHelper getInstance() {
  28. return sORMLITEHELPER;
  29. }
  30. public Dao getLiteDao(Class clazz) throws SQLException {
  31. Dao dao = null;
  32. String className = clazz.getSimpleName();
  33. synchronized (this) {
  34. if (mDaoMap.containsKey(className)) {
  35. dao = mDaoMap.get(className);
  36. }
  37. if (dao == null) {
  38. dao = super.getDao(clazz);
  39. mDaoMap.put(className, dao);
  40. }
  41. }
  42. return dao;
  43. }
  44. @Override
  45. public void close() {
  46. for (String key : mDaoMap.keySet()) {
  47. Dao dao = mDaoMap.get(key);
  48. dao = null;
  49. }
  50. super.close();
  51. }
  52. @Override
  53. public void onCreate(SQLiteDatabase database, ConnectionSource connectionSource) {
  54. try {
  55. TableUtils.createTable(connectionSource, LiteInfo.class);
  56. } catch (SQLException e) {
  57. e.printStackTrace();
  58. }
  59. }
  60. @Override
  61. public void onUpgrade(SQLiteDatabase database, ConnectionSource connectionSource, int oldVersion, int newVersion) {
  62. try {
  63. TableUtils.dropTable(connectionSource, LiteInfo.class, true);
  64. } catch (SQLException e) {
  65. e.printStackTrace();
  66. }
  67. onCreate(database, connectionSource);
  68. }
  69. }

接着定义DAO类LiteInfoDao

LiteInfoDao.java
  1. package okhttp.mutex.com.orm_demo.ormlite;
  2. import com.j256.ormlite.dao.Dao;
  3. import com.j256.ormlite.stmt.PreparedQuery;
  4. import java.sql.SQLException;
  5. import java.util.List;
  6. public class LiteInfoDao {
  7. private Dao<LiteInfo, Long> mLiteDao;
  8. public LiteInfoDao() {
  9. OrmLiteHelper helper = OrmLiteHelper.getInstance();
  10. try {
  11. mLiteDao = helper.getLiteDao(LiteInfo.class);
  12. } catch (SQLException e) {
  13. e.printStackTrace();
  14. }
  15. }
  16. /**
  17. * 插入一条数据
  18. *
  19. * @param info
  20. */
  21. public void addInfo(LiteInfo info) {
  22. if (mLiteDao == null)
  23. return;
  24. try {
  25. mLiteDao.createIfNotExists(info);
  26. } catch (SQLException e) {
  27. e.printStackTrace();
  28. }
  29. }
  30. public LiteInfo queryInfo(long id) {
  31. try {
  32. return mLiteDao.queryForId(id);
  33. } catch (SQLException e) {
  34. e.printStackTrace();
  35. }
  36. return null;
  37. }
  38. public List<LiteInfo> query() {
  39. try {
  40. return mLiteDao.queryForAll();
  41. } catch (SQLException e) {
  42. e.printStackTrace();
  43. }
  44. return null;
  45. }
  46. }

最后测试一下上面是否工作正常

MainActivity.java
  1. public class MainActivity extends AppCompatActivity {
  2. private LiteInfoDao mOrmDao;
  3. @Override
  4. protected void onCreate(Bundle savedInstanceState) {
  5. super.onCreate(savedInstanceState);
  6. setContentView(R.layout.activity_main);
  7. mOrmDao = new LiteInfoDao();
  8. initOrmInfo();
  9. queryOrmOri();
  10. queryOrmOri(1);
  11. }
  12. private void queryOrmOri() {
  13. List<LiteInfo> list = mOrmDao.query();
  14. for (LiteInfo info : list) {
  15. Log.i("mutex", info.toString());
  16. }
  17. }
  18. private void queryOrmOri(long id) {
  19. LiteInfo info = mOrmDao.queryInfo(id);
  20. Log.w("mutex", info.toString());
  21. }
  22. private void initOrmInfo() {
  23. for (int i = 0; i < 10; i++) {
  24. LiteInfo info = new LiteInfo(i, "name" + i, i + 20);
  25. mOrmDao.addInfo(info);
  26. }
  27. }
  28. }

验证结果

此处输入图片的描述

参考文章:
GreenDAO Website Github
OrmLite Website Github

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注