新增sharding-jdbc-demo分库分表演示工程(sharding-sphere3.1.0)
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
package com.sharding;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
/**
|
||||
* sharding-jdbc demo
|
||||
*
|
||||
* @author zlt
|
||||
*/
|
||||
@SpringBootApplication
|
||||
public class ShardingApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(ShardingApplication.class, args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.sharding;
|
||||
|
||||
import com.central.common.utils.IdGenerator;
|
||||
|
||||
/**
|
||||
* sharding-jdbc demo
|
||||
*
|
||||
* @author zlt
|
||||
*/
|
||||
public class TestApplication {
|
||||
public static void main(String[] args) {
|
||||
for (int i = 0; i < 100; i++) {
|
||||
long userId = IdGenerator.getId();
|
||||
System.out.println(userId);
|
||||
userId = userId & 15;
|
||||
System.out.println(userId);
|
||||
}
|
||||
/*
|
||||
long orderId = IdGenerator.getId();
|
||||
System.out.println(orderId);
|
||||
orderId = (orderId << 4) | userId;
|
||||
System.out.println(orderId);*/
|
||||
}
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
package com.sharding.demo.config;
|
||||
|
||||
import com.baomidou.mybatisplus.core.injector.ISqlInjector;
|
||||
import com.baomidou.mybatisplus.extension.injector.LogicSqlInjector;
|
||||
import com.central.db.config.DefaultMybatisPlusConfig;
|
||||
import org.mybatis.spring.annotation.MapperScan;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* @author zlt
|
||||
*/
|
||||
@Configuration
|
||||
@MapperScan({"com.sharding.demo.mapper*"})
|
||||
public class MybatisPlusConfig extends DefaultMybatisPlusConfig {
|
||||
/**
|
||||
* 逻辑删除注入器
|
||||
*/
|
||||
@Bean
|
||||
public ISqlInjector sqlInjector(){
|
||||
return new LogicSqlInjector();
|
||||
}
|
||||
}
|
||||
+68
@@ -0,0 +1,68 @@
|
||||
package com.sharding.demo.controller;
|
||||
|
||||
import com.sharding.demo.model.User;
|
||||
import com.sharding.demo.service.IUserService;
|
||||
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 java.util.List;
|
||||
|
||||
/**
|
||||
* @author zlt
|
||||
*/
|
||||
@RestController
|
||||
public class UserController {
|
||||
private final IUserService userService;
|
||||
|
||||
@Autowired
|
||||
public UserController(IUserService userService) {
|
||||
this.userService = userService;
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化数据
|
||||
*/
|
||||
@GetMapping("/init")
|
||||
public String initDate() {
|
||||
String companyId;
|
||||
for (int i = 0; i < 100; i++) {
|
||||
User u = new User();
|
||||
if (i % 2 == 0) {
|
||||
companyId = "alibaba";
|
||||
} else {
|
||||
companyId = "baidu";
|
||||
}
|
||||
u.setCompanyId(companyId);
|
||||
u.setName(String.valueOf(i));
|
||||
userService.save(u);
|
||||
}
|
||||
return "success";
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询列表
|
||||
*/
|
||||
@GetMapping("/")
|
||||
public List<User> list() {
|
||||
return userService.list();
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询单条记录
|
||||
*/
|
||||
@GetMapping("/{id}")
|
||||
public User get(@PathVariable Long id) {
|
||||
return userService.getById(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 清除数据
|
||||
*/
|
||||
@GetMapping("/clean")
|
||||
public String clean() {
|
||||
userService.remove(null);
|
||||
return "success";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.sharding.demo.mapper;
|
||||
|
||||
import com.central.db.mapper.SuperMapper;
|
||||
import com.sharding.demo.model.User;
|
||||
|
||||
/**
|
||||
* @author zlt
|
||||
*/
|
||||
public interface UserMapper extends SuperMapper<User> {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.sharding.demo.model;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import com.central.common.model.SuperEntity;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
/**
|
||||
* @author zlt
|
||||
*/
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = false)
|
||||
@TableName("user")
|
||||
public class User extends SuperEntity {
|
||||
private static final long serialVersionUID = 8898492657846787286L;
|
||||
private String companyId;
|
||||
private String name;
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.sharding.demo.service;
|
||||
|
||||
import com.central.common.service.ISuperService;
|
||||
import com.sharding.demo.model.User;
|
||||
|
||||
/**
|
||||
* @author zlt
|
||||
*/
|
||||
public interface IUserService extends ISuperService<User> {
|
||||
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
package com.sharding.demo.service.impl;
|
||||
|
||||
import com.central.common.service.impl.SuperServiceImpl;
|
||||
import com.sharding.demo.mapper.UserMapper;
|
||||
import com.sharding.demo.model.User;
|
||||
import com.sharding.demo.service.IUserService;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* @author zlt
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
public class UserServiceImpl extends SuperServiceImpl<UserMapper, User> implements IUserService {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
sharding:
|
||||
jdbc:
|
||||
datasource:
|
||||
names: ds-master,ds-slave-0,ds-slave-1
|
||||
ds-master:
|
||||
type: com.alibaba.druid.pool.DruidDataSource
|
||||
driver-class-name: com.mysql.jdbc.Driver
|
||||
url: jdbc:mysql://${zlt.datasource.ip}:3306/demo_ds_master?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull
|
||||
username: ${zlt.datasource.username}
|
||||
password: ${zlt.datasource.password}
|
||||
ds-slave-0:
|
||||
type: com.alibaba.druid.pool.DruidDataSource
|
||||
driver-class-name: com.mysql.jdbc.Driver
|
||||
url: jdbc:mysql://${zlt.datasource.ip}:3306/demo_ds_slave_0?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull
|
||||
username: ${zlt.datasource.username}
|
||||
password: ${zlt.datasource.password}
|
||||
ds-slave-1:
|
||||
type: com.alibaba.druid.pool.DruidDataSource
|
||||
driver-class-name: com.mysql.jdbc.Driver
|
||||
url: jdbc:mysql://${zlt.datasource.ip}:3306/demo_ds_slave_1?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull
|
||||
username: ${zlt.datasource.username}
|
||||
password: ${zlt.datasource.password}
|
||||
config:
|
||||
masterslave:
|
||||
load-balance-algorithm-type: round_robin
|
||||
name: ds_ms
|
||||
master-data-source-name: ds-master
|
||||
slave-data-source-names: ds-slave-0,ds-slave-1
|
||||
props:
|
||||
sql:
|
||||
show: true
|
||||
@@ -0,0 +1,29 @@
|
||||
sharding:
|
||||
jdbc:
|
||||
datasource:
|
||||
names: ds-0,ds-1
|
||||
ds-0:
|
||||
type: com.alibaba.druid.pool.DruidDataSource
|
||||
driver-class-name: com.mysql.jdbc.Driver
|
||||
url: jdbc:mysql://${zlt.datasource.ip}:3306/demo_ds_0?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull
|
||||
username: ${zlt.datasource.username}
|
||||
password: ${zlt.datasource.password}
|
||||
ds-1:
|
||||
type: com.alibaba.druid.pool.DruidDataSource
|
||||
driver-class-name: com.mysql.jdbc.Driver
|
||||
url: jdbc:mysql://${zlt.datasource.ip}:3306/demo_ds_1?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull
|
||||
username: ${zlt.datasource.username}
|
||||
password: ${zlt.datasource.password}
|
||||
config:
|
||||
sharding:
|
||||
default-database-strategy:
|
||||
inline:
|
||||
sharding-column: id
|
||||
algorithm-expression: ds-$->{id % 2}
|
||||
tables:
|
||||
user:
|
||||
actual-data-nodes: ds-$->{0..1}.user
|
||||
#key-generator-column-name: id
|
||||
props:
|
||||
sql:
|
||||
show: true
|
||||
@@ -0,0 +1,29 @@
|
||||
sharding:
|
||||
jdbc:
|
||||
datasource:
|
||||
names: ds-alibaba,ds-baidu
|
||||
ds-alibaba:
|
||||
type: com.alibaba.druid.pool.DruidDataSource
|
||||
driver-class-name: com.mysql.jdbc.Driver
|
||||
url: jdbc:mysql://${zlt.datasource.ip}:3306/demo_ds_alibaba?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull
|
||||
username: ${zlt.datasource.username}
|
||||
password: ${zlt.datasource.password}
|
||||
ds-baidu:
|
||||
type: com.alibaba.druid.pool.DruidDataSource
|
||||
driver-class-name: com.mysql.jdbc.Driver
|
||||
url: jdbc:mysql://${zlt.datasource.ip}:3306/demo_ds_baidu?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull
|
||||
username: ${zlt.datasource.username}
|
||||
password: ${zlt.datasource.password}
|
||||
config:
|
||||
sharding:
|
||||
default-database-strategy:
|
||||
inline:
|
||||
sharding-column: company_id
|
||||
algorithm-expression: ds-$->{company_id}
|
||||
tables:
|
||||
user:
|
||||
actual-data-nodes: ds-$->{['alibaba','baidu']}.user
|
||||
key-generator-column-name: id
|
||||
props:
|
||||
sql:
|
||||
show: true
|
||||
@@ -0,0 +1,36 @@
|
||||
sharding:
|
||||
jdbc:
|
||||
datasource:
|
||||
names: ds,ds-0,ds-1
|
||||
ds:
|
||||
type: com.alibaba.druid.pool.DruidDataSource
|
||||
driver-class-name: com.mysql.jdbc.Driver
|
||||
url: jdbc:mysql://${zlt.datasource.ip}:3306/demo_ds?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull
|
||||
username: ${zlt.datasource.username}
|
||||
password: ${zlt.datasource.password}
|
||||
ds-0:
|
||||
type: com.alibaba.druid.pool.DruidDataSource
|
||||
driver-class-name: com.mysql.jdbc.Driver
|
||||
url: jdbc:mysql://${zlt.datasource.ip}:3306/demo_ds_0?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull
|
||||
username: ${zlt.datasource.username}
|
||||
password: ${zlt.datasource.password}
|
||||
ds-1:
|
||||
type: com.alibaba.druid.pool.DruidDataSource
|
||||
driver-class-name: com.mysql.jdbc.Driver
|
||||
url: jdbc:mysql://${zlt.datasource.ip}:3306/demo_ds_1?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull
|
||||
username: ${zlt.datasource.username}
|
||||
password: ${zlt.datasource.password}
|
||||
config:
|
||||
sharding:
|
||||
default-data-source-name: ds
|
||||
default-database-strategy:
|
||||
inline:
|
||||
sharding-column: id
|
||||
algorithm-expression: ds-$->{id % 2}
|
||||
tables:
|
||||
user:
|
||||
actual-data-nodes: ds-$->{0..1}.user
|
||||
key-generator-column-name: id
|
||||
props:
|
||||
sql:
|
||||
show: true
|
||||
@@ -0,0 +1,23 @@
|
||||
sharding:
|
||||
jdbc:
|
||||
datasource:
|
||||
names: ds
|
||||
ds:
|
||||
type: com.alibaba.druid.pool.DruidDataSource
|
||||
driver-class-name: com.mysql.jdbc.Driver
|
||||
url: jdbc:mysql://${zlt.datasource.ip}:3306/demo_ds?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull
|
||||
username: ${zlt.datasource.username}
|
||||
password: ${zlt.datasource.password}
|
||||
config:
|
||||
sharding:
|
||||
tables:
|
||||
user:
|
||||
actual-data-nodes: ds.user_$->{0..1}
|
||||
table-strategy:
|
||||
inline:
|
||||
sharding-column: id
|
||||
algorithm-expression: user_$->{id % 2}
|
||||
key-generator-column-name: id
|
||||
props:
|
||||
sql:
|
||||
show: true
|
||||
@@ -0,0 +1,22 @@
|
||||
server:
|
||||
port: 11001
|
||||
|
||||
spring:
|
||||
profiles:
|
||||
active: sharding-databases
|
||||
application:
|
||||
name: sharding-jdbc-demo
|
||||
|
||||
zlt:
|
||||
datasource:
|
||||
ip: 192.168.28.131
|
||||
username: root
|
||||
password: 1q2w3e4r
|
||||
|
||||
mybatis-plus:
|
||||
mapper-locations: classpath:/mapper/*Mapper.xml
|
||||
#实体扫描,多个package用逗号或者分号分隔
|
||||
typeAliasesPackage: com.sharding.demo.model
|
||||
global-config:
|
||||
db-config:
|
||||
id-type: ID_WORKER
|
||||
Reference in New Issue
Block a user