使用ibatis操作数据库的封装

Posted on

使用ibatis操作数据库的封装

使用ibatis操作数据库的封装**

近期刚进入公司,也是本人的第一份正式工作,公司使用的ORM框架是ibatis,下面代码是对batis dao的一个封装,主要继承自spring的SqlMapClientDaoSupport,负责为单个Entity 提供CRUD操作的IBatis DAO基类,使用该基类,可以减少不少代码量。

view plaincopy to clipboardprint?

  1. package com.nfschina.utils.dao.ibatis

  2. import java.io.Serializable;

  3. import java.sql.Connection;

  4. import java.sql.ResultSet;

  5. import java.sql.SQLException;

  6. import java.util.HashMap;

  7. import java.util.List;

  8. import java.util.Map;

  9. import org.apache.commons.beanutils.PropertyUtils;

  10. import org.apache.commons.lang.StringUtils;

  11. import org.springframework.util.Assert;

  12. import com.ibatis.sqlmap.engine.impl.SqlMapClientImpl;

  13. import com.ibatis.sqlmap.engine.mapping.sql.stat.StaticSql;

  14. import com.ibatis.sqlmap.engine.mapping.statement.MappedStatement;

  15. importcom.nfschina.utils.BaseException;

  16. import com.nfschina.utils.DataPage;

  17. import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport

  18. ///

  19. /* IBatis Dao的泛型基类.

  20. /* 继承于Spring的SqlMapClientDaoSupport,提供分页函数和若干便捷查询方法,并对返回值作了泛型类型转换.

  21. /*/

  22. @SuppressWarnings("unchecked")

  23. public class IBatisGenericDao extends SqlMapClientDaoSupport {

  24. public static final String POSTFIX_INSERT = ".insert";

  25. public static final String POSTFIX_UPDATE = ".update";

  26. public static final String POSTFIX_DELETE = ".delete";

  27. public static final String POSTFIX_DELETE_PRIAMARYKEY = ".deleteByPrimaryKey";

  28. public static final String POSTFIX_SELECT = ".select";

  29. public static final String POSTFIX_SELECTMAP = ".selectByMap";

  30. public static final String POSTFIX_SELECTSQL = ".selectBySql";

  31. public static final String POSTFIX_COUNT = ".count";

  32. ///

  33. /* 根据ID获取对象

  34. /*

  35. /* @throws BaseException

  36. /* @throws SQLException

  37. /*/

  38. public T get(Class entityClass, Serializable id) throws BaseException, SQLException {

  39. T o = (T) getSqlMapClient().queryForObject(entityClass.getName() + POSTFIX_SELECT, id);

  40. if (o == null)

  41. throw new BaseException(BaseException.DATA_NOTFOUND, "未找到实体: " + id);

  42. return o;

  43. }

  44. ///

  45. /* 获取全部对象

  46. /* @throws SQLException

  47. /*/

  48. public List getAll(Class entityClass) throws SQLException {

  49. return getSqlMapClient().queryForList(entityClass.getName() + POSTFIX_SELECT, null);

  50. }

  51. ///

  52. /* 新增对象

  53. /* @throws SQLException

  54. /*/

  55. public void insert(Object o) throws SQLException {

  56. getSqlMapClient().insert(o.getClass().getName() + POSTFIX_INSERT, o);

  57. }

  58. ///

  59. /* 保存对象

  60. /* @throws SQLException

  61. /*/

  62. public int update(Object o) throws SQLException {

  63. return getSqlMapClient().update(o.getClass().getName() + POSTFIX_UPDATE, o);

  64. }

  65. ///

  66. /* 删除对象

  67. /* @throws SQLException

  68. /*/

  69. public int remove(Object o) throws SQLException {

  70. return getSqlMapClient().delete(o.getClass().getName() + POSTFIX_DELETE, o);

  71. }

  72. ///

  73. /* 根据ID删除对象

  74. /* @throws SQLException

  75. /*/

  76. public int removeById(Class entityClass, Serializable id) throws SQLException {

  77. return getSqlMapClient().delete(entityClass.getName() + POSTFIX_DELETE_PRIAMARYKEY, id);

  78. }

  79. ///

  80. /* map查询.

  81. /*

  82. /* @param map

  83. /* 包含各种属性的查询

  84. /* @throws SQLException

  85. /*/

  86. public List find(Class entityClass, Map map) throws SQLException {

  87. if (map == null)

  88. return this.getSqlMapClient().queryForList(entityClass.getName() + POSTFIX_SELECT, null);

  89. else {

  90. map.put("findBy", "True");

  91. return this.getSqlMapClient()

  92. .queryForList(entityClass.getName() + POSTFIX_SELECTMAP, map);

  93. }

  94. }

  95. ///

  96. /* sql 查询.

  97. /*

  98. /* @param sql

  99. /* 直接sql的语句(需要防止注入式攻击)

  100. /* @throws SQLException

  101. /*/

  102. public List find(Class entityClass, String sql) throws SQLException {

  103. Assert.hasText(sql);

  104. if (StringUtils.isEmpty(sql))

  105. return this.getSqlMapClient().queryForList(entityClass.getName() + POSTFIX_SELECT, null);

  106. else

  107. return this.getSqlMapClient()

  108. .queryForList(entityClass.getName() + POSTFIX_SELECTSQL, sql);

  109. }

  110. ///

  111. /* 根据属性名和属性值查询对象.

  112. /*

  113. /* @return 符合条件的对象列表

  114. /* @throws SQLException

  115. /*/

  116. public List findBy(Class entityClass, String name, Object value) throws SQLException {

  117. Assert.hasText(name);

  118. Map map = new HashMap();

  119. map.put(name, value);

  120. return find(entityClass, map);

  121. }

  122. ///

  123. /* 根据属性名和属性值查询对象.

  124. /*

  125. /* @return 符合条件的唯一对象

  126. /*/

  127. public T findUniqueBy(Class entityClass, String name, Object value) {

  128. Assert.hasText(name);

  129. Map map = new HashMap();

  130. try {

  131. PropertyUtils.getProperty(entityClass.newInstance(), name);

  132. map.put(name, value);

  133. map.put("findUniqueBy", "True");

  134. return (T) getSqlMapClient().queryForObject(entityClass.getName() + POSTFIX_SELECTMAP,

  135. map);

  136. } catch (Exception e) {

  137. logger.error("Error when propertie on entity," + e.getMessage(), e.getCause());

  138. return null;

  139. }

  140. }

  141. ///

  142. /* 根据属性名和属性值以Like AnyWhere方式查询对象.

  143. /* @throws SQLException

  144. /*/

  145. public List findByLike(Class entityClass, String name, String value) throws SQLException {

  146. Assert.hasText(name);

  147. Map map = new HashMap();

  148. map.put(name, value);

  149. map.put("findLikeBy", "True");

  150. return getSqlMapClient().queryForList(entityClass.getName() + POSTFIX_SELECTMAP, map);

  151. }

  152. ///

  153. /* 判断对象某些属性的值在数据库中不存在重复

  154. /*

  155. /* @param tableName

  156. /* 数据表名字

  157. /* @param names

  158. /* 在POJO里不能重复的属性列表,以逗号分割 如"name,loginid,password"

  159. /* FIXME how about in different schema?

  160. /*/

  161. public boolean isNotUnique(Object entity, String tableName, String names) {

  162. try {

  163. String primarykey;

  164. Connection con = getSqlMapClient().getCurrentConnection();

  165. ResultSet dbMetaData = con.getMetaData().getPrimaryKeys(con.getCatalog(), null, tableName);

  166. dbMetaData.next();

  167. if (dbMetaData.getRow() > 0) {

  168. primarykey = dbMetaData.getString(4);

  169. if (names.indexOf(primarykey) > -1)

  170. return false;

  171. } else {

  172. return true;

  173. }

  174. } catch (SQLException e) {

  175. logger.error(e.getMessage(), e);

  176. return false;

  177. }

  178. return false;

  179. }

  180. ///

  181. /* 分页查询函数,使用PaginatedList.

  182. /*

  183. /* @param pageNo

  184. /* 页号,从0开始.

  185. /* @throws SQLException

  186. /*/

  187. @SuppressWarnings("rawtypes")

  188. public DataPage pagedQuery(String sqlName, HashMap hashMap, Integer pageNo, Integer pageSize)

  189. throws SQLException {

  190. if (pageNo == null || pageSize == null) {

  191. List list = getSqlMapClient().queryForList(sqlName, hashMap);

  192. if (list == null || list.size() == 0) {

  193. return new DataPage();

  194. } else {

  195. return new DataPage(0, list.size(), list.size(), list);

  196. }

  197. } else {

  198. Assert.hasText(sqlName);

  199. Assert.isTrue(pageNo >= 1, "pageNo should start from 1");

  200. // Count查询

  201. Integer totalCount = (Integer) getSqlMapClient().queryForObject(sqlName + ".Count", hashMap);

  202. if (totalCount < 1) {

  203. return new DataPage();

  204. }

  205. // 实际查询返回分页对象

  206. int startIndex = DataPage.getStartOfPage(pageNo, pageSize);

  207. hashMap.put("startIndex", startIndex);

  208. hashMap.put("pageSize", pageSize);

  209. List list = getSqlMapClient().queryForList(sqlName, hashMap);

  210. return new DataPage(startIndex, totalCount, pageSize, list);

  211. }

  212. }

  213. public String getMappedSQL(String sqlName) {

  214. String sql = null;

  215. SqlMapClientImpl sqlmap = (SqlMapClientImpl) getSqlMapClient();

  216. MappedStatement stmt = sqlmap.getMappedStatement(sqlName);

  217. StaticSql staticSql = (StaticSql) stmt.getSql();

  218. sql = staticSql.getSql(null, null);

  219. return sql;

  220. }

  221. }

