android orm持久层框架

时间:2022-04-25
本文章向大家介绍android orm持久层框架,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
android数据库开发  
 
Android中内置了sqlite,但是常用的开发语言java是面向对象的,而数据库是关系型的,二者之间的转化每次都很麻烦(主要是我对sql语言不熟悉)。而Java Web开发中有很多orm框架,但是想直接放到Android上用有些麻烦。尝试了一下找Android的orm框架,说实话还有好几个。  
 
实现考虑的是:androrm  
官网:http://androrm.the-pixelpla.net/ 
说实话,这个我实在没有弄懂,一共两个包。  
一个是依赖包:Apache Commons - Lang (2.6)  
另外一个就是主包:androrm.jar   不管怎么下载的都不能使用...  
 
然后有考虑了一下db4o  
官网:http://www.db4o.com/ 
官网上的介绍说是已经支持Android了,但是我一是觉得包有点大,而是觉得速度有点慢  
 
最后看到的就是ormlite  
官网:http://ormlite.com/ 
一共两个包:一个是ormlite-core-4.24.jar,另一个是ormlite-android-4.24.jar  
从以下网址可以下载到:http://ormlite.com/releases/ 
 
下面按照惯例来个Hello world  
新建Android项目:HelloOrmLite  
 
 
添加文件夹:libs,将所需的两个包复制到其中。添加引用  
 
新建一个model:Hello.java  
 
 
package cn.sdx.model;   
 
import com.j256.ormlite.field.DatabaseField;   
 
public class Hello {   
@DatabaseField(generatedId = true)   
int id;   
@DatabaseField 
String word;   
 
public Hello() {   
}   
 
@Override 
public String toString() {   
  StringBuilder sb = new StringBuilder();   
  sb.append("id=").append(id);   
  sb.append(" ,word=").append(word);   
 return sb.toString();   
}   
 
}   
 
@DatabaseField是声明id为数据库字段,generatedId =true声明id为自增长  
然后重写了toString()  
 
再添加一个DataHelper.java  
 
 
package cn.sdx.utils;   
 
import java.sql.SQLException;   
 
import android.content.Context;   
import android.database.sqlite.SQLiteDatabase;   
import android.util.Log;   
 
 
import cn.sdx.model.Hello;   
 
import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;   
import com.j256.ormlite.dao.Dao;   
import com.j256.ormlite.support.ConnectionSource;   
import com.j256.ormlite.table.TableUtils;   
 
public class DataHelper extends OrmLiteSqliteOpenHelper {   
 
private static final String DATABASE_NAME = "HelloOrmlite.db";   
private static final int DATABASE_VERSION = 1;   
private Dao<Hello, Integer> helloDao = null;   
 
public DataHelper(Context context) {   
 super(context, DATABASE_NAME, null, DATABASE_VERSION);   
}   
 
@Override 
public void onCreate(SQLiteDatabase db, ConnectionSource connectionSource) {   
 try {   
   TableUtils.createTable(connectionSource, Hello.class);   
  } catch (SQLException e) {   
   Log.e(DataHelper.class.getName(), "创建数据库失败", e);   
   e.printStackTrace();   
  }   
}   
 
@Override 
public void onUpgrade(SQLiteDatabase db, ConnectionSource connectionSource, int arg2,   
 int arg3) {   
 try {   
   TableUtils.dropTable(connectionSource, Hello.class, true);   
   onCreate(db, connectionSource);   
  } catch (SQLException e) {   
   Log.e(DataHelper.class.getName(), "更新数据库失败", e);   
   e.printStackTrace();   
  }   
}   
 
@Override 
public void close() {   
 super.close();   
  helloDao = null;   
}   
 
public Dao<Hello, Integer> getHelloDataDao() throws SQLException {   
 if (helloDao == null) {   
   helloDao = getDao(Hello.class);   
  }   
 return helloDao;   
}   
}   
 
 
在布局文件中添加一个TextView  
HelloOrmliteActivity.java中添加对数据库的操作  
 
代码如下:  
 
 
package cn.sdx;   
 
import java.sql.SQLException;   
import java.util.List;   
 
import com.j256.ormlite.android.apptools.OrmLiteBaseActivity;   
import com.j256.ormlite.dao.Dao;   
 
import android.os.Bundle;   
import android.widget.TextView;   
import cn.sdx.model.Hello;   
import cn.sdx.utils.DataHelper;   
 
public class HelloOrmliteActivity extends OrmLiteBaseActivity<DataHelper> {   
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) {   
 super.onCreate(savedInstanceState);   
  setContentView(R.layout.main);   
  TextView tv = (TextView) this.findViewById(R.id.output);   
 try {   
   Dao<Hello, Integer> helloDao = getHelper().getHelloDataDao();   
 // 添加数据  
 for (int i = 0; i < 2; i++) {   
    Hello hello = new Hello("Hello" + i);   
    helloDao.create(hello);   
   }   
   tv.setText(tv.getText() + "n" + "添加数据完成");   
 // 查询添加的数据  
   List<Hello> hellos = helloDao.queryForAll();   
 for (Hello h : hellos) {   
    tv.setText(tv.getText() + "n" + h.toString());   
   }   
 // 删除数据第一条数据  
   helloDao.delete(hellos.get(0));   
   tv.setText(tv.getText() + "n" + "删除数据完成");   
 // 重新查询数据  
   hellos = helloDao.queryForAll();   
 for (Hello h : hellos) {   
    tv.setText(tv.getText() + "n" + h.toString());   
   }   
 // 修改数据  
   Hello h1 = hellos.get(0);   
   h1.setWord("这是修改过的数据");   
   tv.setText(tv.getText() + "n" + "修改数据完成");   
   helloDao.update(h1);   
 // 重新查询数据  
   hellos = helloDao.queryForAll();   
 for (Hello h : hellos) {   
    tv.setText(tv.getText() + "n" + h.toString());   
   }   
 
  } catch (SQLException e) {   
 // TODO Auto-generated catch block  
   e.printStackTrace();   
  }   
 
}   
}   
 
 
 
以上实现了数据库操作相关的增删改,下面是效果:  
 
 
 
 
   OrmLite的功能非常强大,Model类的声明中非常重要,外键约束,非空检查等等问题都有相对的处理方法。