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

自己写一个增强的JdbcDaoTemplate,欢迎拍砖

应用场景如下,一个实体,需要对应N个表,这N个表的结构一致,但是后缀不同。用户登录后,取得用户的组,组名即为那张表的后缀。Hibernate做这个不容易,所以考虑采用JDBC来完成。

自己胡乱写了一个增强的JdbcDaoTemplate类,可以实现一些简单的查询,不必写sql语句。

也是靠我自己的想法来写的,有什么不妥之处,欢迎拍砖。

/**
 * 提供一些简单的方法供子类使用
 * 其中RowMapper接口有3个现成的实现类,分别是:
 * 1、BeanPropertyRowMapper,提供将查询结果转换为对象,数据库列名应带下划线命名,会自动转换为骆驼命名规则的字段名
 * 2、ColumnMapRowMapper,返回一个List对象,对象中的每一个元素都是一个Map对象,Map实际上是org.apache.commons.collections.map.ListOrderedMap
 * 3、SingleColumnRowMapper,返回一个List对象,对象中的每个元素是数据库中的某列的值,SingleColumnRowMapper的构造方法可以传入需要的数据类型
 * 上述3个实现不能满足要求时,可自行实现RowMapper接口
 * @author Sunshine
 *
 */
@SuppressWarnings("unchecked")
public class BasicJdbcDaoSupport<T, PK extends Serializable> extends JdbcDaoSupport {

	protected Class<T> entityClass;

	public BasicJdbcDaoSupport() {
		this.entityClass = ReflectionUtils.getSuperClassGenricType(getClass());
	}

	@Resource(name = "secondaryDataSource")
	public void setSuperDataSource(DataSource dataSource) {
		super.setDataSource(dataSource);
	}

	public <X> X get(final PK id, final RowMapper rowMapper) {
		String sql = "select * from " + getTableName(entityClass) + " where id = ?";
		List results = getJdbcTemplate().query(sql, new Object[] { id }, rowMapper);
		return (X) DataAccessUtils.singleResult(results);
	}

	public <X> List<X> getAll(final RowMapper rowMapper) {
		String sql = "select * from " + getTableName(entityClass);
		return getJdbcTemplate().query(sql, rowMapper);
	}

	public <X> List<X> getAll(final String orderBy, final boolean isAsc, final RowMapper rowMapper) {
		String direction = (isAsc == true) ? "asc" : "desc";
		String sql = "select * from " + getTableName(entityClass) + " order by " + orderBy + " " + direction;
		return getJdbcTemplate().query(sql, rowMapper);
	}

	public <X> List<X> find(final String[] properties, final Object[] values, final RowMapper rowMapper) {
		Assert.notNull(properties, "properties cannot be null");
		Assert.notNull(values, "values cannot be null");
		if (properties.length == 0 || values.length == 0 || properties.length != values.length) {
			throw new IllegalArgumentException("invalid properties or values");
		}
		String sql = "select * from " + getTableName(entityClass) + " where ";
		String[] columns = new String[properties.length];
		for (int i = 0; i < properties.length; i++) {
			columns[i] = underscoreName(properties[i]) + " = ?"; // 将属性转换为数据库对应列名
		}
		sql += StringUtils.join(columns, " and ");
		return getJdbcTemplate().query(sql, values, rowMapper);
	}

	public <X> List<X> find(final String property, final Object value, final RowMapper rowMapper) {
		return find(new String[] { property }, new Object[] { value }, rowMapper);
	}

	public <X> List<X> find(final Map<String, Object> map, final RowMapper rowMapper) {
		List<String> properties = new ArrayList<String>();
		List<Object> values = new ArrayList<Object>();
		for (Map.Entry<String, Object> entry : map.entrySet()) {
			properties.add(entry.getKey());
			values.add(entry.getValue());
		}
		return find(properties.toArray(new String[properties.size()]), values.toArray(new Object[values.size()]),
				rowMapper);
	}

	public <X> List<X> find(final String sql, final RowMapper rowMapper) {
		return getJdbcTemplate().query(sql, rowMapper);
	}

	public <X> List<X> find(final String sql, final Object[] values, final RowMapper rowMapper) {
		return getJdbcTemplate().query(sql, values, rowMapper);
	}

	public <X> List<X> find(final String property, final List<?> list, final RowMapper rowMapper) {
		if (list.size() > 0) {
			List<String> placeholders = new ArrayList<String>();
			for (int i = 0; i < list.size(); i++) {
				placeholders.add("?");
			}
			String sql = "select * from " + getTableName(entityClass) + " where " + underscoreName(property) + " in ";
			sql += "