hap配置多数据源

ap官方文档地址;http://eco.hand-china.com/doc/hap/latest/dev_guide/01.getting_start/09_multi_data_source.html

官方文档没有讲解hap默认配置,只是讲了怎么配置多数据源,对于我第一次配置的时候,不免遇到很多问题,下面是我对问题的描述跟解决方案

 

Hap多数据源 是已mapper作为维度来配置的,即mapper下的方法都用一个数据源

 

hap默认配置单数据源,配置文件为applicationContext.xml文件,

故我们使用配置的时候,尽量避免用此文件名,否则会导致hap很多类都初始化加载不对,从而导致启动报错

我们可以看WEB-INF/classes/spring/applicationContext.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:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
                           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
                           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
                           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
                           http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd">
    <context:property-placeholder location="classpath:config.properties"/>
    <bean id="coreApplicationListener" class="com.hand.hap.core.ApplicationListenerBean"/>
    <!-- middleware datasource  com.hand.hap.core.JndiObjectFactoryBean-->
    <!-- jndi dataSource-->
    <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiName" value="${db.jndiName}"/>
    </bean>
    <!-- builtin datasource -->
    <!--<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">-->
    <!--<property name="driverClassName"-->
    <!--value="com.mysql.jdbc.Driver"/>-->
    <!--<property name="url" value="jdbc:mysql://127.0.0.1:3306/hap_dev"/>-->
    <!--<property name="username" value="hap_dev"/>-->
    <!--<property name="password" value="hap_dev"/>-->
    <!--</bean>-->
    <bean id="languageProvider" class="com.hand.hap.core.impl.LanguageProviderImpl"/>
    <bean id="messageSource" class="com.hand.hap.core.i18n.CacheMessageSource"/>
    <!-- config transactionManager -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <tx:annotation-driven transaction-manager="transactionManager"/>
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="mapperLocations" value="classpath*:/**/*Mapper.xml"/>
        <property name="plugins">
            <array>
                <bean class="com.hand.hap.core.interceptor.RequestContextInterceptor"/>
                <bean class="com.hand.hap.core.interceptor.MultiLanguageInterceptor"/>
                <bean class="com.hand.hap.core.interceptor.SecurityTokenInterceptor"/>
                <bean class="com.hand.hap.core.interceptor.OvnInterceptor"/>
                <bean class="com.hand.hap.core.interceptor.AuditInterceptor"/>
                <bean class="com.github.pagehelper.PageHelper"/>
                <bean class="com.hand.hap.core.interceptor.CacheJoinInterceptor">
                    <property name="cacheJoinType" ref="cacheJoinType"></property>
                </bean>
            </array>
        </property>
        <property name="configLocation" value="classpath:mybatis-configuration.xml"/>
    </bean>
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg index="0" name="sqlSessionFactory" ref="sqlSessionFactory"/>
    </bean>
    <!-- mapper definition -->
    <bean id="mapperScannerConfigurer" class="com.hand.hap.mybatis.spring.MapperScannerConfigurer">
        <property name="basePackage" value="*.**.mapper"/>
        <property name="processPropertyPlaceHolders" value="true"/>
        <property name="propertiesMap">
            <map>
                <entry key="mappers" value="com.hand.hap.mybatis.common.Mapper"/>
                <entry key="IDENTITY" value="${mybatis.identity}"/>
                <entry key="dataBaseType" value="${db.type}"/>
                <entry key="seqFormat" value="{3}_s.nextVal"/>
                <entry key="enableMethodAnnotation" value="true"/>
            </map>
        </property>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
    </bean>
    <context:component-scan base-package="**.*.service"/>
    <context:component-scan base-package="**.*.components"/>
    <bean id="requestAD" class="com.hand.hap.core.impl.ServiceExecutionAdvice"/>
    <aop:config expose-proxy="true">
        <aop:pointcut id="servicePointcut" expression="execution(* *..*ServiceImpl.*(..))"/>
        <aop:advisor advice-ref="requestAD" pointcut-ref="servicePointcut"/>
    </aop:config>
    <!-- 密码加密 PasswordEncoder-->
    <bean id="passwordManager" class="com.hand.hap.security.PasswordManager">
        <property name="siteWideSecret" value="Zxa1pO6S6uvBMlY"/>
    </bean>
    <!-- 线程池 -->
    <bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
        <!-- 核心线程数 -->
        <property name="corePoolSize" value="5"/>
        <!-- 最大线程数 -->
        <property name="maxPoolSize" value="250"/>
        <!-- 队列最大长度 -->
        <property name="queueCapacity" value="1000"/>
        <!-- 线程池维护线程所允许的空闲时间,默认为60s -->
        <property name="keepAliveSeconds" value="120"/>
    </bean>
    <bean id="freeMarkerBeanProvider" class="com.hand.hap.core.web.FreeMarkerBeanProvider"/>
    <bean id="distributeLockTemplate" class="com.hand.hap.lock.impl.DistributedLockFactoryBean">
        <property name="mode" value="MULTIPLE"/>
    </bean>
