Skip to content

Commit

Permalink
不分库,只分表+读写分离案例
Browse files Browse the repository at this point in the history
  • Loading branch information
尹吉欢 committed Apr 22, 2019
1 parent 77292a6 commit 5262baa
Show file tree
Hide file tree
Showing 19 changed files with 568 additions and 0 deletions.
58 changes: 58 additions & 0 deletions sjdbc-sharding-table-read-write-springboot/pom.xml
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-sharding-table-read-write-springboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>sjdbc-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>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.cxytiandi.sharding;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
* 不分库,只分表+读写分离案例
* @author yinjihuan
*/
@SpringBootApplication
public class ShardingTableReadWriteApplication {
public static void main(String[] args) {
SpringApplication.run(ShardingTableReadWriteApplication.class, args);
}
}
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();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
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.RestController;

import com.cxytiandi.sharding.po.LouDong;
import com.cxytiandi.sharding.service.LouDongService;

@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";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
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.setId(i);
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);
}

}
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;
}

}
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;
}


}
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();
}
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);
}
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);

}
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);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.cxytiandi.sharding.service;

import java.util.List;

import com.cxytiandi.sharding.po.User;

public interface UserService {

List<User> list();

Long add(User user);

User findById(Long id);

User findByName(String name);

}
Loading

0 comments on commit 5262baa

Please sign in to comment.