如何在 iBatis ORM 中进行日志记录
我们大量使用 iBatis + Spring。我们从数据库获取的所有内容都是通过存储过程获取的。有时,在解决问题时,我们想知道发送到 SP 的确切参数以及执行的 SP 名称。目前我们通过调试代码来做到这一点(这很痛苦)。我们想向 iBatis 添加某种日志记录,以便它打印出 SP 的名称 + 参数值。我们使用 log4j,以下是我们的 iBatis 结构。
映射:
<procedure id="getReportData" parameterMap="getReportDataCall">
{call get_rpt (?,?,?,?)}
</procedure>
<parameterMap id="getReportDataCall" class="map">
<parameter property="type" jdbcType="String" javaType="java.lang.String" mode="IN"/>
<parameter property="month" jdbcType="Int" javaType="java.lang.Integer" mode="IN"/>
<parameter property="Result0" jdbcType="ORACLECURSOR" javaType="java.sql.ResultSet" mode="OUT" resultMap="result1"/>
<parameter property="Result1" jdbcType="ORACLECURSOR" javaType="java.sql.ResultSet" mode="OUT" resultMap="result2"/>
</parameterMap>
<resultMap id="select-first-result-hq" class="VO">
<result property="column1" column="columna"/>
<result property="column2" column="columnb"/>
</resultMap
从 DAO 调用 iBatis
HashMap parm = new HashMap ();
parm.put("type", type_val);
parm.put("month", month_val);
getSqlMapClientTemplate().queryForList("mymappingName.getReportData", parm);
如您所见,参数位于 HashMap
中。我可以创建一个自己的类+方法,它将 HashMap 和过程名称作为参数,然后将哈希图的所有键/值对和过程名称打印到日志中。但是,如果我这样做...在调用 SP 之前,我必须将该调用添加到我的所有 DAO 中。
有没有更简单的解决方案?这会避免我再次接触我的所有代码吗?
We use iBatis + Spring intensively. Everything we get from the DB is fetched via stored procedures. At times when troubleshooting an issue we want to know the exact parameters that were sent to the SP and SP name that was executed. Currently we do this by debugging the code (which is a pain). We would like to add some sort of logging to iBatis so it prints out name of the SP + parameter values. We use log4j and following is our iBatis structure.
Mapping:
<procedure id="getReportData" parameterMap="getReportDataCall">
{call get_rpt (?,?,?,?)}
</procedure>
<parameterMap id="getReportDataCall" class="map">
<parameter property="type" jdbcType="String" javaType="java.lang.String" mode="IN"/>
<parameter property="month" jdbcType="Int" javaType="java.lang.Integer" mode="IN"/>
<parameter property="Result0" jdbcType="ORACLECURSOR" javaType="java.sql.ResultSet" mode="OUT" resultMap="result1"/>
<parameter property="Result1" jdbcType="ORACLECURSOR" javaType="java.sql.ResultSet" mode="OUT" resultMap="result2"/>
</parameterMap>
<resultMap id="select-first-result-hq" class="VO">
<result property="column1" column="columna"/>
<result property="column2" column="columnb"/>
</resultMap
Calling iBatis from DAO
HashMap parm = new HashMap ();
parm.put("type", type_val);
parm.put("month", month_val);
getSqlMapClientTemplate().queryForList("mymappingName.getReportData", parm);
As you see the parameters are just in a HashMap
. I could make a class+method of my own which takes in the HashMap
and procedure name as parameters and just prints out all the key/value pairs of the hashmap and the procedure name to the log. However, if I do this...I will have to add that call to all my DAO's before I call the SP.
Is there any simpler solution to this? that would avoid me touching all my code again?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我也非常广泛地使用 iBatis 和 Spring,并且通常只是将 java.sql.* 类的日志记录级别提高到 DEBUG。我通常在 log4j.properties 中执行类似的操作:
这会记录传递给参数占位符的 sql 和值。
I've used iBatis and Spring pretty extensively too and generally just up the logging level to DEBUG for the java.sql.* classes. I generally do something like this in log4j.properties:
This logs both the sql and values that are passed to the parameter placeholders.