一次性向所有MySQL Select Queries添加一列 [英] Add a column to all MySQL Select Queries in a single shot

查看:150
本文介绍了一次性向所有MySQL Select Queries添加一列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



例如,代码中的原始查询如下所示:

尝试为所有MySQL在运行时选择查询选择查询。 >

  select a,b,c from ...... 
选择x,y from ...

所有这些都需要在运行时 修改为:

  select a,b,c / * Comment * / from ... 
选择x,y / * Comment * / from ...

应用程序在Hibernate 4.2.1上运行。
我只能想到的解决方案是扩展 org.hibernate.dialect.MySQLDialect 并添加 / * Comment * / 在新的 CustomMySQLDialect 中。

对要修改哪种方法来完成此操作有点困惑。希望任何指向正确的方向。



Can transformSelectString(java.lang.String) method in org.hibernate.dialect.Dialect 被覆盖来完成这个任务吗?编辑1:自定义MySQL方言中的
transformSelectString不适用于运行时SQL修改

$ b

>解决方案

创建自定义数据库拦截器

  package com.felix.dao.interceptor; 
import org.hibernate.EmptyInterceptor;

public class CustomDBInterceptor extends EmptyInterceptor {
$ b $ @Override
public String onPrepareStatement(String sql){
String commentStr =/ * Comment * /
返回super.onPrepareStatement(commentStr + sql);





$ p $在Spring Context文件中,configure会话工厂的拦截器:

 < bean id =customDBInterceptorclass =com.felix.dao.interceptor。 CustomDBInterceptor/> 
< bean id =sessionFactory
class =org.springframework.orm.hibernate4.LocalSessionFactoryBean>
< property name =dataSourceref =datasource/>
< property name =entityInterceptor>
< ref bean =customDBInterceptor/>
< / property>
...
< / bean>

确保Custom DB Interceptor对sessionFactory没有循环依赖关系。
通过以上所述,通过会话工厂触发的所有查询都会被拦截,修改,然后传递给 onPrepareStatement 方法。


Trying to add a comment to all MySQL Select Queries in my web application at runtime.

For example, the original queries in the code looks like:

select a,b,c from ......
select x,y from...

All of these need to be modified at runtime to:

select a,b,c /*Comment*/ from ...
select x,y /*Comment*/ from ...

The application runs on Hibernate 4.2.1. Only solution I can think of is extending the org.hibernate.dialect.MySQLDialect and add the /*Comment*/ in the new CustomMySQLDialect.

A little confused on which method to modify to accomplish this. Would appreciate any pointer in the right direction.

Can transformSelectString(java.lang.String) method in org.hibernate.dialect.Dialect be overridden to accomplish this?

EDIT 1: transformSelectString in a Custom MySQL Dialect is not working for runtime SQL modification

解决方案

Create a Custom DB Interceptor

package com.felix.dao.interceptor;
import org.hibernate.EmptyInterceptor;

public class CustomDBInterceptor extends EmptyInterceptor {

  @Override
  public String onPrepareStatement(String sql) {
    String commentStr = "/*Comment*/"
    return super.onPrepareStatement(commentStr+sql);
  }

}

In the Spring Context file, configure the Interceptor for the session factory:

<bean id="customDBInterceptor" class="com.felix.dao.interceptor.CustomDBInterceptor"/>
<bean id="sessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="datasource" />
    <property name="entityInterceptor">
        <ref bean="customDBInterceptor"/>
    </property>
    ...
</bean>

Make sure the Custom DB Interceptor does not have a cyclic dependency on the sessionFactory. With the above, all queries fired through the session factory, are intercepted, modified and then passed to the onPrepareStatement method.

这篇关于一次性向所有MySQL Select Queries添加一列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