1. 程式人生 > >spring + mybatis + mysql(主從) 配置多個resource(讀寫分離)

spring + mybatis + mysql(主從) 配置多個resource(讀寫分離)

改造原有的專案 實現讀寫分離

準備工作:
1.需要一個能夠執行的spring + mybatis的專案
2.mysql 主從資料庫

  • 拆分Dao(按照讀寫去拆分)
    這裡寫圖片描述

  • 拆分mapping檔案
    這裡寫圖片描述

  • 新增資料庫配置檔案(jdbc.properties)
    這裡寫圖片描述

  • *呼叫(需要用哪個就在service中注入相應的dao)
    這裡寫圖片描述

  • 修改applicationContenxt.xml

<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"
> <!-- 自動掃描 --> <context:component-scan base-package="com.pkl.dubbo" /> <!-- 引入配置檔案 --> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list
> <value>classpath:
jdbc.properties</value> <value>classpath:redis.properties</value> </list> </property> </bean> <bean id="masterDataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <property name="url" value="${url}" /> <property name="username" value="${username}" /> <property name="password" value="${password}" /> <!--初始化連線大小 --> <property name="initialSize" value="${initialSize}"></property> <!-- 連線池最大數量 --> <property name="maxActive" value="${maxActive}"></property> <!-- 連線池最大空閒 --> <property name="maxIdle" value="${maxIdle}"></property> <!-- 連線池最小空閒 --> <property name="minIdle" value="${minIdle}"></property> <!-- 獲取連線最大等待時間 --> <property name="maxWait" value="${maxWait}"></property> <!-- 配置間隔多久才進行一次檢測,檢測需要關閉的空閒連線,單位是毫秒 --> <property name="timeBetweenEvictionRunsMillis" value="60000" /> <!-- 配置一個連線在池中最小生存的時間,單位是毫秒 --> <property name="minEvictableIdleTimeMillis" value="300000" /> <property name="validationQuery" value="SELECT 'x'" /> <property name="testWhileIdle" value="true" /> <property name="testOnBorrow" value="false" /> <property name="testOnReturn" value="false" /> <!-- 開啟PSCache,並且制定每個連線PSCache的大小 --> <property name="poolPreparedStatements" value="true" /> <property name="maxPoolPreparedStatementPerConnectionSize" value="50" /> <!-- 配置監控統計攔截的filters,去掉後監控介面的sql無法統計 加log4j 是為了打印出獲取conn和回收connlog--> <property name="filters" value="stat,log4j" /> </bean> <bean id="slaveDataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <property name="url" value="${slave_url}" /> <property name="username" value="${slave_username}" /> <property name="password" value="${slave_password}" /> <property name="initialSize" value="${initialSize}"></property> <property name="maxActive" value="${maxActive}"></property> <property name="maxIdle" value="${maxIdle}"></property> <property name="minIdle" value="${minIdle}"></property> <property name="maxWait" value="${maxWait}"></property> <property name="timeBetweenEvictionRunsMillis" value="60000" /> <property name="minEvictableIdleTimeMillis" value="300000" /> <property name="validationQuery" value="SELECT 'x'" /> <property name="testWhileIdle" value="true" /> <property name="testOnBorrow" value="false" /> <property name="testOnReturn" value="false" /> <property name="poolPreparedStatements" value="true" /> <property name="maxPoolPreparedStatementPerConnectionSize" value="50" /> <property name="filters" value="stat,log4j" /> </bean> <!-- (事務管理)transaction manager, use JtaTransactionManager for global tx --> <bean id="masterTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="masterDataSource" /> </bean> <bean id="masterTransactionTemplate" class="org.springframework.transaction.support.TransactionTemplate"> <property name="transactionManager" ref="masterTransactionManager" /> </bean> <!-- springMyBatis完美整合,不需要mybatis的配置對映檔案 --> <bean id="masterSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="masterDataSource" /> <!-- 自動掃描mapping.xml檔案 --> <property name="mapperLocations" value="classpath:mapping/write/*.xml"></property> <property name="plugins"> <array> <ref bean="sqlStatementInterceptor"/> </array> </property> </bean> <bean id="sqlStatementInterceptor" class="com.pkl.dubbo.interceptor.MybatisInterceptor"></bean> <!-- DAO介面所在包名,Spring會自動查詢其下的類 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.pkl.dubbo.dao.write" /> <property name="sqlSessionFactoryBeanName" value="masterSessionFactory"></property> </bean> <!-- slave --> <bean id="slaveTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="slaveDataSource" /> </bean> <bean id="slaveTransactionTemplate" class="org.springframework.transaction.support.TransactionTemplate"> <property name="transactionManager" ref="slaveTransactionManager" /> </bean> <bean id="slaveSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="slaveDataSource" /> <property name="mapperLocations" value="classpath:mapping/read/*.xml"></property> <property name="plugins"> <array> <ref bean="sqlStatementInterceptor"/> </array> </property> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.pkl.dubbo.dao.read" /> <property name="sqlSessionFactoryBeanName" value="slaveSessionFactory"></property> </bean> <!-- Configuration transaction advice --> <tx:advice id="txAdvice" transaction-manager="masterTransactionManager"> <tx:attributes> <tx:method name="add*" propagation="REQUIRED" /> <tx:method name="update*" propagation="REQUIRED" /> <tx:method name="delete*" propagation="REQUIRED" /> <tx:method name="get*" read-only="true" propagation="SUPPORTS" /> <tx:method name="list*" read-only="true" propagation="SUPPORTS" /> </tx:attributes> </tx:advice> <!-- Configuration transaction aspect --> <aop:config> <aop:pointcut id="systemServicePointcut" expression="execution(* com.pkl.dubbo.service.*.*(..))" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="systemServicePointcut" /> </aop:config> </beans>
  • *我使用的連線池是Druid(以下是依賴資訊)
<!-- alibaba DB connection Pool -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.0.16</version>
        </dependency>