温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

spring mvc 配置 mybatis sql拦截器

发布时间:2020-08-12 08:36:07 来源:网络 阅读:1562 作者:www1056349063 栏目:软件技术


直接上代码:

mybatis配置中 添加  <property name="plugins"> 如下:


  <bean id="sqlSessionFactory" class="com.hotent.core.mybatis.SqlSessionFactoryFactoryBean">

        <property name="configLocation" value="classpath:/conf/configuration.xml"/>

        <property name="mapperLocations" >

        <list>

        <value>classpath:/com/*/maper/*.map.xml</value>

        </list>

        </property>

        <property name="dataSource" ref="dataSource"/>

         <property name="plugins">  

            <ref bean="paginationInterceptor"/>  

        </property>  

    </bean>

    <!-- MyBatis sql拦截器-->  

    <bean id="paginationInterceptor" class="com.mybatis.interceptor.PageInterceptor">  

        <property name="dialect" value="oracle"/>   

        <property name="pageSqlId" value=".*"/>  

    </bean>   


有的SqlSessionFactoryFactoryBean类中没有plugins属性

(private Interceptor[] plugins;生成setter方法 ),

定义属性后将插件添加到Configuration conf = sqlSessionFactory.getConfiguration();如下


if (null!=this.plugins) {

     for (Interceptor plugin : this.plugins) {

     conf.addInterceptor(plugin);

       if (this.logger.isDebugEnabled()) {

         this.logger.debug("Registered plugin: '" + plugin + "'");

       }

     }

}


关于拦截类 PageInterceptor

spring mvc 配置 mybatis sql拦截器



如果要保存sql以及sql对应的值问题:


sql数据问题:sql中有特殊符号是无法保存到数据库中的,通过url编码后保存到数据库,如果要查看时再反编码即可

sql对应值的问题:1.对象转为json数据,2,直接获取


public static String getStringParame(Object obj){

 JSONObject json = JSONObject.fromObject(obj);//将java对象转换为json对象

 String str = json.toString();//将json对象转换为字符串

 //不是json直接返回字符串

 if(str.equals("") || str.equals("{}")){

 str = obj.toString();

 }

 return str;

}


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI