【开发笔记】- 在Grails下查看打印真实的SQL

以往我们都是在hibernate里面开启sql,在grails里面只需要在 DataSource.groovy 里面的一个dataSource加入一个

logSql = true即可,但是这样加后发出的SQL是这样的 , insert into t_user values(?,?,?)  看不到 ? 里面的值

 

如果我想看到里面真实的值,即:insert into t_user values(1,'test','test') 

采用log4jdbc即可,步骤如下:

1.下载 log4jdbc4-1.2.1.jar 加入 lib 下  (见附件)

2. 在 grails-app/conf/spring/resources.groovy 文件里面加入一个bean让spring管理,如下:

beans = {
    log4jdbcInterceptor(net.sf.log4jdbc.DataSourceSpyInterceptor)

    dataSourceLog4jdbcAutoProxyCreator(org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator){
        interceptorNames = 'log4jdbcInterceptor'
        beanNames = 'dataSource'
    }
    
}

3. 在log4j中开启对 log4jdbc的支持。在grails-app/conf/Config.groovy里面的 log4j 里面加入:

off       'jdbc.sqlonly',
           'jdbc.audit',
           'jdbc.resultset',
           'jdbc.connection'

    info   'jdbc.sqltiming'

现在在你的项目中再试试,可以看到真实的SQL 了

2011-07-04 11:00:12,443 [http-8090-3] INFO  jdbc.sqltiming  - 
    select
        count(*) as y0_ 
    from
        user this_  {executed in 16 msec}

posted @ 2019-10-15 17:55  多搞学习少搞事情  阅读(268)  评论(0编辑  收藏  举报