获取操作数据库的sql并输出带参数的

   对p6spy源码分析;自己写了段代码实现了把hibernate 输出的?映射到了具体的参数值:(本代码可能会有性能上的问题;敬请谅解 以后改进)

 

基本思想 在hibernate 和数据库之间的连接层中间加入自己定义的datasource:

 

spring 中的文件配置如下

 

 <bean id="dataSource1"  class="com.yineng.datasource.C1DataSource">
            <property name="datasource" ref="dataSource"></property>
    </bean>
   
  
    <bean id="jdbcTemplet"
  class="org.springframework.jdbc.core.JdbcTemplate">
  <property name="dataSource" ref="dataSource1"></property>
 </bean>

 

自定义的C1DataSource类如下:

 

 /**
 
*@see 把前台所有操作的原生SQL记录到指定的文件中
 *
 */
public class C1DataSource implements DataSource{
   
    private DataSource  datasource;
    private Connection  injectConnect;
 
 @Override
 public Connection getConnection() throws SQLException {
  // TODO Auto-generated method stub
  Connection connection = null;
  
  if(datasource!=null){
      connection = this.datasource.getConnection();
  }else if(injectConnect!=null) {
   connection = injectConnect;
  }else{
   throw new SQLException("connection or datasource is required inject!");
  }
  
  C1CoreFactory  coreFactory = new C1CoreFactory();
  C1WriteLog.setConnection(connection);//为写日志时做准备
  
  return coreFactory.getConnection(connection);//
返回了自定义的Connection
 }

 @Override
 public Connection getConnection(String username, String password)
   throws SQLException {
  // TODO Auto-generated method stub
  return this.datasource.getConnection(username, password);
 }

 

自定义的C1Connection implements Connection 代码如下:

主要重写了其中的两个方法(还有其它的 不贴了)

@Override
 public Statement createStatement(int resultSetType, int resultSetConcurrency)
   throws SQLException {
  // TODO Auto-generated method stub
  return this.passthru.createStatement(resultSetType, resultSetConcurrency);
 }

 @Override
 public Statement createStatement(int resultSetType,
   int resultSetConcurrency, int resultSetHoldability)
   throws SQLException {
  // TODO Auto-generated method stub
  return this.passthru.createStatement(resultSetType, resultSetConcurrency, resultSetHoldability);
 }

 

还重写了部分类 重写类如下

C1Array.java 

C1CallableStatement.java 

C1PreparedStatement.java

C1ResultSet.java

C1Statement.java

我们可以在这些代码中加入自己想要的代码(代码提供下载)

 

我们还可以做些备份工作比如 在删除或更新一条数据之前我们可以把数据先select 放到文件中然后再执行删除(可以实现传说中的数据闪回)。

 


 


                                   

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值