Ibatis中获得执行的SQL及字段信息


import com.ibatis.sqlmap.engine.mapping.result.ResultMapping;

/**
* @description Ibatis Sql 实体类
* @author 杨惠
* @version 1.0
* @date 2010-8-13
*/

public class IbatisSql {

// SQL语句
private String sql = "";
// SQL字段信息
private ResultMapping[] resultMappings = null;

/**
* 获得:SQL语句
*
* @return the sql
*/

public final String getSql() {
return sql;
}

/**
* 设置:SQL语句
*
* @param sql
* the sql to set
*/

public final void setSql(String sql) {
if (sql != null) {
this.sql = sql;
}
}

/**
* 获得:SQL字段信息
*
* @return the resultMappings
*/

public final ResultMapping[] getResultMappings() {
return resultMappings;
}

/**
* 设置:SQL字段信息
*
* @param resultMappings
* the resultMappings to set
*/

public final void setResultMappings(ResultMapping[] resultMappings) {
if (resultMappings != null) {
this.resultMappings = resultMappings;
}
}

}


import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.engine.mapping.result.ResultMapping;
import com.ibatis.sqlmap.engine.mapping.sql.Sql;
import com.ibatis.sqlmap.engine.mapping.statement.MappedStatement;
import com.ibatis.sqlmap.engine.scope.SessionScope;
import com.ibatis.sqlmap.engine.scope.StatementScope;

/**
* @description Ibatis SQL公共类
* @author 杨惠
* @version 1.0
* @date 2010-8-13
*/

public class IbatisUtil {


/**
* 获得Ibatis 执行参数,并转成实体类IbatisSql
*
* @param id
* SQL的ID
* @param sqlMapClient
* com.ibatis.sqlmap.client.SqlMapClient;
* @param parameterObject
* 参数对象
* @return IbatisSql
*/
public static final IbatisSql getIbatisSql(String id,
SqlMapClient sqlMapClient, Object parameterObject) {
IbatisSql ibatisSql = new IbatisSql();
MappedStatement mappedStatement = sqlMapClient.getDelegate()
.getMappedStatement(id);
Sql sql = mappedStatement.getSql();
SessionScope sessionScope = new SessionScope();
sessionScope.incrementRequestStackDepth();
StatementScope statementScope = new StatementScope(sessionScope);
mappedStatement.initRequest(statementScope);
String strSql = sql.getSql(statementScope, parameterObject);
strSql = strSql != null ? strSql.trim().toLowerCase() : strSql;
ibatisSql.setSql(strSql);// 设置SQL语句
ResultMapping[] resultMappingsArray = statementScope.getResultMap().getResultMappings();//获得字段信息
ibatisSql.setResultMappings(resultMappingsArray);
sql.cleanup(statementScope);
sessionScope.cleanup();
mappedStatement = null;
sql = null;
sessionScope = null;
statementScope = null;
id = null;
parameterObject = null;
return ibatisSql;
}

}
修改Ibatis源码:
在SqlMapExecutor接口中增加如下代码:
public abstract SqlMapExecutorDelegate getDelegate();

调用示例:
XML:返回一个List<HashMap>类型
<select id="queryBusiTypeBySettleDateAsList" parameterClass="spBalance"
resultClass="java.util.HashMap">
select s.applicationtypeid as applicationtypeid,d.pname as
busitypename from (select ApplicationTypeID from tb_spbalance
where settledate=#settleDate# group by ApplicationTypeID)
s,tb_datadict_collection d where d.spbusitype='Mobile' and
d.property='BusiType' and s.ApplicationTypeID=d.pvalue order by d.seq asc
</select>
代码:
@SuppressWarnings("unchecked")
public List<List<String>> queryBusiTypeBySettleDateAsList(
SPBalance spBalance) {
//必须先执行查询方法
List<Map<?, ?>> listMap = sqlClient.queryForList( "queryBusiTypeBySettleDateAsList", spBalance);
//然后调用此方法才能获得SQL及字段信息
IbatisSql ibatisSql = IbatisUtil.getIbatisSql("queryBusiTypeBySettleDateAsList", this
.getSqlMapClient(), spBalance);
return CommonCovert.listMapToList(listMap, ibatisSql);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值