http://blog.csdn.net/lmj623565791/article/details/39122981
http://www.2cto.com/kf/201505/400136.html
http://blog.csdn.net/OyangYujun/article/details/45938905
http://ormlite.com/javadoc/ormlite-core/doc-files/ormlite_2.html#DAO-Usage
compile 'com.j256.ormlite:ormlite-android:5.0'
@DatabaseTable(tableName = CRM_DT_BAOGAO_FB4.TABLE_NAME)
public class CRM_DT_BAOGAO_FB4 {
public CRM_DT_BAOGAO_FB4() {
}
@Override
public String toString() {
return super.toString();
}
public static final String TABLE_NAME = "CRM_DT_BAOGAO_FB4";
@DatabaseField(columnName = "JYJL_ID")
private String JYJL_ID;
@DatabaseField(columnName = "ATT1")
private String ATT1;
public String getJYJL_ID() {
return JYJL_ID;
}
public void setJYJL_ID(String JYJL_ID) {
this.JYJL_ID = JYJL_ID;
}
public String getATT1() {
return ATT1;
}
public void setATT1(String ATT1) {
this.ATT1 = ATT1;
}
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource) {
Log.d("myorm", "表开始新建");
try {
TableUtils.createTable(connectionSource, CRM_DT_BAOGAO_FB1.class); //附录
Log.d("MyOrm", "表创建成功");
} catch (SQLException e) {
Log.d("MyOrm", "表创建失败");
e.printStackTrace();
}
}
public class CRM_DT_BAOGAO_FB4_Dao extends BaseDao<CRM_DT_BAOGAO_FB4,Integer> {
public CRM_DT_BAOGAO_FB4_Dao(Context context) {
super(context);
}
@Override
public Dao<CRM_DT_BAOGAO_FB4, Integer> getDao() {
try {
return getHelper().getDao(CRM_DT_BAOGAO_FB4.class);
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
}
public abstract class BaseDao<T, ID> {
private DatabaseHelper helper;
public BaseDao(Context context) {
helper = DatabaseHelper.getInstance(context);
}
public DatabaseHelper getHelper() {
return helper;
}
//两个泛型约束 一个是对应的实体类类型,一个是主键类型
public abstract Dao<T, ID> getDao();
//条件查询
public List<T> queryForEq(String fieldName, Object value) {
List<T> list = new ArrayList<>();
try {
list = getDao().queryForEq(fieldName, value);
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
public int add(T t) {
try {
return getDao().create(t);
} catch (SQLException e) {
e.printStackTrace();
}
return -1;
}
public int addList(List<T> list){
try {
return getDao().create(list);
} catch (SQLException e) {
e.printStackTrace();
}
return -1;
}
public List<T> getAll(){
List<T> list = new ArrayList<>();
try {
list = getDao().queryForAll();
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
public T get(ID id){
try {
return getDao().queryForId(id);
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
//会全部更新 方法很蠢 前提获取所有字段信息 遗弃不用
public int update(T t){
try {
return getDao().update(t);
} catch (SQLException e) {
e.printStackTrace();
}
return -1;
}
//在DAO的函数不满足你的灵活性时,可能要用到原生更新语句,更新语句必须包含保留关键字 INSERT, DELETE,或者UPDATE,
//(sql语句,参数)
//String... 可变长度参数列表 new String[]{"",""}
public int updateRaw(String statement,String... arguments) {
try {
return getDao().updateRaw(statement, arguments);
} catch (SQLException e) {
e.printStackTrace();
}
return -1;
}
//更新ID,其他值不变
public int updateId(T t, ID id) {
try {
return getDao().updateId(t, id);
} catch (SQLException e) {
e.printStackTrace();
}
return -1;
}
/**
* 删除该id的数据
*/
public int delete(ID id){
try {
return getDao().deleteById(id);
} catch (SQLException e) {
e.printStackTrace();
}
return -1;
}
/**
* 删除所有
*/
public int deleteAll() {
try {
return getDao().deleteBuilder().delete();
} catch (SQLException e) {
e.printStackTrace();
}
return -1;
}
}
CRM_DT_BAOGAO_FB4_Dao dao4 = new CRM_DT_BAOGAO_FB4_Dao(getActivity());
List<CRM_DT_BAOGAO_FB4> list4 = new ArrayList<>();
list4 = dao4.queryForEq("JYJL_ID", ID);
if (list4.size() != 0) {
table.setATT1(list4.get(0).getATT1() == null ? "" : list4.get(0).getATT1());
}
http://blog.csdn.net/lmj623565791/article/details/39122981
http://www.2cto.com/kf/201505/400136.html
http://blog.csdn.net/OyangYujun/article/details/45938905
http://ormlite.com/javadoc/ormlite-core/doc-files/ormlite_2.html#DAO-Usage
转载于:https://blog.51cto.com/session/1743947
http://www.ormlite.com/
3.封装与使用
下面我以一个Java Bean为例,简单的对ormlite进行封装。以下是一个聊天消息的JavaBean 取名为ChatMsgEntity
我们不需要在类的前面添加注解,此时,该类映射的数据库表的名称就是类的名称。我们在需要入库的每一个java bean的Field上添加注解,表示这对应着数据库中的字段。对于自增加的主键,我们使用generatedId = truepackage edu.njupt.zhb.model; import com.j256.ormlite.field.DatabaseField; /** * 一个聊天消息的JavaBean *@author: ZhengHaibo *web: http://blog.csdn.net/nuptboyzhb *mail: zhb931706659@126.com *2013-12-23 Nanjing,njupt,China */ public class ChatMsgEntity { @DatabaseField(generatedId = true) private int id; @DatabaseField private String nickName;//群组的昵称 @DatabaseField private String name;// 消息来自 @DatabaseField private String date;// 消息日期 @DatabaseField private String message;// 消息内容 @DatabaseField private int msgType;// 消息类型 @DatabaseField private boolean recv = true;// 是否为收到的消息 public int getMsgType() { return msgType; } public ChatMsgEntity() { } public ChatMsgEntity(String name, String date, String message, int msgType, boolean recv) { super(); this.name = name; this.date = date; this.message = message; this.msgType = msgType; this.recv = recv; } public boolean isRecv() { return recv; } public void setRecv(boolean recv) { this.recv = recv; } public void setMsgType(int msgType) { this.msgType = msgType; } public String getName() { return name; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getNickName() { return nickName; } public void setNickName(String nickName) { this.nickName = nickName; } public void setName(String name) { this.name = name; } public String getDate() { return date; } public void setDate(String date) { this.date = date; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } }
实现自己的OrmLiteSqliteOpenHelperpackage edu.njupt.zhb.dao; import java.sql.SQLException; import java.util.concurrent.atomic.AtomicInteger; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.util.Log; 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 edu.njupt.zhb.model.ChatMsgEntity; /** * Database helper class used to manage the creation and upgrading of your database. This class also usually provides * the DAOs used by the other classes. */ public class MsgDatabaseHelper extends OrmLiteSqliteOpenHelper { // name of the database file for your application -- change to something appropriate for your app private static final String DATABASE_NAME = "chatmsg.db"; // any time you make changes to your database objects, you may have to increase the database version private static final int DATABASE_VERSION = 3; // the DAO object we use to access the ChatMsgEntity table private Dao<ChatMsgEntity, Integer> simpleDao = null; private static final AtomicInteger usageCounter = new AtomicInteger(0); // we do this so there is only one helper private static MsgDatabaseHelper helper = null; private MsgDatabaseHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } /** * Get the helper, possibly constructing it if necessary. For each call to this method, there should be 1 and only 1 * call to {@link #close()}. */ public static synchronized MsgDatabaseHelper getHelper(Context context) { if (helper == null) { helper = new MsgDatabaseHelper(context); } usageCounter.incrementAndGet(); return helper; } /** * This is called when the database is first created. Usually you should call createTable statements here to create * the tables that will store your data. */ @Override public void onCreate(SQLiteDatabase db, ConnectionSource connectionSource) { try { Log.i(MsgDatabaseHelper.class.getName(), "onCreate"); TableUtils.createTable(connectionSource, ChatMsgEntity.class); } catch (SQLException e) { Log.e(MsgDatabaseHelper.class.getName(), "Can't create database", e); throw new RuntimeException(e); } } /** * This is called when your application is upgraded and it has a higher version number. This allows you to adjust * the various data to match the new version number. */ @Override public void onUpgrade(SQLiteDatabase db, ConnectionSource connectionSource, int oldVersion, int newVersion) { try { Log.i(MsgDatabaseHelper.class.getName(), "onUpgrade"); TableUtils.dropTable(connectionSource, ChatMsgEntity.class, true); // after we drop the old databases, we create the new ones onCreate(db, connectionSource); } catch (SQLException e) { Log.e(MsgDatabaseHelper.class.getName(), "Can't drop databases", e); throw new RuntimeException(e); } } /** * Returns the Database Access Object (DAO) for our ChatMsgEntity class. It will create it or just give the cached * value. */ public Dao<ChatMsgEntity, Integer> getChatMsgEntityDao() throws SQLException { if (simpleDao == null) { simpleDao = getDao(ChatMsgEntity.class); } return simpleDao; } /** * Close the database connections and clear any cached DAOs. For each call to {@link #getHelper(Context)}, there * should be 1 and only 1 call to this method. If there were 3 calls to {@link #getHelper(Context)} then on the 3rd * call to this method, the helper and the underlying database connections will be closed. */ @Override public void close() { if (usageCounter.decrementAndGet() == 0) { super.close(); simpleDao = null; helper = null; } } }
定义Dao接口/* * $filename: OrmSqliteDao.java,v $ * $Date: 2013-12-25 $ * Copyright (C) ZhengHaibo, Inc. All rights reserved. * This software is Made by Zhenghaibo. */ package edu.njupt.zhb.dao; import java.util.List; import android.R.bool; /* *@author: ZhengHaibo *web: http://blog.csdn.net/nuptboyzhb *mail: zhb931706659@126.com *2013-12-25 Nanjing,njupt,China */ public interface OrmSqliteDao<T>{ public boolean save(T object); public boolean saveOrUpdate(T object); public List<T> find(); public boolean update(T object); public boolean delete(T object); public boolean deleteAll(); public boolean executeSql(String sql); public T findById(); }
实现OrmLiteDao的封装/* * $filename: ChatMsgEntityDao.java,v $ * $Date: 2013-12-25 $ * Copyright (C) ZhengHaibo, Inc. All rights reserved. * This software is Made by Zhenghaibo. */ package edu.njupt.zhb.dao; import java.sql.SQLException; import java.util.List; import android.content.Context; import com.j256.ormlite.dao.Dao; import edu.njupt.zhb.model.ChatMsgEntity; /* *@author: ZhengHaibo *web: http://blog.csdn.net/nuptboyzhb *mail: zhb931706659@126.com *2013-12-25 Nanjing,njupt,China */ public class ChatMsgEntityDao implements OrmSqliteDao<ChatMsgEntity> { private Dao<ChatMsgEntity, Integer> simpleDao = null; public ChatMsgEntityDao(Context context) { // TODO Auto-generated constructor stub try { simpleDao = MsgDatabaseHelper.getHelper(context).getChatMsgEntityDao(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Override public boolean save(ChatMsgEntity object) { // TODO Auto-generated method stub try { simpleDao.create(object); return true; } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return false; } @Override public List<ChatMsgEntity> find() { // TODO Auto-generated method stub try { List<ChatMsgEntity> result = simpleDao.queryForAll(); return result; } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } @Override public boolean update(ChatMsgEntity object) { // TODO Auto-generated method stub try { simpleDao.update(object); return true; } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return false; } @Override public boolean delete(ChatMsgEntity object) { // TODO Auto-generated method stub try { simpleDao.delete(object); return true; } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return false; } @Override public ChatMsgEntity findById() { // TODO Auto-generated method stub return null; } @Override public boolean deleteAll() { // TODO Auto-generated method stub try { List<ChatMsgEntity> list = simpleDao.queryForAll(); for(ChatMsgEntity msg:list){ delete(msg); } return true; } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return false; } @Override public boolean saveOrUpdate(ChatMsgEntity object) { // TODO Auto-generated method stub try { simpleDao.createOrUpdate(object); return true; } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return false; } @Override public boolean executeSql(String sql) { // TODO Auto-generated method stub try { simpleDao.executeRaw(sql); return true; } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return false; } }
在Activity中使用Dao对象OrmSqliteDao<ChatMsgEntity> msgDao = new ChatMsgEntityDao(this);
然后就可以使用msgDao对ChatMsgEntity实体类进行“增删改查”的操作了。
软件简介
ORMLite是一个轻量级的Java对象关系映射持久层框架。支持包括 MySQL、Postgres、Microsoft SQL
Server、H2、Derby、HSQLDB和Sqlite等在内的数据库。提供灵活的QueryBuilder来构建复杂的数据查询。强大的抽象DAO类,只需5行代码便能够自动生成SQL来创建和删除数据库表格。
示例代码:
public class AccountApp {
public static void main(String[] args) throws Exception {
// this uses h2 by default but change to match your database
String databaseUrl = “jdbc:h2:mem:account”;
// create a connection source to our database
ConnectionSource connectionSource = new JdbcConnectionSource(databaseUrl);
// instantiate the dao
AccountDaoImpl accountDao = new AccountDaoImpl(connectionSource);
// if you need to create the ‘accounts’ table make this call
TableUtils.createTable(connectionSource, Account.class);