Mysql读写分离

一、配置读写数据源datasource

<bean id="dataSourceWrite"class="org.apache.commons.dbcp.BasicDataSource"scope="singleton">

      <property name="driverClassName"value="${jdbc.driverClassName}" />

      <property name="url"value="${jdbc.url}" />

      <property name="username"value="${jdbc.username}" />

      <property name="password"value="${jdbc.password}" />

      <property name="maxActive"value="${jdbc.maxActive}" />

      <property name="initialSize"value="${jdbc.initialSize}" />

      <property name="validationQuery"value="select 1" />

      <property name="testWhileIdle"value="true" />

      <property name="minEvictableIdleTimeMillis"value="1800000" />

      <property name="minIdle"value="${jdbc.minIdle}" />

      <property name="maxIdle"value="${jdbc.maxIdle}" />

      <property name="removeAbandoned"value="${jdbc.removeAbandoned}" />

      <property name="removeAbandonedTimeout"value="${jdbc.removeAbandonedTimeout}" />

      <property name="logAbandoned"value="${jdbc.logAbandoned}" />

      <property name="maxWait"value="${jdbc.maxWait}" />

   </bean>

  

   <bean id="dataSourceReadOnly"class="org.apache.commons.dbcp.BasicDataSource"scope="singleton">

      <property name="driverClassName"value="${jdbc.driverClassName}" />

      <property name="url"value="${jdbc.url2}" />

      <property name="username"value="${jdbc.username}" />

      <property name="password"value="${jdbc.password}" />

      <property name="maxActive"value="${jdbc.maxActive}" />

      <property name="initialSize"value="${jdbc.initialSize}" />

      <property name="validationQuery"value="select 1" />

      <property name="testWhileIdle"value="true" />

      <property name="minEvictableIdleTimeMillis"value="1800000" />

      <property name="minIdle"value="${jdbc.minIdle}" />

      <property name="maxIdle"value="${jdbc.maxIdle}" />

      <property name="removeAbandoned"value="${jdbc.removeAbandoned}" />

      <property name="removeAbandonedTimeout"value="${jdbc.removeAbandonedTimeout}" />

      <property name="logAbandoned"value="${jdbc.logAbandoned}" />

      <property name="maxWait"value="${jdbc.maxWait}" />

   </bean>


二、配置读写sqlsessionfactory

<bean id="writeSqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean">

      <property name="dataSource"ref="dataSourceWrite"/>

      <property name="configLocation"value="classpath:mybatis/sqlmap-config.xml"/>

      <property name="mapperLocations"value="classpath:mybatis/write/**/*.xml"/>

      <property name="plugins">

            <array>

                <bean class="com.lifang.wkeyes.DbAccessInterceptor">

                </bean>

            </array>

        </property>

   </bean>

  

   <bean id="readSqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean">

      <property name="dataSource"ref="dataSourceReadOnly"/>

      <property name="configLocation"value="classpath:mybatis/sqlmap-config.xml"/>

      <property name="mapperLocations"value="classpath:mybatis/read/**/*.xml"/>

      <property name="plugins">

            <array>

                <bean class="com.lifang.wkeyes.DbAccessInterceptor">

                </bean>

            </array>

        </property>

   </bean>

三、指定读写的相应的mapper类,MapperScannerConfigurer

<bean id="writeMapperScannerConfigurer"class="org.mybatis.spring.mapper.MapperScannerConfigurer">

      <property name="basePackage"value="com.lifang.crm.dao.write"/>

      <property name="sqlSessionFactoryBeanName"value="writeSqlSessionFactory"/>

   </bean>

 

   <bean id="readMapperScannerConfigurer"class="org.mybatis.spring.mapper.MapperScannerConfigurer">

      <property name="basePackage"value="com.lifang.crm.dao.read"/>

      <property name="sqlSessionFactoryBeanName"value="readSqlSessionFactory"/>

   </bean>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值