Android 使用com.j256.ormlite

时间:2021-04-29 14:46:51

在web开发中经常采用的hibernate,在android也提供了一个ormlite

导入所需jar包后

/**
 * SQLiteHelperOrm.java
 * 版权所有(C) 2014
 * 创建者:cuiran 2014-2-12 下午3:18:40
 */
package com.ghyf.mplay.database;

import java.sql.SQLException;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.ghyf.mplay.application.BaseCookie;
import com.ghyf.mplay.po.POPriorityText;
import com.ghyf.mplay.po.POTest;
import com.ghyf.mplay.util.LogUtil;
import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;

/**
 * TODO
 * @author cuiran
 * @version 1.0.0
 */
public class SQLiteHelperOrm extends OrmLiteSqliteOpenHelper {
	private static final String TAG="SQLiteHelperOrm";
	private static final String DATABASE_NAME = "mplay.db";
	private static final int DATABASE_VERSION = 3;

	public SQLiteHelperOrm(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	public SQLiteHelperOrm() {
		super(BaseCookie.getContext(), DATABASE_NAME, null, DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db, ConnectionSource connectionSource) {
		try {
			TableUtils.createTable(connectionSource, POTest.class);
			TableUtils.createTable(connectionSource, POPriorityText.class);
		} catch (SQLException e) {
			LogUtil.e(TAG,"onCreate",e);
		}
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, ConnectionSource connectionSource, int arg2, int arg3) {
		try {
			TableUtils.dropTable(connectionSource, POTest.class, true);
			TableUtils.dropTable(connectionSource, POPriorityText.class, true);
			onCreate(db, connectionSource);
		} catch (SQLException e) {
			LogUtil.e(TAG,"onUpgrade",e);
		}
	}

}

/**
 * DbHelper.java
 * 版权所有(C) 2014
 * 创建者:cuiran 2014-2-12 下午3:37:07
 */
package com.ghyf.mplay.database;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.annotation.TargetApi;
import android.content.ContentValues;
import android.os.Build;

import com.ghyf.mplay.util.LogUtil;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.UpdateBuilder;

/**
 *  DbHelper 数据库操作类
 * @author cuiran
 * @version 1.0.0
 */
public class DbHelper<T> {
	private static final String TAG="DbHelper";
	/** 新增一条记录 */
	public int create(T po) {
		SQLiteHelperOrm db = new SQLiteHelperOrm();
		try {
			Dao dao = db.getDao(po.getClass());
			return dao.create(po);
		} catch (SQLException e) {
			LogUtil.e(TAG,"create",e);
		} finally {
			if (db != null)
				db.close();
		}
		return -1;
	}

	public boolean exists(T po, Map<String, Object> where) {
		SQLiteHelperOrm db = new SQLiteHelperOrm();
		try {
			Dao dao = db.getDao(po.getClass());
			if (dao.queryForFieldValues(where).size() > 0) {
				return true;
			}
		} catch (SQLException e) {
			LogUtil.e(TAG,"exists",e);
		} finally {
			if (db != null)
				db.close();
		}
		return false;
	}

	public int createIfNotExists(T po, Map<String, Object> where) {
		SQLiteHelperOrm db = new SQLiteHelperOrm();
		try {
			Dao dao = db.getDao(po.getClass());
			if (dao.queryForFieldValues(where).size() < 1) {
				return dao.create(po);
			}
		} catch (SQLException e) {
			LogUtil.e(TAG,"createIfNotExists",e);
		} finally {
			if (db != null)
				db.close();
		}
		return -1;
	}

	/** 查询一条记录 */
	public List<T> queryForEq(Class<T> c, String fieldName, Object value) {
		SQLiteHelperOrm db = new SQLiteHelperOrm();
		try {
			Dao dao = db.getDao(c);
			return dao.queryForEq(fieldName, value);
		} catch (SQLException e) {
			LogUtil.e(TAG,"queryForEq",e);
		} finally {
			if (db != null)
				db.close();
		}
		return new ArrayList<T>();
	}

	/** 删除一条记录 */
	public int remove(T po) {
		SQLiteHelperOrm db = new SQLiteHelperOrm();
		try {
			Dao dao = db.getDao(po.getClass());
			return dao.delete(po);
		} catch (SQLException e) {
			LogUtil.e(TAG,"remove",e);
		} finally {
			if (db != null)
				db.close();
		}
		return -1;
	}

	/**
	 * 根据特定条件更新特定字段
	 *
	 * @param c
	 * @param values
	 * @param columnName where字段
	 * @param value where值
	 * @return
	 */
	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	public int update(Class<T> c, ContentValues values, String columnName, Object value) {
		SQLiteHelperOrm db = new SQLiteHelperOrm();
		try {
			Dao dao = db.getDao(c);
			UpdateBuilder<T, Long> updateBuilder = dao.updateBuilder();
			updateBuilder.where().eq(columnName, value);
			for (String key : values.keySet()) {
				updateBuilder.updateColumnValue(key, values.get(key));
			}
			return updateBuilder.update();
		} catch (SQLException e) {
			LogUtil.e(TAG,"update",e);
		} finally {
			if (db != null)
				db.close();
		}
		return -1;
	}

	/** 更新一条记录 */
	public int update(T po) {
		SQLiteHelperOrm db = new SQLiteHelperOrm();
		try {

			Dao dao = db.getDao(po.getClass());
			return dao.update(po);
		} catch (SQLException e) {
			LogUtil.e(TAG,"update",e);
		} finally {
			if (db != null)
				db.close();
		}
		return -1;
	}

	/** 查询所有记录 */
	public List<T> queryForAll(Class<T> c) {
		SQLiteHelperOrm db = new SQLiteHelperOrm();
		try {
			Dao dao = db.getDao(c);
			return dao.queryForAll();
		} catch (SQLException e) {
			LogUtil.e(TAG,"queryForAll",e);
		} finally {
			if (db != null)
				db.close();
		}
		return new ArrayList<T>();
	}

}

新建一个PO

/**
 * POTest.java
 * 版权所有(C) 2014
 * 创建者:cuiran 2014-2-12 下午3:25:08
 */
package com.ghyf.mplay.po;

import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;

/**
 *  POTest DB的测试PO
 * @author cuiran
 * @version 1.0.0
 */
@DatabaseTable(tableName = "test")
public class POTest {

	@DatabaseField(generatedId = true)
	public long _id;
	/** 标题 */
	@DatabaseField
	public String title;
	/** 标题 */
	@DatabaseField
	public int position;
	public POTest(long _id, String title, int position) {
		super();
		this._id = _id;
		this.title = title;
		this.position = position;
	}
	public POTest() {
		super();
	}

}