forked from yinjihuan/sharding-jdbc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Spring Boot版 Sharding JDBC 分库分表+读写分离案例
- Loading branch information
尹吉欢
committed
Apr 23, 2019
1 parent
c520847
commit 1c6dd83
Showing
21 changed files
with
598 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>com.cxytiandi</groupId> | ||
<artifactId>sjdbc-db-sharding-table-read-write-springboot</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<packaging>jar</packaging> | ||
|
||
<name>sjdbc-db-sharding-table-read-write-springboot</name> | ||
<url>http://maven.apache.org</url> | ||
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
</properties> | ||
|
||
<parent> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-parent</artifactId> | ||
<version>2.0.6.RELEASE</version> | ||
<relativePath /> | ||
</parent> | ||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.mybatis.spring.boot</groupId> | ||
<artifactId>mybatis-spring-boot-starter</artifactId> | ||
<version>2.0.1</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>mysql</groupId> | ||
<artifactId>mysql-connector-java</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>com.alibaba</groupId> | ||
<artifactId>druid-spring-boot-starter</artifactId> | ||
<version>1.1.16</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.apache.shardingsphere</groupId> | ||
<artifactId>sharding-jdbc-spring-boot-starter</artifactId> | ||
<version>4.0.0-RC1</version> | ||
</dependency> | ||
|
||
<!-- <dependency> | ||
<groupId>io.shardingjdbc</groupId> | ||
<artifactId>sharding-jdbc-spring-boot-starter</artifactId> | ||
<version>2.0.0.M3</version> | ||
</dependency> --> | ||
</dependencies> | ||
</project> |
18 changes: 18 additions & 0 deletions
18
...-springboot/src/main/java/com/cxytiandi/sharding/ShardingDbTableReadWriteApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.cxytiandi.sharding; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
/** | ||
* Spring Boot版 Sharding JDBC 分库分表+读写分离案例 | ||
* | ||
* @author yinjihuan | ||
* | ||
* @about http://cxytiandi.com/about | ||
*/ | ||
@SpringBootApplication | ||
public class ShardingDbTableReadWriteApplication { | ||
public static void main(String[] args) { | ||
SpringApplication.run(ShardingDbTableReadWriteApplication.class, args); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
...springboot/src/main/java/com/cxytiandi/sharding/algorithm/MyPreciseShardingAlgorithm.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.cxytiandi.sharding.algorithm; | ||
|
||
import java.util.Collection; | ||
|
||
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingAlgorithm; | ||
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingValue; | ||
|
||
/** | ||
* 自定义分片算法 | ||
* | ||
* @author yinjihuan | ||
* | ||
*/ | ||
public class MyPreciseShardingAlgorithm implements PreciseShardingAlgorithm<Long> { | ||
|
||
@Override | ||
public String doSharding(Collection<String> availableTargetNames, PreciseShardingValue<Long> shardingValue) { | ||
for (String tableName : availableTargetNames) { | ||
if (tableName.endsWith(shardingValue.getValue() % 4 + "")) { | ||
return tableName; | ||
} | ||
} | ||
throw new IllegalArgumentException(); | ||
} | ||
|
||
} |
39 changes: 39 additions & 0 deletions
39
...d-write-springboot/src/main/java/com/cxytiandi/sharding/controller/LouDongController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.cxytiandi.sharding.controller; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import com.cxytiandi.sharding.po.LouDong; | ||
import com.cxytiandi.sharding.po.User; | ||
import com.cxytiandi.sharding.service.LouDongService; | ||
import com.cxytiandi.sharding.service.UserService; | ||
|
||
@RestController | ||
public class LouDongController { | ||
|
||
@Autowired | ||
private LouDongService louDongService; | ||
|
||
@GetMapping("/lds") | ||
public Object list() { | ||
return louDongService.list(); | ||
} | ||
|
||
@GetMapping("/ld/add") | ||
public Object add() { | ||
for (long i = 0; i < 10; i++) { | ||
LouDong louDong = new LouDong(); | ||
louDong.setId(i+"a"); | ||
louDong.setCity("深圳"); | ||
louDong.setRegion("宝安"); | ||
louDong.setName("李四"); | ||
louDong.setLdNum("A"); | ||
louDong.setUnitNum("2"); | ||
louDongService.addLouDong(louDong); | ||
} | ||
return "success"; | ||
} | ||
|
||
} |
43 changes: 43 additions & 0 deletions
43
...read-write-springboot/src/main/java/com/cxytiandi/sharding/controller/UserController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.cxytiandi.sharding.controller; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import com.cxytiandi.sharding.po.User; | ||
import com.cxytiandi.sharding.service.UserService; | ||
|
||
@RestController | ||
public class UserController { | ||
|
||
@Autowired | ||
private UserService userService; | ||
|
||
@GetMapping("/users") | ||
public Object list() { | ||
return userService.list(); | ||
} | ||
|
||
@GetMapping("/add") | ||
public Object add() { | ||
for (long i = 0; i < 100; i++) { | ||
User user = new User(); | ||
user.setCity("深圳"); | ||
user.setName("李四"); | ||
userService.add(user); | ||
} | ||
return "success"; | ||
} | ||
|
||
@GetMapping("/users/{id}") | ||
public Object get(@PathVariable Long id) { | ||
return userService.findById(id); | ||
} | ||
|
||
@GetMapping("/users/query") | ||
public Object get(String name) { | ||
return userService.findByName(name); | ||
} | ||
|
||
} |
70 changes: 70 additions & 0 deletions
70
...sharding-table-read-write-springboot/src/main/java/com/cxytiandi/sharding/po/LouDong.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package com.cxytiandi.sharding.po; | ||
|
||
/** | ||
* 不分表 | ||
* @author yinjihuan | ||
* | ||
*/ | ||
public class LouDong { | ||
|
||
private String id; | ||
|
||
private String city; | ||
|
||
private String region; | ||
|
||
private String name; | ||
|
||
private String ldNum; | ||
|
||
private String unitNum; | ||
|
||
public String getId() { | ||
return id; | ||
} | ||
|
||
public void setId(String id) { | ||
this.id = id; | ||
} | ||
|
||
public String getCity() { | ||
return city; | ||
} | ||
|
||
public void setCity(String city) { | ||
this.city = city; | ||
} | ||
|
||
public String getRegion() { | ||
return region; | ||
} | ||
|
||
public void setRegion(String region) { | ||
this.region = region; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public String getLdNum() { | ||
return ldNum; | ||
} | ||
|
||
public void setLdNum(String ldNum) { | ||
this.ldNum = ldNum; | ||
} | ||
|
||
public String getUnitNum() { | ||
return unitNum; | ||
} | ||
|
||
public void setUnitNum(String unitNum) { | ||
this.unitNum = unitNum; | ||
} | ||
|
||
} |
45 changes: 45 additions & 0 deletions
45
...db-sharding-table-read-write-springboot/src/main/java/com/cxytiandi/sharding/po/User.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.cxytiandi.sharding.po; | ||
|
||
import java.io.Serializable; | ||
|
||
/** | ||
* 分表 | ||
* @author yinjihuan | ||
* | ||
*/ | ||
public class User implements Serializable { | ||
|
||
private static final long serialVersionUID = -1205226416664488559L; | ||
|
||
private Long id; | ||
|
||
private String city = ""; | ||
|
||
private String name = ""; | ||
|
||
public Long getId() { | ||
return id; | ||
} | ||
|
||
public void setId(Long id) { | ||
this.id = id; | ||
} | ||
|
||
public String getCity() { | ||
return city; | ||
} | ||
|
||
public void setCity(String city) { | ||
this.city = city; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
|
||
} |
14 changes: 14 additions & 0 deletions
14
...d-write-springboot/src/main/java/com/cxytiandi/sharding/repository/LouDongRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.cxytiandi.sharding.repository; | ||
|
||
import java.util.List; | ||
import org.apache.ibatis.annotations.Mapper; | ||
import com.cxytiandi.sharding.po.LouDong; | ||
|
||
|
||
@Mapper | ||
public interface LouDongRepository { | ||
|
||
Long addLouDong(LouDong louDong); | ||
|
||
List<LouDong> list(); | ||
} |
20 changes: 20 additions & 0 deletions
20
...read-write-springboot/src/main/java/com/cxytiandi/sharding/repository/UserRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.cxytiandi.sharding.repository; | ||
|
||
import java.util.List; | ||
|
||
import org.apache.ibatis.annotations.Mapper; | ||
|
||
import com.cxytiandi.sharding.po.User; | ||
|
||
|
||
@Mapper | ||
public interface UserRepository { | ||
|
||
Long addUser(User user); | ||
|
||
List<User> list(); | ||
|
||
User findById(Long id); | ||
|
||
User findByName(String name); | ||
} |
13 changes: 13 additions & 0 deletions
13
...le-read-write-springboot/src/main/java/com/cxytiandi/sharding/service/LouDongService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.cxytiandi.sharding.service; | ||
|
||
import java.util.List; | ||
|
||
import com.cxytiandi.sharding.po.LouDong; | ||
|
||
public interface LouDongService { | ||
|
||
List<LouDong> list(); | ||
|
||
Long addLouDong(LouDong louDong); | ||
|
||
} |
27 changes: 27 additions & 0 deletions
27
...ead-write-springboot/src/main/java/com/cxytiandi/sharding/service/LouDongServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.cxytiandi.sharding.service; | ||
|
||
import java.util.List; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
|
||
import com.cxytiandi.sharding.po.LouDong; | ||
import com.cxytiandi.sharding.repository.LouDongRepository; | ||
|
||
@Service | ||
public class LouDongServiceImpl implements LouDongService { | ||
|
||
@Autowired | ||
private LouDongRepository louDongRepository; | ||
|
||
@Override | ||
public List<LouDong> list() { | ||
return louDongRepository.list(); | ||
} | ||
|
||
@Override | ||
public Long addLouDong(LouDong louDong) { | ||
return louDongRepository.addLouDong(louDong); | ||
} | ||
|
||
} |
Oops, something went wrong.