1 项目目录
2 配置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 编写配置文件
spring:jpa:show-sql: truehibernate:ddl-auto: nonedatabase-platform: org.hibernate.dialect.MySQL5InnoDBDialect
sharding:jdbc:####ds1datasource:names: ds1ds1:password: 123456type: com.alibaba.druid.pool.DruidDataSourcedriver-class-name: com.mysql.jdbc.Driverurl: jdbc:mysql://192.168.222.156:3306/ds_1username: rootconfig:sharding:tables:t_order:table-strategy:inline:#### 根据userid 进行分片sharding-column: user_idalgorithm-expression: ds_1.t_order_$->{user_id % 2}actual-data-nodes: ds1.t_order_$->{0..1}props:sql:### 开启分片日志show: false
4 编写实体类
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 {@Idprivate 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 编写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 编写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 {@Autowiredprivate 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 编写启动类
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 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 启动项目
访问端口
http://localhost:8080/inserOrder
查看数据库已分库插入
http://localhost:8080/getOrderAll