SSM动态多数据源

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));
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

桀骜浮沉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值