xUtils系列之DbUtils-增,删,更新,替换操作
来源:互联网 发布:java图形程序设计 编辑:程序博客网 时间:2024/06/05 02:17
DbUtils实现了很方便的数据操作,基本一行代码就能搞定,所以实在是没啥写的,姑且贴下相关代码,方便之后查看.
增:
<span style="font-size:14px;">public void save(Object entity) throws DbException { try { beginTransaction(); createTableIfNotExist(entity.getClass()); execNonQuery(SqlInfoBuilder.buildInsertSqlInfo(this, entity)); setTransactionSuccessful(); } finally { endTransaction(); }}public void saveAll(List<?> entities) throws DbException { if (entities == null || entities.size() == 0) return; try { beginTransaction(); createTableIfNotExist(entities.get(0).getClass()); for (Object entity : entities) { execNonQuery(SqlInfoBuilder.buildInsertSqlInfo(this, entity)); } setTransactionSuccessful(); } finally { endTransaction(); }}</span>下面这两个函数和save类似,不同的是,如果id是自增,DbUtils会自动给对象的id字段赋值.
public boolean saveBindingId(Object entity) throws DbException { boolean result = false; try { beginTransaction(); createTableIfNotExist(entity.getClass()); result = saveBindingIdWithoutTransaction(entity); setTransactionSuccessful(); } finally { endTransaction(); } return result;}public void saveBindingIdAll(List<?> entities) throws DbException { if (entities == null || entities.size() == 0) return; try { beginTransaction(); createTableIfNotExist(entities.get(0).getClass()); for (Object entity : entities) { if (!saveBindingIdWithoutTransaction(entity)) { throw new DbException("saveBindingId error, transaction will not commit!"); } } setTransactionSuccessful(); } finally { endTransaction(); }}当不确定是保存还是更新时,可以用下面两个函数:
public void saveOrUpdate(Object entity) throws DbException { try { beginTransaction(); createTableIfNotExist(entity.getClass()); saveOrUpdateWithoutTransaction(entity); setTransactionSuccessful(); } finally { endTransaction(); }}public void saveOrUpdateAll(List<?> entities) throws DbException { if (entities == null || entities.size() == 0) return; try { beginTransaction(); createTableIfNotExist(entities.get(0).getClass()); for (Object entity : entities) { saveOrUpdateWithoutTransaction(entity); } setTransactionSuccessful(); } finally { endTransaction(); }}
删:
<span style="font-size:14px;">public void deleteById(Class<?> entityType, Object idValue) throws DbException { if (!tableIsExist(entityType)) return; try { beginTransaction(); execNonQuery(SqlInfoBuilder.buildDeleteSqlInfo(this, entityType, idValue)); setTransactionSuccessful(); } finally { endTransaction(); }}public void delete(Object entity) throws DbException { if (!tableIsExist(entity.getClass())) return; try { beginTransaction(); execNonQuery(SqlInfoBuilder.buildDeleteSqlInfo(this, entity)); setTransactionSuccessful(); } finally { endTransaction(); }}public void delete(Class<?> entityType, WhereBuilder whereBuilder) throws DbException { if (!tableIsExist(entityType)) return; try { beginTransaction(); execNonQuery(SqlInfoBuilder.buildDeleteSqlInfo(this, entityType, whereBuilder)); setTransactionSuccessful(); } finally { endTransaction(); }}public void deleteAll(List<?> entities) throws DbException { if (entities == null || entities.size() == 0 || !tableIsExist(entities.get(0).getClass())) return; try { beginTransaction(); for (Object entity : entities) { execNonQuery(SqlInfoBuilder.buildDeleteSqlInfo(this, entity)); } setTransactionSuccessful(); } finally { endTransaction(); }}public void deleteAll(Class<?> entityType) throws DbException { delete(entityType, null);}</span>更新:
public void update(Object entity, String... updateColumnNames) throws DbException { if (!tableIsExist(entity.getClass())) return; try { beginTransaction(); execNonQuery(SqlInfoBuilder.buildUpdateSqlInfo(this, entity, updateColumnNames)); setTransactionSuccessful(); } finally { endTransaction(); }}public void update(Object entity, WhereBuilder whereBuilder, String... updateColumnNames) throws DbException { if (!tableIsExist(entity.getClass())) return; try { beginTransaction(); execNonQuery(SqlInfoBuilder.buildUpdateSqlInfo(this, entity, whereBuilder, updateColumnNames)); setTransactionSuccessful(); } finally { endTransaction(); }}public void updateAll(List<?> entities, String... updateColumnNames) throws DbException { if (entities == null || entities.size() == 0 || !tableIsExist(entities.get(0).getClass())) return; try { beginTransaction(); for (Object entity : entities) { execNonQuery(SqlInfoBuilder.buildUpdateSqlInfo(this, entity, updateColumnNames)); } setTransactionSuccessful(); } finally { endTransaction(); }}public void updateAll(List<?> entities, WhereBuilder whereBuilder, String... updateColumnNames) throws DbException { if (entities == null || entities.size() == 0 || !tableIsExist(entities.get(0).getClass())) return; try { beginTransaction(); for (Object entity : entities) { execNonQuery(SqlInfoBuilder.buildUpdateSqlInfo(this, entity, whereBuilder, updateColumnNames)); } setTransactionSuccessful(); } finally { endTransaction(); }}替换:(替换和更新类似,但是替换会先删除原表匹配的数据项,然后增加新的数据项)
<span style="font-size:14px;">public void replace(Object entity) throws DbException { try { beginTransaction(); createTableIfNotExist(entity.getClass()); execNonQuery(SqlInfoBuilder.buildReplaceSqlInfo(this, entity)); setTransactionSuccessful(); } finally { endTransaction(); }}public void replaceAll(List<?> entities) throws DbException { if (entities == null || entities.size() == 0) return; try { beginTransaction(); createTableIfNotExist(entities.get(0).getClass()); for (Object entity : entities) { execNonQuery(SqlInfoBuilder.buildReplaceSqlInfo(this, entity)); } setTransactionSuccessful(); } finally { endTransaction(); }}</span>
0 0
- xUtils系列之DbUtils-增,删,更新,替换操作
- xUtils系列之DbUtils-概览
- xUtils系列之DbUtils-查询
- xUtils系列之DbUtils-Id,NoAutoIncrement注解
- xUtils系列之DbUtils-Check注解
- xUtils系列之DbUtils-Column注解
- xUtils系列之DbUtils-Table注解
- xUtils系列之DbUtils-Transient注解
- xUtils系列之DbUtils-Unique注解
- xUtils系列之DbUtils-NotNull注解
- xUtils系列之DbUtils-Foreign注解
- xUtils系列之DbUtils-Finder注解
- xUtils系列之DbUtils-保存自定义类型
- xUtils系列之DbUtils-保存自定义类型
- xUtils框架之DbUtils
- xUtils系列之DbUtils-开启Sqlite3外键约束
- XUtils之ViewUtils和DBUtils
- Xutils 中 对数据库的操作 DButils
- LINUX兴趣小组面试总结
- jar -help
- HDU 4455 线段树
- android学习笔记 内容提供者
- 用类加载器的方式管理资源和配置文件
- xUtils系列之DbUtils-增,删,更新,替换操作
- POJ 2406 KMP
- cf-538B Quasi Binary【贪心】
- POJ 2823 单调队列
- POJ 3321 树状数组
- UML简单介绍(二)——UML中的图及其特征
- POJ 3461 KMP
- JAR WAR EAR包的区别
- e5-5uva12096