package com.nfschina.utils.dao.ibatis import java.io.Serializable; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.commons.beanutils.PropertyUtils; import org.apache.commons.lang.StringUtils; import org.springframework.util.Assert; import com.ibatis.sqlmap.engine.impl.SqlMapClientImpl; import com.ibatis.sqlmap.engine.mapping.sql.stat.StaticSql; import com.ibatis.sqlmap.engine.mapping.statement.MappedStatement; importcom.nfschina.utils.BaseException; import com.nfschina.utils.DataPage; import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport /// / IBatis Dao的泛型基类. / 继承于Spring的SqlMapClientDaoSupport,提供分页函数和若干便捷查询方法,并对返回值作了泛型类型转换. // @SuppressWarnings("unchecked") public class IBatisGenericDao extends SqlMapClientDaoSupport { public static final String POSTFIX_INSERT = ".insert"; public static final String POSTFIX_UPDATE = ".update"; public static final String POSTFIX_DELETE = ".delete"; public static final String POSTFIX_DELETE_PRIAMARYKEY = ".deleteByPrimaryKey"; public static final String POSTFIX_SELECT = ".select"; public static final String POSTFIX_SELECTMAP = ".selectByMap"; public static final String POSTFIX_SELECTSQL = ".selectBySql"; public static final String POSTFIX_COUNT = ".count"; /// / 根据ID获取对象 / / @throws BaseException / @throws SQLException // public T get(Class entityClass, Serializable id) throws BaseException, SQLException { T o = (T) getSqlMapClient().queryForObject(entityClass.getName() + POSTFIX_SELECT, id); if (o == null) throw new BaseException(BaseException.DATA_NOTFOUND, "未找到实体: " + id); return o; } /// / 获取全部对象 / @throws SQLException // public List getAll(Class entityClass) throws SQLException { return getSqlMapClient().queryForList(entityClass.getName() + POSTFIX_SELECT, null); } /// / 新增对象 / @throws SQLException // public void insert(Object o) throws SQLException { getSqlMapClient().insert(o.getClass().getName() + POSTFIX_INSERT, o); } /// / 保存对象 / @throws SQLException // public int update(Object o) throws SQLException { return getSqlMapClient().update(o.getClass().getName() + POSTFIX_UPDATE, o); } /// / 删除对象 / @throws SQLException // public int remove(Object o) throws SQLException { return getSqlMapClient().delete(o.getClass().getName() + POSTFIX_DELETE, o); } /// / 根据ID删除对象 / @throws SQLException // public int removeById(Class entityClass, Serializable id) throws SQLException { return getSqlMapClient().delete(entityClass.getName() + POSTFIX_DELETE_PRIAMARYKEY, id); } /// / map查询. / / @param map / 包含各种属性的查询 / @throws SQLException // public List find(Class entityClass, Map map) throws SQLException { if (map == null) return this.getSqlMapClient().queryForList(entityClass.getName() + POSTFIX_SELECT, null); else { map.put("findBy", "True"); return this.getSqlMapClient() .queryForList(entityClass.getName() + POSTFIX_SELECTMAP, map); } } /// / sql 查询. / / @param sql / 直接sql的语句(需要防止注入式攻击) / @throws SQLException // public List find(Class entityClass, String sql) throws SQLException { Assert.hasText(sql); if (StringUtils.isEmpty(sql)) return this.getSqlMapClient().queryForList(entityClass.getName() + POSTFIX_SELECT, null); else return this.getSqlMapClient() .queryForList(entityClass.getName() + POSTFIX_SELECTSQL, sql); } /// / 根据属性名和属性值查询对象. / / @return 符合条件的对象列表 / @throws SQLException // public List findBy(Class entityClass, String name, Object value) throws SQLException { Assert.hasText(name); Map map = new HashMap(); map.put(name, value); return find(entityClass, map); } /// / 根据属性名和属性值查询对象. / / @return 符合条件的唯一对象 // public T findUniqueBy(Class entityClass, String name, Object value) { Assert.hasText(name); Map map = new HashMap(); try { PropertyUtils.getProperty(entityClass.newInstance(), name); map.put(name, value); map.put("findUniqueBy", "True"); return (T) getSqlMapClient().queryForObject(entityClass.getName() + POSTFIX_SELECTMAP, map); } catch (Exception e) { logger.error("Error when propertie on entity," + e.getMessage(), e.getCause()); return null; } } /// / 根据属性名和属性值以Like AnyWhere方式查询对象. / @throws SQLException // public List findByLike(Class entityClass, String name, String value) throws SQLException { Assert.hasText(name); Map map = new HashMap(); map.put(name, value); map.put("findLikeBy", "True"); return getSqlMapClient().queryForList(entityClass.getName() + POSTFIX_SELECTMAP, map); } /// / 判断对象某些属性的值在数据库中不存在重复 / / @param tableName / 数据表名字 / @param names / 在POJO里不能重复的属性列表,以逗号分割 如"name,loginid,password"
/
FIXME how about in different schema? // public boolean isNotUnique(Object entity, String tableName, String names) { try { String primarykey; Connection con = getSqlMapClient().getCurrentConnection(); ResultSet dbMetaData = con.getMetaData().getPrimaryKeys(con.getCatalog(), null, tableName); dbMetaData.next(); if (dbMetaData.getRow() > 0) { primarykey = dbMetaData.getString(4); if (names.indexOf(primarykey) > -1) return false; } else { return true; } } catch (SQLException e) { logger.error(e.getMessage(), e); return false; } return false; } /// / 分页查询函数,使用PaginatedList. / / @param pageNo / 页号,从0开始. / @throws SQLException /*/ @SuppressWarnings("rawtypes") public DataPage pagedQuery(String sqlName, HashMap hashMap, Integer pageNo, Integer pageSize) throws SQLException { if (pageNo == null || pageSize == null) { List list = getSqlMapClient().queryForList(sqlName, hashMap); if (list == null || list.size() == 0) { return new DataPage(); } else { return new DataPage(0, list.size(), list.size(), list); } } else { Assert.hasText(sqlName); Assert.isTrue(pageNo >= 1, "pageNo should start from 1"); // Count查询 Integer totalCount = (Integer) getSqlMapClient().queryForObject(sqlName + ".Count", hashMap); if (totalCount < 1) { return new DataPage(); } // 实际查询返回分页对象 int startIndex = DataPage.getStartOfPage(pageNo, pageSize); hashMap.put("startIndex", startIndex); hashMap.put("pageSize", pageSize); List list = getSqlMapClient().queryForList(sqlName, hashMap); return new DataPage(startIndex, totalCount, pageSize, list); } } public String getMappedSQL(String sqlName) { String sql = null; SqlMapClientImpl sqlmap = (SqlMapClientImpl) getSqlMapClient(); MappedStatement stmt = sqlmap.getMappedStatement(sqlName); StaticSql staticSql = (StaticSql) stmt.getSql(); sql = staticSql.getSql(null, null); return sql; } }

