ormlite介绍一
概述
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 里面打出来的
ormlite介绍一的更多相关文章
- Android 数据库框架OrmLite的使用(一)
在这里记录下最基本的用法,官网上可了解相关的介绍. 1.下载OrmLite jar 在下载android的:ormlite-android-4.48.jar和ormlite-core-4.48.jar ...
- 谈谈我的入门级实体框架Loogn.OrmLite
每次看到有新的ORM的时候,我总会留意一下,因为自己也写过一个这样的框架,人总是有比较之心的.我可能会down下来跑一跑,也可能不会这么做,这个取决于跑起来的难易程度.我是很懒的,有XML配置或其他稍 ...
- GreenDao介绍
GreenDao介绍 greenDAO 是一个将对象映射到 SQLite 数据库中的轻量且快速的 ORM 解决方案 何为ORM? ORM(Object/Relation Mapping): 对象/关系 ...
- OrmLite数据库的使用方法
第一步:导入架包 1.将orm的两个支持包放入project视图下的你的工程的lib目录里(这两个JAR包网上都有,GitHub上最新) 2.添加依赖:在file文件目录下的proje ...
- GitHub上排名前100的Android开源库介绍(来自github)
本项目主要对目前 GitHub 上排名前 100 的 Android 开源库进行简单的介绍,至于排名完全是根据 GitHub 搜索 Java 语言选择 (Best Match) 得到的结果,然后过滤了 ...
- Android—Ormlite框架简单的操作数据库
大家在Android项目中或多或少的都会使用数据库,为了提高我们的开发效率,当然少不了数据库ORM框架了,尤其是某些数据库操作特别频繁的app:本篇博客将详细介绍ORMLite的简易用法. 下面开始介 ...
- ServiceStack.OrmLite 笔记2 -增
ServiceStack.OrmLite 笔记2 这篇主要介绍 增加 db.Insert(new Employee { Id = 1, Name = "Employee 1" }) ...
- [ 转]Android快速开发–使用ORMLite操作数据库
OrmLite是一个数据库操作辅助的开源框架,主要面向Java语言.在Android面向数据库开发中,是一个比较流行的开源框架,方便操作而且功能强大,今天来学习一下,最近的项目中也有所涉及,写个博客来 ...
- ormlite性能对比
看了一下现在的android设备,性能都不差,就懒得直接用sqlite,直接上ORM框架把,上网搜了一圈,觉得androrm, ormlite 这两个不错,当然,还有点别的,这里就不多做介绍,竟然说明 ...
随机推荐
- [AHOI2005]约数研究
题目描述 科学家们在Samuel星球上的探险得到了丰富的能源储备,这使得空间站中大型计算机“Samuel II”的长时间运算成为了可能.由于在去年一年的辛苦工作取得了不错的成绩,小联被允许用“Samu ...
- bzoj 4894: 天赋
Description 小明有许多潜在的天赋,他希望学习这些天赋来变得更强.正如许多游戏中一样,小明也有n种潜在的天赋,但有 一些天赋必须是要有前置天赋才能够学习得到的.也就是说,有一些天赋必须是要在 ...
- [SPOJ962]Intergalactic Map 拆点+最大流
Jedi knights, Qui-Gon Jinn and his young apprentice Obi-Wan Kenobi, are entrusted by Queen Padmé Ami ...
- 【Toll!Revisited(uva 10537)】
题目来源:蓝皮书P331 ·这道题使得我们更加深刻的去理解Dijkstra! 在做惯了if(dis[u]+w<dis[v])的普通最短路后,这道选择路径方案不是简单的比大小的题横在了 ...
- [BZOJ]1047 理想的正方形(HAOI2007)
真·水题.小C本来是不想贴出来的,但是有一股来自东方的神秘力量催促小C发出来. Description 有一个a*b的整数组成的矩阵,现请你从中找出一个n*n的正方形区域,使得该区域所有数中的最大值和 ...
- 华科机考:a+b
时间限制:1秒空间限制:32768K 题目描述 计算a+b的和 每行包行两个整数a和b 对于每行输入对应输出一行a和b的和 输入 1 5 输出 6 吐槽:这尼玛是机考题? 代码: #include & ...
- 实现string类
/* 实现string类 */ class String { public: String(const char *str=NULL); //构造函数 S ...
- 在 TensorFlow 中实现文本分类的卷积神经网络
在TensorFlow中实现文本分类的卷积神经网络 Github提供了完整的代码: https://github.com/dennybritz/cnn-text-classification-tf 在 ...
- Debugging TensorFlow models 调试 TensorFlow 模型
Debugging TensorFlow models Symbolic nature of TensorFlow makes it relatively more difficult to debu ...
- idea热部署
<!-- 热部署模块 --> <dependency> <groupId>org.springframework.boot</groupId> < ...