概述

     

ORMlite是类似hibernate的对象映射框架,主要面向java语言,同时,是时下最流行的android面向数据库的的编程工具。
官方网站:http://ormlite.com/ 
如果需要开发android,只需要下载core和android两个jar包:

ORMlite的使用

1,建立映射关系

Ormlite与数据库的映射关系式通过注释来说明的。
注释分为对于表的和对于单个列的:@DatabaseTable ,注释表的, @DatabaseField 注释单个列的。
看例子很好很好懂:

解释一下上面的例子,如果想以类student来建立一张表。

· 首先注释:table,@DatabaseTable如果默认为类名的话,后面不需要添加类名注释。

· 然后:确定需要的字段,在字段上面添加注释:@DatabaseField 如果对字段有特别的要求,那么添加以下相关的注释,例如id。

同理,school类为:

@DatabaseTable(tableName = "school")
public class School {@DatabaseField(generatedId=true)private int id;@DatabaseField(columnName = "name")private String name;@DatabaseFieldprivate String location;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getLocation() {return location;}public void setLocation(String location) {this.location = location;}
}

2,建立数据库和基本的工具

在android的开发中,google原版封装了一个SqliteOpenHelper,供开发者调用,在OrmLite中,对原版的工具进行了加强,提供一个继承自SqliteOpenHelper的OrmLiteSqliteOpenHelper工具。

像android一样,我们继承这个工具类。

public class DBHelper extends OrmLiteSqliteOpenHelper{private final static int DATABASE_VERSION = 1;Dao<Student, Integer> mStudentDao;Dao<School, Integer> mSchoolDao;private static final String DB_NAME = "orm";private static DBHelper mDbHelper;private DBHelper(Context context) {super(context, DB_NAME, null, DATABASE_VERSION);}public static DBHelper getInstance(Context context) {if (mDbHelper == null) {mDbHelper = new DBHelper(context);}return mDbHelper;}@Overridepublic void onCreate(SQLiteDatabase arg0, ConnectionSource arg1) {try {TableUtils.createTableIfNotExists(connectionSource, Student.class);TableUtils.createTableIfNotExists(connectionSource, School.class);} catch (SQLException e) {e.printStackTrace();}}@Overridepublic void onUpgrade(SQLiteDatabase arg0, ConnectionSource arg1, int arg2,int arg3) {}public Dao<Student, Integer> getStudentDao() throws SQLException {if (mStudentDao == null) {mStudentDao = getDao(Student.class);}return mStudentDao;}public Dao<School, Integer> getSchoolDao() throws SQLException {if (mSchoolDao == null) {mSchoolDao = getDao(School.class);}return mSchoolDao;}}

如果写过android的SqliteOpenHelper对这个继承类的写法一定不会陌生。

我解释一下这个的写法:

· 构造函数:

必须调用父类的构造函数,能给它提供的参数有:来自android的context,数据库名称,和版本号。

· GetInstance方法:

这个只是为了方便,让DbHelper只保留一个对象的实例,即单例模型。

· OnCreate

实现父类的抽象方法,创建数据库。

· OnUpgrade

在构造方法中的version如果改变的话,调用这个方法,至于想做什么,这个你来定。常用于app 的版本更新。

· getStudentDao和 getSchoolDao

获取数据库的dao对象,这些dao对象用于后来的数据库操作。dao的声明时泛型了两个参数,第一个是dao操作的关联对象,第二个是 标记数据表的ID。这个ID一般很少使用,除非对数据表的ID进行操作的时候。

3,测试

public class MainActivity extends Activity {private static final String TAG = "MainActivity";DBHelper mDbHelper;Dao<Student, Integer> mStudentDao;Dao<School, Integer> mSchoolDao;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);mDbHelper = DBHelper.getInstance(this);try {mSchoolDao = mDbHelper.getSchoolDao();mStudentDao = mDbHelper.getStudentDao();} catch (SQLException e) {Log.e(TAG, "constructor exception", e);}testDao();}private void testDao() {Student student1 = new Student();student1.setName("miles");student1.setSchoolId(0);Student student2 = new Student();student2.setName("li");student2.setSchoolId(0);School school1 = new School();school1.setName("university");school1.setLocation("shanghai");School school2 = new School();school2.setName("middle school");school2.setLocation("hubei");try {mSchoolDao.create(school1);mSchoolDao.create(school2);mStudentDao.create(student1);mStudentDao.create(student2);//获取表中所有的student。List<Student> students=mStudentDao.queryForAll();Log.e(TAG, "before delete the student list:size is:"+students.size());for (int i = 0; i < students.size(); i++) {Log.e(TAG, students.get(i).getName());}mStudentDao.delete(student1);students=mStudentDao.queryForAll();Log.e(TAG, "after delete the student list:"+students.size());for (int i = 0; i < students.size(); i++) {Log.e(TAG, students.get(i).getName());}} catch (SQLException e) {e.printStackTrace();}}
}

在DDMS里面的 File Explore里面的data/data/项目包名/databases里面可以看到有一个db的文件。

可以看到log 里面打出来的

说明做了添加和删除操作,同时查找也成功了。

Ormlite 介绍 一相关推荐

