新增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 {
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user