1. 數據源也就是jdbc.properties,因為是主從讀寫分離,那么肯定有兩個數據源了
jdbc.driver=org.mariadb.jdbc.Driver # 從庫,只讀 slave.jdbc.url=jdbc:mariadb://xxx.xxx.xxx.xxx:3306/xxx?characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&noAccessToProcedureBodies=true&autoReconnect=true slave.jdbc.username=xxx slave.jdbc.password=xxx # 主庫,需要寫 master.jdbc.url=jdbc:mariadb://xxx.xxx.xxx.xxx:3306/xxx?characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&noAccessToProcedureBodies=true&autoReconnect=true master.jdbc.username=xxx master.jdbc.password=xxx
這個非常簡單和普通的區別不是很大,另外數據庫的驅動是:mariadb,動下面就是第二個配置文件spring.xml
2. spring.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:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd "> <!-- Import properties file --> <context:property-placeholder location="classpath:jdbc.properties" /> <!-- Auto Scan --> <context:component-scan base-package="cn.bridgeli.demo" /> </beans>
這個文件很簡單,有兩個作用,①. 引入數據源配置文件;②. spring掃描的文件的路徑
3. spring-mybatis.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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd "> <bean id="slaveDataSourceImpl" class="com.jolbox.bonecp.BoneCPDataSource" destroy-method="close"> <property name="driverClass" value="${jdbc.driver}" /> <property name="jdbcUrl" value="${slave.jdbc.url}" /> <property name="username" value="${slave.jdbc.username}" /> <property name="password" value="${slave.jdbc.password}" /> <!-- 檢查數據庫連接池中空閑連接的間隔時間,單位是分,默認值:240,如果要取消則設置為0 --> <property name="idleConnectionTestPeriodInMinutes" value="10" /> <!-- 連接池中未使用的鏈接最大存活時間,單位是分,默認值:60,如果要永遠存活設置為0 --> <property name="idleMaxAgeInMinutes" value="10" /> <!-- 每個分區最大的連接數 --> <property name="maxConnectionsPerPartition" value="20" /> <!-- 每個分區最小的連接數 --> <property name="minConnectionsPerPartition" value="10" /> <!-- 分區數 ,默認值2,最小1,推薦3-4,視應用而定 --> <property name="partitionCount" value="3" /> <!-- 每次去拿數據庫連接的時候一次性要拿幾個,默認值:2 --> <property name="acquireIncrement" value="3" /> <!-- 緩存prepared statements的大小,默認值:0 --> <property name="statementsCacheSize" value="50" /> <!-- 在做keep-alive的時候的SQL語句 --> <property name="connectionTestStatement" value="select 1 from dual" /> <!-- 在每次到數據庫取連接的時候執行的SQL語句,只執行一次 --> <property name="initSQL" value="select 1 from dual" /> <property name="closeConnectionWatch" value="false" /> <property name="logStatementsEnabled" value="true" /> <property name="transactionRecoveryEnabled" value="true" /> </bean> <bean id="masterDataSourceImpl" class="com.jolbox.bonecp.BoneCPDataSource" destroy-method="close"> <property name="driverClass" value="${jdbc.driver}" /> <property name="jdbcUrl" value="${master.jdbc.url}" /> <property name="username" value="${master.jdbc.username}" /> <property name="password" value="${master.jdbc.password}" /> <!-- 檢查數據庫連接池中空閑連接的間隔時間,單位是分,默認值:240,如果要取消則設置為0 --> <property name="idleConnectionTestPeriodInMinutes" value="10" /> <!-- 連接池中未使用的鏈接最大存活時間,單位是分,默認值:60,如果要永遠存活設置為0 --> <property name="idleMaxAgeInMinutes" value="10" /> <!-- 每個分區最大的連接數 --> <property name="maxConnectionsPerPartition" value="20" /> <!-- 每個分區最小的連接數 --> <property name="minConnectionsPerPartition" value="10" /> <!-- 分區數 ,默認值2,最小1,推薦3-4,視應用而定 --> <property name="partitionCount" value="3" /> <!-- 每次去拿數據庫連接的時候一次性要拿幾個,默認值:2 --> <property name="acquireIncrement" value="3" /> <!-- 緩存prepared statements的大小,默認值:0 --> <property name="statementsCacheSize" value="50" /> <!-- 在做keep-alive的時候的SQL語句 --> <property name="connectionTestStatement" value="select 1 from dual" /> <!-- 在每次到數據庫取連接的時候執行的SQL語句,只執行一次 --> <property name="initSQL" value="select 1 from dual" /> <property name="closeConnectionWatch" value="false" /> <property name="logStatementsEnabled" value="true" /> <property name="transactionRecoveryEnabled" value="true" /> </bean> <!-- DataSource/Master --> <bean id="masterDataSource" class="org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy"> <property name="targetDataSource" ref="masterDataSourceImpl" /> </bean> <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> <!-- DataSource/Slave --> <bean id="slaveDataSource" class="org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy"> <property name="targetDataSource" ref="slaveDataSourceImpl" /> </bean> <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> <!-- Mybatis/Master --> <bean id="masterSqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="masterDataSource"></property> <property name="configLocation" value="classpath:mybatis.xml" /> <property name="typeAliasesPackage" value="cn.bridgeli.demo.entity" /> <property name="mapperLocations"> <list> <value>classpath:cn/bridgeli/demo/mapper/master/*.xml</value> </list> </property> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="cn.bridgeli.demo.mapper.master" /> <property name="sqlSessionFactoryBeanName" value="masterSqlSessionFactory" /> </bean> <!-- Mybatis/Slave --> <bean id="slaveSqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="slaveDataSource"></property> <property name="configLocation" value="classpath:mybatis.xml" /> <property name="typeAliasesPackage" value="cn.bridgeli.demo.entity" /> <property name="mapperLocations"> <list> <value>classpath:cn/bridgeli/demo/mapper/slave/*.xml</value> </list> </property> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="cn.bridgeli.demo.mapper.slave" /> <property name="sqlSessionFactoryBeanName" value="slaveSqlSessionFactory" /> </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(* cn.bridgeli.demo.service.*.*(..))" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="systemServicePointcut" /> </aop:config> </beans>
這個配置文件老夫是完整的copy了下來,看起來也比較易懂,就不做解釋了,需要說明的mybatis下那些dao的接口,分別對應cn.bridgeli.demo.mapper.master、cn.bridgeli.demo.mapper.slave,cn.bridgeli.demo.mapper.master下的這些dao接口是要寫的,另一個是讀的,這些接口對應的配置文件肯定就是他們對應的文件夾下面的xml文件了,在將來的項目中幾乎可以照抄
4. mybatis的配置文件mybatis.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <settings> <setting name="cacheEnabled" value="true" /> <setting name="lazyLoadingEnabled" value="true" /> </settings> <plugins> <plugin interceptor="com.github.miemiedev.mybatis.paginator.OffsetLimitInterceptor"> <property name="dialectClass" value="com.github.miemiedev.mybatis.paginator.dialect.MySQLDialect" /> <property name="asyncTotalCount" value="true" /> </plugin> </plugins> </configuration>
這個配置文件就一個分頁,在前一篇文章中寫過,就不多做解釋了
5. 最后因為用的數據源的問題,就把pom.xml文件需要引入的幾個依賴,也摘出如下
<dependency> <groupId>com.jolbox</groupId> <artifactId>bonecp</artifactId> <version>0.8.0.RELEASE</version> </dependency> <dependency> <groupId>com.jolbox</groupId> <artifactId>bonecp-spring</artifactId> <version>0.8.0.RELEASE</version> </dependency> <dependency> <groupId>org.mariadb.jdbc</groupId> <artifactId>mariadb-java-client</artifactId> <version>1.1.7</version> </dependency>
需要說明的我們用常見的c3p0或者dbcp的數據源也是可以的,至于本例中為什么用了這個沒有聽說過的jolbox(老夫是沒聽說過),老夫也不知道,知道他和常見的c3p0和dbcp有什么區別的請留言交流,謝謝