</beans>

可以看一下这个配置文件配置的维度是项目下所有的mapper跟mapper.xml文件,如果在你项目路径下添加一个applicationContext.xml文件,必须把hap资源的ben初始化

 

理解了hap的单数据源加载方式之后,然后去看hap文档提供的多数据源配置http://eco.hand-china.com/doc/hap/latest/dev_guide/01.getting_start/09_multi_data_source.html

 

我这里也是用applicationContext.xml文件配置。

1.首先添加的包名

    这里我想到一种配置方式:com目录下,zentao的文件用第二个数据源,其他的还是用第一个数据源。当然,方法不止我一种

    也可以,com目录全用第一个数据源,在com同级目录新建一个文件夹,配置mapper扫描目录对应修改即可

下面是我的配置文件

 

<?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:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
                           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
                           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
                           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
                           http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd">
    <context:property-placeholder location="classpath:config.properties"/>
    <bean id="coreApplicationListener" class="com.hand.hap.core.ApplicationListenerBean"/>
    <!-- middleware datasource  com.hand.hap.core.JndiObjectFactoryBean-->
    <!-- jndi dataSource-->
    <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiName" value="${db.jndiName}"/>
    </bean>
    <bean id="dataSource2" class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiName" value="${db2.jndiName}"/>
    </bean>
    <!-- builtin datasource -->
    <!--<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">-->
    <!--<property name="driverClassName"-->
    <!--value="com.mysql.jdbc.Driver"/>-->
    <!--<property name="url" value="jdbc:mysql://127.0.0.1:3306/hap_dev"/>-->
    <!--<property name="username" value="hap_dev"/>-->
    <!--<property name="password" value="hap_dev"/>-->
    <!--</bean>-->
    <bean id="languageProvider" class="com.hand.hap.core.impl.LanguageProviderImpl"/>
    <bean id="messageSource" class="com.hand.hap.core.i18n.CacheMessageSource"/>
    <!-- config transactionManager -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <bean id="transactionManager2" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource2"/>
    </bean>
    <tx:annotation-driven transaction-manager="transactionManager"/>
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="mapperLocations">
            <array>
                <!--<value>classpath*:/com/fsl/operate/**/*Mapper.xml</value>-->
                <value>classpath*:/**/*Mapper.xml</value>
                <!--<value>classpath*:/com/fsl/lcp/**/*Mapper.xml</value>
                <value>classpath*:/com/hand/hap/**/*Mapper.xml</value>-->
            </array>
        </property>
        <property name="plugins">
            <array>
                <bean class="com.hand.hap.core.interceptor.RequestContextInterceptor"/>
                <bean class="com.hand.hap.core.interceptor.MultiLanguageInterceptor"/>
                <bean class="com.hand.hap.core.interceptor.SecurityTokenInterceptor"/>
                <bean class="com.hand.hap.core.interceptor.OvnInterceptor"/>
                <bean class="com.hand.hap.core.interceptor.AuditInterceptor"/>
                <bean class="com.github.pagehelper.PageHelper"/>
                <bean class="com.hand.hap.core.interceptor.CacheJoinInterceptor">
                    <property name="cacheJoinType" ref="cacheJoinType"></property>
                </bean>
                <!--add by junYang 2018/12/04 audit-->
                <bean class="com.fsl.lcp.audit.interceptor.AuditExplainInterceptor"/>
                <!--add by tanqian 2018/12/04  auth-->
                <bean class="com.fsl.lcp.auth.interceptor.AuthLcpInterceptor"/>
            </array>
        </property>
        <property name="configLocation" value="classpath:mybatis-configuration.xml"/>
    </bean>
    <bean id="sqlSessionFactory2" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource2"/>
        <property name="mapperLocations">
            <array>
                <value>classpath*:/com/zentao/**/*Mapper.xml</value>
            </array>
        </property>
        <property name="plugins">
            <array>
                <bean class="com.hand.hap.core.interceptor.RequestContextInterceptor"/>
                <bean class="com.hand.hap.core.interceptor.MultiLanguageInterceptor"/>
                <bean class="com.hand.hap.core.interceptor.SecurityTokenInterceptor"/>
                <bean class="com.hand.hap.core.interceptor.OvnInterceptor"/>
                <bean class="com.hand.hap.core.interceptor.AuditInterceptor"/>
                <bean class="com.github.pagehelper.PageHelper"/>
                <bean class="com.hand.hap.core.interceptor.CacheJoinInterceptor">
                    <property name="cacheJoinType" ref="cacheJoinType"></property>
                </bean>
                <!--add by junYang 2018/12/04 audit-->
                <bean class="com.fsl.lcp.audit.interceptor.AuditExplainInterceptor"/>
                <!--add by tanqian 2018/12/04  auth-->
                <bean class="com.fsl.lcp.auth.interceptor.AuthLcpInterceptor"/>
            </array>
        </property>
        <property name="configLocation" value="classpath:mybatis-configuration.xml"/>
    </bean>
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg index="0" name="sqlSessionFactory" ref="sqlSessionFactory"/>
    </bean>
    <!-- mapper definition -->
    <bean id="mapperScannerConfigurer" class="com.hand.hap.mybatis.spring.MapperScannerConfigurer">
        <property name="basePackage" value="com.hand.hap.**.mapper,com.fsl.**.mapper"/>
        <property name="processPropertyPlaceHolders" value="true"/>
        <property name="propertiesMap">
            <map>
                <entry key="mappers" value="com.hand.hap.mybatis.common.Mapper"/>
                <entry key="mappers" value="com.fsl.lcp.mybatis.common.ExtensionMapper"/>
                <entry key="IDENTITY" value="${mybatis.identity}"/>
                <entry key="dataBaseType" value="${db.type}"/>
                <entry key="seqFormat" value="{3}_s.nextVal"/>
                <entry key="enableMethodAnnotation" value="true"/>
            </map>
        </property>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
    </bean>
    <bean id="mapperScannerConfigurer2" class="com.hand.hap.mybatis.spring.MapperScannerConfigurer">
        <property name="basePackage" value="com.zentao.**.mapper"/>
        <property name="processPropertyPlaceHolders" value="true"/>
        <property name="propertiesMap">
            <map>
                <entry key="mappers" value="com.hand.hap.mybatis.common.Mapper"/>
                <entry key="mappers" value="com.fsl.lcp.mybatis.common.ExtensionMapper"/>
                <entry key="IDENTITY" value="${mybatis.identity}"/>
                <entry key="dataBaseType" value="${db2.type}"/>
                <entry key="seqFormat" value="{3}_s.nextVal"/>
                <entry key="enableMethodAnnotation" value="true"/>
            </map>
        </property>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory2"/>
    </bean>
    <context:component-scan base-package="**.*.service"/>
    <!--<context:component-scan base-package="com.fsl.operate.service"/>-->
    <!--<context:component-scan base-package="com.fsl.zentao.service"/>-->
    <context:component-scan base-package="**.*.components"/>
    <bean id="requestAD" class="com.hand.hap.core.impl.ServiceExecutionAdvice"/>
    <aop:config expose-proxy="true">
        <aop:pointcut id="servicePointcut" expression="execution(* *..*ServiceImpl.*(..))"/>
        <aop:advisor advice-ref="requestAD" pointcut-ref="servicePointcut"/>
    </aop:config>
    <!-- 密码加密 PasswordEncoder-->
    <bean id="passwordManager" class="com.hand.hap.security.PasswordManager">
        <property name="siteWideSecret" value="Zxa1pO6S6uvBMlY"/>
    </bean>
    <!-- 线程池 -->
    <bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
        <!-- 核心线程数 -->
        <property name="corePoolSize" value="5"/>
        <!-- 最大线程数 -->
        <property name="maxPoolSize" value="250"/>
        <!-- 队列最大长度 -->
        <property name="queueCapacity" value="1000"/>
        <!-- 线程池维护线程所允许的空闲时间,默认为60s -->
        <property name="keepAliveSeconds" value="120"/>
    </bean>
    <bean id="freeMarkerBeanProvider" class="com.hand.hap.core.web.FreeMarkerBeanProvider"/>
    <bean id="distributeLockTemplate" class="com.hand.hap.lock.impl.DistributedLockFactoryBean">
        <property name="mode" value="MULTIPLE"/>
    </bean>
</beans>

下面是我对多数据源配置加载的一个理解

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值