您的位置:首页 > 数据库

Ormlite数据库

2015-11-16 19:13 429 查看
一般的项目中,Android自身提供的SQLite数据库,可以满足轻量级的数据存储应用,但是,只要是存储模型稍微复杂的项目,以及数据结构模型复杂的应用,就很难再用SQLite支撑整个项目的数据存储。何况,使用SQLite编写的代码,代码后续维护和管理不容易,所以,上规模的项目中,很有必要引入一种更好用、对开发者更友好的第三方ORM数据库框架:ORMlite。
ORMLite官方的简介说:“Object Relational Mapping Lite (ORM Lite) provides some simple, lightweight functionality for persisting Java objects to SQL
databases while avoiding the complexity and overhead of more standard ORM packages.”。
ORMLite是对象关系映射(Object Relational Mapping)数据库的一种轻量级SQL数据库的开发包(packages)。提供简单易用的DAO。
ORMLite官方主页:http://ormlite.com 
Java语言编写,支持Java,Android平台,本文重点介绍如何在Android平台的应用开发中使用ORMLite。
在Android应用开发中使用ORMLite,首先要下载ORMLite的开发jar包,jar资源包下载主页:http://ormlite.com/releases 
具体到Android,需要在 http://ormlite.com/releases 页面下载两个jar
包(本文基于ORMLite的版本是:ormlite 4.49-SNAPSHOT):
(1)core列表下的jar包;
(2)android列表下的jar包。

项目 libs中添加



Ormlite 中

public class ORMLiteDatabaseHelper extends OrmLiteSqliteOpenHelper{

private static ORMLiteDatabaseHelper mDatabaseHelper = null;

private Dao<Student,Integer> mStudentDao = null;
private Dao<MyClass,Integer> mClassDao = null;

private final static String Database_NAME = "ormlite.db";
private final static int Database_VERSION = 1;

public ORMLiteDatabaseHelper(Context context, String databaseName, CursorFactory factory, int databaseVersion) {
super(context, databaseName, factory, databaseVersion);
}

public static ORMLiteDatabaseHelper getInstance(Context context){
if(mDatabaseHelper == null){

mDatabaseHelper = new ORMLiteDatabaseHelper(context, Database_NAME, null, Database_VERSION);
}
return mDatabaseHelper;
}
@Override
public void onCreate(SQLiteDatabase arg0, ConnectionSource arg1) {

try {
TableUtils.createTableIfNotExists(arg1, MyClass.class);//创建班级表格
TableUtils.createTableIfNotExists(arg1, Student.class);//创建学生表格
} catch (SQLException e) {
e.printStackTrace();
}

}

@Override
public void onUpgrade(SQLiteDatabase arg0, ConnectionSource arg1, int arg2, int arg3) {

}

public Dao<Student,Integer> getStudentDao(){
if(mStudentDao == null){

try {
mStudentDao = this.getDao(Student.class);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return mStudentDao;
}
public Dao<MyClass,Integer> getClassDao(){
if(mClassDao == null){

try {
mClassDao = this.getDao(MyClass.class);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return mClassDao;
}
@Override
public void close(){
super.close();

mStudentDao = null;
mClassDao = null;
}
}

Service 中

<span style="font-size:10px;">public class MyService extends Service{

ORMLiteDatabaseHelper mDatabaseHelper;
Dao<Student,Integer> mStudentDao;
Dao<MyClass,Integer> mClassDao;

@Override
public void onCreate() {
super.onCreate();
mDatabaseHelper = ORMLiteDatabaseHelper.getInstance(MyService.this);
mStudentDao = mDatabaseHelper.getStudentDao();
mClassDao = mDatabaseHelper.getClassDao();

}

@Override
public IBinder onBind(Intent intent) {
return new MyBind();

}
public class MyBind extends Binder{

public MyService getService(){
return MyService.this;
}
}

public void createSqlLite(){

new Thread(new Runnable() {

@Override
public void run() {

for(int i=1;i<4;i++){

MyClass myclass = new MyClass();

myclass.myclass_id = i;
myclass.name = i+"班";

try {
mClassDao.createIfNotExists(myclass);//将数据添加到班级表格中
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String[] studentName = { "王二", "张三", "李四", "刘五", "赵六" };
String[] studentSex = { "男", "女" };
Random rand = new Random();
for(int j=0;j<5;j++){

Student stu = new Student();

stu.student_id = j;
stu.name = studentName[rand.nextInt(5)];
stu.age = 18 + j;
stu.sex = studentSex[rand.nextInt(2)];

stu.mclass = myclass;//将学生对应到所指定的班级

try {
mStudentDao.createIfNotExists(stu);//将数据添加到学生表格中
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}).start();
}

public ForeignCollection<Student> query(Integer id){

try {
MyClass mclass = mClassDao.queryForId(id);//以指定ID查找对应的班级

ForeignCollection<Student> student = mclass.student;

return student;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}</span>

创建Student类

public class Student {

public Student() {

}

@DatabaseField(generatedId = true)//generatedId = true id自增长
public long id;

@DatabaseField(columnName = "student_id")
public int student_id;

@DatabaseField(columnName = "name")
public String name;

@DatabaseField(columnName = "age")
public int age;

@DatabaseField(columnName = "sex")
public String sex;

@DatabaseField(foreign = true, foreignAutoRefresh = true)//foreign = true, foreignAutoRefresh = true固定写法
public MyClass mclass;

@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + ", sex=" + sex + "]";
}

}

创建Class类

public class MyClass {

public MyClass() {
}

@DatabaseField(id = true)
public int myclass_id;

@DatabaseField(dataType = DataType.STRING)
public String name;

@ForeignCollectionField(eager = false)
public ForeignCollection<Student> student = null;
}

MainActivity中

public class MainActivity extends Activity implements OnClickListener {

MyService mService;
ServiceConnection sc;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Button creat = (Button) findViewById(R.id.creat);
Button query = (Button) findViewById(R.id.query);

creat.setOnClickListener(this);
query.setOnClickListener(this);

sc = new ServiceConnection() {

@Override
public void onServiceDisconnected(ComponentName name) {

}

@Override
public void onServiceConnected(ComponentName name, IBinder service) {
MyBind mbind = (MyBind) service;

mService = mbind.getService();
}
};
Intent intent_bind = new Intent(this, MyService.class);
bindService(intent_bind, sc, Service.BIND_AUTO_CREATE);
}

@Override
public void onClick(View v) {

switch (v.getId()) {
case R.id.creat:

mService.createSqlLite();

break;
case R.id.query:

ForeignCollection<Student> student = mService.query(1);
for (Student stu : student) {
Log.d("", stu.name + "");
}
break;

default:
break;
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: