@linux1s1s
2016-12-02T13:11:41.000000Z
字数 4732
阅读 1978
AndroidRefine
2016-11
先普及一下数据库的关联关系,可以直接参看博文数据库设计(一对一、一对多、多对多)
相信大家在
android
开发过程中大多数项目都需要使用android的数据库sqllite
,但是每次直接利用android
提供的api来增删改查数据表有点繁琐,做javaweb
的都知道有一款数据库框架叫Hibernate
,android
端同样有类似这样的框架OrmLite
,
这里通过一个简单的栗子直观感受一下ORM框架数据持久化提供的便利
看一下gradle配置文件
//ORMLite
compile group: 'com.j256.ormlite', name: 'ormlite-android', version: '5.0'
compile 'com.j256.ormlite:ormlite-core:5.0'
获取ORMLite的maven地址可供参考
首先是POJO类
package okhttp.mutex.com.orm_demo.ormlite;
import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;
@DatabaseTable(tableName = "lite_info")
public class LiteInfo {
@DatabaseField(id = true)
private long id;
@DatabaseField
private String name;
@DatabaseField
private int age;
public LiteInfo() {
}
public LiteInfo(long id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String namel) {
this.name = namel;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "LiteInfo{" +
"id=" + id +
", namel='" + name + '\'' +
", age=" + age +
'}';
}
}
简要介绍一下上面的注解
- @DatabaseTable:表示定义了一个数据表,如果不指定名字,在Android中会以类名作为表名,如packageInfo就是SQLite数据库中的表名,我们也可以指定表名,@DatabaseTable(tableName = "tb_package") 。
- @DatabaseField:表示定义了数据中的一个字段,id表示数据中的一个主键,如果指定为generatedId,表示自动增长id,我们不需要给它赋值。其他字段,可以使用columnName来指定字段名,canBeNull表示是否为空,这些赋值可以按照以下来指定
-(id = true, canBeNull = false)
然后定义OrmLiteHelper
继承自OrmLiteSqliteOpenHelper
package okhttp.mutex.com.orm_demo.ormlite;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
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;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
public class OrmLiteHelper extends OrmLiteSqliteOpenHelper {
private static final String TAB_NAME = "orm_lite_table_name";
private static OrmLiteHelper sORMLITEHELPER;
private Map<String, Dao> mDaoMap = new HashMap<>();
private OrmLiteHelper(Context context) {
super(context, TAB_NAME, null, 1);
}
public static void createInstance(Context context) {
if (sORMLITEHELPER == null) {
synchronized (OrmLiteHelper.class) {
if (sORMLITEHELPER == null) {
sORMLITEHELPER = new OrmLiteHelper(context);
}
}
}
}
public static OrmLiteHelper getInstance() {
return sORMLITEHELPER;
}
public Dao getLiteDao(Class clazz) throws SQLException {
Dao dao = null;
String className = clazz.getSimpleName();
synchronized (this) {
if (mDaoMap.containsKey(className)) {
dao = mDaoMap.get(className);
}
if (dao == null) {
dao = super.getDao(clazz);
mDaoMap.put(className, dao);
}
}
return dao;
}
@Override
public void close() {
for (String key : mDaoMap.keySet()) {
Dao dao = mDaoMap.get(key);
dao = null;
}
super.close();
}
@Override
public void onCreate(SQLiteDatabase database, ConnectionSource connectionSource) {
try {
TableUtils.createTable(connectionSource, LiteInfo.class);
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase database, ConnectionSource connectionSource, int oldVersion, int newVersion) {
try {
TableUtils.dropTable(connectionSource, LiteInfo.class, true);
} catch (SQLException e) {
e.printStackTrace();
}
onCreate(database, connectionSource);
}
}
接着定义DAO类LiteInfoDao
package okhttp.mutex.com.orm_demo.ormlite;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.PreparedQuery;
import java.sql.SQLException;
import java.util.List;
public class LiteInfoDao {
private Dao<LiteInfo, Long> mLiteDao;
public LiteInfoDao() {
OrmLiteHelper helper = OrmLiteHelper.getInstance();
try {
mLiteDao = helper.getLiteDao(LiteInfo.class);
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* 插入一条数据
*
* @param info
*/
public void addInfo(LiteInfo info) {
if (mLiteDao == null)
return;
try {
mLiteDao.createIfNotExists(info);
} catch (SQLException e) {
e.printStackTrace();
}
}
public LiteInfo queryInfo(long id) {
try {
return mLiteDao.queryForId(id);
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
public List<LiteInfo> query() {
try {
return mLiteDao.queryForAll();
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
}
最后测试一下上面是否工作正常
public class MainActivity extends AppCompatActivity {
private LiteInfoDao mOrmDao;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mOrmDao = new LiteInfoDao();
initOrmInfo();
queryOrmOri();
queryOrmOri(1);
}
private void queryOrmOri() {
List<LiteInfo> list = mOrmDao.query();
for (LiteInfo info : list) {
Log.i("mutex", info.toString());
}
}
private void queryOrmOri(long id) {
LiteInfo info = mOrmDao.queryInfo(id);
Log.w("mutex", info.toString());
}
private void initOrmInfo() {
for (int i = 0; i < 10; i++) {
LiteInfo info = new LiteInfo(i, "name" + i, i + 20);
mOrmDao.addInfo(info);
}
}
}