1. applicationContext.xml
<!-- Spring提供的iBatis的SqlMap配置-->
<bean id="sqlMapClient" class="com.ks.tjrw.jl.dao.impl.ibatis.KSSqlMapClientFactoryBean">
<property name="configLocation" value="classpath:SqlMapConfig.xml" />
<property name="dataSource" ref="dataSource" />
</bean>
2. SqlMapConfig.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMapConfig PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<properties resource="db.properties" />
<settings cacheModelsEnabled="true" enhancementEnabled="true"
lazyLoadingEnabled="true" maxRequests="64" maxSessions="20"
maxTransactions="20" useStatementNamespaces="false" />
<sqlMap resource="com/ks/tjrw/jl/sqlFile/Pdgxxt.xml" />
<sqlMap resource="com/ks/tjrw/jl/sqlFile/Pdxljdjxjyjl.xml" />
<sqlMap resource="com/ks/tjrw/jl/sqlFile/Zmqfh.xml" />
<sqlMap resource="com/ks/tjrw/jl/sqlFile/ZmqfhSub.xml" />
<sqlMap resource="com/ks/tjrw/jl/sqlFile/Clhzlx.xml" />
</sqlMapConfig>
3.列举其中一个
com/ks/tjrw/jl/sqlFile/Pdgxxt.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd" >
<sqlMap namespace="Pdgxxt">
<typeAlias alias="Pdgxxt" type="com.ks.tjrw.jl.model.Pdgxxt" />
<resultMap id="get-PdxSub-result" class="com.ks.tjrw.jl.model.Pdgxxt">
<result property="powerNo" column="POWER_DISTRIBUTION_NO"/>
<result property="name" column="NAME"/>
<result property="number" column="NUMBER"/>
<result property="linkAmount" column="LINK_AMOUNT"/>
<result property="linkSituation" column="LINK_SITUATION"/>
<result property="verity" column="VERITY_SITUATION"/>
<result property="createTime" column="CREATE_TIME"/>
<result property="lineNo" column="LINE_CHECK_NO"/>
</resultMap>
<select id="findPdxSubByPk" parameterClass="Pdgxxt" resultClass="Pdgxxt" resultMap="get-PdxSub-result">
select * from JL_PDXLJDJXJYJL_PDGXXT where POWER_DISTRIBUTION_NO=#powerNo#
</select>
<!-- 查询配电线路接地、接线检验记录_配电干线表实体 -->
<select id="getPdg" resultClass="Pdgxxt" parameterClass="java.lang.String" resultMap="get-PdxSub-result">
select * from JL_PDXLJDJXJYJL_PDGXXT where 1=1
<dynamic>
<isNotEmpty prepend="and">
LINE_CHECK_NO=#lineNo#
</isNotEmpty>
</dynamic>
</select>
<!-- 插入配电线路接地、接线检验记录_配电干线表实体 -->
<insert id="insertPdg" parameterClass="Pdgxxt">
insert into JL_PDXLJDJXJYJL_PDGXXT(POWER_DISTRIBUTION_NO)
values (#powerNo#)
</insert>
<!-- 修改配电线路接地、接线检验记录_配电干线表实体 -->
<update id="updatePdg" parameterClass="Pdgxxt">
update JL_PDXLJDJXJYJL_PDGXXT set POWER_DISTRIBUTION_NO=#powerNo#
<dynamic>
<isNotNull prepend="," property="lineNo">
LINE_CHECK_NO=#lineNo#
</isNotNull>
<isNotNull prepend="," property="name">
"NAME"=#name#
</isNotNull>
<isNotNull prepend="," property="number">
"NUMBER"=#number#
</isNotNull>
<isNotNull prepend="," property="linkAmount">
LINK_AMOUNT=#linkAmount#
</isNotNull>
<isNotNull prepend="," property="linkSituation">
LINK_SITUATION=#linkSituation#
</isNotNull>
<isNotNull prepend="," property="verity">
VERITY_SITUATION=#verity#
</isNotNull>
<isNotNull prepend="," property="createTime">
CREATE_TIME=#createTime#
</isNotNull>
</dynamic>
where POWER_DISTRIBUTION_NO=#powerNo#
</update>
<!-- 删除配电线路接地、接线检验记录_配电干线表实体 -->
<delete id="deletePdg" parameterClass="java.util.List">
delete JL_PDXLJDJXJYJL_PDGXXT
<iterate prepend="WHERE" open="(" close=")"
conjunction="OR">
LINE_CHECK_NO=#matList[]#
</iterate>
</delete>
<delete id="deletePdxSubByCondition" parameterClass="java.lang.String">
delete from JL_PDXLJDJXJYJL_PDGXXT where LINE_CHECK_NO=#lineNo#
</delete>
</sqlMap>
4.对应的action
PdxAction---》列举edit方法
public String editPdx(){
pdxSubList = pdxSubService.findPdxs(pdx.getLineCheckNo());
pdx = pdxDao.findPdxByNo(pdx);
return newpath("/tjrw/jl/pdx.jsp");
}
5.
@Repository("PdxljdjxjyjlDaoImpl")
public class PdxljdjxjyjlDaoImpl extends BaseDaoiBatis<Pdxljdjxjyjl> implements PdxljdjxjyjlDao {
public List<Pdxljdjxjyjl> findPdxByQuery(Object obj, Pagination p) {
return queryForList("getPdx",obj,p);
}
public boolean insertPdx(Pdxljdjxjyjl pdx) {
return insert("insertPdx",pdx);
}
public boolean updatePdx(Pdxljdjxjyjl pdx) {
return update("updatePdx",pdx);
}
public boolean deletePdx(List<String> pkList) {
return delete("deletePdx",pkList);
}
public long getAllCounts(){
return getObjectTotal("getPdx");
}
public long getAllCounts(Object obj){
return getObjectTotal("getPdx",obj);
}
public Pdxljdjxjyjl findPdxByNo(Pdxljdjxjyjl pdx){
return (Pdxljdjxjyjl) queryForObject("findPdxByPk",pdx);
}
}
/**
*
* 类说明:数据操作基类,实现了物理分页查询,增,删,改等基本功能
* 作者:lf
* 创建时间:2011-4-4 下午04:59:48
*/
public class BaseDaoiBatis<T> {
private Logger logger = null;
@Resource(name="sqlMapClient")
private SqlMapClient sqlMapper = null;
public BaseDaoiBatis(){
getLogger();
}
public SqlMapClient getSqlMapper() {
return sqlMapper;
}
public void setSqlMapper(SqlMapClient sqlMapper) {
this.sqlMapper = sqlMapper;
}
protected Logger getLogger() {
if (null == logger) {
logger = Logger.getLogger(getClass().getName());
}
return logger;
}
/**
* 根据传入的sqlId创建查询总记录数的count sqlId
* 创建CountStatement,并将其添加到MapStatement(HashMap)中
*/
private void prepareCountQuery(String sqlId) {
//将传入的sqlId转换为__sqlId__Count__
String countQuery = CountStatementUtil.getCountStatementId(sqlId);
if (logger.isDebugEnabled()) {
logger.debug("Convert " + sqlId + " to " + countQuery);
}
if (sqlMapper instanceof SqlMapClientImpl) {
SqlMapExecutorDelegate delegate = ((SqlMapClientImpl) sqlMapper).getDelegate();
try {
delegate.getMappedStatement(countQuery);
}catch (SqlMapException e) {
delegate.addMappedStatement(CountStatementUtil.
createCountStatement(delegate.getMappedStatement(sqlId)));
}
}
}
/**
* 根据传入的sqlId和对象参数,查询满足条件的总记录条数
*/
public long getObjectTotal(String sqlId, Object parameterObject) {
long count = 0l;
prepareCountQuery(sqlId); //创建CountStatement,并将其添加到MapStatement(HashMap)中
try {
count = (Long) sqlMapper.queryForObject(
CountStatementUtil.getCountStatementId(sqlId),
parameterObject);
} catch (SQLException e) {
printErrorInfo(e);
}
return count;
}
/**
* 根据传入的sqlId查询满足条件的总记录条数
*/
public long getObjectTotal(String sqlId) {
long count = 0l;
prepareCountQuery(sqlId); //创建CountStatement,并将其添加到MapStatement(HashMap)中
try {
count = (Long) sqlMapper.queryForObject(
CountStatementUtil.getCountStatementId(sqlId));
} catch (SQLException e) {
printErrorInfo(e);
}
return count;
}
/**
* 根据传入的sqlId查询数据库
*/
public T queryForObject(String sqlId) {
T results = null;
try {
results = (T) sqlMapper.queryForObject(sqlId);
} catch (SQLException e) {
printErrorInfo(e);
}
return results;
}
/**
* 根据传入的sqlId,和对象参数parameterObject查询数据库
*/
public T queryForObject(String sqlId, Object parameterObject) {
T results = null;
try {
results = (T) sqlMapper.queryForObject(sqlId, parameterObject);
} catch (SQLException e) {
printErrorInfo(e);
}
return results;
}
/**
* 使用sql语句查询列表,返回List集合
* 待执行的sql查询语句,该sql语句存在于XML配置文件中,此处只需要引用相应的ID即可
*/
public List<T> queryForList(String sqlId) {
List<T> results = null;
try {
results = sqlMapper.queryForList(sqlId);
} catch (SQLException e) {
printErrorInfo(e);
}
return results;
}
public List<T> queryForList(String sqlId, Object parameterObject) {
List<T> results = null;
try {
results = sqlMapper.queryForList(sqlId, parameterObject);
} catch (SQLException e) {
printErrorInfo(e);
}
return results;
}
/**
* 功能说明:分页查询
* @param sqlId sql查询语句
* @param pagination.getCurrentPage() 起始页面
* @param pagination.getPageSize() 每页的记录条数
* @return
*/
public List<T> queryForList(String sqlId,Pagination pagination) {
List<T> results = null;
try {
results = sqlMapper.queryForList(sqlId,
pagination.getCurrentPage(), pagination.getPageSize());
} catch (SQLException e) {
printErrorInfo(e);
}
return results;
}
public List<T> queryForList(String sqlId, Object parameterObject,Pagination pagination) {
List<T> results = null;
try {
results = sqlMapper.queryForList(sqlId, parameterObject,
pagination.getCurrentPage(),pagination.getPageSize());
} catch (SQLException e) {
printErrorInfo(e);
}
return results;
}
/**
* 向数据库执行插入操作
*
* @param sqlId 待执行的sql插入操作语句,该sql语句存在于XML配置文件中,此处只需要引用相应的ID即可
* @param parameterObject 待插入的对象
* @return 在配置文件中配置<selectKey>
*/
public boolean insert(String sqlId, Object parameterObject) {
try {
sqlMapper.insert(sqlId, parameterObject);
} catch (SQLException e) {
printErrorInfo(e);
return false;
}
return true;
}
/**
* 向数据库执行插入操作,并返回配置文件中<selectKey>所返回的值
*
* @param sqlId
* @param parameterObject
* @return
*/
public int insertAndReturnPK(String sqlId, Object parameterObject) {
int returnValue = 0;
try {
returnValue = Integer.parseInt(sqlMapper.insert(sqlId,
parameterObject).toString());
} catch (SQLException e) {
printErrorInfo(e);
}
return returnValue;
}
/**
* 向数据库执行修改操作
*
* @param sqlId 待执行的sql修改操作语句,该sql语句存在于XML配置文件中,此处只需要引用相应的ID即可
* @param parameterObject 待修改的对象
* @return
*/
public boolean update(String sqlId, Object parameterObject) {
try {
sqlMapper.update(sqlId, parameterObject);
} catch (SQLException e) {
printErrorInfo(e);
return false;
}
return true;
}
/**
* 向数据库执行删除操作
*
* @param sqlId
* 待执行的sql删除操作语句,该sql语句存在于XML配置文件中,此处只需要引用相应的ID即可
* @param parameterObject
* 待删除的对象
*/
public boolean delete(String sqlId, Object parameterObject) {
try {
sqlMapper.delete(sqlId, parameterObject);
} catch (SQLException e) {
printErrorInfo(e);
return false;
}
return true;
}
/**
* 打印错误信息,以便于调试
*/
private void printErrorInfo(SQLException e) {
logger.error("error in class " + this.getClass() + ":" + e.toString()
+ ",caused by:" + e.getCause()+ ",more information:" + e.getMessage());
}
}