MyBatis 配置SQL打印
在 SqlMappingConfig.xml 中配置以下代码:
<!--配置sql打印-->
<settings><setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
运行效果:会显示 SQL 语句,查询结果,总共几条数据,最后把数据封装成对象。
Opening JDBC Connection
Created connection 19717364.
Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@12cdcf4]
==> Preparing: SELECT * FROM `customer` WHERE cust_id = ?
==> Parameters: 1(Integer)
<== Columns: cust_id, cust_name, cust_profession, cust_phone, email
<== Row: 1, 鲁班, 射手, 13499887733, 12341241@qq.com
<== Total: 1
Customer{cust_id=1, cust_name='鲁班', cust_profession='射手', cust_phone='13499887733', email='12341241@qq.com'}
Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@12cdcf4]
Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@12cdcf4]
Returned connection 19717364 to pool.
注意:
由以上结果可知,我们配置在 Customer.xml 中的 sql 语句会发生转换,
#{} 在运行时会被转化成 ?
配置在 Customer.xml 中的代码:
SELECT * FROM `customer` WHERE cust_id = #{cust_id}
运行时的代码:
SELECT * FROM `customer` WHERE cust_id = ?
并且 ? 在实际执行时会给参数添加单引号,从而变成 '?'