在Android开发中使用ORMLite--篇一
来源:互联网 发布:科比2000年季后赛数据 编辑:程序博客网 时间:2024/06/04 13:26
前言
在android开发中,数据需要本地存储,当需要进行一系列的操作时,当然是使用sqlite。标准做法是继承SQLiteOpenHelper,重写onCreate和onUpdate方法。存在比较大的问题是,sql语句需要自己写,比较复杂繁琐而且重复率高,于是找了一个orm框架在新写的app中进行试用。
准备
下载地址:http://ormlite.com/releases/
下载core和android两个jar包,并引入工程即可
开发
模型注解
首先定义一个最常见的人员模型
// tableName定义表名,缺省为类名@DatabaseTable(tableName = "table_person") public class Person{ @DatabaseField(generatedId = true) private int id; // columnName定义字段名,缺省为变量名 @DatabaseField(columnName = "first_name") private String firtName; @DatabaseField private String lastName; // 需要定义一个无参构造函数,ormlite用来查询时用反射构建对象 public Person() { } // other constructors... // getters and setters...}
@DatabaseField注解域说明如下:
官网原文
完成Helper
与原生的SQLiteOpenHelper相似,使用ORMLite需要继承OrmLiteSqliteOpenHelper,如下:
public class DBHelper extends OrmLiteSqliteOpenHelper { private DBHelper(Context context) { super(context, TABLE_NAME, null, VERSION); } // 创建表在这里就非常简单了,使用TableUtils的createTable方法即可,不需要写CREATE TABLE等等的sql语句。 @Override public void onCreate(SQLiteDatabase database, ConnectionSource connectionSource) { try { TableUtils.createTable(connectionSource, Person.class); } catch (SQLException e) { e.printStackTrace(); } } // 重写onUpdate方法 @Override public void onUpgrade(SQLiteDatabase database, ConnectionSource connectionSource, int oldVersion, int newVersion) { try { // 删除旧表再新建表,也可以对表进行相应的更新操作 TableUtils.dropTable(connectionSource, Person.class, true); onCreate(database, connectionSource); } catch (SQLException e) { e.printStackTrace(); } }}
获取Dao
对于orm来说,dao是用于数据库操作的通常做法。在Helper中加入如下代码:
// 声明静态实例private static DBHelper instance;// 声明daoprivate Dao<Person, Integer> personDao;/** * 单例获取该Helper * * @param context * @return */ public static synchronized DBHelper getHelper(Context context) { context = context.getApplicationContext(); if (instance == null) { synchronized (DatabaseHelper.class) { if (instance == null) instance = new DatabaseHelper(context); } } return instance; } // 获取某个dao,有多个表的情况,可能需要写多个获取dao的方法public Dao<Person, Integer> getPersonDao() throws SQLException { if (personDao == null) { personDao = getDao(Person.class); } return personDao;}// 释放资源@Overridepublic void close() { super.close(); personDao = null;}// 另外也可以有通用方法public synchronized Dao getDao(Class clazz) throws SQLException { Dao dao = null; String className = clazz.getSimpleName(); if (daos.containsKey(className)) { dao = daos.get(className); } if (dao == null) { dao = super.getDao(clazz); daos.put(className, dao); } return dao; }
单独创建dao类
public class PersonDao { private Context context; public PersonDao(Context context) { this.context = context; } public void add(Person person) { try { DBHelper.getHelper(context).getPersonDao().create(person); } catch (SQLException e) { e.printStackTrace(); } }}
使用
准备好后,使用起来也非常方便
// 获取helperDBHelper helper = DBHelper.getHelper(getContext());// 构造Person xiaoming = new Person();xiaoming.setFirstName("Ming");xiaoming.setLastName("Xiao");// 保存helper.getPersonDao().create(xiaoming);
0 0
- 在Android开发中使用ORMLite--篇一
- 在Android中使用ORMLite
- 在android中使用OrmLite数据库框架
- Android中使用ormlite
- Android开发中ormlite混淆
- Android中使用ormlite实现持久化(一)--HelloOrmLite
- Android中使用ormlite实现持久化(一)--HelloOrmLite
- Android使用ORMLite操作数据库(一)
- ormlite 在android中 排序 条件查询
- ormlite 在android中 排序 条件查询
- Android快速开发--使用ORMLite操作数据库
- Android快速开发--使用ORMLite操作数据库
- Android快速开发--使用ORMLite操作数据库
- Android 数据库开始开发ORMLite的使用
- Android中如何使用OrmLite持久化
- Android中使用OrmLite(三):批处理
- Android中OrmLite数据库使用总结
- Android中OrmLite数据库的使用
- 实现短信验证码自动拦截读取
- 删除数组重复元素的方法
- DIV结构的点击收缩展开左边栏代码(html+css+js)
- 【不定期更新】常用下载资源整合
- 幂等策略-尝试插入
- 在Android开发中使用ORMLite--篇一
- Android Hook框架Xposed原理与源代码分析
- Java 多维数组遍历详解
- 读取文件夹下的所有文件
- 关于数据结构中栈的若干使用
- MathType破解版中窗口灰色问题该怎样解决
- python登录认证HTTP并打印头信息
- idea下导出可执行的jar包并在windows中双击运行的解决方法
- phalapi-入门篇3(请求和返回)