Apache iBatis(现已迁至Google Code下发展,更名为MyBatis)是当前IT项目中使用很广泛的一个半自动ORM框架,区别于Hibernate之类的全自动框架,iBatis对数据库的操作拥有更加灵活的控制,对于那些经常需要调用本地数据库函数自定义SQL语句,或是喜欢自己优化SQL执行效率的开发者来说,iBatis是一个非常不错的选择。
而得到广泛应用的开源企业架构Springframework,也很好的将其进行了集成,使得iBatis在 Springframework中的使用更加便利、快捷。开发者所要做的就是继承Springframework中提供的SqlMapClientDaoSupport类即可。下面将简单介绍使用spring中集成的ibatis进行项目中dao层基类封装,以方便开发。
1、SqlMapClientFactoryBean 的装配
SqlMapClientFactoryBean是SqlMapClientTemplate使用的基础,如果在Springframework应用中没有装配SqlMapClientFactoryBean,那么SqlMapClientTemplate将不可用,报空指针错误。其配置信息如下:
/WEB-INF/classes/org/bussiness/config/ibatis/SqlMapConfig.xml
2、继承使用SqlMapClientDaoSupport类
2.1)首先定义一个IbaseDao接口提供各种场景的查询、修改、删除、分页查询的各种抽象功能方法
package org.biframework.dao.ibatis;
import com.ibatis.common.util.PaginatedList;
import java.util.List;
import org.biframework.exception.DaoException;
public abstract interface IbaseDao
{
public abstract Object getObject(String paramString, Object paramObject)
throws DaoException;
@SuppressWarnings("unchecked")
public abstract List getList(String paramString, Object paramObject)
throws DaoException;
public abstract PaginatedList getPgntList(String paramString1, Object paramObject, String paramString2)
throws DaoException;
public abstract PaginatedList getPgntList(String paramString1, Object paramObject, String paramString2, int paramInt)
throws DaoException;
@SuppressWarnings("unchecked")
public abstract List getListUseSameStmt(String paramString, Object[] paramArrayOfObject)
throws DaoException;
public abstract int update(String paramString, Object paramObject)
throws DaoException;
public abstract int transUpdateSameOpt(String paramString, Object[] paramArrayOfObject)
throws DaoException;
public abstract int transUpdate(Object[][] paramArrayOfObject)
throws DaoException;
@SuppressWarnings("unchecked")
public List getList(String statementName, Object parameterObject,
int skipResults, int maxResults) throws DaoException;
}
备注:该层也可以不写
2.2)继承使用SqlMapClientDaoSupport类并实现IbaseDao接口
package org.biframework.dao.ibatis;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.biframework.exception.DaoException;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
import com.ibatis.common.util.PaginatedList;
public class baseDao extends SqlMapClientDaoSupport implements IbaseDao {
@SuppressWarnings("unused")
private static Log log;
protected static final int PAGE_SIZE = 15;
@SuppressWarnings("unchecked")
static Class class$0;
public baseDao() {
}
@SuppressWarnings("unchecked")
public List getList(String statementName, Object parameterObject)
throws DaoException {
List list = getSqlMapClientTemplate().queryForList(statementName,
parameterObject);
return list;
}
@SuppressWarnings("unchecked")
public List getListUseSameStmt(String statementName, Object objectParam[])
throws DaoException {
List list = null;
List temp = null;
if (statementName == null || objectParam == null|| objectParam.length == 0){
return list;
}else{
for (int i = 0; i < objectParam.length; i++) {
if (list == null){
list = new ArrayList();
temp = getSqlMapClientTemplate().queryForList(statementName,objectParam[i]);
}
if (temp != null){
list.addAll(temp);
}
}
}
return list;
}
@SuppressWarnings("unchecked")
public Object getObject(String statementName, Object parameterObject)
throws DaoException {
Object result = null;
List list = getSqlMapClientTemplate().queryForList(statementName,parameterObject);
if (list != null && list.size() > 0){
result = list.get(0);
}
return result;
}
public PaginatedList getPgntList(String statementName,
Object parameterObject, String pageDirection) throws DaoException {
PaginatedList list = getSqlMapClientTemplate().queryForPaginatedList(
statementName, parameterObject, 15);
if ("next".equals(pageDirection))
list.nextPage();
else if ("previous".equals(pageDirection))
list.previousPage();
else if ("first".equals(pageDirection))
list.isFirstPage();
else if ("last".equals(pageDirection))
list.isLastPage();
return list;
}
public PaginatedList getPgntList(String statementName,
Object parameterObject, String pageDirection, int pageSize)
throws DaoException {
PaginatedList list = getSqlMapClientTemplate().queryForPaginatedList(
statementName, parameterObject, pageSize);
if ("next".equals(pageDirection)) {
System.out.println("下一页");
list.nextPage();
} else if ("previous".equals(pageDirection)) {
System.out.println("上一页");
list.previousPage();
} else if ("first".equals(pageDirection)) {
System.out.println("首页");
list.isFirstPage();
} else if ("last".equals(pageDirection)) {
System.out.println("末页");
list.isLastPage();
}
return list;
}
public int update(String statementName, Object parameterObject)
throws DataAccessException
{
Integer result = (Integer)execute(new SqlMapClientCallback()
{
private final String val$statementName;
private final Object val$parameterObject;
public Object doInSqlMapClient(SqlMapExecutor executor)
throws SQLException
{
return new Integer(executor.update(this.val$statementName, this.val$parameterObject));
}
});
return result.intValue();
}
*/
public int transUpdate(Object statementAndparameter[][])
throws DaoException {
Object statements[] = statementAndparameter[0];
Object parameters[] = statementAndparameter[1];
int result = 0;
for (int i = 0; i < statements.length; i++) {
String name = (String) statements[i];
Object param = parameters[i];
result += getSqlMapClientTemplate().update(name, param);
}
return result;
}
public int transUpdateSameOpt(String statementName, Object objectParam[])
throws DaoException {
int result = 0;
if (statementName == null || objectParam == null
|| objectParam.length == 0)
return result;
for (int i = 0; i < objectParam.length; i++)
result += getSqlMapClientTemplate().update(statementName,
objectParam[i]);
return result;
}
public int update(String statementName, Object parameterObject)
throws DaoException {
int result = getSqlMapClientTemplate().update(statementName,
parameterObject);
return result;
}
static {
log = LogFactory.getLog(org.biframework.dao.ibatis.baseDao.class);
}
@SuppressWarnings("unchecked")
public List getList(String statementName, Object parameterObject,
int skipResults, int maxResults) throws DaoException {
return getSqlMapClientTemplate().queryForList(statementName,
parameterObject, skipResults, maxResults);
}
}
3、进行dao层配置,并进行查询等操作
3.1)所有的dao层都继承baseDao类
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.biframework.dao.ibatis.baseDao;
import org.biframework.exception.DaoException;
import org.bussiness.product.detailquery.bo.StPolicyBean;
public class StPolicyDao extends baseDao {
protected static Log log = LogFactory.getLog(StPolicyDao.class);
@SuppressWarnings("unchecked")
public List getStPerm(StPBean param) throws DaoException{
return super.getList("getShortPrem", param);
}
public Object getStPermCount(StPBean param) throws DaoException{
return super.getObject("getShortPremCount", param);
}
}
}
3.2)进行dao装配 detailQuery-applicationContext.xml
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。



