spring实现简单读写分离

 想来笔者作为一名java新手,不是什么大牛,没有过人的编程天赋,只是一名普普通通的程序员,基础也不是很扎实,但是有一颗成为大牛的心,不断总结才会有进步,在成长的路上留下自己的轨迹。

 读写分离,就是两台数据库,一台做插入,另一台做查询,数据库之间数据如何同步,笔者能力有限,只能是简单的读写分离,普通的做法是定义多个数据源,利用hibernateTemplate或者是jdbcTemplate实现查询或者插入。利用spring提供的DriverManagerDataSource方法也能简单的实现。

        定义父数据源parentDataSource 

<bean id="parentDatasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
      <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
      <property name="username" value="root"/>
      <property name="password" value="root"/>
</bean>

定义主库和备库

        <!--主库-->

 <bean id="masterDataSource" parent="parentDatasource">
      <property name="url" value="jdbc:mysql://localhost:3306/mysql_master"/>
 </bean>

  <!--从库-->
   
  

<bean id="slaveDataSource" parent="parentDatasource">
      <property name="url" value="jdbc:mysql://localhost:3306/mysql_slave"/>
 </bean>

定义CustomerRoutingDataSource,根据不同的key得到不同的数据源

<bean id="dataSource" class="com.mall.dao.good.CustomerRoutingDataSource">
          <property name="targetDataSources">
             <map key-type="java.lang.String">
                  <entry key="master" value-ref="masterDataSource"></entry>
               	  <entry key="slave" value-ref="slaveDataSource"></entry>
            </map>
      	  </property>
          <property name="defaultTargetDataSource" ref="masterDataSource"></property>
</bean>

    CustomerRoutingDataSource.java  

 public class CustomerRoutingDataSource extends AbstractRoutingDataSource {
    protected Object determineCurrentLookupKey() {
        return CustomerContextHander.getCustomerType();
    }
  }

    CustomerContextHander.hava    

 

 public class CustomerContextHander {
    private static final ThreadLocal<String> contextHander=new ThreadLocal<String>();//线程安全
    public static void setCustomerType(String customerType){
        contextHander.set(customerType);
    }
    public static String getCustomerType(){
        return contextHander.get();
    }
    public static void clearCustomerType(){
        contextHander.remove();
    }
} 

   注入jdbcTemplate中,利用jdbcTemplate实现插入和查询

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
     <property name="dataSource" ref="dataSource"></property>
 </bean>

   测试  TestMaster.java

 

public class TestMaster {
 public static void main(String[] args){
     ApplicationContext ctx= new ClassPathXmlApplicationContext(new String[]{"/spring/applicationContext.xml"});//启动spring容器
     UserDao userDao = (UserDao)ctx.getBean("userDaoImpl");//获取userDaoImpl
     CustomerContextHander.setCustomerType("slave");//设置需要操作的数据库
     List<Users> users=userDao.getListAll();
     for(Users user:users){
         System.out.println(user.getUsername());
     }
 }
}

  简单的读写分离就实现了,笔者也是参考了别人的文章,自己动手实现,数据库中数据如何同步不知道如何实现,在以后的日子中慢慢探索吧,慢慢见证自己的成长之路,也希望大家都能不断提高,不断总结,成为大牛,借用自己非常喜欢的一句话,生活的每一天都是挑战,自己离成功还很远。 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值