Skip to content

Commit 2c2d21b

Browse files
committed
add Sharding-JDBC
1 parent b0bbec4 commit 2c2d21b

File tree

14 files changed

+282
-0
lines changed

14 files changed

+282
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ github:
3131
| [SpringBoot-MyBaits-PageHelper](SpringBoot-MyBaits-PageHelper) | Spring-Boot 分页实现 |
3232
| [SpringBoot-MyBatis-JPA](SpringBoot-MyBatis-JPA/README.md) | Spring-Boot JPA |
3333
| [SpringBoot-MyBatis-Plus](SpringBoot-MyBatis-Plus/README.md) | SpringBoot MyBatis-Plus |
34+
| [SpringBoot-Sharding-JDBC](SpringBoot-Sharding-JDBC/README.md) | 数据库读写分离 |
3435
|[SpringBoot-ElasticSearch](SpringBoot-ElasticSearch/README.md) | SpringBoot ElasticSearch |
3536
|[SpringBoot-EasyExcel](SpringBoot-EasyExcel/README.md) | SpringBoot-EasyExcel |
3637
|[SpringBoot-Captcha](SpringBoot-Captcha/README.md) | 图片验证码 |

SpringBoot-Sharding-JDBC/.gitignore

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
HELP.md
2+
target/
3+
!.mvn/wrapper/maven-wrapper.jar
4+
!**/src/main/**/target/
5+
!**/src/test/**/target/
6+
7+
### STS ###
8+
.apt_generated
9+
.classpath
10+
.factorypath
11+
.project
12+
.settings
13+
.springBeans
14+
.sts4-cache
15+
16+
### IntelliJ IDEA ###
17+
.idea
18+
*.iws
19+
*.iml
20+
*.ipr
21+
22+
### NetBeans ###
23+
/nbproject/private/
24+
/nbbuild/
25+
/dist/
26+
/nbdist/
27+
/.nb-gradle/
28+
build/
29+
!**/src/main/**/build/
30+
!**/src/test/**/build/
31+
32+
### VS Code ###
33+
.vscode/

SpringBoot-Sharding-JDBC/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# SpringBoot Sharding-JDBC
2+
3+
4+
5+
[Sharding-Jdbc 实现读写分离 + 分库分表,写得太好了!](https://mp.weixin.qq.com/s/gakwUKWB_WTqhMS2cMo_jw)

SpringBoot-Sharding-JDBC/pom.xml

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>org.springframework.boot</groupId>
7+
<artifactId>spring-boot-starter-parent</artifactId>
8+
<version>2.7.7</version>
9+
<relativePath/> <!-- lookup parent from repository -->
10+
</parent>
11+
<groupId>com.example</groupId>
12+
<artifactId>demo</artifactId>
13+
<version>0.0.1-SNAPSHOT</version>
14+
<name>demo</name>
15+
<description>Demo project for Spring Boot</description>
16+
<properties>
17+
<java.version>1.8</java.version>
18+
</properties>
19+
<dependencies>
20+
<dependency>
21+
<groupId>org.springframework.boot</groupId>
22+
<artifactId>spring-boot-starter-web</artifactId>
23+
</dependency>
24+
<dependency>
25+
<groupId>com.baomidou</groupId>
26+
<artifactId>mybatis-plus-boot-starter</artifactId>
27+
<version>3.5.2</version>
28+
</dependency>
29+
<dependency>
30+
<groupId>mysql</groupId>
31+
<artifactId>mysql-connector-java</artifactId>
32+
<scope>runtime</scope>
33+
</dependency>
34+
35+
<dependency>
36+
<groupId>org.apache.shardingsphere</groupId>
37+
<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
38+
<version>4.0.0</version>
39+
</dependency>
40+
<dependency>
41+
<groupId>org.projectlombok</groupId>
42+
<artifactId>lombok</artifactId>
43+
<optional>true</optional>
44+
</dependency>
45+
<dependency>
46+
<groupId>org.springframework.boot</groupId>
47+
<artifactId>spring-boot-starter-test</artifactId>
48+
<scope>test</scope>
49+
</dependency>
50+
</dependencies>
51+
<build>
52+
<plugins>
53+
<plugin>
54+
<groupId>org.springframework.boot</groupId>
55+
<artifactId>spring-boot-maven-plugin</artifactId>
56+
<configuration>
57+
<excludes>
58+
<exclude>
59+
<groupId>org.projectlombok</groupId>
60+
<artifactId>lombok</artifactId>
61+
</exclude>
62+
</excludes>
63+
</configuration>
64+
</plugin>
65+
</plugins>
66+
</build>
67+
68+
</project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.example.demo;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class Application {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(Application.class, args);
11+
}
12+
13+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.example.demo.controller;
2+
3+
import com.example.demo.entity.User;
4+
import com.example.demo.service.UserService;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.web.bind.annotation.*;
7+
8+
@RestController
9+
@RequestMapping("/api/user")
10+
public class UserController {
11+
12+
@Autowired
13+
private UserService userService;
14+
15+
@PostMapping("/addUser")
16+
public User addUser(@RequestBody User user){
17+
System.out.println(user);
18+
userService.save(user);
19+
20+
return user;
21+
}
22+
23+
@GetMapping("/getUserById/{id}")
24+
public User getUserById(@PathVariable Long id){
25+
User user = userService.getById(id);
26+
user.setId(14L);
27+
return user;
28+
}
29+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.example.demo.entity;
2+
3+
import lombok.Data;
4+
5+
import java.io.Serializable;
6+
7+
@Data
8+
public class User implements Serializable {
9+
private Long id;
10+
private String name;
11+
private Integer age;
12+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.example.demo.mapper;
2+
3+
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
4+
import com.example.demo.entity.User;
5+
import org.apache.ibatis.annotations.Mapper;
6+
7+
@Mapper
8+
public interface UserMapper extends BaseMapper<User> {
9+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.example.demo.service;
2+
3+
import com.baomidou.mybatisplus.extension.service.IService;
4+
import com.example.demo.entity.User;
5+
6+
public interface UserService extends IService<User> {
7+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.example.demo.service.impl;
2+
3+
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
4+
import com.example.demo.entity.User;
5+
import com.example.demo.mapper.UserMapper;
6+
import com.example.demo.service.UserService;
7+
import org.springframework.stereotype.Service;
8+
9+
@Service
10+
public class UserServiceImpl
11+
extends ServiceImpl<UserMapper, User>
12+
implements UserService {
13+
}

0 commit comments

Comments
 (0)