前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Mybatis通过Interceptor来简单实现影子表进行动态sql读取和写入 续

Mybatis通过Interceptor来简单实现影子表进行动态sql读取和写入 续

作者头像
chinotan
发布2019-04-03 15:53:47
1.9K0
发布2019-04-03 15:53:47
举报

    继上一篇Mybatis通过Interceptor来简单实现影子表进行动态sql读取和写入 地址:https://my.oschina.net/u/3266761/blog/3014017

    之后留了一个小坑,那就是希望能够根据控制层传输过来的是否采用影子表标识来动态的进行影子表的读取和写入,而不是写死在代码中

    此次的目的就是解决这个问题:结合之前写的一篇文章:ThreadLocal实现线程安全 地址:https://my.oschina.net/u/3266761/blog/2032404

    这次解决的方案就是结合ThreadLocal进行解决的,首先对ThredLocal进行一个简单的总结:

    引用自:http://www.iteye.com/topic/103804

    首先,ThreadLocal 不是用来解决共享对象的多线程访问问题的,一般情况下,通过ThreadLocal.set() 到线程中的对象是该线程自己使用的对象,其他线程是不需要访问的,也访问不到的。各个线程中访问的是不同的对象。 

    另外,说ThreadLocal使得各线程能够保持各自独立的一个对象,并不是通过ThreadLocal.set()来实现的,而是通过每个线程中的new 对象 的操作来创建的对象,每个线程创建一个,不是什么对象的拷贝或副本。通过ThreadLocal.set()将这个新创建的对象的引用保存到各线程的自己的一个map中,每个线程都有这样一个map,执行ThreadLocal.get()时,各线程从自己的map中取出放进去的对象,因此取出来的是各自自己线程中的对象,ThreadLocal实例是作为map的key来使用的。 

    如果ThreadLocal.set()进去的东西本来就是多个线程共享的同一个对象,那么多个线程的ThreadLocal.get()取得的还是这个共享对象本身,还是有并发访问问题。

    下面来看一个hibernate中典型的ThreadLocal的应用: 

代码语言:javascript
复制
private static final ThreadLocal threadSession = new ThreadLocal();  
  
public static Session getSession() throws InfrastructureException {  
    Session s = (Session) threadSession.get();  
    try {  
        if (s == null) {  
            s = getSessionFactory().openSession();  
            threadSession.set(s);  
        }  
    } catch (HibernateException ex) {  
        throw new InfrastructureException(ex);  
    }  
    return s;  
}  

    可以看到在getSession()方法中,首先判断当前线程中有没有放进去session,如果还没有,那么通过sessionFactory().openSession()来创建一个session,再将session set到线程中,实际是放到当前线程的ThreadLocalMap这个map中,这时,对于这个session的唯一引用就是当前线程中的那个ThreadLocalMap(下面会讲到),而threadSession作为这个值的key,要取得这个session可以通过threadSession.get()来得到,里面执行的操作实际是先取得当前线程中的ThreadLocalMap,然后将threadSession作为key将对应的值取出。这个session相当于线程的私有变量,而不是public的。 

    显然,其他线程中是取不到这个session的,他们也只能取到自己的ThreadLocalMap中的东西。要是session是多个线程共享使用的,那还不乱套了。 

    试想如果不用ThreadLocal怎么来实现呢?可能就要在action中创建session,然后把session一个个传到service和dao中,这可够麻烦的。或者可以自己定义一个静态的map,将当前thread作为key,创建的session作为值,put到map中,应该也行,这也是一般人的想法,但事实上,ThreadLocal的实现刚好相反,它是在每个线程中有一个map,而将ThreadLocal实例作为key,这样每个map中的项数很少,而且当线程销毁时相应的东西也一起销毁了,不知道除了这些还有什么其他的好处。 

    总之,ThreadLocal不是用来解决对象共享访问问题的,而主要是提供了保持对象的方法和避免参数传递的方便的对象访问方式。归纳了两点: 

1。每个线程中都有一个自己的ThreadLocalMap类对象,可以将线程自己的对象保持到其中,各管各的,线程可以正确的访问到自己的对象。 

