feat:新增shardingsphere

This commit is contained in:
fanxb 2019-03-22 17:26:38 +08:00
parent bc231c2f85
commit 2c481c9982
18 changed files with 469 additions and 0 deletions

29
spring-boot/sjdemo/.gitignore vendored Normal file
View File

@ -0,0 +1,29 @@
HELP.md
/target/
!.mvn/wrapper/maven-wrapper.jar
### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
/build/
### VS Code ###
.vscode/

View File

@ -0,0 +1,69 @@
<?xml version="1.0" encoding="UTF-8"?>
<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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.fanxb</groupId>
<artifactId>sjdemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>sjdemo</name>
<description>sharding-jdbc分库分表demo</description>
<properties>
<java.version>1.8</java.version>
</properties>
<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.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>io.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
<version>3.1.0</version>
</dependency>
<!--druid versoin-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.14</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,15 @@
package com.fanxb.sjdemo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan(basePackages = "com.fanxb.sjdemo.dao")
public class SjdemoApplication {
public static void main(String[] args) {
SpringApplication.run(SjdemoApplication.class, args);
}
}

View File

@ -0,0 +1,12 @@
package com.fanxb.sjdemo.dao;
/**
* 类功能简述
* 类功能详述
*
* @author fanxb
* @date 2019/3/22 15:21
*/
public interface BaseOperate <T>{
long addOne(T t);
}

View File

@ -0,0 +1,15 @@
package com.fanxb.sjdemo.dao;
import com.fanxb.sjdemo.entity.Order;
/**
* 类功能简述
* 类功能详述
*
* @author fanxb
* @date 2019/3/22 16:25
*/
public interface OrderDao {
long addOne(Order order);
}

View File

@ -0,0 +1,19 @@
package com.fanxb.sjdemo.dao;
import com.fanxb.sjdemo.entity.User;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
* 类功能简述
* 类功能详述
*
* @author fanxb
* @date 2019/3/22 15:29
*/
public interface UserDao {
long addOne(User user);
User getOneById(int id);
}

View File

@ -0,0 +1,15 @@
package com.fanxb.sjdemo.entity;
import lombok.Data;
import java.util.Date;
@Data
public class Order {
private long orderId;
private long uId;
private Date createTime;
private long totalPrice;
}

View File

@ -0,0 +1,14 @@
package com.fanxb.sjdemo.entity;
import lombok.Data;
@Data
public class OrderItem {
private long orderItemId;
private long orderId;
private String name;
private long price;
}

View File

@ -0,0 +1,33 @@
package com.fanxb.sjdemo.entity;
public class User {
private long uId;
private String name;
private long age;
public long getuId() {
return uId;
}
public void setuId(long uId) {
this.uId = uId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public long getAge() {
return age;
}
public void setAge(long age) {
this.age = age;
}
}

View File

@ -0,0 +1,28 @@
package com.fanxb.sjdemo.service;
import com.fanxb.sjdemo.dao.OrderDao;
import com.fanxb.sjdemo.entity.Order;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* 类功能简述
* 类功能详述
*
* @author fanxb
* @date 2019/3/22 16:44
*/
@Service
public class OrderService {
private Logger logger = LoggerFactory.getLogger(OrderService.class);
@Autowired
private OrderDao orderDao;
public void insertOne(Order order) {
long id = orderDao.addOne(order);
logger.info("订单插入id{}", order.getOrderId());
}
}

View File

@ -0,0 +1,35 @@
package com.fanxb.sjdemo.service;
import com.fanxb.sjdemo.dao.UserDao;
import com.fanxb.sjdemo.entity.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* 类功能简述
* 类功能详述
*
* @author fanxb
* @date 2019/3/22 15:36
*/
@Service
public class UserService {
Logger logger = LoggerFactory.getLogger(UserService.class);
@Autowired
private UserDao dao;
public void addOne(String name, int age) {
User user = new User();
user.setName(name);
user.setAge(age);
long id= dao.addOne(user);
logger.info("插入id:{}", user.getuId());
}
public User getOne(int id){
return dao.getOneById(id);
}
}

View File

@ -0,0 +1,47 @@
sharding:
jdbc:
datasource:
names: ds0,ds1,ds2
ds0:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://10.82.27.177:3306/ds0
username: root
password: 123456
ds1:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://10.82.27.177:3306/ds1
username: root
password: 123456
ds2:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://10.82.27.177:3306/ds2
username: root
password: 123456
config:
sharding:
# 默认数据源,可以将不分库分表的数据表放在这里
default-data-source-name: ds2
default-database-strategy:
inline:
sharding-column: uId
algorithm-expression: ds$->{uId % 2}
tables:
order:
key-generator-column-name: orderId
actual-data-nodes: ds$->{0..1}.order$->{0..1}
table-strategy:
inline:
sharding-column: orderId
algorithm-expression: order$->{orderId%2}
order_item:
key-generator-column-name: orderItemId
actual-data-nodes: ds$->{0..1}.order_item$->{0..1}
table-strategy:
inline:
sharding-column: orderId
algorithm-expression: orderItemId$->{orderId%2}
props:
sql.show: true

View File

@ -0,0 +1,8 @@
spring:
profiles:
active: sharding
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.fanxb.sjdemo.entity
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.fanxb.sjdemo.dao.OrderDao">
<insert id="addOne" useGeneratedKeys="true" keyProperty="orderId">
insert into order(uId,createTime,totalPrice) values(#{uId},#{createTime},#{totalPrice})
</insert>
</mapper>

View File

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.fanxb.sjdemo.dao.UserDao">
<insert id="addOne" useGeneratedKeys="true" keyProperty="uId">
insert into user(name,age) values(#{name},#{age})
</insert>
<select id="getOneById" resultType="User">
select * from user where uId=#{id}
</select>
</mapper>

View File

@ -0,0 +1,36 @@
package com.fanxb.sjdemo;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.Statement;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SjdemoApplicationTests {
private Logger logger = LoggerFactory.getLogger(this.getClass());
@Resource
private DataSource dataSource;
@Test
public void contextLoads() throws Exception {
String sql = "SELECT i.* FROM order o JOIN order_item i ON o.orderId=i.orderId WHERE o.uId=? AND o.orderId=?";
try (
Connection conn = dataSource.getConnection();
Statement statement = conn.createStatement();
) {
Boolean res = statement.execute("insert into user(name,age) value('2012-12-12 12:12:12',1212)");
logger.info(res.toString());
}
}
}

View File

@ -0,0 +1,35 @@
package com.fanxb.sjdemo.serviceTest;
import com.fanxb.sjdemo.entity.Order;
import com.fanxb.sjdemo.service.OrderService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.Date;
/**
* 类功能简述
* 类功能详述
*
* @author fanxb
* @date 2019/3/22 15:47
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class OrderServiceTest {
@Autowired
private OrderService orderService;
@Test
public void addOrderTest() {
Order order = new Order();
order.setUId(12);
order.setCreateTime(new Date());
order.setTotalPrice(12);
orderService.insertOne(order);
}
}

View File

@ -0,0 +1,37 @@
package com.fanxb.sjdemo.serviceTest;
import com.fanxb.sjdemo.service.UserService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
/**
* 类功能简述
* 类功能详述
*
* @author fanxb
* @date 2019/3/22 15:47
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserServiceTest {
private Logger logger = LoggerFactory.getLogger(UserServiceTest.class);
@Autowired
private UserService userService;
@Test
public void addUserTest(){
userService.addOne("xiaoming",12);
}
@Test
public void getOneTest(){
logger.info(userService.getOne(1).toString());
}
}