如何在Spring JDBC中启用SQL查询控制台日志记录

时间:2019-06-09 19:06:43

标签: java spring prepared-statement spring-jdbc

我是准备好的语句的新手,我正在尝试使用通过jdbcTemplate进行的基本插入和更新,该方法也可以正常工作,但是我希望在consle中为sql查询打印日志,但我不确定如何实现。我已在application.properties文件中添加了必需的属性,但无法正常工作。请提供一些适当的建议或推荐链接来帮助我,以解决我的问题。预先感谢...

SampleConfiguration.java

@Configuration
public class SampleConfiguration 
{
    @Bean
    public DataSource mysqlDataSource() 
    {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("org.hsqldb.jdbcDriver");
        dataSource.setUrl("jdbc:hsqldb:hsql://localhost/");
        dataSource.setUsername("SA");
        dataSource.setPassword(""); 
        return dataSource;
    }

    @Bean
    public JdbcTemplate jdbcTemplate(DataSource dataSource)
    {
        JdbcTemplate jdbcTemplate = new JdbcTemplate();
        jdbcTemplate.setDataSource(dataSource);
        return jdbcTemplate;
    }

}

SampleService.java

@Service
public interface SampleService 
{
    public int insert();
    public long getCount();
}

SampleServiceImpl.java

@Service
public class SampleServiceImpl implements SampleService 
{

    @Autowired
    JdbcTemplate jdbcTemplate;

    @Override
    public int insert()
    {
        return jdbcTemplate.update("insert into batch values(?,?)","field1","field2");
    }

    @Override
    public long getCount()
    {
        return jdbcTemplate.queryForObject("select count(*) from batch", Long.class);
    }

}

SpringjdbcApplication.java

@SpringBootApplication
public class SpringjdbcApplication 
{
    public static void main(String[] args) 
    {
        ApplicationContext context  = SpringApplication.run(SpringjdbcApplication.class, args);
        SampleService service = context.getBean(SampleService.class);
        System.err.println("The number of rows inserted = "+service.insert());
        System.err.println("The count of batch class is = "+service.getCount());
    }

}

Entity.java

@Entity
public class Batch implements Serializable 
{

    private static final long serialVersionUID = -5687736664713755991L;

    @Id
    @Column(name="field1")
    private String field1;

    @Column(name="field2")
    private String field2;

        ... getter, setter and no-arg constructor
}

application.properties

spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=none
spring.jpa.properties.hibernate.format_sql=true

logging.level.org.hibernate.SQL=debug
logging.level.org.hibernate.type.descriptor.sql=trace

pom.xml

           <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.hsqldb</groupId>
            <artifactId>hsqldb</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

我想知道如何在控制台上打开sql语句记录,以及如何设置其他属性(例如,关闭或打开自动提交)?

1 个答案:

答案 0 :(得分:0)

您应该将此添加到属性文件中

logging.level.org.springframework.jdbc.core = TRACE

这将启用Spring Jdbc日志