2。将一个共用的ThreadLocal静态实例作为key,将不同对象的引用保存到不同线程的ThreadLocalMap中,然后在线程执行的各处通过这个静态ThreadLocal实例的get()方法取得自己线程保存的那个对象,避免了将这个对象作为参数传递的麻烦。 

    当然如果要把本来线程共享的对象通过ThreadLocal.set()放到线程中也可以,可以实现避免参数传递的访问方式,但是要注意get()到的是那同一个共享对象,并发访问问题要靠其他手段来解决。但一般来说线程共享的对象通过设置为某类的静态变量就可以实现方便的访问了,似乎没必要放到线程中。

    ThreadLocal的应用场合,我觉得最适合的是按线程多实例(每个线程对应一个实例)的对象的访问,并且这个对象很多地方都要用到。 这次就很合适

    首先需要定义一个静态全局变量,类型是ThredLocal<Boolean>类型的,用来判断是否需要进行测试,如果是测试的话,则进行影子表的读写

代码语言:javascript
复制
package cn.chinotan.dto.request;

import java.io.Serializable;

/**
 * @program: test
 * @description: 公共请求
 * @author: xingcheng
 * @create: 2019-03-02 17:18
 **/
public class CommonRequest implements Serializable {

    private static final long serialVersionUID = -2617189175983301155L;

    public static ThreadLocal<Boolean> isTest = new ThreadLocal<Boolean>() {
        @Override
        protected Boolean initialValue() {
            return false;
        }
    };

    public static Boolean isTest() {
        return CommonRequest.isTest.get();
    }

    public static void setTest(Boolean test) {
        CommonRequest.isTest.set(test);
    }
}

    接下来定义一个controller切面,对请求进行拦截,将测试变量记录在当前的线程的ThreadLocalMap中,之后mybatis的Interceptor从当前线程无需参数进行拿取,之后便可以进行判断是否需要进行影子表的操作

代码语言:javascript
复制
package cn.chinotan.interceptor;

import cn.chinotan.dto.request.CommonRequest;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Objects;

/**
 * @program: test
 * @description:
 * @author: xingcheng
 * @create: 2019-03-02 18:29
 **/
public class TestInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        String isTestHeader = httpServletRequest.getHeader("isTest");
        // 先从header中去,取不到就从queryParam中取
        if (StringUtils.isNotBlank(isTestHeader)) {
            Boolean isTestBoolean = Objects.equals(isTestHeader, "true");
            CommonRequest.setTest(isTestBoolean);
        } else {
            String isTestParam = httpServletRequest.getParameter("isTest");
            if (StringUtils.isNotBlank(isTestParam)) {
                Boolean isTestBoolean = Objects.equals(isTestParam, "true");
                CommonRequest.setTest(isTestBoolean);
            }
        }
        return true;
    }
    
}
代码语言:javascript
复制
package cn.chinotan.config;

import cn.chinotan.interceptor.TestInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @program: test
 * @description:
 * @author: xingcheng
 * @create: 2019-03-02 18:33
 **/
@Configuration
public class WebAppConfig implements WebMvcConfigurer {

    @Override
    public void addInterceptors(InterceptorRegistry registry){
        registry.addInterceptor(new TestInterceptor()).addPathPatterns("/**");
    }
    
}
代码语言:javascript
复制
package cn.chinotan.interceptor;

import cn.chinotan.aop.TableConfig;
import cn.chinotan.controller.UserController;
import cn.chinotan.dto.request.CommonRequest;
import cn.chinotan.service.Strategy;
import cn.chinotan.service.impl.BakStrategy;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.*;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.SystemMetaObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Proxy;
import java.lang.reflect.Type;
import java.sql.Connection;
import java.util.Map;
import java.util.Properties;

/**
 * 完成插件签名:
 * 告诉MyBatis当前插件用来拦截哪个对象的哪个方法
 * type  指四大对象拦截哪个对象,
 * method : 代表拦截哪个方法  ,在StatementHandler 中查看,需要拦截的方法
 * args  :代表参数
 */
@Component
@Intercepts({
        @Signature(type = StatementHandler.class, method = "prepare", args = {
                Connection.class, Integer.class})})
public class ShareStatementPlugin implements Interceptor {

    private static final Logger LOG = LoggerFactory.getLogger(ShareStatementPlugin.class);

