master-slave-db-selector Maven Webapp

master-slave-db-selector

License

License

GroupId

GroupId

cn.bridgeli
ArtifactId

ArtifactId

master-slave-db-selector
Last Version

Last Version

0.0.2
Release Date

Release Date

Type

Type

jar
Description

Description

master-slave-db-selector Maven Webapp
master-slave-db-selector
Project URL

Project URL

https://github.com/bridgeli/master-slave-db-selector
Source Code Management

Source Code Management

https://github.com/bridgeli/master-slave-db-selector

Download master-slave-db-selector

How to add to project

<!-- https://jarcasting.com/artifacts/cn.bridgeli/master-slave-db-selector/ -->
<dependency>
    <groupId>cn.bridgeli</groupId>
    <artifactId>master-slave-db-selector</artifactId>
    <version>0.0.2</version>
</dependency>
// https://jarcasting.com/artifacts/cn.bridgeli/master-slave-db-selector/
implementation 'cn.bridgeli:master-slave-db-selector:0.0.2'
// https://jarcasting.com/artifacts/cn.bridgeli/master-slave-db-selector/
implementation ("cn.bridgeli:master-slave-db-selector:0.0.2")
'cn.bridgeli:master-slave-db-selector:jar:0.0.2'
<dependency org="cn.bridgeli" name="master-slave-db-selector" rev="0.0.2">
  <artifact name="master-slave-db-selector" type="jar" />
</dependency>
@Grapes(
@Grab(group='cn.bridgeli', module='master-slave-db-selector', version='0.0.2')
)
libraryDependencies += "cn.bridgeli" % "master-slave-db-selector" % "0.0.2"
[cn.bridgeli/master-slave-db-selector "0.0.2"]

Dependencies

provided (8)

Group / Artifact Type Version
org.slf4j : slf4j-log4j12 jar 1.7.7
org.aspectj : aspectjweaver jar 1.8.2
org.springframework : spring-jdbc jar 4.1.1.RELEASE
org.springframework : spring-aop jar 4.1.1.RELEASE
org.springframework : spring-context jar 4.1.1.RELEASE
org.springframework : spring-tx jar 4.1.1.RELEASE
org.mybatis : mybatis-spring jar 1.2.2
com.alibaba : druid jar 1.1.10

test (1)

Group / Artifact Type Version
junit : junit jar 4.10

Project Modules

There are no modules declared in this project.

master-slave-db-selector

使用方法,配置模板

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
       xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

    <!-- 配置写数据源 -->
    <bean id="masterDataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
        <property name="driverClassName" value="${bridgeli.jdbc.driver}" />
        <property name="url" value="${bridgeli.jdbc.url}" />
        <property name="username" value="${bridgeli.jdbc.username}" />
        <property name="password" value="${bridgeli.jdbc.password}" />
        <property name="validationQuery" value="${bridgeli.jdbc.validationQuery}" />
        <property name="initialSize" value="1" />
        <property name="maxActive" value="20" />
        <property name="minIdle" value="0" />
        <property name="maxWait" value="60000" />
        <property name="testOnBorrow" value="false" />
        <property name="testOnReturn" value="false" />
        <property name="testWhileIdle" value="true" />
        <property name="timeBetweenEvictionRunsMillis" value="60000" />
        <property name="minEvictableIdleTimeMillis" value="25200000" />
        <property name="removeAbandoned" value="true" />
        <property name="removeAbandonedTimeout" value="1800" />
        <property name="logAbandoned" value="true" />
        <property name="filters" value="stat" />
    </bean>

    <!-- 配置读数据源 -->
    <bean id="parentSlaveDataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
        <property name="driverClassName" value="${bridgeli.jdbc.driver}" />
        <property name="validationQuery" value="${bridgeli.jdbc.validationQuery}" />
        <property name="initialSize" value="1" />
        <property name="maxActive" value="20" />
        <property name="minIdle" value="0" />
        <property name="maxWait" value="60000" />
        <property name="testOnBorrow" value="false" />
        <property name="testOnReturn" value="false" />
        <property name="testWhileIdle" value="true" />
        <property name="timeBetweenEvictionRunsMillis" value="60000" />
        <property name="minEvictableIdleTimeMillis" value="25200000" />
        <property name="removeAbandoned" value="true" />
        <property name="removeAbandonedTimeout" value="1800" />
        <property name="logAbandoned" value="true" />
        <property name="filters" value="stat" />
    </bean>
    <bean id="slaveDataSource1" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close" parent="parentSlaveDataSource">
        <property name="url" value="${bridgeli_slave1.jdbc.url}" />
        <property name="username" value="${bridgeli_slave1.jdbc.username}" />
        <property name="password" value="${bridgeli_slave1.jdbc.password}" />
    </bean>

    <bean id="dataSource" class="cn.bridgeli.masterslavedbselector.MasterSlaveDataSource">
        <property name="targetDataSources">
            <map>
                <entry key-ref="masterDataSource" value-ref="masterDataSource"/>
                <entry key-ref="slaveDataSource1" value-ref="slaveDataSource1"/>
            </map>
        </property>
        <property name="defaultTargetDataSource" ref="masterDataSource"/>
        <property name="masterSlaveSelector" ref="dataSelector"/>
    </bean>

    <bean id="dataSelector" class="cn.bridgeli.masterslavedbselector.MasterSlaveSelectorByPoll">
        <property name="masters">
            <list>
                <ref bean="masterDataSource"/>
            </list>
        </property>
        <property name="slaves">
            <list>
                <ref bean="slaveDataSource1"/>
            </list>
        </property>
        <property name="defaultDataSource" ref="masterDataSource"/>
    </bean>

    <aop:aspectj-autoproxy/>

    <!-- mybaits 数据工厂 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <!-- 自动扫描所有注解的路径 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="cn.bridgeli.mapper" />
        <!-- <property name="sqlSessionFactory" ref="sqlSessionFactory" /> -->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
    </bean>

    <!-- 数据库切面 -->
    <bean id="masterSlaveAspect" class="cn.bridgeli.masterslavedbselector.MasterSlaveAspect">
        <property name="prefixMasters">
            <list>
                <value>save</value>
                <value>update</value>
                <value>delete</value>
            </list>
        </property>
    </bean>
    <aop:config>
        <aop:aspect id="c" ref="masterSlaveAspect">
            <aop:pointcut id="tx" expression="execution(* cn.bridgeli.service..*.*(..))"/>
            <aop:before pointcut-ref="tx" method="before"/>
        </aop:aspect>
    </aop:config>

    <context:annotation-config />
    <context:component-scan base-package="cn.bridgeli" />
</beans>

Versions

Version
0.0.2
0.0.1