mybatis中如何動態列印sql語句到控制檯
阿新 • • 發佈:2018-11-16
前提你已經配置好了你的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>