view plaincopy to clipboardprint?

  1. package com.nfschina.utils.dao.ibatis;

  2. import java.io.Serializable;

  3. import java.lang.reflect.InvocationTargetException;

  4. import java.sql.SQLException;

  5. import java.util.List;

  6. import java.util.Map;

  7. import org.apache.commons.beanutils.PropertyUtils;

  8. import org.apache.commons.lang.StringUtils;

  9. import org.springframework.orm.ObjectRetrievalFailureException;

  10. import com.nfschina.utils.BaseException;

  11. import com.nfschina.utils.GenericsUtils;

  12. import

  13. ///

  14. /* 负责为单个Entity 提供CRUD操作的IBatis DAO基类.

  15. /*

  16. /* 子类只要在类定义时指定所管理Entity的Class, 即拥有对单个Entity对象的CRUD操作.

  17. /*

  18. /*

     

  19. /* public class UserManagerIbatis extends IBatisEntityDao {

  20. /* }

  21. /*

  22. /*/

  23. public class IBatisEntityDao extends IBatisGenericDao {

  24. ///

  25. /* DAO所管理的Entity类型.

  26. /*/

  27. protected Class entityClass;

  28. protected String primaryKeyName;

  29. ///

  30. /* 在构造函数中将泛型T.class赋给entityClass.

  31. /*/

  32. @SuppressWarnings("unchecked")

  33. public IBatisEntityDao() {

  34. entityClass = (Class) GenericsUtils.getSuperClassGenricType(getClass());

  35. }

  36. ///

  37. /* 根据属性名和属性值查询对象.

  38. /*

  39. /* @return 符合条件的对象列表

  40. /* @throws SQLException

  41. /*/

  42. public List findBy(String name, Object value) throws SQLException {

  43. return findBy(getEntityClass(), name, value);

  44. }

  45. ///

  46. /* 根据属性的名值对查询对象

  47. /* @param map

  48. /* @return

  49. /* @throws SQLException

  50. /*/

  51. public List find(Map map) throws SQLException{

  52. return find(getEntityClass(), map);

  53. }

  54. ///

  55. /* 根据属性的名值对查询唯一对象

  56. /* @param map

  57. /* @return

  58. /* @throws SQLException

  59. /*/

  60. public T findUniqueByMap(Map map) throws SQLException{

  61. List list = find(getEntityClass(), map);

  62. if(list == null || list.size() <= 0){

  63. return null;

  64. }

  65. return list.get(0);

  66. }

  67. ///

  68. /* 根据属性名和属性值以Like AnyWhere方式查询对象.

  69. /* @throws SQLException

  70. /*/

  71. public List findByLike(String name, String value) throws SQLException {

  72. return findByLike(getEntityClass(), name, value);

  73. }

  74. ///

  75. /* 根据属性名和属性值查询单个对象.

  76. /*

  77. /* @return 符合条件的唯一对象

  78. /*/

  79. public T findUniqueBy(String name, Object value) {

  80. return findUniqueBy(getEntityClass(), name, value);

  81. }

  82. ///

  83. /* 根据ID获取对象.

  84. /*

  85. /* @throws BaseException

  86. /* @throws SQLException

  87. /*/

  88. public T get(Serializable id) throws BaseException, SQLException {

  89. return get(getEntityClass(), id);

  90. }

  91. ///

  92. /* 获取全部对象.

  93. /* @throws SQLException

  94. /*/

  95. public List getAll() throws SQLException {

  96. return getAll(getEntityClass());

  97. }

  98. ///

  99. /* 取得entityClass.

  100. /*

  101. /* JDK1.4不支持泛型的子类可以抛开Class entityClass,重载此函数达到相同效果。

  102. /*/

  103. protected Class getEntityClass() {

  104. return entityClass;

  105. }

  106. public String getPrimaryKeyName() {

  107. if (StringUtils.isEmpty(primaryKeyName))

  108. primaryKeyName = "id";

  109. return primaryKeyName;

  110. }

  111. protected Object getPrimaryKeyValue(Object o) throws NoSuchMethodException, IllegalAccessException,

  112. InvocationTargetException, InstantiationException {

  113. return PropertyUtils.getProperty(entityClass.newInstance(), getPrimaryKeyName());

  114. }

  115. ///

  116. /* 根据ID移除对象.

  117. /* @throws SQLException

  118. /*/

  119. public int removeById(Serializable id) throws SQLException {

  120. return removeById(getEntityClass(), id);

  121. }

  122. ///

  123. /* 保存对象. 为了实现IEntityDao 我在内部使用了insert和upate 2个方法.

  124. /* @throws SQLException

  125. /*/

  126. public void saveOrUpdate(Object o) throws SQLException {

  127. Object primaryKey;

  128. try {

  129. primaryKey = getPrimaryKeyValue(o);

  130. } catch (Exception e) {

  131. throw new ObjectRetrievalFailureException(entityClass, e);

  132. }

  133. if (primaryKey == null)

  134. insert(o);

  135. else

  136. update(o);

  137. }

  138. public void setPrimaryKeyName(String primaryKeyName) {

  139. this.primaryKeyName = primaryKeyName;

  140. }

  141. public String getIdName(Class<?> clazz) {

  142. return "id";

  143. }

  144. }

