小弟最近做项目,在用IBATIS写DAO层代码,想写以个公共的方法,只需给出一个参数,就能根据此参数查找相应表的记录,并返回结果,求高手指导,小弟新手。不胜感谢!
这种是将table表名当作参数传入
select * from $value$
这种是将table表名和where条件用map包装后传入
select * from $table$ where $condition$
以上两种写法里,都要用$$包含变量名,而不是##。
看看下面的程序代码,不知能否对楼主有所帮助
IBaseDao.java
[code="java"]
public interface IBaseDao {
public Object getObject(String statementId, Object paramObject)
throws SQLException;
@SuppressWarnings("unchecked")
public List getList(String statementId, Object paramObject)
throws SQLException;
public Integer getCount(String statementId, Object paramObject)
throws SQLException;
public Object insert(String statementId, Object paramObject)
throws SQLException;
@SuppressWarnings("unchecked")
public int insertForBatch(String statementId, List paramList)
throws SQLException;
public int delete(String statementId, Object paramObject)
throws SQLException;
@SuppressWarnings("unchecked")
public int deleteForBatch(String statementId, List paramList)
throws SQLException;
public int update(String statementId, Object paramObject)
throws SQLException;
@SuppressWarnings("unchecked")
public int updateForBatch(String statementId, List paramList)
throws SQLException;
}
[/code]
BaseDao.java
[code="java"]public class BaseDao extends SqlMapClientDaoSupport implements IBaseDao {
@SuppressWarnings("unchecked")
@Override
public int deleteForBatch(String statementId, List paramList)
throws SQLException {
getSqlMapClient().startBatch();
for (Iterator i = paramList.iterator(); i.hasNext();) {
getSqlMapClientTemplate().delete(statementId, i.hasNext());
}
return getSqlMapClient().executeBatch();
}
@Override
public Integer getCount(String statementId, Object paramObject)
throws SQLException {
return (Integer) getSqlMapClientTemplate().queryForObject(statementId,
paramObject);
}
@SuppressWarnings("unchecked")
@Override
public List getList(String statementId, Object paramObject)
throws SQLException {
return getSqlMapClientTemplate().queryForList(statementId, paramObject);
}
@Override
public Object getObject(String statementId, Object paramObject)
throws SQLException {
return getSqlMapClientTemplate().queryForObject(statementId,
paramObject);
}
@SuppressWarnings("unchecked")
@Override
public int insertForBatch(String statementId, List paramList)
throws SQLException {
getSqlMapClient().startBatch();
for (Object obj : paramList) {
getSqlMapClientTemplate().insert(statementId, obj);
}
return getSqlMapClient().executeBatch();
}
@SuppressWarnings("unchecked")
@Override
public int updateForBatch(String statementId, List paramList)
throws SQLException {
getSqlMapClient().startBatch();
for (Object obj : paramList) {
getSqlMapClientTemplate().update(statementId, obj);
}
return getSqlMapClient().executeBatch();
}
@Override
public int delete(String statementId, Object paramObject)
throws SQLException {
return getSqlMapClientTemplate().delete(statementId, paramObject);
}
@Override
public Object insert(String statementId, Object paramObject)
throws SQLException {
return getSqlMapClientTemplate().insert(statementId, paramObject);
}
@Override
public int update(String statementId, Object paramObject)
throws SQLException {
return getSqlMapClientTemplate().update(statementId, paramObject);
}
}[/code]
sevice层的调用
YtManage.java
[code="java"]public class YtManage {
private IBaseDao baseDao;
private static final String COMMIDITY_SELECT = "commidity.select";
public BussinessModel selece(BussinessModel model) throws Throwable {
return (BussinessModel) baseDao.getObject(COMMIDITY_SELECT, model);
}
/**
* @param baseDao
* the baseDao to set
*/
public void setBaseDao(IBaseDao baseDao) {
this.baseDao = baseDao;
}
}
[/code]