├── BootShardingStudy
├── .classpath
├── .project
├── .settings
│ ├── org.eclipse.core.resources.prefs
│ ├── org.eclipse.jdt.core.prefs
│ └── org.eclipse.m2e.core.prefs
├── pom.xml
├── src
│ ├── main
│ │ ├── java
│ │ │ └── com
│ │ │ │ └── think
│ │ │ │ ├── conf
│ │ │ │ ├── DataSourceConfig.java
│ │ │ │ ├── MyBatisMapperScannerConfig.java
│ │ │ │ ├── MybatisConf.java
│ │ │ │ ├── ProgramShardingAlgorithm.java
│ │ │ │ ├── SingleKeyModuloDatabaseShardingAlgorithm.java
│ │ │ │ └── XbDataSource.java
│ │ │ │ ├── dao
│ │ │ │ └── OrderMapper.java
│ │ │ │ ├── entity
│ │ │ │ └── TOrder.java
│ │ │ │ └── main
│ │ │ │ └── Application.java
│ │ └── resources
│ │ │ ├── application.properties
│ │ │ └── log4j.properties
│ └── test
│ │ └── java
│ │ └── com
│ │ └── think
│ │ └── AppTest.java
└── target
│ ├── classes
│ ├── META-INF
│ │ ├── MANIFEST.MF
│ │ └── maven
│ │ │ └── com.think
│ │ │ └── BootShardingStudy
│ │ │ ├── pom.properties
│ │ │ └── pom.xml
│ ├── application.properties
│ ├── com
│ │ └── think
│ │ │ ├── conf
│ │ │ ├── DataSourceConfig.class
│ │ │ ├── MyBatisMapperScannerConfig.class
│ │ │ ├── MybatisConf.class
│ │ │ ├── ProgramShardingAlgorithm.class
│ │ │ ├── SingleKeyModuloDatabaseShardingAlgorithm.class
│ │ │ └── XbDataSource.class
│ │ │ ├── dao
│ │ │ └── OrderMapper.class
│ │ │ ├── entity
│ │ │ └── TOrder.class
│ │ │ └── main
│ │ │ └── Application.class
│ └── log4j.properties
│ └── test-classes
│ └── com
│ └── think
│ └── AppTest.class
├── README.md
├── test0.sql
└── test1.sql
/BootShardingStudy/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
--------------------------------------------------------------------------------
/BootShardingStudy/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | BootShardingStudy
4 |
5 |
6 |
7 |
8 |
9 | org.eclipse.jdt.core.javabuilder
10 |
11 |
12 |
13 |
14 | org.eclipse.m2e.core.maven2Builder
15 |
16 |
17 |
18 |
19 |
20 | org.eclipse.jdt.core.javanature
21 | org.eclipse.m2e.core.maven2Nature
22 |
23 |
24 |
--------------------------------------------------------------------------------
/BootShardingStudy/.settings/org.eclipse.core.resources.prefs:
--------------------------------------------------------------------------------
1 | eclipse.preferences.version=1
2 | encoding//src/main/java=UTF-8
3 | encoding//src/test/java=UTF-8
4 | encoding/=UTF-8
5 |
--------------------------------------------------------------------------------
/BootShardingStudy/.settings/org.eclipse.jdt.core.prefs:
--------------------------------------------------------------------------------
1 | eclipse.preferences.version=1
2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5 | org.eclipse.jdt.core.compiler.compliance=1.7
6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate
7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate
8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate
9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
11 | org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
12 | org.eclipse.jdt.core.compiler.source=1.7
13 |
--------------------------------------------------------------------------------
/BootShardingStudy/.settings/org.eclipse.m2e.core.prefs:
--------------------------------------------------------------------------------
1 | activeProfiles=
2 | eclipse.preferences.version=1
3 | resolveWorkspaceProjects=true
4 | version=1
5 |
--------------------------------------------------------------------------------
/BootShardingStudy/pom.xml:
--------------------------------------------------------------------------------
1 |
3 | 4.0.0
4 |
5 | com.think
6 | BootShardingStudy
7 | 0.0.1-SNAPSHOT
8 | jar
9 |
10 | BootShardingStudy
11 | http://maven.apache.org
12 |
13 |
14 | org.springframework.boot
15 | spring-boot-starter-parent
16 | 1.4.2.RELEASE
17 |
18 |
19 | UTF-8
20 | 1.4.1
21 | 4.1.6
22 | 3.4.0
23 | 1.0.12
24 | 5.1.34
25 | 1.3.2
26 | 1.1.1
27 | 1.3.8.RELEASE
28 |
29 |
30 |
31 |
32 |
33 | org.springframework.boot
34 | spring-boot-starter
35 |
36 |
37 | org.springframework.boot
38 | spring-boot-starter-logging
39 |
40 |
41 |
42 |
43 |
44 |
45 | org.springframework.boot
46 | spring-boot-starter-log4j
47 | ${log4j.version}
48 |
49 |
50 |
51 | org.springframework.boot
52 | spring-boot-starter-test
53 | test
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 | com.dangdang
63 | sharding-jdbc-core
64 | ${sharding-jdbc.version}
65 |
66 |
67 | com.dangdang
68 | sharding-jdbc-config-spring
69 | ${sharding-jdbc.version}
70 |
71 |
72 | com.dangdang
73 | sharding-jdbc-self-id-generator
74 | ${sharding-jdbc.version}
75 |
76 |
77 | org.springframework.boot
78 | spring-boot-starter-web
79 |
80 |
81 | junit
82 | junit
83 | test
84 |
85 |
86 | org.mybatis.spring.boot
87 | mybatis-spring-boot-starter
88 | ${spring.mybatis.version}
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 | mysql
98 | mysql-connector-java
99 |
100 |
101 |
102 | com.alibaba
103 | druid
104 | ${durid.version}
105 |
106 |
107 | org.mybatis
108 | mybatis
109 | ${mybatis.version}
110 |
111 |
112 |
113 |
114 |
115 |
116 | org.mybatis.generator
117 | mybatis-generator-maven-plugin
118 | ${mybatis.maven.version}
119 |
120 | true
121 | true
122 |
123 |
124 |
125 | mysql
126 | mysql-connector-java
127 | ${mysql.driver.version}
128 |
129 |
130 |
131 |
132 |
133 |
134 |
--------------------------------------------------------------------------------
/BootShardingStudy/src/main/java/com/think/conf/DataSourceConfig.java:
--------------------------------------------------------------------------------
1 | package com.think.conf;
2 |
3 | import javax.sql.DataSource;
4 |
5 | import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
6 | import org.springframework.boot.context.properties.ConfigurationProperties;
7 | import org.springframework.context.annotation.Bean;
8 | import org.springframework.context.annotation.Configuration;
9 | import org.springframework.context.annotation.Primary;
10 |
11 | /***
12 | * 配置多个数据源
13 | *
14 | * @author thinkstop
15 | *
16 | */
17 | @Configuration
18 | public class DataSourceConfig {
19 |
20 | @Bean(name = "primaryDataSource")
21 | @Primary
22 | // 配置文件中前缀
23 | @ConfigurationProperties(prefix = "spring.datasource.primary")
24 | public DataSource primaryDataSource() {
25 | return DataSourceBuilder.create().build();
26 | }
27 |
28 | @Bean(name = "secondaryDataSource")
29 | @ConfigurationProperties(prefix = "spring.datasource.secondary")
30 | public DataSource secondaryDataSource() {
31 | return DataSourceBuilder.create().build();
32 | }
33 |
34 | }
35 |
--------------------------------------------------------------------------------
/BootShardingStudy/src/main/java/com/think/conf/MyBatisMapperScannerConfig.java:
--------------------------------------------------------------------------------
1 | package com.think.conf;
2 |
3 | import org.mybatis.spring.mapper.MapperScannerConfigurer;
4 | import org.springframework.boot.autoconfigure.AutoConfigureAfter;
5 | import org.springframework.context.annotation.Configuration;
6 |
7 | @Configuration
8 | @AutoConfigureAfter(MybatisConf.class)
9 | public class MyBatisMapperScannerConfig {
10 | public MapperScannerConfigurer mapperScannerConfigurer() {
11 | MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
12 | mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory");
13 | mapperScannerConfigurer.setBasePackage("com.think.dao");// mapper类路径
14 | return mapperScannerConfigurer;
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/BootShardingStudy/src/main/java/com/think/conf/MybatisConf.java:
--------------------------------------------------------------------------------
1 | package com.think.conf;
2 |
3 | import javax.sql.DataSource;
4 |
5 | import org.apache.ibatis.mapping.Environment;
6 | import org.apache.ibatis.session.SqlSessionFactory;
7 | import org.apache.ibatis.session.SqlSessionFactoryBuilder;
8 | import org.apache.ibatis.transaction.TransactionFactory;
9 | import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
10 | import org.mybatis.spring.SqlSessionTemplate;
11 | import org.springframework.beans.factory.annotation.Autowired;
12 | import org.springframework.context.annotation.Bean;
13 | import org.springframework.context.annotation.Configuration;
14 | import org.springframework.jdbc.datasource.DataSourceTransactionManager;
15 | import org.springframework.transaction.PlatformTransactionManager;
16 | import org.springframework.transaction.annotation.EnableTransactionManagement;
17 |
18 | /**
19 | * mybatis的配置
20 | *
21 | * @author donghuating
22 | *
23 | */
24 | @Configuration
25 | @EnableTransactionManagement
26 | public class MybatisConf {
27 |
28 | @Autowired
29 | private XbDataSource xbDataSource;
30 |
31 | /**
32 | * 获取sqlFactory
33 | *
34 | * @return
35 | * @throws Exception
36 | */
37 | @Bean(name = "sqlSessionFactory")
38 | public SqlSessionFactory sqlSessionFactoryBean() throws Exception {
39 | /**
40 | * sharding-jdbc 产生的DataSource
41 | */
42 | DataSource dataSource = xbDataSource.getShardingDataSource();
43 | TransactionFactory transactionFactory = new JdbcTransactionFactory();
44 | Environment environment = new Environment("development", transactionFactory, dataSource);
45 | org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration(
46 | environment);
47 | // Dao层包路径
48 | configuration.addMappers("com.think.dao.*");
49 | SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
50 | return sqlSessionFactory;
51 | }
52 |
53 | @Bean
54 | public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
55 | return new SqlSessionTemplate(sqlSessionFactory);
56 | }
57 |
58 | @Bean
59 | public PlatformTransactionManager annotationDrivenTransactionManager() {
60 |
61 | return new DataSourceTransactionManager(xbDataSource.getShardingDataSource());
62 | }
63 |
64 | }
65 |
--------------------------------------------------------------------------------
/BootShardingStudy/src/main/java/com/think/conf/ProgramShardingAlgorithm.java:
--------------------------------------------------------------------------------
1 | package com.think.conf;
2 |
3 | import java.util.Collection;
4 | import java.util.LinkedHashSet;
5 |
6 | import com.dangdang.ddframe.rdb.sharding.api.ShardingValue;
7 | import com.dangdang.ddframe.rdb.sharding.api.strategy.table.SingleKeyTableShardingAlgorithm;
8 | import com.google.common.collect.Range;
9 |
10 | /**
11 | * table 分片算法
12 | *
13 | * @author donghuating
14 | *
15 | */
16 | public class ProgramShardingAlgorithm implements SingleKeyTableShardingAlgorithm {
17 |
18 | public String doEqualSharding(Collection availableTargetNames, ShardingValue shardingValue) {
19 | for (String each : availableTargetNames) {
20 | if (each.endsWith(shardingValue.getValue() % 2 + "")) {
21 | return each;
22 | }
23 | }
24 | throw new UnsupportedOperationException();
25 | }
26 |
27 | public Collection doInSharding(Collection availableTargetNames,
28 | ShardingValue shardingValue) {
29 | Collection result = new LinkedHashSet(availableTargetNames.size());
30 | Collection values = shardingValue.getValues();
31 | for (Integer value : values) {
32 | for (String tableNames : availableTargetNames) {
33 | if (tableNames.endsWith(value % 2 + "")) {
34 | result.add(tableNames);
35 | }
36 | }
37 | }
38 | return result;
39 | }
40 |
41 | public Collection doBetweenSharding(Collection availableTargetNames,
42 | ShardingValue shardingValue) {
43 | Collection result = new LinkedHashSet(availableTargetNames.size());
44 | Range range = shardingValue.getValueRange();
45 | for (Integer i = range.lowerEndpoint(); i <= range.upperEndpoint(); i++) {
46 | for (String each : availableTargetNames) {
47 | if (each.endsWith(i % 2 + "")) {
48 | result.add(each);
49 | }
50 | }
51 | }
52 | return result;
53 | }
54 |
55 | }
56 |
--------------------------------------------------------------------------------
/BootShardingStudy/src/main/java/com/think/conf/SingleKeyModuloDatabaseShardingAlgorithm.java:
--------------------------------------------------------------------------------
1 | package com.think.conf;
2 |
3 | import java.util.Collection;
4 | import java.util.LinkedHashSet;
5 |
6 | import com.dangdang.ddframe.rdb.sharding.api.ShardingValue;
7 | import com.dangdang.ddframe.rdb.sharding.api.strategy.database.SingleKeyDatabaseShardingAlgorithm;
8 | import com.google.common.collect.Range;
9 |
10 | /***
11 | * dataBase分库算法
12 | *
13 | * @author donghuating
14 | *
15 | */
16 | public class SingleKeyModuloDatabaseShardingAlgorithm implements SingleKeyDatabaseShardingAlgorithm {
17 |
18 | public String doEqualSharding(Collection availableTargetNames, ShardingValue shardingValue) {
19 | for (String each : availableTargetNames) {
20 | if (each.endsWith(shardingValue.getValue() % 2 + "")) {
21 | return each;
22 | }
23 | }
24 | throw new UnsupportedOperationException();
25 | }
26 |
27 | public Collection doInSharding(Collection availableTargetNames,
28 | ShardingValue shardingValue) {
29 | Collection result = new LinkedHashSet<>(availableTargetNames.size());
30 | Collection values = shardingValue.getValues();
31 | for (Integer value : values) {
32 | for (String dataSourceName : availableTargetNames) {
33 | if (dataSourceName.endsWith(value % 2 + "")) {
34 | result.add(dataSourceName);
35 | }
36 | }
37 | }
38 | return result;
39 | }
40 |
41 | public Collection doBetweenSharding(Collection availableTargetNames,
42 | ShardingValue shardingValue) {
43 | Collection result = new LinkedHashSet<>(availableTargetNames.size());
44 | Range range = shardingValue.getValueRange();
45 | for (Integer i = range.lowerEndpoint(); i <= range.upperEndpoint(); i++) {
46 | for (String each : availableTargetNames) {
47 | if (each.endsWith(i % 2 + "")) {
48 | result.add(each);
49 | }
50 | }
51 | }
52 | return result;
53 | }
54 |
55 | }
56 |
--------------------------------------------------------------------------------
/BootShardingStudy/src/main/java/com/think/conf/XbDataSource.java:
--------------------------------------------------------------------------------
1 | package com.think.conf;
2 |
3 | import java.util.ArrayList;
4 | import java.util.HashMap;
5 | import java.util.List;
6 | import java.util.Map;
7 |
8 | import javax.annotation.PostConstruct;
9 | import javax.sql.DataSource;
10 |
11 | import org.springframework.beans.factory.annotation.Autowired;
12 | import org.springframework.beans.factory.annotation.Qualifier;
13 | import org.springframework.stereotype.Component;
14 |
15 | import com.dangdang.ddframe.rdb.sharding.api.ShardingDataSourceFactory;
16 | import com.dangdang.ddframe.rdb.sharding.api.rule.DataSourceRule;
17 | import com.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule;
18 | import com.dangdang.ddframe.rdb.sharding.api.rule.TableRule;
19 | import com.dangdang.ddframe.rdb.sharding.api.strategy.database.DatabaseShardingStrategy;
20 | import com.dangdang.ddframe.rdb.sharding.api.strategy.table.TableShardingStrategy;
21 |
22 | /***
23 | * sharding-jdbc 配置数据源和分库分表规则
24 | *
25 | * @author donghuating
26 | *
27 | */
28 | @Component
29 | public class XbDataSource {
30 |
31 | @Autowired
32 | private DataSource primaryDataSource;
33 |
34 | @Autowired
35 | @Qualifier("secondaryDataSource")
36 | private DataSource secondaryDataSource;
37 |
38 | private DataSource shardingDataSource;
39 |
40 | @PostConstruct
41 | public void init() {
42 | Map map = new HashMap();
43 | map.put("testdb0", primaryDataSource);
44 | map.put("testdb1", secondaryDataSource);
45 | DataSourceRule dataSourceRule = new DataSourceRule(map);
46 | List tableRuleList = new ArrayList();
47 | List pList = new ArrayList();
48 | for (int i = 0; i < 2; i++) {
49 | pList.add("t_order_" + i);
50 | }
51 | tableRuleList.add(new TableRule.TableRuleBuilder("t_order").actualTables(pList).dataSourceRule(dataSourceRule)
52 | .tableShardingStrategy(new TableShardingStrategy("order_id", new ProgramShardingAlgorithm())).build());
53 | ShardingRule shardingRule = ShardingRule.builder().dataSourceRule(dataSourceRule)
54 | .databaseShardingStrategy(
55 | new DatabaseShardingStrategy("user_id", new SingleKeyModuloDatabaseShardingAlgorithm()))
56 | .tableRules(tableRuleList).build();
57 | shardingDataSource = ShardingDataSourceFactory.createDataSource(shardingRule);
58 | }
59 |
60 | public DataSource getShardingDataSource() {
61 | return shardingDataSource;
62 | }
63 | }
64 |
--------------------------------------------------------------------------------
/BootShardingStudy/src/main/java/com/think/dao/OrderMapper.java:
--------------------------------------------------------------------------------
1 | package com.think.dao;
2 |
3 | import org.apache.ibatis.annotations.Insert;
4 | import org.apache.ibatis.annotations.Mapper;
5 | import org.apache.ibatis.annotations.Result;
6 | import org.apache.ibatis.annotations.Results;
7 | import org.apache.ibatis.annotations.Select;
8 |
9 | import com.think.entity.TOrder;
10 |
11 | @Mapper
12 | public interface OrderMapper {
13 |
14 | @Insert("INSERT INTO t_order (order_id,user_id) VALUES (#{orderId},#{userId})")
15 | public void insert(TOrder order);
16 |
17 | @Results(value = { @Result(property = "userId", column = "user_id"),
18 | @Result(property = "orderId", column = "order_id"), })
19 | @Select("SELECT * FROM t_order WHERE order_id=#{id}")
20 | public TOrder findById(int id);
21 |
22 | }
23 |
--------------------------------------------------------------------------------
/BootShardingStudy/src/main/java/com/think/entity/TOrder.java:
--------------------------------------------------------------------------------
1 | package com.think.entity;
2 |
3 | public class TOrder {
4 |
5 | private int orderId;
6 |
7 | private int userId;
8 |
9 | public int getOrderId() {
10 | return orderId;
11 | }
12 |
13 | public void setOrderId(int orderId) {
14 | this.orderId = orderId;
15 | }
16 |
17 | public int getUserId() {
18 | return userId;
19 | }
20 |
21 | public void setUserId(int userId) {
22 | this.userId = userId;
23 | }
24 |
25 | }
26 |
--------------------------------------------------------------------------------
/BootShardingStudy/src/main/java/com/think/main/Application.java:
--------------------------------------------------------------------------------
1 | package com.think.main;
2 |
3 | import org.mybatis.spring.annotation.MapperScan;
4 | import org.springframework.boot.SpringApplication;
5 | import org.springframework.boot.autoconfigure.SpringBootApplication;
6 | import org.springframework.context.annotation.ComponentScan;
7 |
8 | @SpringBootApplication
9 | @ComponentScan("com.*.**")
10 | @MapperScan("com.think.dao")
11 | public class Application {
12 | public static void main(String[] args) {
13 | SpringApplication.run(Application.class, args);
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/BootShardingStudy/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | server.port=9099
2 |
3 | spring.datasource.primary.name=testdb0
4 | spring.datasource.primary.url=jdbc:mysql://127.0.0.1:3306/test0
5 | spring.datasource.primary.username=root
6 | spring.datasource.primary.password=root
7 | spring.datasource.primary.driver-class-name=com.mysql.jdbc.Driver
8 |
9 | spring.datasource.secondary.name=testdb1
10 | spring.datasource.secondary.url=jdbc:mysql://127.0.0.1:3306/test1
11 | spring.datasource.secondary.username=root
12 | spring.datasource.secondary.password=root
13 | spring.datasource.secondary.driver-class-name=com.mysql.jdbc.Driver
14 |
15 | spring.datasource.initialSize=5
16 | spring.datasource.minIdle=5
17 | spring.datasource.maxActive=20
18 | # \u914d\u7f6e\u83b7\u53d6\u8fde\u63a5\u7b49\u5f85\u8d85\u65f6\u7684\u65f6\u95f4
19 | spring.datasource.maxWait=60000
20 | # \u914d\u7f6e\u95f4\u9694\u591a\u4e45\u624d\u8fdb\u884c\u4e00\u6b21\u68c0\u6d4b\uff0c\u68c0\u6d4b\u9700\u8981\u5173\u95ed\u7684\u7a7a\u95f2\u8fde\u63a5\uff0c\u5355\u4f4d\u662f\u6beb\u79d2
21 | spring.datasource.timeBetweenEvictionRunsMillis=60000
22 | # \u914d\u7f6e\u4e00\u4e2a\u8fde\u63a5\u5728\u6c60\u4e2d\u6700\u5c0f\u751f\u5b58\u7684\u65f6\u95f4\uff0c\u5355\u4f4d\u662f\u6beb\u79d2
23 | spring.datasource.minEvictableIdleTimeMillis=300000
24 | spring.datasource.validationQuery=SELECT 1 FROM DUAL
25 | spring.datasource.testWhileIdle=true
26 | spring.datasource.testOnBorrow=false
27 | spring.datasource.testOnReturn=false
--------------------------------------------------------------------------------
/BootShardingStudy/src/main/resources/log4j.properties:
--------------------------------------------------------------------------------
1 | log4j.rootCategory=INFO,stdout
2 |
3 | # \u63a7\u5236\u53f0\u8f93\u51fa
4 | log4j.appender.stdout=org.apache.log4j.ConsoleAppender
5 | log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
6 | log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %5p %c{1}:%L - %m%n
7 |
--------------------------------------------------------------------------------
/BootShardingStudy/src/test/java/com/think/AppTest.java:
--------------------------------------------------------------------------------
1 | package com.think;
2 |
3 | import org.junit.Test;
4 | import org.junit.runner.RunWith;
5 | import org.springframework.beans.factory.annotation.Autowired;
6 | import org.springframework.boot.test.context.SpringBootTest;
7 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
8 |
9 | import com.think.dao.OrderMapper;
10 | import com.think.entity.TOrder;
11 |
12 | @RunWith(SpringJUnit4ClassRunner.class)
13 | @SpringBootTest
14 | public class AppTest {
15 |
16 | @Autowired
17 | private OrderMapper orderMapper;
18 |
19 | @Test
20 | public void test() {
21 | TOrder order = new TOrder();
22 | order.setUserId(888);
23 | order.setOrderId(77);
24 | orderMapper.insert(order);
25 | }
26 |
27 | @Test
28 | public void test2() {
29 | TOrder order = orderMapper.findById(31);
30 | System.out.println(order.getOrderId() + "" + order.getUserId());
31 | }
32 |
33 | }
34 |
--------------------------------------------------------------------------------
/BootShardingStudy/target/classes/META-INF/MANIFEST.MF:
--------------------------------------------------------------------------------
1 | Manifest-Version: 1.0
2 | Implementation-Title: BootShardingStudy
3 | Implementation-Version: 0.0.1-SNAPSHOT
4 | Built-By: donghuating
5 | Implementation-Vendor-Id: com.think
6 | Build-Jdk: 1.8.0_51
7 | Implementation-URL: http://maven.apache.org
8 | Created-By: Maven Integration for Eclipse
9 | Implementation-Vendor: Pivotal Software, Inc.
10 |
11 |
--------------------------------------------------------------------------------
/BootShardingStudy/target/classes/META-INF/maven/com.think/BootShardingStudy/pom.properties:
--------------------------------------------------------------------------------
1 | #Generated by Maven Integration for Eclipse
2 | #Sat Apr 01 14:54:44 CST 2017
3 | version=0.0.1-SNAPSHOT
4 | groupId=com.think
5 | m2e.projectName=BootShardingStudy
6 | m2e.projectLocation=/Users/donghuating/dev/study/WORK/BootShardingStudy
7 | artifactId=BootShardingStudy
8 |
--------------------------------------------------------------------------------
/BootShardingStudy/target/classes/META-INF/maven/com.think/BootShardingStudy/pom.xml:
--------------------------------------------------------------------------------
1 |
3 | 4.0.0
4 |
5 | com.think
6 | BootShardingStudy
7 | 0.0.1-SNAPSHOT
8 | jar
9 |
10 | BootShardingStudy
11 | http://maven.apache.org
12 |
13 |
14 | org.springframework.boot
15 | spring-boot-starter-parent
16 | 1.4.2.RELEASE
17 |
18 |
19 | UTF-8
20 | 1.4.1
21 | 4.1.6
22 | 3.4.0
23 | 1.0.12
24 | 5.1.34
25 | 1.3.2
26 | 1.1.1
27 | 1.3.8.RELEASE
28 |
29 |
30 |
31 |
32 |
33 | org.springframework.boot
34 | spring-boot-starter
35 |
36 |
37 | org.springframework.boot
38 | spring-boot-starter-logging
39 |
40 |
41 |
42 |
43 |
44 |
45 | org.springframework.boot
46 | spring-boot-starter-log4j
47 | ${log4j.version}
48 |
49 |
50 |
51 | org.springframework.boot
52 | spring-boot-starter-test
53 | test
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 | com.dangdang
63 | sharding-jdbc-core
64 | ${sharding-jdbc.version}
65 |
66 |
67 | com.dangdang
68 | sharding-jdbc-config-spring
69 | ${sharding-jdbc.version}
70 |
71 |
72 | com.dangdang
73 | sharding-jdbc-self-id-generator
74 | ${sharding-jdbc.version}
75 |
76 |
77 | org.springframework.boot
78 | spring-boot-starter-web
79 |
80 |
81 | junit
82 | junit
83 | test
84 |
85 |
86 | org.mybatis.spring.boot
87 | mybatis-spring-boot-starter
88 | ${spring.mybatis.version}
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 | mysql
98 | mysql-connector-java
99 |
100 |
101 |
102 | com.alibaba
103 | druid
104 | ${durid.version}
105 |
106 |
107 | org.mybatis
108 | mybatis
109 | ${mybatis.version}
110 |
111 |
112 |
113 |
114 |
115 |
116 | org.mybatis.generator
117 | mybatis-generator-maven-plugin
118 | ${mybatis.maven.version}
119 |
120 | true
121 | true
122 |
123 |
124 |
125 | mysql
126 | mysql-connector-java
127 | ${mysql.driver.version}
128 |
129 |
130 |
131 |
132 |
133 |
134 |
--------------------------------------------------------------------------------
/BootShardingStudy/target/classes/application.properties:
--------------------------------------------------------------------------------
1 | server.port=9099
2 |
3 | spring.datasource.primary.name=testdb0
4 | spring.datasource.primary.url=jdbc:mysql://127.0.0.1:3306/test0
5 | spring.datasource.primary.username=root
6 | spring.datasource.primary.password=root
7 | spring.datasource.primary.driver-class-name=com.mysql.jdbc.Driver
8 |
9 | spring.datasource.secondary.name=testdb1
10 | spring.datasource.secondary.url=jdbc:mysql://127.0.0.1:3306/test1
11 | spring.datasource.secondary.username=root
12 | spring.datasource.secondary.password=root
13 | spring.datasource.secondary.driver-class-name=com.mysql.jdbc.Driver
14 |
15 | spring.datasource.initialSize=5
16 | spring.datasource.minIdle=5
17 | spring.datasource.maxActive=20
18 | # \u914d\u7f6e\u83b7\u53d6\u8fde\u63a5\u7b49\u5f85\u8d85\u65f6\u7684\u65f6\u95f4
19 | spring.datasource.maxWait=60000
20 | # \u914d\u7f6e\u95f4\u9694\u591a\u4e45\u624d\u8fdb\u884c\u4e00\u6b21\u68c0\u6d4b\uff0c\u68c0\u6d4b\u9700\u8981\u5173\u95ed\u7684\u7a7a\u95f2\u8fde\u63a5\uff0c\u5355\u4f4d\u662f\u6beb\u79d2
21 | spring.datasource.timeBetweenEvictionRunsMillis=60000
22 | # \u914d\u7f6e\u4e00\u4e2a\u8fde\u63a5\u5728\u6c60\u4e2d\u6700\u5c0f\u751f\u5b58\u7684\u65f6\u95f4\uff0c\u5355\u4f4d\u662f\u6beb\u79d2
23 | spring.datasource.minEvictableIdleTimeMillis=300000
24 | spring.datasource.validationQuery=SELECT 1 FROM DUAL
25 | spring.datasource.testWhileIdle=true
26 | spring.datasource.testOnBorrow=false
27 | spring.datasource.testOnReturn=false
--------------------------------------------------------------------------------
/BootShardingStudy/target/classes/com/think/conf/DataSourceConfig.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/markdebug/springboot-shardingjdbc/716ceddebd5a4e1abe0bcdbdf8197c13a36b779b/BootShardingStudy/target/classes/com/think/conf/DataSourceConfig.class
--------------------------------------------------------------------------------
/BootShardingStudy/target/classes/com/think/conf/MyBatisMapperScannerConfig.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/markdebug/springboot-shardingjdbc/716ceddebd5a4e1abe0bcdbdf8197c13a36b779b/BootShardingStudy/target/classes/com/think/conf/MyBatisMapperScannerConfig.class
--------------------------------------------------------------------------------
/BootShardingStudy/target/classes/com/think/conf/MybatisConf.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/markdebug/springboot-shardingjdbc/716ceddebd5a4e1abe0bcdbdf8197c13a36b779b/BootShardingStudy/target/classes/com/think/conf/MybatisConf.class
--------------------------------------------------------------------------------
/BootShardingStudy/target/classes/com/think/conf/ProgramShardingAlgorithm.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/markdebug/springboot-shardingjdbc/716ceddebd5a4e1abe0bcdbdf8197c13a36b779b/BootShardingStudy/target/classes/com/think/conf/ProgramShardingAlgorithm.class
--------------------------------------------------------------------------------
/BootShardingStudy/target/classes/com/think/conf/SingleKeyModuloDatabaseShardingAlgorithm.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/markdebug/springboot-shardingjdbc/716ceddebd5a4e1abe0bcdbdf8197c13a36b779b/BootShardingStudy/target/classes/com/think/conf/SingleKeyModuloDatabaseShardingAlgorithm.class
--------------------------------------------------------------------------------
/BootShardingStudy/target/classes/com/think/conf/XbDataSource.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/markdebug/springboot-shardingjdbc/716ceddebd5a4e1abe0bcdbdf8197c13a36b779b/BootShardingStudy/target/classes/com/think/conf/XbDataSource.class
--------------------------------------------------------------------------------
/BootShardingStudy/target/classes/com/think/dao/OrderMapper.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/markdebug/springboot-shardingjdbc/716ceddebd5a4e1abe0bcdbdf8197c13a36b779b/BootShardingStudy/target/classes/com/think/dao/OrderMapper.class
--------------------------------------------------------------------------------
/BootShardingStudy/target/classes/com/think/entity/TOrder.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/markdebug/springboot-shardingjdbc/716ceddebd5a4e1abe0bcdbdf8197c13a36b779b/BootShardingStudy/target/classes/com/think/entity/TOrder.class
--------------------------------------------------------------------------------
/BootShardingStudy/target/classes/com/think/main/Application.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/markdebug/springboot-shardingjdbc/716ceddebd5a4e1abe0bcdbdf8197c13a36b779b/BootShardingStudy/target/classes/com/think/main/Application.class
--------------------------------------------------------------------------------
/BootShardingStudy/target/classes/log4j.properties:
--------------------------------------------------------------------------------
1 | log4j.rootCategory=INFO,stdout
2 |
3 | # \u63a7\u5236\u53f0\u8f93\u51fa
4 | log4j.appender.stdout=org.apache.log4j.ConsoleAppender
5 | log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
6 | log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %5p %c{1}:%L - %m%n
7 |
--------------------------------------------------------------------------------
/BootShardingStudy/target/test-classes/com/think/AppTest.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/markdebug/springboot-shardingjdbc/716ceddebd5a4e1abe0bcdbdf8197c13a36b779b/BootShardingStudy/target/test-classes/com/think/AppTest.class
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # springboot-shardingjdbc
2 | 使用spring boot+sharding-jdbc+mybatis 实现分库分表
3 |
--------------------------------------------------------------------------------
/test0.sql:
--------------------------------------------------------------------------------
1 |
2 |
3 | SET NAMES utf8;
4 | SET FOREIGN_KEY_CHECKS = 0;
5 |
6 | -- ----------------------------
7 | -- Table structure for `t_order_0`
8 | -- ----------------------------
9 | DROP TABLE IF EXISTS `t_order_0`;
10 | CREATE TABLE `t_order_0` (
11 | `order_id` int(11) NOT NULL,
12 | `user_id` int(11) NOT NULL,
13 | PRIMARY KEY (`order_id`)
14 | ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
15 |
16 | -- ----------------------------
17 | -- Table structure for `t_order_1`
18 | -- ----------------------------
19 | DROP TABLE IF EXISTS `t_order_1`;
20 | CREATE TABLE `t_order_1` (
21 | `order_id` int(11) NOT NULL,
22 | `user_id` int(11) NOT NULL,
23 | PRIMARY KEY (`order_id`)
24 | ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
25 |
26 | SET FOREIGN_KEY_CHECKS = 1;
27 |
--------------------------------------------------------------------------------
/test1.sql:
--------------------------------------------------------------------------------
1 |
2 |
3 | SET NAMES utf8;
4 | SET FOREIGN_KEY_CHECKS = 0;
5 |
6 | -- ----------------------------
7 | -- Table structure for `t_order_0`
8 | -- ----------------------------
9 | DROP TABLE IF EXISTS `t_order_0`;
10 | CREATE TABLE `t_order_0` (
11 | `order_id` int(11) NOT NULL,
12 | `user_id` int(11) NOT NULL,
13 | PRIMARY KEY (`order_id`)
14 | ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
15 |
16 | -- ----------------------------
17 | -- Table structure for `t_order_1`
18 | -- ----------------------------
19 | DROP TABLE IF EXISTS `t_order_1`;
20 | CREATE TABLE `t_order_1` (
21 | `order_id` int(11) NOT NULL,
22 | `user_id` int(11) NOT NULL,
23 | PRIMARY KEY (`order_id`)
24 | ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
25 |
26 | SET FOREIGN_KEY_CHECKS = 1;
27 |
--------------------------------------------------------------------------------