背景:如果不想通过配置log4j的方式来打印ibatis执行的sql语句和参数param,可以使用如下方法在控制台打印sql语句和参数
public static void main(String[] args) { try { //声明配置文件的名称(映射文件被定义在其中) String resource = "com/test/tele/bean/iBatis-config-test.xml"; //利用工具类Resources来读取到配置文件 Reader reader = Resources.getResourceAsReader(resource); //创建SqlMapClient接口的变量实例 SqlMapClient sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader); Map paramMap = new HashMap(); paramMap.put("isDisabled","0"); paramMap.put("belongType","1"); List list = sqlMap.queryForList("T_Process.selectExtDeptInfoByMap", paramMap); //debug 获取动态运行时sql以及参数 SqlMapExecutorDelegate delegate = ((ExtendedSqlMapClient) sqlMap).getDelegate(); MappedStatement ms = delegate.getMappedStatement("T_Process.selectExtDeptInfoByMap");//这个getXXSQL就是你想要获取的,在xml文件中定义的sql语句的id Sql sql = ms.getSql(); RequestScope requestScope = new RequestScope(); requestScope.setStatement(ms); String sqlStr = sql.getSql(requestScope,paramMap); Object[] sqlParam = sql.getParameterMap(requestScope, paramMap).getParameterObjectValues(requestScope, paramMap); System.out.println("运行时sql为" + sqlStr); for (int i = 0; i < sqlParam.length; i++) { System.out.println("第[" + (i + 1) + "]个参数为:" + sqlParam[i]); } } catch (Exception e) { e.printStackTrace(); } }
参考内容:
https://blog.csdn.net/libertine1993/article/details/52461801
https://blog.csdn.net/njuptsoz/article/details/83334583
原文地址:https://www.cnblogs.com/leite/p/10245492.html
时间: 2024-10-12 15:51:11