zoukankan      html  css  js  c++  java
  • spring集成mybatis配置多个数据源,通过aop自动切换

    spring集成mybatis,配置多个数据源并自动切换。

    spring-mybatis.xml如下:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
                            http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
                            http://www.springframework.org/schema/context
                            http://www.springframework.org/schema/context/spring-context-3.1.xsd
                            http://www.springframework.org/schema/mvc
                            http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
    
        <!-- 引入配置文件 -->
        <bean id="propertyConfigurer"
                      class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations" >
            <list>
                <!--<value>classpath:db.properties </value>-->
                <value>classpath:generator.properties </value>
                <value>classpath:db-test.properties</value>
    
            </list>
    
        </property>
    </bean>
    
        <!--添加多个数据源-->
        <bean id="vbaseDataSource" class="org.apache.commons.dbcp.BasicDataSource"
              destroy-method="close">
            <property name="driverClassName" value="${driver}" />
            <property name="url" value="${baseUrl}" />
            <property name="username" value="${baseUser}" />
            <property name="password" value="${basePassword}" />
            <!-- 初始化连接大小 -->
            <property name="initialSize" value="${initialSize}"></property>
            <!-- 连接池最大数量 -->
            <property name="maxActive" value="${maxActive}"></property>
            <!-- 连接池最大空闲 -->
            <property name="maxIdle" value="${maxIdle}"></property>
            <!-- 连接池最小空闲 -->
            <property name="minIdle" value="${minIdle}"></property>
            <!-- 获取连接最大等待时间 -->
            <property name="maxWait" value="${maxWait}"></property>
        </bean>
    
    
        <bean id="vso1DataSource" class="org.apache.commons.dbcp.BasicDataSource"
              destroy-method="close">
            <property name="driverClassName" value="${driver}" />
            <property name="url" value="${vso1Url}" />
            <property name="username" value="${vso1User}" />
            <property name="password" value="${vso1Password}" />
            <!-- 初始化连接大小 -->
            <property name="initialSize" value="${initialSize}"></property>
            <!-- 连接池最大数量 -->
            <property name="maxActive" value="${maxActive}"></property>
            <!-- 连接池最大空闲 -->
            <property name="maxIdle" value="${maxIdle}"></property>
            <!-- 连接池最小空闲 -->
            <property name="minIdle" value="${minIdle}"></property>
            <!-- 获取连接最大等待时间 -->
            <property name="maxWait" value="${maxWait}"></property>
        </bean>
    
      <!--添加多个数据源-->
        <bean id="multipleDataSource" class="com.cmsz.crm.db.MultipleDataSource">
            <property name="defaultTargetDataSource" ref="vso1DataSource"/>
            <property name="targetDataSources">
                <map>
                    <entry key="vbaseDataSource" value-ref="vbaseDataSource"/>
                    <entry key="vso1DataSource" value-ref="vso1DataSource"/>
                </map>
            </property>
        </bean>
    
        <!-- spring和MyBatis整合,不需要mybatis的配置映射文件 -->
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="multipleDataSource" />
            <!-- 自动扫描mapping.xml文件 -->
            <property name="mapperLocations" value="classpath:com/cmsz/crm/mapper/*.xml"/>
            <!-- 配置分页插件 -->
            <property name="plugins">
                <array>
                    <!--pagehelper5.0版本需要设置成PageInterceptor,dialect改成helperDialect-->
                    <bean class="com.github.pagehelper.PageInterceptor">
                        <property name="properties">
                            <value>
                                helperDialect=mysql
                                reasonable=true
                            </value>
                        </property>
                    </bean>
                </array>
            </property>
        </bean>
    
        <bean id="baseMapper" class="org.mybatis.spring.mapper.MapperFactoryBean" abstract="true">
            <property name="sqlSessionFactory" ref="sqlSessionFactory" />
        </bean>
    
        <!--新建MapperFactoryBean实例,继承自baseMapper,指定dao层的接口-->
        <bean id="workOrderMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"
              parent="baseMapper">
            <property name="mapperInterface"
                      value="com.cmsz.crm.dao.WorkOrderDao" />
        </bean>
    
        <bean id="esbTraceMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"
              parent="baseMapper">
            <property name="mapperInterface"
                      value="com.cmsz.crm.dao.EsbTraceDao" />
        </bean>
    
    
        <!-- (事务管理)transaction manager -->
        <bean id="transactionManager"
              class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="multipleDataSource" />
        </bean>
    
        <!-- DAO接口所在包名,Spring会自动查找其下的类,并将其定义为一个Spring Bean -->
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <property name="basePackage" value="com.cmsz.crm.dao" />
            <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
        </bean>
    
    
    </beans>

    spring-mvc配置如下:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
                                http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
                                http://www.springframework.org/schema/context
                                http://www.springframework.org/schema/context/spring-context-3.1.xsd
                                http://www.springframework.org/schema/mvc
                                http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
                                http://www.springframework.org/schema/aop
                                http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">
        <!--处理静态资源-->
        <mvc:default-servlet-handler/>
        <!--扫描目录-->
        <context:component-scan base-package="com.cmsz.crm" />
        <!--注解驱动-->
        <mvc:annotation-driven/>
         <!--aop相关-->
        <aop:aspectj-autoproxy />
     
    
        <!--避免IE执行AJAX时,返回JSON出现下载文件 -->
        <bean id="mappingJacksonHttpMessageConverter"
              class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
            <property name="supportedMediaTypes">
                <list>
                    <value>text/html;charset=UTF-8</value>
                </list>
            </property>
        </bean>
        <!-- 启动SpringMVC的注解功能,完成请求和注解POJO的映射 -->
        <bean
                class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
            <property name="messageConverters">
                <list>
                    <ref bean="mappingJacksonHttpMessageConverter" /> <!-- JSON转换器 -->
                </list>
            </property>
        </bean>
        <!-- 定义跳转的文件的前后缀 ,视图模式配置-->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <!-- 这里的配置我的理解是自动给后面action的方法return的字符串加上前缀和后缀,变成一个 可用的url地址 -->
            <property name="prefix" value="/" />
            <property name="suffix" value=".jsp" />
        </bean>
    
        <!-- 配置文件上传,如果没有使用文件上传可以不用配置,当然如果不配,那么配置文件中也不必引入上传组件包 -->
        <bean id="multipartResolver"
              class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
            <!-- 默认编码 -->
            <property name="defaultEncoding" value="utf-8" />
            <!-- 文件大小最大值 -->
            <property name="maxUploadSize" value="10485760000" />
            <!-- 内存中的最大值 -->
            <property name="maxInMemorySize" value="40960" />
        </bean>
    
    </beans>

    Dao层如下:

    EsbTraceDao如下:

    public interface EsbTraceDao {
    
        List<EsbTrace> getTraceListByTime(
                           @Param("startTime") String startTime,@Param("endTime")  String endTime);
    }

    WorkOrderDao如下:

    public interface WorkOrderDao {
        WorkOrder selectByPrimaryKey(@Param("id") Long id);
    
    }

    Service层的如下:

    EsbTraceServiceImpl.java如下:

    注意,要在@Service注解后方写上Service名称,这样方便在测试代码中调用。

    @Service("esbTraceService")
    public class EsbTraceServiceImpl implements EsbTraceService {
        @Autowired
        private EsbTraceDao esbTraceDao;
    
        @Override
        public List<EsbTrace> getTraceListByTime( String startTime, String endTime) {
            return esbTraceDao.getTraceListByTime( startTime, endTime);
        }
    
    }

    WorkServiceImpl如下:

    @Service("workOrderService")
    public class WorkOrderServiceImpl  implements WorkOrderService{
        @Autowired
        private WorkOrderDao workOrderDao;
    
        @Override
        public WorkOrder selectByPrimaryKey(Long id) {
            return workOrderDao.selectByPrimaryKey(id);
        }
    }

    以上是基本的文件,接下来的是数据源相关的设置。

    通过继承AbstractRoutingDataSource,来设置DataSourceKey。

    public class MultipleDataSource extends AbstractRoutingDataSource {
        private static final ThreadLocal<String> threadLocaldataSource = new InheritableThreadLocal<String>();
    
        public static void setDataSourceKey(String dataSource) {
            threadLocaldataSource.set(dataSource);
        }
    
        @Override
        protected Object determineCurrentLookupKey() {
            return threadLocaldataSource.get();
        }
    }

    通过aop自动切换数据源,如下:

    aop依赖包的导入,aop注解,以及表达式的理解,可以参考博客:

    https://blog.csdn.net/yerenyuan_pku/article/details/52865330

    @Component
    @Aspect
    @Order(0)         //使aop的启动顺序先于事务。这样能在事务之前切换数据源。此处未经验证,存疑。
    public class MultipleDataSourceAspectAdvice {
        public  MultipleDataSourceAspectAdvice(){
            System.out.println("==========================>MultipleDataSourceAspectAdvice构造函数。");
        }
    
        @Pointcut("execution (* com.cmsz.crm.dao.*.*(..))")
        private void cutMethod() {} // 声明一个切入点,cutMethod为切入点名称
    
    
        //拦截对应的切入点
        @Around("cutMethod()")
        public Object doAround(ProceedingJoinPoint jp) throws Throwable {
            if (jp.getTarget() instanceof EsbTraceDao) {
                MultipleDataSource.setDataSourceKey("vbaseDataSource");
            } else if (jp.getTarget() instanceof WorkOrderDao) {
                MultipleDataSource.setDataSourceKey("vso1DataSource");
            }else {
                System.out.println("==================>proceedingJoinPoint.getTarget():"+jp.getTarget());
            }
            return jp.proceed();
        }
    }

    通过Service层操作的测试代码如下:

    public class SpringTest {
        public static void main(String[] args) {
            ApplicationContext applicationContext=new ClassPathXmlApplicationContext(new String[]{"spring-mvc.xml","spring-mybatis.xml"});
    //获取Servicen层的对象        
            WorkOrderService workOrderService=(WorkOrderService) applicationContext.getBean("workOrderService");
            EsbTraceService esbTraceService=(EsbTraceService)applicationContext.getBean("esbTraceService");
    //下面这行用来手动切换数据源,注释后通过MultipleDataSourceAspectAdvice中的aop自动切换数据源
    //        MultipleDataSource.setDataSourceKey("vso1DataSource");
            
            WorkOrder workOrder= workOrderService.selectByPrimaryKey(100000004220380L);
            System.out.println("===================>BillId:"+workOrder.getBillId());
            System.out.println("===================>自动切换数据源查询.");
    
            List<EsbTrace> esbTraceList=   esbTraceService.getTraceListByTime("ESB_TRACE_20180528",
                    "2018-05-28 09:00:00","2018-05-28 23:59:59");
            for(EsbTrace esbTrace: esbTraceList) {
                System.out.println(  esbTrace.getMondId()+"  "+esbTrace.getInterfaceCode());
            }
    
        }
    }

    注意:

    以上测试代码中是通过Service层去操作,如果想直接通过dao层接口去操作,

    那么mybatis.xml的配置中,MapperFactoryBean类的实例bean的id要和Dao层接口名一致。 如下示:  

      <bean id="workOrderDao" class="org.mybatis.spring.mapper.MapperFactoryBean"
              parent="baseMapper">
            <property name="mapperInterface"
                      value="com.cmsz.crm.dao.WorkOrderDao" />
        </bean>

    通过Dao层直接访问的测试代码如下:

    public class WorkOrderTest {
        public static void main(String[] args) {
          ApplicationContext applicationContext=new ClassPathXmlApplicationContext(new String[]{"spring-mvc.xml","spring-mybatis.xml"});
    WorkOrderDao workOrderDao= applicationContext.getBean(WorkOrderDao.class);
    WorkOrder workOrder= workOrderDao.selectByPrimaryKey(100000004220380L);
    System.out.println("===================>BillId:"+ workOrder.getBillId());
       }
    }

    参考博客:

    https://www.cnblogs.com/lzrabbit/p/3750803.html

    https://blog.csdn.net/yizhenn/article/details/52384607

    https://blog.csdn.net/yerenyuan_pku/article/details/52865330

  • 相关阅读:
    uwsgi 的启动停止重启
    项目部署(Python3 + ubuntu 16.04(ali server) + Nginx + uWSGI + Django 1.11)
    阿里云连接上Mysql数据库(10061 errors)
    本地数据库上传到阿里云Mysql
    Django 用户的修改密码接口
    django 的忘记密码接口
    Git光速入门
    缓存问题:一致性、穿击、穿透、雪崩、污染
    redis进阶-高可用:主从复制详解
    Redis分片技术(Redis Cluster)
  • 原文地址:https://www.cnblogs.com/expiator/p/9213912.html
Copyright © 2011-2022 走看看