日期:2014-05-16  浏览次数:20492 次

基于Spring jdbcTemple做的泛型DAO

采用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 "