1 Star 4 Fork 3

AndyChen/mybatis-plugin-shard

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
db-source.xml 10.71 KB
一键复制 编辑 原始数据 按行查看 历史
<?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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:jdbc.properties</value>
</list>
</property>
</bean>
<bean id="wall-config" class="com.alibaba.druid.wall.WallConfig">
<property name="multiStatementAllow" value="true"/>
</bean>
<bean id="wall-filter" class="com.alibaba.druid.wall.WallFilter">
<property name="config" ref="wall-config"/>
</bean>
<bean id="stat-filter" class="com.alibaba.druid.filter.stat.StatFilter">
<property name="slowSqlMillis" value="10000"/>
<property name="logSlowSql" value="true"/>
<property name="mergeSql" value="true"/>
</bean>
<!-- system -->
<bean id="dataSourceSystem" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.system.url}"/>
<property name="username" value="${jdbc.system.username}"/>
<property name="password" value="${jdbc.system.password}"/>
<property name="initialSize" value="${pool.initialPoolSize}"/>
<property name="minIdle" value="${pool.minPoolSize}"/>
<property name="maxActive" value="${pool.maxPoolSize}"/>
<property name="validationQuery" value="select 1"/>
<property name="timeBetweenEvictionRunsMillis" value="60000"/>
<property name="minEvictableIdleTimeMillis" value="300000"/>
<property name="removeAbandoned" value="true"/>
<!-- 超时时间;单位为秒 -->
<property name="removeAbandonedTimeout" value="30"/>
<property name="filters" value="log4j2"/>
<property name="proxyFilters">
<list>
<ref bean="stat-filter"/>
<ref bean="wall-filter"/>
</list>
</property>
</bean>
<!-- student -->
<bean id="dataSourceStudent" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.student.url}"/>
<property name="username" value="${jdbc.student.username}"/>
<property name="password" value="${jdbc.student.password}"/>
<property name="initialSize" value="${pool.initialPoolSize}"/>
<property name="minIdle" value="${pool.minPoolSize}"/>
<property name="maxActive" value="${pool.maxPoolSize}"/>
<property name="validationQuery" value="select 1"/>
<property name="timeBetweenEvictionRunsMillis" value="60000"/>
<property name="minEvictableIdleTimeMillis" value="300000"/>
<property name="removeAbandoned" value="true"/>
<!-- 超时时间;单位为秒 -->
<property name="removeAbandonedTimeout" value="30"/>
<property name="filters" value="log4j2"/>
<property name="proxyFilters">
<list>
<ref bean="stat-filter"/>
<ref bean="wall-filter"/>
</list>
</property>
</bean>
<!-- finance -->
<bean id="dataSourceFinance" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.finance.url}"/>
<property name="username" value="${jdbc.finance.username}"/>
<property name="password" value="${jdbc.finance.password}"/>
<property name="initialSize" value="${pool.initialPoolSize}"/>
<property name="minIdle" value="${pool.minPoolSize}"/>
<property name="maxActive" value="${pool.maxPoolSize}"/>
<property name="validationQuery" value="select 1"/>
<property name="timeBetweenEvictionRunsMillis" value="60000"/>
<property name="minEvictableIdleTimeMillis" value="300000"/>
<property name="removeAbandoned" value="true"/>
<!-- 超时时间;单位为秒 -->
<property name="removeAbandonedTimeout" value="30"/>
<property name="filters" value="log4j2"/>
<property name="proxyFilters">
<list>
<ref bean="stat-filter"/>
<ref bean="wall-filter"/>
</list>
</property>
</bean>
<!-- biz -->
<bean id="dataSourceBiz" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.biz.url}"/>
<property name="username" value="${jdbc.biz.username}"/>
<property name="password" value="${jdbc.biz.password}"/>
<property name="initialSize" value="${pool.initialPoolSize}"/>
<property name="minIdle" value="${pool.minPoolSize}"/>
<property name="maxActive" value="${pool.maxPoolSize}"/>
<property name="validationQuery" value="select 1"/>
<property name="timeBetweenEvictionRunsMillis" value="60000"/>
<property name="minEvictableIdleTimeMillis" value="300000"/>
<property name="removeAbandoned" value="true"/>
<!-- 超时时间;单位为秒 -->
<property name="removeAbandonedTimeout" value="30"/>
<property name="filters" value="log4j2"/>
<property name="proxyFilters">
<list>
<ref bean="stat-filter"/>
<ref bean="wall-filter"/>
</list>
</property>
</bean>
<bean id="dataSource" class="common.aspect.ChooseDataSource" primary="true">
<!-- 默认数据源 -->
<property name="defaultTargetDataSource" ref="dataSourceSystem"/>
<!-- 下面的各个 key 需要配置到 ShardConfig 的 schemaKeyList,见下面的:<bean id="shardConfig" class="mis.api.common.shard.shard.ShardConfig" > -->
<property name="targetDataSources">
<map key-type="java.lang.String">
<entry key="system" value-ref="dataSourceSystem"/>
<entry key="student" value-ref="dataSourceStudent"/>
<entry key="finance" value-ref="dataSourceFinance"/>
<entry key="biz" value-ref="dataSourceBiz"/>
</map>
</property>
</bean>
<!-- 以下配置,部分表名只是用于配置示例,仅为了更好的展示如何配置。
本项目没有用到的表名有:edu_class、biz_trade_order、biz_item、biz_item_sku
-->
<bean id="shardConfig" class="common.shard.ShardConfig" >
<!-- 列表值为 dataSource.targetDataSources 的 keys -->
<property name="schemaKeyList">
<list>
<value>system</value>
<value>student</value>
<value>finance</value>
<value>biz</value>
</list>
</property>
<!-- 基于服务接口分库策略,
把针对某个 schema 的接口配置在该数据源 key 对应的 list 下,没有就不配置
-->
<property name="shardSchemaInterfaceClassNameList">
<map>
<entry key="student">
<list>
<value>biz.service.facade.IEduStudentService</value>
</list>
</entry>
</map>
</property>
<!-- 分表策略
直接将 ShardRequest.shardKeyTable(优先级高于后者) 或 ShardRequest.shardKeyTableNumber 作为分表后缀的表。
需要配合 shardKeyTable 或 shardKeyTableNumber 使用,二选一,shardKeyTable 的优先级高于 shardKeyTableNumber,如 shardKeyTable=3,则下面的 edu_student 最终分表为 edu_student_3
ShardRequest 参见:https://gitee.com/uncleAndyChen/mybatis-plugin-shard/blob/master/common/common-shard/src/main/java/common/shard/ShardRequest.java
-->
<property name="shardTableDirectlyList">
<list>
<value>edu_student</value>
<value>edu_class</value>
</list>
</property>
<!-- 分表策略
通过两个数相除取余作为后缀的表,配合 ShardRequest.shardKeyTableNumber 使用
ShardRequest 参见:https://gitee.com/uncleAndyChen/mybatis-plugin-shard/blob/master/common/common-shard/src/main/java/common/shard/ShardRequest.java
-->
<!-- key 将作为 shardKeyTableNumber 的除数(取余), 余数作为分表后缀-->
<!-- shardKeyTableNumber 通过 ShardRequest 传递,在请求 api 时传递 -->
<property name="shardTableDivideList">
<map>
<entry key="10">
<list>
<value>biz_trade</value>
<value>biz_trade_order</value>
</list>
</entry>
<entry key="5">
<list>
<value>biz_item</value>
<value>biz_item_sku</value>
</list>
</entry>
</map>
</property>
<!-- 打印分表的 sql 语句,测试阶段或排查问题才需要设置为 true,否则请设置为 false 即不打印。-->
<property name="printShardSqlInfo" value="true" />
<!-- 不需要分表的 sql 语句列表,以下这句为 MyBatis 操作数据库新增记录时,查询新增的主键值的语句 -->
<property name="notNeedShardSqlList">
<list>
<value>SELECT LAST_INSERT_ID()</value>
</list>
</property>
</bean>
<!-- 用于切面,实现拦截数据库操作,实现分库分表的类 -->
<bean id="dataSourceAspect" class="common.aspect.DataSourceAspect">
<property name="shardTableConfigView" ref="shardConfig" />
</bean>
<!-- 定义切面,用于拦截数据库操作,实现分库分表 -->
<aop:config proxy-target-class="true">
<aop:aspect id="dataSourceAspect" ref="dataSourceAspect" order="1">
<aop:pointcut id="point" expression="(execution(* biz.service.impl.*.*(..)))"/>
<aop:before pointcut-ref="point" method="before"/>
<aop:after pointcut-ref="point" method="afterHandler"/>
</aop:aspect>
</aop:config>
</beans>
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/uncleAndyChen/mybatis-plugin-shard.git
git@gitee.com:uncleAndyChen/mybatis-plugin-shard.git
uncleAndyChen
mybatis-plugin-shard
mybatis-plugin-shard
master

搜索帮助