├── README.md
└── Sharding-Sphere
├── pom.xml
└── src
└── main
├── java
└── com
│ └── czh
│ └── sharding
│ ├── JdbcShardingApplication.java
│ ├── controller
│ └── UserInfoController.java
│ ├── model
│ └── UserInfo.java
│ └── repository
│ └── UserInfoRepository.java
└── resources
├── application.properties
└── sharding-jdbc.yml
/README.md:
--------------------------------------------------------------------------------
1 | # springboot-shardingJDBC
2 | 使用Spring Boot + Sharding-JDBC 快速简单地实现数据库读写分离
3 | 本项目对应博文地址
4 | https://blog.csdn.net/qq_22152261/article/details/80374990
5 | 欢迎指正
6 |
--------------------------------------------------------------------------------
/Sharding-Sphere/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | com.czh
8 | sharding
9 | 0.0.1-SNAPSHOT
10 | jar
11 |
12 | sharding
13 | Demo project for Spring Boot
14 |
15 |
16 | org.springframework.boot
17 | spring-boot-starter-parent
18 | 1.5.12.RELEASE
19 |
20 |
21 |
22 |
23 | UTF-8
24 | UTF-8
25 | 1.8
26 |
27 |
28 |
29 |
30 | org.springframework.boot
31 | spring-boot-starter-data-jpa
32 |
33 |
34 | org.apache.tomcat
35 | tomcat-jdbc
36 |
37 |
38 |
39 |
40 | org.springframework.boot
41 | spring-boot-starter-web
42 |
43 |
44 |
45 | mysql
46 | mysql-connector-java
47 |
48 |
49 |
50 | com.zaxxer
51 | HikariCP
52 |
53 |
54 |
55 | io.shardingjdbc
56 | sharding-jdbc-core
57 | 2.0.3
58 |
59 |
60 | org.springframework.boot
61 | spring-boot-starter-test
62 | test
63 |
64 |
65 |
66 |
67 |
68 |
69 | org.springframework.boot
70 | spring-boot-maven-plugin
71 |
72 |
73 |
74 |
75 |
--------------------------------------------------------------------------------
/Sharding-Sphere/src/main/java/com/czh/sharding/JdbcShardingApplication.java:
--------------------------------------------------------------------------------
1 | package com.czh.sharding;
2 |
3 | import java.io.FileNotFoundException;
4 | import java.io.IOException;
5 | import java.sql.SQLException;
6 |
7 | import javax.sql.DataSource;
8 |
9 | import org.springframework.boot.SpringApplication;
10 | import org.springframework.boot.autoconfigure.SpringBootApplication;
11 | import org.springframework.context.annotation.Bean;
12 | import org.springframework.util.ResourceUtils;
13 |
14 | import io.shardingjdbc.core.api.MasterSlaveDataSourceFactory;
15 |
16 | /**
17 | * 入口
18 |
19 | * @author 菜头君
20 |
21 | * @date 2018年5月19日
22 | */
23 | @SpringBootApplication
24 | public class JdbcShardingApplication {
25 |
26 | public static void main(String[] args) {
27 | SpringApplication.run(JdbcShardingApplication.class, args);
28 | }
29 |
30 | /**
31 | * 配置读写分离数据源
32 | * @return
33 | * @throws FileNotFoundException
34 | * @throws SQLException
35 | * @throws IOException
36 | */
37 | @Bean
38 | public DataSource dataSource() throws FileNotFoundException, SQLException, IOException {
39 | return MasterSlaveDataSourceFactory.createDataSource(ResourceUtils.getFile("classpath:sharding-jdbc.yml"));
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/Sharding-Sphere/src/main/java/com/czh/sharding/controller/UserInfoController.java:
--------------------------------------------------------------------------------
1 | package com.czh.sharding.controller;
2 |
3 | import java.util.List;
4 |
5 | import org.springframework.beans.factory.annotation.Autowired;
6 | import org.springframework.web.bind.annotation.GetMapping;
7 | import org.springframework.web.bind.annotation.PathVariable;
8 | import org.springframework.web.bind.annotation.RestController;
9 |
10 | import com.czh.sharding.model.UserInfo;
11 | import com.czh.sharding.repository.UserInfoRepository;
12 |
13 | /**
14 | * 用户信息控制器
15 |
16 | * @author 菜头君
17 |
18 | * @date 2018年5月19日
19 | */
20 | @RestController
21 | public class UserInfoController {
22 | @Autowired
23 | UserInfoRepository userInfoRepository;
24 |
25 | /**
26 | * 获取所有用户信息
27 | * @return
28 | */
29 | @GetMapping("/userinfo")
30 | public List getUserInfos(){
31 | return userInfoRepository.findAll();
32 | }
33 |
34 | /**
35 | * 增加新用户
36 | * @param name
37 | * @return
38 | */
39 | @GetMapping("/userinfo/{name}")
40 | public UserInfo addUserInfo(@PathVariable String name){
41 | UserInfo userInfo = new UserInfo();
42 | userInfo.setName(name);
43 | return userInfoRepository.save(userInfo);
44 | }
45 |
46 | /**
47 | * 增加新用户后再立即查找该用户信息
48 | * @param name
49 | * @return
50 | */
51 | @GetMapping("/userinfo/wr/{name}")
52 | public UserInfo writeAndRead(@PathVariable String name) {
53 | UserInfo userInfo = new UserInfo();
54 | userInfo.setName(name);
55 | userInfoRepository.saveAndFlush(userInfo);
56 | return userInfoRepository.findOne(userInfo.getId());
57 | }
58 | }
59 |
--------------------------------------------------------------------------------
/Sharding-Sphere/src/main/java/com/czh/sharding/model/UserInfo.java:
--------------------------------------------------------------------------------
1 | package com.czh.sharding.model;
2 |
3 | import javax.persistence.Column;
4 | import javax.persistence.Entity;
5 | import javax.persistence.GeneratedValue;
6 | import javax.persistence.GenerationType;
7 | import javax.persistence.Id;
8 | import javax.persistence.Table;
9 |
10 | /**
11 | * 用户信息实体
12 |
13 | * @author 菜头君
14 |
15 | * @date 2018年5月19日
16 | */
17 | @Table(name ="userinfo")
18 | @Entity
19 | public class UserInfo {
20 | @Id
21 | @GeneratedValue(strategy=GenerationType.IDENTITY)
22 | private int id;
23 | @Column
24 | private String name;
25 | public int getId() {
26 | return id;
27 | }
28 | public void setId(int id) {
29 | this.id = id;
30 | }
31 | public String getName() {
32 | return name;
33 | }
34 | public void setName(String name) {
35 | this.name = name;
36 | }
37 |
38 | }
39 |
--------------------------------------------------------------------------------
/Sharding-Sphere/src/main/java/com/czh/sharding/repository/UserInfoRepository.java:
--------------------------------------------------------------------------------
1 | package com.czh.sharding.repository;
2 |
3 | import org.springframework.data.jpa.repository.JpaRepository;
4 |
5 | import com.czh.sharding.model.UserInfo;
6 |
7 | /**
8 | * 用户表操作接口
9 |
10 | * @author 菜头君
11 |
12 | * @date 2018年5月19日
13 | */
14 | public interface UserInfoRepository extends JpaRepository{
15 |
16 | }
17 |
--------------------------------------------------------------------------------
/Sharding-Sphere/src/main/resources/application.properties:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/codercaizh/springboot-shardingJDBC/0f73a3b3cb76e27c043e45f5716eb752d99ae203/Sharding-Sphere/src/main/resources/application.properties
--------------------------------------------------------------------------------
/Sharding-Sphere/src/main/resources/sharding-jdbc.yml:
--------------------------------------------------------------------------------
1 | dataSources:
2 | db_master: !!com.zaxxer.hikari.HikariDataSource
3 | driverClassName: com.mysql.jdbc.Driver
4 | jdbcUrl: jdbc:mysql://192.168.10.100:3306/sphere?useUnicode=true&characterEncoding=utf-8&useSSL=false
5 | username: root
6 | password: 123456
7 | db_slave: !!com.zaxxer.hikari.HikariDataSource
8 | driverClassName: com.mysql.jdbc.Driver
9 | jdbcUrl: jdbc:mysql://192.168.10.100:3307/sphere?useUnicode=true&characterEncoding=utf-8&useSSL=false
10 | username: root
11 | password: 123456
12 | masterSlaveRule:
13 | name: db_ms
14 | masterDataSourceName: db_master
15 | slaveDataSourceNames: [db_slave]
--------------------------------------------------------------------------------