Spring, MyBatis????????????ú????
???????????? ???????[ 2015/5/27 14:53:18 ] ??????????????????
????????????????????????????? dataSource??????sqlSessionFactory??????transactionManager????????????????MapperScannerConfigurer ??????——???sqlSessionFactoryBeanName???????????sqlSessionFactory??????????????????????????????? mapper ???????????? sqlSessionFactory??
??????????????????????????????????????????????????????????У???????????????????????÷??????????????????????????master-slave?????????????????????????master-slave?????????????????????????????????????????????μ????á?????????Щ????????select?????????????slave????У???????update??delete???????????????master????е??????????Щ??????????select??????????????????master?????——????????????????????????????????????????????master????????????????????2??????????е?????????????????????????????master????У??????????slave?????У????slave???????????????????????????????????????????????????????????????
???????????master-slave?????????????????????????????????????????????Щselect??????slave?????Щselect??????slave????????????????????????????ò????????
????2. ???? AbstractRoutingDataSource ?? AOP ??????????????
????????????????????????????DataSource??ThreadLocalRountingDataSource???????AbstractRoutingDataSource??????????????????ThreadLocalRountingDataSource??? master ?? slave ??????????????? AOP ??????????????Щ?????? master ???????????Щ????????? slave??????????濴????????
????1??????????enum?????????????????
????package net.aazj.enums;
????/**
????* ??????????master/slave
????*/
????public enum DataSources {
????MASTER?? SLAVE
????}
????2????? TheadLocal ???????????????????????????(key)??
package net.aazj.util;
import net.aazj.enums.DataSources;
public class DataSourceTypeManager {
private static final ThreadLocal<DataSources> dataSourceTypes = new ThreadLocal<DataSources>(){
@Override
protected DataSources initialValue(){
return DataSources.MASTER;
}
};
public static DataSources get(){
return dataSourceTypes.get();
}
public static void set(DataSources dataSourceType){
dataSourceTypes.set(dataSourceType);
}
public static void reset(){
dataSourceTypes.set(DataSources.MASTER0);
}
}
????3?????? ThreadLocalRountingDataSource?????AbstractRoutingDataSource??
????package net.aazj.util;
????import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
????public class ThreadLocalRountingDataSource extends AbstractRoutingDataSource {
????@Override
????protected Object determineCurrentLookupKey() {
????return DataSourceTypeManager.get();
????}
????}
????4??????????????? ThreadLocalRountingDataSource ??? master ?? slave ?????????
<context:component-scan base-package="net.aazj.service??net.aazj.aop" />
<context:component-scan base-package="net.aazj.aop" />
<!-- ??????????? -->
<context:property-placeholder location="classpath:config/db.properties" />
<!-- ?????????Master -->
<bean name="dataSourceMaster" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="url" value="${jdbc_url}" />
<property name="username" value="${jdbc_username}" />
<property name="password" value="${jdbc_password}" />
<!-- ??????????С -->
<property name="initialSize" value="0" />
<!-- ????????????????? -->
<property name="maxActive" value="20" />
<!-- ????????? -->
<property name="maxIdle" value="20" />
<!-- ?????С???? -->
<property name="minIdle" value="0" />
<!-- ????????????? -->
<property name="maxWait" value="60000" />
</bean>
<!-- ?????????Slave -->
<bean name="dataSourceSlave" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="url" value="${jdbc_url_slave}" />
<property name="username" value="${jdbc_username_slave}" />
<property name="password" value="${jdbc_password_slave}" />
<!-- ??????????С -->
<property name="initialSize" value="0" />
<!-- ????????????????? -->
<property name="maxActive" value="20" />
<!-- ????????? -->
<property name="maxIdle" value="20" />
<!-- ?????С???? -->
<property name="minIdle" value="0" />
<!-- ????????????? -->
<property name="maxWait" value="60000" />
</bean>
<bean id="dataSource" class="net.aazj.util.ThreadLocalRountingDataSource">
<property name="defaultTargetDataSource" ref="dataSourceMaster" />
<property name="targetDataSources">
<map key-type="net.aazj.enums.DataSources">
<entry key="MASTER" value-ref="dataSourceMaster"/>
<entry key="SLAVE" value-ref="dataSourceSlave"/>
<!-- ???????????dataSource -->
</map>
</property>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:config/mybatis-config.xml" />
<property name="mapperLocations" value="classpath*:config/mappers/**/*.xml" />
</bean>
<!-- Transaction manager for a single JDBC DataSource -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- ???annotation???????? -->
<tx:annotation-driven transaction-manager="transactionManager" />
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="net.aazj.mapper" />
<!-- <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> -->
</bean>
????????spring??????????У????????master??????slave???????????dataSourceMaster??dataSourceSlave????dataSource????????<bean id="dataSource" class="net.aazj.util.ThreadLocalRountingDataSource"> ?У??????????dataSource?????????? key ?????????dataSourceMaster?? dataSourceSlave???
???????????????????????漰???????????????????SPASVOС??(021-61079698-8054)?????????????????????????
??????
??????????????????ù????????ù?????????????????????????????з????????????????ù????е?SVN???ù????????????汾????????cmmi???ù??????ù?????????????ù???????????(2)???ù???????????(1)SCM??????ù???????????????ù??????????????????????е????ù??????????????ù??????ù????????????汾?????????????????ù???Nginx???ù??? - ???????DCMP??????etcd?????ù?????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11????????
?????????App Bug???????????????????????Jmeter?????????QC??????APP????????????????app?????е????????jenkins+testng+ant+webdriver??????????????JMeter????HTTP???????Selenium 2.0 WebDriver ??????