Mybatis多线程下如何使用Example详解

 更新时间:2019年12月15日 08:45:28   作者:k2  
这篇文章主要给大家介绍了关于Mybatis多线程下如何使用Example的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用Mybatis具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧

前言

服务器每收到一个请求,都会从线程池中调度一个空闲线程来处理,spring整合的web时,controller和service一般都是单例的,这样导致无论你的Example标注的是单例还是多例,同一个service下的Example也只有一个,多线程访问时产生的

问题如下

问题详情

工程目录结构如下

MyService 的service()方法接收两个参数并据此查询数据库

@Service
public class MyService {
 @Autowired
 StudentMapper studentMapper;
 @Autowired
 StudentExample studentExample;
 public void service(Integer begin,Integer end){
  StudentExample.Criteria criteria1 = studentExample.createCriteria();
  criteria1.andAgeBetween(begin,end);
  List<Student> list=studentMapper.selectByExample(studentExample);
  studentExample.clear();
  System.out.println(list);
 }
}

当同时有两个请求时,两个请求的StudentExample相同

请求1如下

begin=2,end=8

请求2如下

begin=4,end=8

先放行请求1,请求1成功添加条件

再放行请求2,请求2添加失败

这时如果请求2在请求1执行查询操作前就已经执行完studentExample.clear (),请求1的查询条件就失效了

至此两个请求都没有得到正确的结果。

解决方案

可以使用ThreadLocal为每个线程配备单独的Example,为保证每次都能获取到值,这里对ThreadLocal简单扩展一下,如果当前线程没有对应的Example(多例),就从spring容器中获取一个并与这个线程绑定。

ThreadLocalExtension

public class ThreadLocalExtension<T> extends ThreadLocal<T> {
   //获取ApplicationContext方法见下
  @Autowired
  ApplicationContext applicationContext;
  public ThreadLocalExtension(){
    super();
  }
  public T get(Class<T> example){
    T bean=super.get();
    if(bean==null){
      super.set((T) applicationContext.getBean(example));
    }
    return super.get();
  }
}

spring泛型依赖注入

由于Example会有很多个,所以这里使用了泛型,spring4.0提供了对泛型依赖注入的支持。

首先实际类型对应的ThreadLocalExtension交由spring管理

@Repository
public class StudentExampleThreadLocal extends ThreadLocalExtension<StudentExample> {
}

然后直接在代码中注入

@Autowired
ThreadLocalExtension<StudentExample> studentExampleThreadLocal;

修改后的MyService

@Service
public class MyService {
  @Autowired
  StudentMapper studentMapper;
  @Autowired
  ThreadLocalExtension<StudentExample> studentExampleThreadLocal;
  public void service(Integer begin,Integer end){
    StudentExample studentExample = studentExampleThreadLocal.get(StudentExample.class);
    StudentExample.Criteria criteria1 = studentExample.createCriteria();
    criteria1.andAgeBetween(begin,end);
    List<Student> list=studentMapper.selectByExample(studentExample);
    studentExample.clear();
    System.out.println(list);
  }
}

获取ApplicationContext

创建一个类实现ApplicationContextAware,并向spring容器中注入applicationContext

@Component
public class ApplicationContextHelper implements ApplicationContextAware {
  private static ApplicationContext applicationContext;

  public ApplicationContextHelper() {
  }
  @Bean(name="applicationContext")
  public ApplicationContext getApplicationContext(){
    return applicationContext;
  }

  @Override
  public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    ApplicationContextHelper.applicationContext = applicationContext;
  }

  public static Object getBean(String beanName) {
    return applicationContext != null?applicationContext.getBean(beanName):null;
  }
}

结果

至此,整个改造完成,看看效果

请求1

请求2


每个请求获取到了不同的StudentExample,也就不存在冲突的问题,并且StudentExample没有大量的创建与销毁,最多只创建了与服务器线程池中线程相同的个数,实现了重复使用

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对脚本之家的支持。

相关文章

  • SpringBoot结合JSR303对前端数据进行校验的示例代码

    SpringBoot结合JSR303对前端数据进行校验的示例代码

    这篇文章主要介绍了SpringBoot结合JSR303对前端数据进行校验的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-09-09
  • Java爬虫实现爬取京东上的手机搜索页面 HttpCliient+Jsoup

    Java爬虫实现爬取京东上的手机搜索页面 HttpCliient+Jsoup

    下面小编就为大家分享一篇Java爬虫实现爬取京东上的手机搜索页面 HttpCliient+Jsoup,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2017-11-11
  • Java文本文件操作方法实例详解

    Java文本文件操作方法实例详解

    这篇文章主要介绍了Java文本文件操作方法,以实例形式较为详细的分析了java操作文本文件的相关技巧,需要的朋友可以参考下
    2015-06-06
  • java 静态工厂代替多参构造器的适用情况与优劣

    java 静态工厂代替多参构造器的适用情况与优劣

    这篇文章主要介绍了java 静态工厂代替多参构造器的优劣,帮助大家更好的理解和使用静态工厂方法,感兴趣的朋友可以了解下
    2020-12-12
  • SpringBoot整合thymeleaf 报错的解决方案

    SpringBoot整合thymeleaf 报错的解决方案

    这篇文章主要介绍了SpringBoot整合thymeleaf 报错的解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-08-08
  • 深入详解Maven中的settings.xml文件配置

    深入详解Maven中的settings.xml文件配置

    settings.xml是Maven的核心配置文件之一,用于全局配置Maven的行为,本文将为大家详细介绍一下settings.xml文件的配置元素和配置示例,希望对大家有所帮助
    2025-10-10
  • SpringBoot项目中@RestControllerAdvice全局异常失效问题的解决

    SpringBoot项目中@RestControllerAdvice全局异常失效问题的解决

    @RestController注解是一个用于定义RESTful Web服务的控制器的特殊注解,它是@Controller和@ResponseBody注解的结合体,意味着你不需要在每个处理请求的方法上都添加@ResponseBody,本文给大家介绍了解决SpringBoot项目中@RestControllerAdvice全局异常失效问题
    2024-11-11
  • Java Map.entry案例详解

    Java Map.entry案例详解

    这篇文章主要介绍了Java Map.entry案例详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下
    2021-08-08
  • idea整合deepseek实现AI辅助编程的流程步骤

    idea整合deepseek实现AI辅助编程的流程步骤

    文章介绍了如何在IntelliJ IDEA中整合DeepSeek平台实现AI辅助编程,步骤包括安装CodeGPT插件、注册DeepSeek开发者账号、配置API密钥以及设置API信息,需要的朋友可以参考下
    2025-02-02
  • spring-data-redis自定义实现看门狗机制

    spring-data-redis自定义实现看门狗机制

    redission看门狗机制是解决分布式锁的续约问题,本文主要介绍了spring-data-redis自定义实现看门狗机制,具有一定的参考价值,感兴趣的可以了解一下
    2024-03-03

最新评论