    @Autowired
    private Map<String, Strategy> strategyMap;

    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        StatementHandler statementHandler = realTarget(invocation.getTarget());
        MetaObject metaObject = SystemMetaObject.forObject(statementHandler);
        doTable(statementHandler, metaObject);
        return invocation.proceed();
    }

    private void doTable(StatementHandler handler, MetaObject metaStatementHandler) throws ClassNotFoundException {
        BoundSql boundSql = handler.getBoundSql();
        String originalSql = boundSql.getSql();

        if (originalSql != null && !originalSql.equals("")) {
            LOG.info("分表前的SQL:{}", originalSql);
            MappedStatement mappedStatement = (MappedStatement) metaStatementHandler
                    .getValue("delegate.mappedStatement");
            String id = mappedStatement.getId();
            String className = id.substring(0, id.lastIndexOf("."));
            Class<?> classObj = Class.forName(className);
            Class baseEntity = null;
            Type[] interfacesTypes = classObj.getGenericInterfaces();
            for (Type type : interfacesTypes) {
                if (type instanceof ParameterizedType) {
                    ParameterizedType interfacesType = (ParameterizedType) interfacesTypes[0];
                    Type t = interfacesType.getActualTypeArguments()[0];
                    baseEntity = (Class) t;
                }
            }
            // 根据配置自动生成分表SQL
            TableConfig tableConfig = classObj.getAnnotation(TableConfig.class);
            // 获取表名 并进行相应转化
            String tableName = baseEntity.getSimpleName().toLowerCase();
            if (StringUtils.isNotBlank(tableConfig.value())) {
                tableName = tableConfig.value();
            }

            if (tableConfig != null && tableConfig.isTest()) {
                // 获取策略来处理
                Strategy strategy = strategyMap.get(tableConfig.strategy());
                if (strategy instanceof BakStrategy) {
                    ThreadLocal<Boolean> isTest = CommonRequest.isTest;
                    Boolean aBoolean = isTest.get();
                    LOG.info("是否测试:{}", aBoolean);
                    if (aBoolean) {
                        String convertedSql = originalSql.replaceAll(tableName, strategy.convert(tableName));
                        metaStatementHandler.setValue("delegate.boundSql.sql", convertedSql);
                        LOG.info("分表后的SQL:{}", convertedSql);
                    }
                }
            }
        }
    }

    @Override
    public Object plugin(Object target) {
        if (target instanceof StatementHandler) {
            return Plugin.wrap(target, this);
        }
        return target;
    }

    @Override
    public void setProperties(Properties properties) {
    }

    /**
     * 获得真正的处理对象,可能多层代理
     *
     * @param target
     * @param <T>
     * @return
     */
    public static <T> T realTarget(Object target) {
        if (Proxy.isProxyClass(target.getClass())) {
            MetaObject metaObject = SystemMetaObject.forObject(target);
            return realTarget(metaObject.getValue("h.target"));
        }
        return (T) target;
    }
}

请求Controller:

代码语言:javascript
复制
package cn.chinotan.controller;


import cn.chinotan.entity.User;
import cn.chinotan.service.UserService;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author xingcheng
 * @since 2019-02-16
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;

    @GetMapping("/list")
    public Object list() {
        List<User> list = userService.list();
        return list;
    }

    @GetMapping("/page/list/{current}/{size}")
    public Object page(@PathVariable("current") Long current, @PathVariable("size") Long size) {
        Page<User> objectPage = new Page<>(current, size);
        IPage<User> page = userService.page(objectPage);
        return page;
    }

    @GetMapping("/save/{name}")
    public Object save(@PathVariable("name") String name) {
        User user = new User();
        user.setCreateTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());
        user.setName(name);
        boolean save = userService.save(user);
        return save;
    }

    @GetMapping("/update/{name}")
    public Object update(@PathVariable("name") String name) {
        User user = new User();
        user.setId(1L);
        user.setCreateTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());
        user.setName(name);
        boolean update = userService.updateById(user);
        return update;
    }
}

下面是测试过程:

接下来,进行写入操作:

分别插入测试和非测试数据参数,看看数据库的情况:

大公告成

代码语言:txt
复制
 (adsbygoogle = window.adsbygoogle || []).push({});
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档