采用Java的反射机制和Spring jdbcTemplate完成的泛型DAO
public interface BaseDao<T, PK extends Serializable> { /** * add one record function * * @param entity * @Description: */ public int add(T entity); /** * add multi record function * * @param entityCollection * @Description: */ public int addList(Collection<T> entityCollection, Class<T> entityClazz); /** * delete one record function * * @param entity * @Description: */ public int delete(T entity); /** * delete one record by id * * @param entityClass * @param id * @Description: */ public int deleteById(Class<T> entityClass, PK id); /** * delete many record function * * @param entityCollection * @param entityClazz * @Description: */ public int deleteList(Collection<T> entityCollection, Class<T> entityClazz); /** * update the record * * @param entity * @Description: */ public int update(T entity); /** * update many records meanwhile * * @param entityCollection * @param entityClazz * @Description: */ public int updateList(Collection<T> entityCollection, Class<T> entityClazz); /** * query all record list * * @param entityClazz * @return * @Description: */ public List<T> findAll(Class<T> entityClazz); /** * find one special record * * @return * @Description: */ public T findById(Class<T> entityClass, PK id); }
@Repository("baseDao") public class BaseDaoImpl<T, PK extends Serializable> extends SimpleDaoSupport implements BaseDao<T, PK> { private final String TABLE_PREFIX = "pub_"; /** * @return * @Description: */ protected SimpleJdbcInsert getSimpleJdbcInsert() { return new SimpleJdbcInsert(this.getJdbcTemplate()); } /* * (non-Javadoc) * * @see com.morningstar.planning.dao.BaseDao#add(java.lang.Class) */ @Override public int add(T entity) { String tableName = this.TABLE_PREFIX + entity.getClass().getSimpleName(); SimpleJdbcInsert insertActor = getSimpleJdbcInsert(); insertActor.setTableName(tableName.toLowerCase()); return insertActor.execute(new BeanPropertySqlParameterSource(entity)); } /* * (non-Javadoc) * * @see com.morningstar.planning.dao.BaseDao#addAll(java.util.Collection) */ @Override public int addList(Collection<T> entityCollection, Class<T> entityClazz) { String tableName = this.TABLE_PREFIX + entityClazz.getSimpleName(); SimpleJdbcInsert insertActor = getSimpleJdbcInsert(); SqlParameterSource[] batchArgs = SqlParameterSourceUtils.createBatch(entityCollection.toArray()); insertActor.setTableName(tableName.toLowerCase()); int[] result = insertActor.executeBatch(batchArgs); return result.length; } /* * (non-Javadoc) * * @see com.morningstar.planning.dao.BaseDao#delete(java.lang.Object) */ @Override public int delete(T entity) { String tableName = this.TABLE_PREFIX + entity.getClass().getSimpleName(); String sql = "DELETE FROM " + tableName + " WHERE id =:id"; return this.getSimpleJdbcTemplate().update(sql, new BeanPropertySqlParameterSource(entity)); } /* * (non-Javadoc) * * @see com.morningstar.planning.dao.BaseDao#deleteById(java.lang.Class, * java.io.Serializable) */ @Override public int deleteById(Class<T> entityClazz, PK id) { String tableName = this.TABLE_PREFIX + entityClazz.getSimpleName(); String sql = "DELETE FROM " + tableName + " WHERE id=?"; return this.getSimpleJdbcTemplate().update(sql, id); } /* * (non-Javadoc) * * @see * com.morningstar.planning.dao.BaseDao#deleteList(java.util.Collection, * java.lang.Class) */ @Override public int deleteList(Collection<T> entityCollection, Class<T> entityClazz) { String tableName = this.TABLE_PREFIX + entityClazz.getSimpleName(); String sql = "DELETE FROM " + tableName + " WHERE id=:id"; SqlParameterSource[] batchArgs = SqlParameterSourceUtils.createBatch(entityCollection.toArray()); int[] result = this.getSimpleJdbcTemplate().batchUpdate(sql, batchArgs); return result.length; } /* * (non-Javadoc) * * @see com.morningstar.planning.dao.BaseDao#update(java.lang.Object) */ @Override @SuppressWarnings("rawtypes") public int update(T entity) { String tableName = this.TABLE_PREFIX + entity.getClass().getSimpleName(); StringBuffer sql = new StringBuffer("UPDATE " + tableName + " SET "); Field[] fields = entity.getClass().getDeclaredFields(); for (int i = 0; i < fields.length; i++) { Field fied = fields[i]; String fiedName = fied.getName(); Class fiedType = fied.getType(); if (!fiedName.equalsIgnoreCase("id") && !fiedType.equals(Collections.class) && !fiedType.equals(Map.class) && !fiedType.equals(List.class) && !fiedType.equals(Set.class)) { sql.append(fiedName + " = :" + fiedName + ","); } } sql.replace(sql.lastIndexOf(","), sql.length(), ""); sql.append(" WHERE id = :id"); SqlParameterSource ps = new BeanPropertySqlParameterSource(entity); return this.getSimpleJdbcTemplate().update(sql.toString(), ps); } /* * (non-Javadoc) * * @see * com.morningstar.planning.dao.BaseDao#updateList(java.util.Collection, * java.lang.Class) */ @Override @SuppressWarnings("rawtypes") public int updateList(Collection<T> entityCollection, Class<T> entityClazz) { String tableName = this.TABLE_PREFIX + entityClazz.getSimpleName(); StringBuffer sql = new StringBuffer("UPDATE " + tableName + " SET "); Field[] fields = entityClazz.getDeclaredFields(); for (int i = 0; i < fields.length; i++) { Field fied = fields[i]; String fiedName = fied.getName(); Class fiedType = fied.getType(); if (!fiedName.equalsIgnoreCase("id") && !fiedType.equals(Collections.class) && !fiedType.equals(Map.class) && !fiedType.equals(List.class) && !fiedType.equals(Set.class)) { sql.append(fiedName + " = :" + fiedName + ","); } } sql.replace(sql.lastIndexOf(","), sql.length(), ""); sql.append(" WHERE id = :id"); SqlParameterSource[] batchArgs = SqlParameterSourceUtils.createBatch(entityCollection.toArray()); int[] result = this.getSimpleJdbcTemplate().batchUpdate(sql.toString(), batchArgs); return result.length; } /* * (non-Javadoc) * * @see com.morningstar.planning.dao.BaseDao#findAll(java.lang.Class) */ @Override public List<T> findAll(Class<T> entityClazz) { String tableName = this.TABLE_PREFIX + entityClazz.getSimpleName(); String sql = "SELECT * FROM " + tableName; return this.getSimpleJdbcTemplate().query(sql, BeanPropertyRowMapper.newInstance(entityClazz)); } /* * (non-Javadoc) * * @see com.morningstar.planning.dao.BaseDao#findById(java.lang.Class, * java.io.Serializable) */ @Override public T findById(Class<T> entityClazz, PK id) { String tableName = this.TABLE_PREFIX + entityClazz.getSimpleName(); String sql = "SELECT * FROM " + tableName + " WHERE id=?"; return this.getSimpleJdbcTemplate().queryForObject(sql, BeanPropertyRowMapper.newInstance(entityClazz), id); } }
/** * * 继承SimpleJdbcDaoSupport类,实现初始化的注入方式 * 目的是所有的继承SimpleDaoSupport类的方法,不需要注入数据源 * 皆采用注解的方式进行配置,不需要在xml中再进行手动注入。 * * @Resource 进行注入,或采用名字的方式都可以,本程序用的是name名字的方式 * @Resource(name = "unitTestDB") * */ public class SimpleDaoSupport extends SimpleJdbcDaoSupport { @Resource(name = "unitTestDB") protected void inject(DataSource dataSource) { super.setDataSource(dataSource); } }