ORMlite是类似hibernate的对象映射框架,主要面向java语言,同时,是时下最流行的android面向数据库的的编程工具。
官方网站:http://ormlite.com/
如果需要开发android,只需要下载core和android两个jar包:
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;
}
}
在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进行操作的时候。
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 里面打出来的