  1. Ormlite 介绍 一

    概述       ORMlite是类似hibernate的对象映射框架,主要面向java语言,同时,是时下最流行的android面向数据库的的编程工具. 官方网站:http://ormlite.com ...

  2. ormlite介绍一

    概述       ORMlite是类似hibernate的对象映射框架,主要面向java语言,同时,是时下最流行的android面向数据库的的编程工具. 官方网站:http://ormlite.com ...

  3. android ormlite框架的理解,Android ORMLite框架入门级介绍

    以前使用数据库都是使用Android官方推荐的SQLiteOpenHelper,用过的都知道,比较难用.一直想找一个比较好用的数据库框架,直到我遇到了ORMLite框架~ 这里写了一个小Demo抛砖引 ...

  4. OrmLite for android--Ormlite的大概介绍

    Ormlite 是一种ORM工具,并且是一种轻量级别的工具.我们可以使用它来对Android中内嵌的sqlite数据库进行相关的操作.Android 的应用程序应使用 Ormlite for andr ...

  5. Ormlite的大概介绍

    Ormlite 是一种ORM工具,并且是一种轻量级别的工具.我们可以使用它来对Android中内嵌的sqlite数据库进行相关的操作.Android 的应用程序应使用 Ormlite for andr ...

  6. ormlite android studio,OrmLite-android入门体验

    入门体验 OrmLite 是一个轻量级的ORM(Object Relational Mapping)数据库工具,方便持久化java对象到数据库 1. 使用准备 使用androidADT创建androi ...

  7. Android ORMLite 框架的入门用法

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/39121377 大家在Android项目中或多或少的都会使用数据库,为了提高我们的 ...

  8. Android 使用ORMLite 操作数据库

    参考:http://blog.csdn.net/cjjky/article/details/7096987 ormlite 方法查询:http://ormlite.com/javadoc/ormlit ...

  9. Android单元测试框架Robolectric3.0介绍(二)

    文章中的所有代码在此:https://github.com/geniusmart/LoveUT ,由于 Robolectric 3.0 和 3.1 版本(包括后续3.x版本)差异不小,该工程中包含这两 ...

最新文章

  1. linux c 随机函数 rand srand 介绍
  2. python画笑脸-python 利用turtle库绘制笑脸和哭脸的例子
  3. 使用HashSet去除重复元素的集合
  4. 文本框input:text
  5. 生成对抗网络(GAN)相比传统训练方法有什么优势?(一)
  6. 从电商用户触点看服务设计趋势
  7. VTK:图片之ImageCorrelation
  8. [渝粤教育] 三江学院 大学生创新基础课程 参考 资料
  9. kafka自定义分区实战
  10. ip别名及其在tcp压力测试时候的作用
  11. 《『若水新闻』客户端开发教程》——09.代码编写(1)
  12. Linux下 C++遍历目录文件
  13. [解答]对‘’未定义的引用 collect2: 错误: ld 返回 1
  14. qt 限制一段时间内对button只能点按一次_299元入手拓牛智能垃圾桶,用第一次想退货,第三天我上瘾了...
  15. matlab 常用函数或符号用法总结
  16. 软件测试优化提案,软件测试改进建议
  17. python代码规范---PEP8(python enhancement proposal)
  18. 不是所有成功都值得尊重
  19. 对宇宙起源的一个现代猜想-重生而非诞生
  20. 超酷震撼 HTML5/CSS3动画应用及源码

热门文章

  1. 使用客户端jedis时报错Could not get a resource from the pool 以及使用Spring Data Redis报错解决方法
  2. Blazeds(一)
  3. java interface和类的 extends与implement 的简单总结
  4. 海康摄像头实时显示与字符叠加详解
  5. 树莓派Ubuntu18.04下无线鼠标延迟问题解决
  6. 根据依序输入的一元多项式的系数输出此多项式
  7. 详解ENet | CPU可以实时的道路分割网络
  8. Concurrent 包结构介绍
  9. 离散数学知识点总结(2)-谓词逻辑
  10. vue-iview异步加载渲染树