package com.nfschina.utils.dao.ibatis; import java.io.Serializable; import java.lang.reflect.InvocationTargetException; import java.sql.SQLException; import java.util.List; import java.util.Map; import org.apache.commons.beanutils.PropertyUtils; import org.apache.commons.lang.StringUtils; import org.springframework.orm.ObjectRetrievalFailureException; import com.nfschina.utils.BaseException; import com.nfschina.utils.GenericsUtils; import /// / 负责为单个Entity 提供CRUD操作的IBatis DAO基类. /

/ 子类只要在类定义时指定所管理Entity的Class, 即拥有对单个Entity对象的CRUD操作. / /

 / public class UserManagerIbatis extends IBatisEntityDao { / } / 
// public class IBatisEntityDao extends IBatisGenericDao { /// / DAO所管理的Entity类型. // protected Class entityClass; protected String primaryKeyName; /// / 在构造函数中将泛型T.class赋给entityClass. // @SuppressWarnings("unchecked") public IBatisEntityDao() { entityClass = (Class) GenericsUtils.getSuperClassGenricType(getClass()); } /// / 根据属性名和属性值查询对象. / / @return 符合条件的对象列表 / @throws SQLException // public List findBy(String name, Object value) throws SQLException { return findBy(getEntityClass(), name, value); } /// / 根据属性的名值对查询对象 / @param map / @return / @throws SQLException // public List find(Map map) throws SQLException{ return find(getEntityClass(), map); } /// / 根据属性的名值对查询唯一对象 / @param map / @return / @throws SQLException // public T findUniqueByMap(Map map) throws SQLException{ List list = find(getEntityClass(), map); if(list == null || list.size() <= 0){ return null; } return list.get(0); } /// / 根据属性名和属性值以Like AnyWhere方式查询对象. / @throws SQLException // public List findByLike(String name, String value) throws SQLException { return findByLike(getEntityClass(), name, value); } /// / 根据属性名和属性值查询单个对象. / / @return 符合条件的唯一对象 // public T findUniqueBy(String name, Object value) { return findUniqueBy(getEntityClass(), name, value); } /// / 根据ID获取对象. / / @throws BaseException / @throws SQLException // public T get(Serializable id) throws BaseException, SQLException { return get(getEntityClass(), id); } /// / 获取全部对象. / @throws SQLException // public List getAll() throws SQLException { return getAll(getEntityClass()); } /// / 取得entityClass. /

