⼀、Sharding-JDBCSSM项⽬读写分离1、⾸先在l⽂件下⾯引⼊
<!-- shardingjdbc -->
<dependency>
<groupId>io.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
<version>3.0.0.M1</version>
</dependency>
<dependency>
<groupId>io.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-namespace</artifactId>
<version>3.0.0.M1</version>
</dependency>
2、添加主从数据库配置信息(其他信息配置⼀致)
jdbc.driver=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@localhost:1521/ds_master--主库
jdbc.slaveurl=jdbc:oracle:thin:@localhost:1521/ds_slave0--从库
jdbc.username=QkHpYHEjYpE=
jdbc.password=QkHpYHEjYpE=
filters=stat
#初始化连接数量
initialSize=3
#最⼩空闲连接数
minIdle=5
#最⼤并发连接数
maxActive=50
#配置获取连接等待超时的时间
maxWait=1000
#性能配置
#打开PSCache,并且指定每个连接上PSCache的⼤⼩
poolPreparedStatements=true
maxOpenPreparedStatements=20
#配置间隔多久才进⾏⼀次检测,检测需要关闭的空闲连接,单位是毫秒
timeBetweenEvictionRunsMillis=50000
#配置⼀个连接在池中最⼩⽣存的时间,单位是毫秒
minEvictableIdleTimeMillis=100000
#⽤来检测连接是否有效的sql,要求是⼀个查询语句。
validationQuery=select 1 from dual
testWhileIdle=true
testOnBorrow=false
testOnReturn=false
3、配置l⽂件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="/schema/beans"
xmlns:context="/schema/context"
xmlns:p="/schema/p"
xmlns:aop="/schema/aop"
xmlns:tx="/schema/tx"
xmlns:mongo="/schema/data/mongo"
xmlns:rabbit="/schema/rabbit"
xmlns:master-slave="shardingsphere.io/schema/shardingsphere/masterslave"
xmlns:xsi="/2001/XMLSchema-instance"
xmlns:mvc="/schema/mvc"
xsi:schemaLocation="/schema/beans
/schema/beans/spring-beans-4.0.xsd
/schema/context
/schema/context/spring-context-4.0.xsd
/schema/aop
/schema/aop/spring-aop-4.0.xsd
/schema/tx
/schema/tx/spring-tx-4.0.xsd
/schema/util
/schema/util/spring-util-4.0.xsd
/schema/mvc
/schema/mvc/spring-mvc.xsd
/schema/data/mongo
/schema/data/mongo/spring-mongo-1.5.xsd
/schema/rabbit
/schema/rabbit/spring-rabbit-1.6.xsd
shardingsphere.io/schema/shardingsphere/masterslave
shardingsphere.io/schema/shardingsphere/masterslave/master-slave.xsd">
<context:annotation-config/>
<!-- 配置包扫描器,扫描@Service主键的类 -->
<context:component-scan base-package="com.**.service">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/>
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.RestController"/> </context:component-scan>
<bean class="b.utils.EncryptPropertyPlaceholderConfigurer" p:locations="classpath:properties/*.properties"/>
<!-- 数据库连接池 -->
<bean id="master" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="filters" value="${filters}" />
<property name="maxActive" value="${maxActive}" />
<property name="initialSize" value="${initialSize}" />
<property name="maxWait" value="${maxWait}" />
<property name="minIdle" value="${minIdle}" />
<property name="timeBetweenEvictionRunsMillis" value="${timeBetweenEvictionRunsMillis}" />
<property name="minEvictableIdleTimeMillis" value="${minEvictableIdleTimeMillis}" />
<property name="validationQuery" value="${validationQuery}" />
<property name="testWhileIdle" value="${testWhileIdle}" />
<property name="testOnBorrow" value="${testOnBorrow}" />
<property name="testOnReturn" value="${testOnReturn}" />
<property name="poolPreparedStatements" value="${poolPreparedStatements}" />
<property name="maxOpenPreparedStatements" value="${maxOpenPreparedStatements}" />
</bean>
<!-- 数据库连接池 -->
<bean id="slave1" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.slaveurl}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="filters" value="${filters}" />
<property name="maxActive" value="${maxActive}" />
<property name="initialSize" value="${initialSize}" />
<property name="maxWait" value="${maxWait}" />
<property name="minIdle" value="${minIdle}" />
<property name="timeBetweenEvictionRunsMillis" value="${timeBetweenEvictionRunsMillis}" />
<property name="minEvictableIdleTimeMillis" value="${minEvictableIdleTimeMillis}" />
<property name="validationQuery" value="${validationQuery}" />
<property name="testWhileIdle" value="${testWhileIdle}" />
<property name="testOnBorrow" value="${testOnBorrow}" />
<property name="testOnReturn" value="${testOnReturn}" />
<property name="poolPreparedStatements" value="${poolPreparedStatements}" />
<property name="maxOpenPreparedStatements" value="${maxOpenPreparedStatements}" />
</bean>
<bean id="randomStrategy" class="api.algorithm.masterslave.RandomMasterSlaveLoadBalanceAlgorithm" />
<master-slave:data-source id="shardingDataSource" master-data-source-name="master" slave-data-
source-names="slave1" strategy-ref="randomStrategy" />
<!-- 事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="shardingDataSource" />
</bean>
<!-- 配置SqlsessionFactory -->
<bean id="sqlSessionFactory" class="batis.spring.SqlSessionFactoryBean">
<!-- 配置数据源 -->
<property name="dataSource" ref="shardingDataSource"/>
<!-- 加载mybatis的配置⽂件 -->
<property name="configLocation" value="classpath:l"/>
</bean>
<!-- 配置包扫描器,扫描mapper接⼝⽣成代理对象放到spring容器中 -->
<bean class="batis.spring.mapper.MapperScannerConfigurer">
<!-- 指定要扫描的包 -->
<property name="basePackage" value="com.**.mapper"/>
</bean>
<bean id="sqlSession" class="batis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory" />
</bean>
<!-- 事务注解驱动,标注@Transactional的类和⽅法将具有事务性
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/> -->
<!-- 切⾯ -->
<aop:config>
<aop:advisor advice-ref="txAdvice"
pointcut="execution(* com.**.service..*(..))" />
</aop:config>
<!-- 通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
springboot aop<tx:attributes>
<!-- 传播⾏为 -->
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="imp*" propagation="REQUIRED" />
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="upd*" propagation="REQUIRED" />
<tx:method name="find*" propagation="REQUIRED" read-only="true" />
<tx:method name="select*" propagation="REQUIRED" read-only="true" />
<tx:method name="get*" propagation="REQUIRED" read-only="true" />
<tx:method name="qry*" propagation="REQUIRED" read-only="true" />
</tx:attributes>
</tx:advice>
<!-- 国际化配置 -->
<bean id="messageSource"
class="t.support.ResourceBundleMessageSource">
<property name="basenames">
<list>
<value>properties/messages</value>
</list>
</property>
<property name="defaultEncoding" value="utf-8" /> </bean>
</beans>
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论