1 project directory
2 Configure maven
<?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>
<groupId>org.example</groupId>
<artifactId>sharding-jdbc-configure-test</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath />
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>io.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
<version>3.0.0.M3</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.9</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
</project>
3. Write configuration file
spring:
jpa:
show-sql: true
hibernate:
ddl-auto: none
database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
sharding:
jdbc:
####ds1
datasource:
names: ds1
ds1:
password: 123456
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://192.168.222.156:3306/ds_1
username: root
config:
sharding:
tables:
t_order:
table-strategy:
inline:
#### 根据userid 进行分片
sharding-column: user_id
algorithm-expression: ds_1.t_order_$->{user_id % 2}
actual-data-nodes: ds1.t_order_$->{0..1}
props:
sql:
### 开启分片日志
show: false
4. Write entity classes
package com.example.demo.entity;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "t_order")
public class OrderEntity {
@Id
private Long orderId;
private Long userId;
public Long getOrderId() {
return orderId;
}
public void setOrderId(Long orderId) {
this.orderId = orderId;
}
public Long getUserId() {
return userId;
}
public void setUserId(Long userId) {
this.userId = userId;
}
}
5. Write Repository
package com.example.demo.repository;
import com.example.demo.entity.OrderEntity;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
import java.util.List;
public interface OrderRepository extends CrudRepository<OrderEntity, Long> {
@Query(value = "SELECT order_id ,user_id FROM t_order where order_id in (?1);", nativeQuery = true)
public List<OrderEntity> findExpiredOrderState(List<String> bpIds);
@Query(value = "SELECT order_id ,user_id FROM t_order where user_id=:userId ", nativeQuery = true)
public List<OrderEntity> findByUserId(@Param("userId") Long userId);
}
6. Write controller
package com.example.demo.controller;
import com.example.demo.entity.OrderEntity;
import com.example.demo.repository.OrderRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;
@RestController
public class OrderController {
@Autowired
private OrderRepository orderRepository;
// 查询所有的订单信息
@RequestMapping("/getOrderAll")
public List<OrderEntity> getOrderAll() {
return (List<OrderEntity>) orderRepository.findAll();
}
// 使用in条件查询
@RequestMapping("/inOrder")
public List<OrderEntity> inOrder() {
List<String> ids = new ArrayList<>();
ids.add("2");
ids.add("3");
ids.add("4");
ids.add("5");
return orderRepository.findExpiredOrderState(ids);
}
// 增加
@RequestMapping("/inserOrder")
public String inserOrder(OrderEntity orderEntity) {
for (int i = 0; i < 100; i++) {
OrderEntity order = new OrderEntity();
order.setOrderId((long) i);
order.setUserId((long) i);
orderRepository.save(order);
}
return "success";
}
}
7. Write startup class
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
@SpringBootApplication
@EnableJpaRepositories(basePackages = "com.example.demo.repository")
public class AppSharding {
public static void main(String[] args) {
SpringApplication.run(AppSharding.class, args);
}
}
8 Create database tables
CREATE TABLE `t_order_0` (
`order_id` bigint(20) NOT NULL,
`user_id` bigint(20) NOT NULL,
PRIMARY KEY (`order_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
CREATE TABLE `t_order_1` (
`order_id` bigint(20) NOT NULL,
`user_id` bigint(20) NOT NULL,
PRIMARY KEY (`order_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
9 Start the project
access port
http://localhost:8080/inserOrder
Check that the database has been inserted into sub-databases