1. 程式人生 > >不需要配置log4j , 調試打印ibatis執行的sql語句和參數

不需要配置log4j , 調試打印ibatis執行的sql語句和參數

art pro int 參數 聲明 isa deb 使用 sts

背景:如果不想通過配置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

不需要配置log4j , 調試打印ibatis執行的sql語句和參數