论坛首页 Java企业应用论坛

ibatis dao的封装讨论(欢迎拍装)

浏览 5929 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2008-09-03  
public interface iBatisDao extends Dao{
	
	public void insert(String map, IValueObject vo)  throws DataAccessException;

	public int update(String map, IValueObject vo)  throws DataAccessException;

	public int delete(String map, IValueObject vo) throws DataAccessException;

	public List find(String map, IValueObject vo) throws DataAccessException;
	
	public IValueObject get(String map,IValueObject vo) throws DataAccessException;
	
	public int count(String map,IValueObject vo) throws DataAccessException;
	
	public String sequence(String map,IValueObject vo) throws DataAccessException;
	
	public String sequence(String seqName) throws DataAccessException;
	public String sequence()  throws DataAccessException;

	/**
	 * 
	 * @param map
	 * @param vo
	 * @param startRow ��ʼȡ��ݵ�Row
	 * @param rowPerPage ÿҳ������
	 * @return
	 */
	public List pageQuery(String map, IValueObject vo,int startRow,int rowPerPage)  throws DataAccessException;

public class iBatisDaoImpl extends SqlMapClientDaoSupport implements iBatisDao {
	private final String DOT = ".";

	private static final String INSERT = "insert";
	private static final String DELETE = "delete";
	private static final String FIND = "find";
	private static final String UPDATE = "update";
	private static final String DEFAULT_SYSTEM_SQLMAP_NAMESPACE = "cn.com.pansky.domain.System";
	private static final String DEFAULT_SYSTEM_SEQENCE = "ccs_system_seq";

	protected final Log logger = LogFactory.getLog(iBatisDaoImpl.class);
	public String statementName;

	private String getStatementName() {
		return this.statementName;
	}

	/**
	 * 把命名空间和ibatis sqlmap的ID关联。
	 * 例 cn.com.pansky.domain.Users.insertUsers
	 * @param statementname
	 * @param vo
	 */
	private void setStatementName(String statementname, IValueObject vo) {
		Assert.hasText(statementname);
		if (vo instanceof IValueObject) {
			this.statementName = StringUtils.hasText(vo.getNameSpace()) ? (vo
					.getNameSpace()
					+ DOT + statementname) : statementname;
		} else {
			this.statementName = vo.getClass().getName() + DOT + statementname;
		}
		if (logger.isDebugEnabled())
			logger.debug("setStatementName->" + this.statementName);
	}

	public int delete(String map, IValueObject vo) throws DataAccessException {
		setStatementName(map, vo);
		int record = getSqlMapClientTemplate().delete(getStatementName(), vo);
		if (logger.isDebugEnabled()) {
			logger.debug("共删除" + vo.getNameSpace() + " " + record + " 条记录");
		}
		return record;

	}

	public List find(String map, IValueObject vo) throws DataAccessException {
		setStatementName(map, vo);
		return getSqlMapClientTemplate().queryForList(getStatementName(), vo);
	}

	public void insert(String map, IValueObject vo) {
		setStatementName(map, vo);
		getSqlMapClientTemplate().insert(getStatementName(), vo);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.com.pansky.dao.IbatisDao#pageQuery(java.lang.String,
	 * cn.com.pansky.dao.IValueObject, int, int)
	 */
	public List pageQuery(String map, IValueObject vo, int startRow,
			int rowPerPage) throws DataAccessException {
		setStatementName(map, vo);
		return getSqlMapClientTemplate().queryForList(getStatementName(), vo,
				startRow, rowPerPage);
	}

	public int update(String map, IValueObject vo) throws DataAccessException {
		setStatementName(map, vo);
		int record = getSqlMapClientTemplate().update(getStatementName(), vo);
		if (logger.isDebugEnabled()) {
			logger.debug("共更新" + vo.getNameSpace() + " " + record + " 条记录");
		}
		return record;
	}

	public IValueObject get(String map, IValueObject vo)
			throws DataAccessException {
		setStatementName(map, vo);
		return (IValueObject) getSqlMapClientTemplate().queryForObject(
				getStatementName(), vo);
	}

	public int count(String map, IValueObject vo) throws DataAccessException {
		setStatementName(map, vo);
		Integer result = (Integer) getSqlMapClientTemplate().queryForObject(
				getStatementName(), vo);
		return result.intValue();
	}

	/**
	 * DefaultService默认使用
	 */
	public int delete(Object vo) throws DataAccessException {
		return this.delete(DELETE, (IValueObject) vo);
	}

	public List find(Object vo) throws DataAccessException {
		return this.find(FIND, (IValueObject) vo);
	}

	public void insert(Object vo) throws DataAccessException {
		this.insert(INSERT, (IValueObject) vo);
	}

	public int update(Object vo) throws DataAccessException {
		return this.update(UPDATE, (IValueObject) vo);
	}

	/**
	 * sequence使用
	 */
	public String sequence(String map, IValueObject vo)
			throws DataAccessException {
		return (String) this.getSqlMapClientTemplate().queryForObject(map,
				vo.getSequence());
	}

	/**
	 * 通过seqName使用Sequence
	 * 
	 * @param seqName
	 * @return
	 */
	public String sequence(String seqName) throws DataAccessException {
		return (String) this.getSqlMapClientTemplate().queryForObject(
				DEFAULT_SYSTEM_SQLMAP_NAMESPACE + DOT + "sequence", seqName);
	}

	/**
	 * 使用系统默认的sequence
	 * 
	 * @return
	 */
	public String sequence() throws DataAccessException {
		return sequence(DEFAULT_SYSTEM_SEQENCE);
	}

}



public interface IValueObject {
	
	public  String getNameSpace();
	
	public String getSequence();
}


封装后,在service层只用DI一个Dao就行了,减少了写,Dao接口,Dao实现及Service层要DI N个Dao的问题。
   发表时间:2008-09-04  
没有泛型,查询不方便.

insert操作没有返回对象, 测试的时候很难对主键生成进行mock.

建议看看ss2里面的ibatis dao超类


0 请登录后投票
   发表时间:2008-09-26  
从代码量的方面考虑,确实值得参考!
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics