现在的位置: 首页 > 综合 > 正文

Ormlite 介绍 一

2018年02月12日 ⁄ 综合 ⁄ 共 5420字 ⁄ 字号 评论关闭

概述

     

     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.   
  4.     @DatabaseField(generatedId=true)  
  5.     private int id;  
  6.   
  7.     @DatabaseField(columnName = "name")  
  8.     private String name;  
  9.   
  10.     @DatabaseField  
  11.     private String location;  
  12.   
  13.     public int getId() {  
  14.         return id;  
  15.     }  
  16.       
  17.     public void setId(int id) {  
  18.         this.id = id;  
  19.     }  
  20.       
  21.     public String getName() {  
  22.         return name;  
  23.     }  
  24.   
  25.     public void setName(String name) {  
  26.         this.name = name;  
  27.     }  
  28.       
  29.     public String getLocation() {  
  30.         return location;  
  31.     }  
  32.       
  33.     public void setLocation(String location) {  
  34.         this.location = location;  
  35.     }  
  36. }  

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

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

 

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

[java] view
plain
copy

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


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

我解释一下这个的写法:

· 构造函数:

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

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

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

可以看到log 里面打出来的

 

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

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

抱歉!评论已关闭.