前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ormlite介绍一

ormlite介绍一

作者头像
xiangzhihong
发布2018-01-30 10:41:21
8130
发布2018-01-30 10:41:21
举报
文章被收录于专栏:向治洪向治洪向治洪

概述

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;  
 
 @DatabaseField 
 private 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;  
    }  
 
 @Override 
 public void onCreate(SQLiteDatabase arg0, ConnectionSource arg1) {  
 try {  
            TableUtils.createTableIfNotExists(connectionSource, Student.class);  
            TableUtils.createTableIfNotExists(connectionSource, School.class);  
        } catch (SQLException e) {  
            e.printStackTrace();  
        }  
    }  
 
 
 @Override 
 public 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;  
 
 @Override 
 protected 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 里面打出来的

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2014-09-26 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 概述
  • ORMlite的使用
    • 1,建立映射关系
      • 2,建立数据库和基本的工具
        • 3,测试
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档