其他分享
首页 > 其他分享> > MyBatisUtil、 MyBatis分页拦截器

MyBatisUtil、 MyBatis分页拦截器

作者:互联网

 

MyBatisUtil

 

package com.edu.common;

import java.io.InputStream;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class MyBatisUtil {
    private static SqlSessionFactory factory;
    private static ThreadLocal<SqlSession> locale = new ThreadLocal<SqlSession>();
    
    static{
        //确保factory只加载一次,并且实例只有一份
        InputStream in = Test.class.getResourceAsStream("/configuration.xml");
        factory = new SqlSessionFactoryBuilder().build(in);
    }
    //获取sqlSession
    public static SqlSession getSession(){
        SqlSession session = locale.get();
        if(session==null){
            session = factory.openSession();
            locale.set(session);
            System.out.println("您建立了一个session");
        }
        return session;
    }
    
    public static void close(){
        SqlSession session = locale.get();
        if(session!=null){
            session.close();
            //灰常重要
            locale.set(null);
        }
    }
    
}

 

PaginationInterceptor : MyBatis分页拦截器
package com.edu.common;

import java.sql.Connection;
import java.util.Properties;

import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Plugin;
import org.apache.ibatis.plugin.Signature;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.session.RowBounds;

//表明拦截StatementHandler类的prepare方法(Connection)
@Intercepts({ @Signature(type = StatementHandler.class, method = "prepare", args = { Connection.class }) })
public class PaginationInterceptor implements Interceptor {
    // intercept(拦截器方法):拦截所有对象
    public Object intercept(Invocation arg0) throws Throwable {
        System.out.println("拦截器执行之前");
        // 执行的这个方法是否带RowBounds参数了
        StatementHandler statementHandler = (StatementHandler) arg0.getTarget();//arg0调取被拦截的(target)对象
        MetaObject metaObject = MetaObject.forObject(statementHandler); //获取原对象:包含SQL执行的相关内容,条件RowBounds
        RowBounds rowBounds = (RowBounds) metaObject.getValue("delegate.rowBounds"); // 返回原对象中的RowBounds对象
        if (rowBounds != null && rowBounds != RowBounds.DEFAULT) { // RowBounds有值,数据还被重新设置过,证明你要分页了
            System.out.println("老子要分页了");
            // 更改SQL语句:
            String Sql = (String) metaObject.getValue("delegate.boundSql.sql"); // 得到原来你要执行的SQL语句
            int offset = rowBounds.getOffset();   // RowBounds里面的start起始行号
            int limit = rowBounds.getLimit();    // RowBounds里面的rows每页行数
            Sql = Sql + " limit " + offset + "," + limit + "";  // 拼接得到MySQL分页的sql语句
            metaObject.setValue("delegate.boundSql.sql", Sql); // 重置分页的SQL语句
            metaObject.setValue("delegate.rowBounds.offset", RowBounds.NO_ROW_OFFSET);// 设置原有的分页的参数为初始化
            metaObject.setValue("delegate.rowBounds.limit", RowBounds.NO_ROW_LIMIT);  // 设置原有的分页的参数为初始化
        }
        Object obj = arg0.proceed();// 拦截对象直接执行
        System.out.println("拦截器执行之后");
        return obj;
    }
    // 返回该拦截器插件
    public Object plugin(Object target) {
        return Plugin.wrap(target, this);
    }
    public void setProperties(Properties properties) {
    }

}

 

标签:MyBatisUtil,拦截器,ibatis,apache,session,org,MyBatis,import,RowBounds
来源: https://www.cnblogs.com/bors/p/MyBatisUtil.html