资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

Mybatis多线程下使用Example的示例

这篇文章主要介绍了Mybatis多线程下使用Example的示例,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。

成都创新互联-成都网站建设公司,专注网站制作、网站建设、网站营销推广,域名与空间,网站空间网站托管运营有关企业网站制作方案、改版、费用等问题,请联系成都创新互联

前言

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

问题如下

问题详情

工程目录结构如下

Mybatis多线程下使用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 list=studentMapper.selectByExample(studentExample);
  studentExample.clear();
  System.out.println(list);
 }
}

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

请求1如下

begin=2,end=8

Mybatis多线程下使用Example的示例

请求2如下

begin=4,end=8

Mybatis多线程下使用Example的示例

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

Mybatis多线程下使用Example的示例

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

Mybatis多线程下使用Example的示例

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

Mybatis多线程下使用Example的示例

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

解决方案

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

ThreadLocalExtension

public class ThreadLocalExtension extends ThreadLocal {
   //获取ApplicationContext方法见下
  @Autowired
  ApplicationContext applicationContext;
  public ThreadLocalExtension(){
    super();
  }
  public T get(Class 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 {
}

然后直接在代码中注入

@Autowired
ThreadLocalExtension studentExampleThreadLocal;

修改后的MyService

@Service
public class MyService {
  @Autowired
  StudentMapper studentMapper;
  @Autowired
  ThreadLocalExtension studentExampleThreadLocal;
  public void service(Integer begin,Integer end){
    StudentExample studentExample = studentExampleThreadLocal.get(StudentExample.class);
    StudentExample.Criteria criteria1 = studentExample.createCriteria();
    criteria1.andAgeBetween(begin,end);
    List 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

Mybatis多线程下使用Example的示例

请求2

Mybatis多线程下使用Example的示例

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

感谢你能够认真阅读完这篇文章,希望小编分享的“Mybatis多线程下使用Example的示例”这篇文章对大家有帮助,同时也希望大家多多支持创新互联,关注创新互联行业资讯频道,更多相关知识等着你来学习!


分享文章:Mybatis多线程下使用Example的示例
转载来于:http://cdkjz.cn/article/jscehc.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220