SSM动态多数据源
1.定义MyThreadLocal类
public class MyThreadLocal {
public static final ThreadLocal<String> threadLocal=new ThreadLocal<String>();
public static String getDataSource() {
return threadLocal.get();
}
public static void setDataSource(String dataSource) {
threadLocal.set(dataSource);
}
}
2.自定义数据源MyDateSource
public class MyDateSource extends AbstractRoutingDataSource {
protected Object determineCurrentLookupKey() {
return MyThreadLocal.getDataSource();
}
}
3.在将自定义的数据源交给spring
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--3307数据源配置-->
<bean id="masterDataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://47.110.157.82:3307/dome"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
<!--3309数据源配置-->
<bean id="slaveDataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://47.110.157.82:3309/dome"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
<!--使用自定义数据源-->
<bean id="myDataSource" class="com.liu.config.MyDateSource">
<!--设置默认数据源-->
<property name="defaultTargetDataSource" ref="masterDataSource"></property>
<property name="targetDataSources">
<map>
<!--通过key来使用对应的数据源-->
<entry key="master" value-ref="masterDataSource"></entry>
<entry key="slave" value-ref="slaveDataSource"></entry>
</map>
</property>
</bean>
<!-- mybatis和spring完美整合,不需要mybatis的配置映射文件 -->
<bean id="masterSqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="myDataSource"/>
</bean>
<!-- Mapper动态代理开发,扫描dao接口包-->
<bean id="masterMapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 注入sqlSessionFactory -->
<property name="sqlSessionFactoryBeanName" value="masterSqlSessionFactory"/>
<!-- 给出需要扫描Dao接口包 -->
<property name="basePackage" value="com.liu.mapper"/>
</bean>
</beans>
4.在service实现类中切换数据源
@Service("userInfoService")
public class UserInfoServiceImpl implements UserInfoService {
@Resource
UserInfoMapper UserInfoMapper;
public List<UserInfo> findUserAll(UserInfoExample userInfoExample) {
MyThreadLocal.setDataSource("master");
return UserInfoMapper.selectByExample(userInfoExample);
}
public int addUser(UserInfo userInfo) {
MyThreadLocal.setDataSource("slave");
return UserInfoMapper.insert(userInfo);
}
}
5.测试
public static void main(String[] args) {
ApplicationContext applicationContext;
UserInfoService userInfoService;
applicationContext=new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");
userInfoService=(UserInfoService)applicationContext.getBean("userInfoService");
UserInfo userInfo= new UserInfo();
userInfo.setName("赵七");
userInfo.setAge(20);
userInfoService.addUser(userInfo);
UserInfoExample userInfoExample = new UserInfoExample();
UserInfoExample.Criteria criteria = userInfoExample.createCriteria();
criteria.andAgeIsNotNull();
List<UserInfo> userAll = userInfoService.findUserAll(userInfoExample);
System.out.println(userAll.get(0));
}