/ JDK1.4不支持泛型的子类可以抛开Class entityClass,重载此函数达到相同效果。 // protected Class getEntityClass() { return entityClass; } public String getPrimaryKeyName() { if (StringUtils.isEmpty(primaryKeyName)) primaryKeyName = "id"; return primaryKeyName; } protected Object getPrimaryKeyValue(Object o) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException { return PropertyUtils.getProperty(entityClass.newInstance(), getPrimaryKeyName()); } /// / 根据ID移除对象. / @throws SQLException // public int removeById(Serializable id) throws SQLException { return removeById(getEntityClass(), id); } /// / 保存对象. 为了实现IEntityDao 我在内部使用了insert和upate 2个方法. / @throws SQLException /*/ public void saveOrUpdate(Object o) throws SQLException { Object primaryKey; try { primaryKey = getPrimaryKeyValue(o); } catch (Exception e) { throw new ObjectRetrievalFailureException(entityClass, e); } if (primaryKey == null) insert(o); else update(o); } public void setPrimaryKeyName(String primaryKeyName) { this.primaryKeyName = primaryKeyName; } public String getIdName(Class<?> clazz) { return "id"; } }

希望本站内容对您有点用处,有什么疑问或建议请在后面留言评论
转载请注明作者(RobinChia)和出处 It so life ,请勿用于任何商业用途