概述

     

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

ORMlite的使用

1,建立映射关系

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

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

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

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

同理,school类为:

[java]  view plain copy
  1. @DatabaseTable(tableName = "school")
  2. public class School {
  3. @DatabaseField(generatedId=true)
  4. private int id;
  5. @DatabaseField(columnName = "name")
  6. private String name;
  7. @DatabaseField
  8. private String location;
  9. public int getId() {
  10. return id;
  11. }
  12. public void setId(int id) {
  13. this.id = id;
  14. }
  15. public String getName() {
  16. return name;
  17. }
  18. public void setName(String name) {
  19. this.name = name;
  20. }
  21. public String getLocation() {
  22. return location;
  23. }
  24. public void setLocation(String location) {
  25. this.location = location;
  26. }
  27. }

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

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

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

[java]  view plain copy
  1. public class DBHelper extends OrmLiteSqliteOpenHelper{
  2. private final static int DATABASE_VERSION = 1;
  3. Dao<Student, Integer> mStudentDao;
  4. Dao<School, Integer> mSchoolDao;
  5. private static final String DB_NAME = "orm";
  6. private static DBHelper mDbHelper;
  7. private DBHelper(Context context) {
  8. super(context, DB_NAME, null, DATABASE_VERSION);
  9. }
  10. public static DBHelper getInstance(Context context) {
  11. if (mDbHelper == null) {
  12. mDbHelper = new DBHelper(context);
  13. }
  14. return mDbHelper;
  15. }
  16. @Override
  17. public void onCreate(SQLiteDatabase arg0, ConnectionSource arg1) {
  18. try {
  19. TableUtils.createTableIfNotExists(connectionSource, Student.class);
  20. TableUtils.createTableIfNotExists(connectionSource, School.class);
  21. } catch (SQLException e) {
  22. e.printStackTrace();
  23. }
  24. }
  25. @Override
  26. public void onUpgrade(SQLiteDatabase arg0, ConnectionSource arg1, int arg2,
  27. int arg3) {
  28. }
  29. public Dao<Student, Integer> getStudentDao() throws SQLException {
  30. if (mStudentDao == null) {
  31. mStudentDao = getDao(Student.class);
  32. }
  33. return mStudentDao;
  34. }
  35. public Dao<School, Integer> getSchoolDao() throws SQLException {
  36. if (mSchoolDao == null) {
  37. mSchoolDao = getDao(School.class);
  38. }
  39. return mSchoolDao;
  40. }
  41. }

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

我解释一下这个的写法:

· 构造函数:

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

· GetInstance方法:

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

· OnCreate

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

· OnUpgrade

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

· getStudentDao和 getSchoolDao

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

3,测试

[java]  view plain copy
  1. public class MainActivity extends Activity {
  2. private static final String TAG = "MainActivity";
  3. DBHelper mDbHelper;
  4. Dao<Student, Integer> mStudentDao;
  5. Dao<School, Integer> mSchoolDao;
  6. @Override
  7. protected void onCreate(Bundle savedInstanceState) {
  8. super.onCreate(savedInstanceState);
  9. mDbHelper = DBHelper.getInstance(this);
  10. try {
  11. mSchoolDao = mDbHelper.getSchoolDao();
  12. mStudentDao = mDbHelper.getStudentDao();
  13. } catch (SQLException e) {
  14. Log.e(TAG, "constructor exception", e);
  15. }
  16. testDao();
  17. }
  18. private void testDao() {
  19. Student student1 = new Student();
  20. student1.setName("miles");
  21. student1.setSchoolId(0);
  22. Student student2 = new Student();
  23. student2.setName("li");
  24. student2.setSchoolId(0);
  25. School school1 = new School();
  26. school1.setName("university");
  27. school1.setLocation("shanghai");
  28. School school2 = new School();
  29. school2.setName("middle school");
  30. school2.setLocation("hubei");
  31. try {
  32. mSchoolDao.create(school1);
  33. mSchoolDao.create(school2);
  34. mStudentDao.create(student1);
  35. mStudentDao.create(student2);
  36. //获取表中所有的student。
  37. List<Student> students=mStudentDao.queryForAll();
  38. Log.e(TAG, "before delete the student list:size is:"+students.size());
  39. for (int i = 0; i < students.size(); i++) {
  40. Log.e(TAG, students.get(i).getName());
  41. }
  42. mStudentDao.delete(student1);
  43. students=mStudentDao.queryForAll();
  44. Log.e(TAG, "after delete the student list:"+students.size());
  45. for (int i = 0; i < students.size(); i++) {
  46. Log.e(TAG, students.get(i).getName());
  47. }
  48. } catch (SQLException e) {
  49. e.printStackTrace();
  50. }
  51. }
  52. }

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

可以看到log 里面打出来的

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

转自:http://blog.csdn.net/yunxiaoxiaoyun/article/details/16873669

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. shanghai hongqiao railway station
  2. 修改Maven源为阿里巴巴的镜像
  3. 666A-Reberland Linguistics(动态规划)
  4. 【BZOJ - 4754】独特的树叶(树哈希)
  5. 类的static成员并用其实现一个单例模式
  6. 如何系统地自学前端(女生),女生发展前端是否是青春饭?
  7. 全球超级计算机500强 中国独占两个第一
  8. wttr.in -- a magical website
  9. 消费者价格研究中的数据分析
  10. linux关闭自动更新,Ubuntu开启系统自动升级与取消自动更新的方法,
  11. jQuery Mobile中固定工具栏header、footer的data-*选项
  12. 以太网MDIO总线调试笔记
  13. 开源 | 写了个微博去广告、屏蔽拉黑插件
  14. vscode、windows快捷键
  15. “3W1H法”浅析三层架构
  16. DT财经:2018北京城市大数据活跃报告
  17. OpenCV在win10安装
  18. 数据库管理工具哪个好?强力推荐Navicat Premium 16 mac中文版
  19. Android 自定义相机 切换相机 参考线(辅助线) 闪光灯 缩放 自动聚焦 Demo
  20. iPhone手机,ibooks为什么突然不能用了?

热门文章

  1. 18.1 项目分析与模块划分(项目设计)-《SSM深入解析与项目实战》
  2. 什么是makefile
  3. Pharo 4.0:简洁新颖的开源Smalltalk开发环境
  4. 一步步的教你如何创建第一个APP?-swift
  5. 2021-2027全球与中国客运机场市场现状及未来发展趋势
  6. Excel催化剂开源第30波-在Excel上尽情地使用LINQ
  7. @RequestBody的使用
  8. mysql技术可行性_MySQL 大企业级应用可行性分析(之三)
  9. Python学习:自定义函数,不可或缺
  10. 牛人如何赚到第一桶金的?不看错过一百万