概述
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
3.
4. @DatabaseField(generatedId=true)
5. private int
6.
7. @DatabaseField(columnName = "name")
8. private
9.
10. @DatabaseField
11. private
12.
13. public int
14. return
15. }
16.
17. public void setId(int
18. this.id = id;
19. }
20.
21. public
22. return
23. }
24.
25. public void
26. this.name = name;
27. }
28.
29. public
30. return
31. }
32.
33. public void
34. this.location = location;
35. }
36. }
2,建立数据库和基本的工具
在android的开发中,google原版封装了一个SqliteOpenHelper,供开发者调用,在OrmLite中,对原版的工具进行了加强,提供一个继承自SqliteOpenHelper的OrmLiteSqliteOpenHelper工具。
像android一样,我们继承这个工具类。
[java]
view plain
copy
1. public class DBHelper extends
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
9.
10. private
11. super(context, DB_NAME, null, DATABASE_VERSION);
12. }
13.
14.
15. public static
16. if (mDbHelper == null) {
17. new
18. }
19. return
20. }
21.
22. @Override
23. public void
24. try
25. class);
26. class);
27. catch
28. e.printStackTrace();
29. }
30. }
31.
32.
33. @Override
34. public void onUpgrade(SQLiteDatabase arg0, ConnectionSource arg1, int
35. int
36. }
37.
38. public Dao<Student, Integer> getStudentDao() throws
39. if (mStudentDao == null) {
40. class);
41. }
42. return
43. }
44.
45. public Dao<School, Integer> getSchoolDao() throws
46. if (mSchoolDao == null) {
47. class);
48. }
49. return
50. }
51.
52.
53. }
如果写过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
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
10. super.onCreate(savedInstanceState);
11. this);
12. try
13. mSchoolDao = mDbHelper.getSchoolDao();
14. mStudentDao = mDbHelper.getStudentDao();
15. catch
16. "constructor exception", e);
17. }
18. testDao();
19. }
20.
21. private void
22. new
23. "miles");
24. 0);
25.
26. new
27. "li");
28. 0);
29.
30. new
31. "university");
32. "shanghai");
33.
34. new
35. "middle school");
36. "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. "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. "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
58. e.printStackTrace();
59. }
60. }
61. }
在DDMS里面的 File Explore里面的data/data/项目包名/databases里面可以看到有一个db的文件。
可以看到log 里面打出来的