[英]How can I configure Spring to show the query performed by JdbcTemplate?
我正在使用Spring开发一批。 要执行查询,我使用JdbcTemplate 。
在我的特定情况下,为扩展Spring JdbcDaoSupport类的DAO类提供了JdbcTemplate ,因此我具有以下DAO:
@Service
@Scope(proxyMode = ScopedProxyMode.TARGET_CLASS)
public class PucManagerColl extends JdbcDaoSupport {
private static Logger log = Logger.getLogger(PucManagerProd.class);
public PucManagerColl() {
System.out.println("Costruzione PucManager");
}
@Autowired
public PucManagerColl(@Qualifier("dataSourcePUCColl") DataSource dataSource) {
setDataSource(dataSource);
}
...............................................................
...............................................................
public void insertTirConsolidatoPolizza(TassoRendimentoInterno tri) {
log.debug("PucManagerColl.insertTirConsolidatoPolizza()");
String sql = "INSERT INTO TirConsolidatoPolizza "
+ "("
+ "Polizzaid, "
+ "PercentualeRendimentoDaInizioGestione, "
+ "PercentualeRendimentoDaInizioAnno, "
+ "PercentualeRendimentoDaInizioTrimestre, "
+ "ControvaloreFinaleBF, "
+ "DataRiferimentoNav"
+ ") "
+ "VALUES (?, ?, ?, ?, ?, ?, ?)";
this.getJdbcTemplate().update(sql,
new Object[] { tri.getPolizzaID(),
tri.getPercRendimentoInizioSottoscrizione(),
tri.getPercRendimentoInizioAnno(),
tri.getPercRendimentoInizioTrimestre(),
tri.getControvaloreQuote(),
tri.getDataRiferimentoNavPUC()});
log.debug("TERMINATO: PucManagerColl.insertTirConsolidatoPolizza()");
}
}
如您所见,该类扩展了Spring类JdbcDaoSupport ,并将数据源注入到构造函数中。
问题是insertTirConsolidatoPolizza()方法无法将记录插入到TirConsolidatoPolizza表中。 我没有错误,也没有异常。 为什么? 您在查询中注意到一些问题吗?
因此,我想调试JdbcTemplate生成的查询,并尝试在数据库提示中手动执行它,但看不到它。
阅读这篇文章: 在Spring JdbcTemplate中看到底层的SQL吗?
以及正式的声明: http ://docs.spring.io/spring/docs/current/spring-framework-reference/html/jdbc.html
在我看来,我可以配置JdbcTemplate以调试模式或类似的方式记录查询。
在另一篇文章中,显示此XML代码片段来做到这一点:
<category name="org.springframework.jdbc.core.JdbcTemplate">
<priority value="debug" />
</category>
但我不知道该放在哪里。
这是我的databaseConfiguration.xml ,其中包含数据库连接的信息(我将此文件导入到主配置文件applicationContext.xml中):
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<tx:annotation-driven transaction-manager="tjtJTransactionManager" />
<!-- DB CONNECTIONS: -->
<bean id="tjtJTransactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
scope="singleton">
<property name="dataSource" ref="dataSourcePUC" />
</bean>
<!-- PROFILO DI PRODUZIONE -->
<beans profile="PROD">
<bean id="dataSourcePUC" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="net.sourceforge.jtds.jdbc.Driver" />
<property name="url" value="jdbc:jtds:sqlserver://XXX.myCompany.YYYY.it:1433/DB_1" />
<property name="username" value="username" />
<property name="password" value="pswd" />
</bean>
</beans>
<!-- PROFILO DI COLLAUDO -->
<beans profile="COLL">
<bean id="dataSourcePUC" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="net.sourceforge.jtds.jdbc.Driver" />
<property name="url" value="jdbc:jtds:sqlserver://XXXX.MYCOMPANY.YYYY.it:1433/DB_2" />
<property name="username" value="username" />
<property name="password" value="pswd" />
</bean>
</beans>
</beans>
如何配置查询日志以查看堆栈跟踪中执行的查询?
如何配置Spring以显示JdbcTemplate执行的查询?
这取决于您使用的日志记录框架。 例如,对于log4j,您可以在log4j.xml中放置以下行
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{dd.MM.yyyy HH:mm:ss} [%t] %m%n" />
</layout>
</appender>
<root>
<priority value ="warn" />
<appender-ref ref="console"/>
</root>
<category name="org.springframework.jdbc.core.JdbcTemplate">
<priority value="debug" />
</category>
</log4j:configuration>
您必须确保log4j.xml在项目类路径中。 有关详细的配置说明,请参见https://logging.apache.org/log4j/2.x/manual/configuration.html 。
PS:顺便说一句,正确的查询应该像
getJdbcTemplate().update(sql,
tri.getPolizzaID(),
tri.getPercRendimentoInizioSottoscrizione(),
tri.getPercRendimentoInizioAnno(),
tri.getPercRendimentoInizioTrimestre(),
tri.getControvaloreQuote(),
tri.getDataRiferimentoNavPUC());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.