关于mybatis中如何动态打印sql语句到控制台

前提你已经配置好了你的Log4j的配置,否则你怎么玩,也不可能在控制台打印日志的。

SpringMVC-servlet.xml

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
       <property name="dataSource" ref="dataSource" />
   <property name="configLocation" value="classpath:mybatis-config.xml"></property>
       <!-- 自动扫描mapping.xml文件 -->  
       <property name="mapperLocations" value="classpath:net/fundway/gzga/mapping/*.xml"></property><!-- mapping文件的路径 -->
</bean>

红色块是我的 映射文件路径

如 userdao.xml的路径

 

net/fundway/gzga/mapping/*.xml
在此路径下符合此路径的sql语句的xml映射文件

 如果你是springboot使用的yml进行配置 ,请无视上面,从下面开始看

application.yml

mybatis:
  mapper-locations: classpath*:mybatis-config.xml
  mapper-locations: classpath*:/Mapper/*.xml 
  type-aliases-package: com.demo.Dao




因为是springboot在application.yml中通过上述方式,指定mybatis的一些配置的路径。
application.yml等同于applicationContext.xml
只不过在springboot中简化繁琐的xml配置,所以采用yml格式,更简洁。
当然你同样可用用xml的配置方式。
想了解更多请自行查看springboot官方对yml的配置说明。

 

mybatis-config.xml:

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <settings>
        <!-- 打印查询语句 -->
        <setting name="logImpl" value="STDOUT_LOGGING" />
    </settings>
</configuration>
 
参考 http://www.mybatis.org/mybatis-3/zh/configuration.html#settings
楼下发现另外一个方法,在springboot中不使用mybatis-config.xml的方式,没试过了,
你们自己跳过去吧

https://blog.csdn.net/u012666996/article/details/79106599
控制台打印:
==>  Preparing: SELECT * FROM t_win WHERE activityId = ? AND batch = ?; 
==> Parameters: 37(Integer), 4(Integer)

转载于:https://my.oschina.net/u/3580626/blog/1842963

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值