Sharding-Jdbc(5):Sharding-Jdbc通过配置文件形式配置分表

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/287047.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

LeetCode Hot100 51.N皇后

题目&#xff1a; 按照国际象棋的规则&#xff0c;皇后可以攻击与之处在同一行或同一列或同一斜线上的棋子。 n 皇后问题 研究的是如何将 n 个皇后放置在 nn 的棋盘上&#xff0c;并且使皇后彼此之间不能相互攻击。 给你一个整数 n &#xff0c;返回所有不同的 n 皇后问题 的…

微机原理与接口技术——中断系统

文章目录 一、中断指令概念1、中断类型码2、中断向量3、中断向量表简述接收到中断指令后操作 二、8086中断指令开中断指令&#xff1a;STI关中断指令&#xff1a;CLI软件中断指令&#xff1a;INT n中断返回指令 IRET 三、微机系统中断分类四、CPU响应可屏蔽与非屏蔽中断的条件响…

34 无聊的小明

数组存放每一次运算后的结果&#xff0c;若有重复则满足小明心意。 #include <iostream> using namespace::std; using std::cout; using std::cin; int pfh(int n) {int sum 0;while(n ! 0){int tn%10;sum sumt*t;n n/10;}return sum; }int wlxm(int n) {int js0;i…

【LeetCode刷题-树】--236.二叉树的最近公共祖先

236.二叉树的最近公共祖先 方法&#xff1a;使用哈希表存储父节点 利用哈希表存储所有节点的父节点&#xff0c;然后就可以利用节点的父节点信息从p节点开始不断向上跳&#xff0c;并记录已经访问过的节点&#xff0c;再从q节点开始不断向上跳&#xff0c;如果碰到已经访问过的…

WebGL开发EDA软件

WebGL是一种用于在Web浏览器中进行高性能图形渲染的JavaScript API&#xff0c;通常用于开发与图形、3D模型渲染相关的Web应用。在EDA&#xff08;Electronic Design Automation&#xff09;软件的开发中&#xff0c;涉及到电子设计和电路仿真等方面&#xff0c;WebGL可以用于创…

使用P3口流水点亮8位LED

#include<reg51.h> //包含单片机寄存器的头文件 /**************************************** 函数功能&#xff1a;延时一段时间 *****************************************/ void delay(void) { unsigned char i,j; for(i0;i<250;i) fo…

ansible远程操作主机功能(1)

自动化运维&#xff08;playbook剧本yaml&#xff09; 是基于Python开发的配置管理和应用部署工具。自动化运维中&#xff0c;现在是异军突起。 Ansible能批量配置&#xff0c;部署&#xff0c;管理上千台主机&#xff0c;类似于Xshell的一键输入的工具&#xff0c;不需要每次…

从零开始在Linux服务器配置并运行YOLO8+Web项目

✅作者简介&#xff1a;大家好&#xff0c;我是 Meteors., 向往着更加简洁高效的代码写法与编程方式&#xff0c;持续分享Java技术内容。 &#x1f34e;个人主页&#xff1a;Meteors.的博客 &#x1f49e;当前专栏&#xff1a; 神经网络&#xff08;随缘更新&#xff09; ✨特色…

【PHD申请文书】motivation letter|不限字数|Medical Imaging and Application

本文目录 APPLICATION ESSAYCriticism思考 Ref: https://essayforum.com/letters/tryst-technology-motivation-erasmus-degree-85383/ APPLICATION ESSAY My tryst with technology - motivation letter for Erasmus Degree in Medical Imaging and Application 原文机翻My…

23级新生C语言周赛(6)(郑州轻工业大学)

题目链接:ZZULIOJ 3110: 数(shu)数(shu)问题 分析: 看到这个题第一步想的是 先把每个平方数给求出来 然后枚举 但是时间复杂度大于1e8 交了一下TLE 但后来打表发现,好数太多了要是枚举的话 注定TLE 能不能间接的去做呢? 把不是的减去,那不就是好数了吗? 这个时候又是打表,会…

智能优化算法应用:基于算术优化算法3D无线传感器网络(WSN)覆盖优化 - 附代码

智能优化算法应用&#xff1a;基于算术优化算法3D无线传感器网络(WSN)覆盖优化 - 附代码 文章目录 智能优化算法应用&#xff1a;基于算术优化算法3D无线传感器网络(WSN)覆盖优化 - 附代码1.无线传感网络节点模型2.覆盖数学模型及分析3.算术优化算法4.实验参数设定5.算法结果6.…

【lesson18】MySQL内置函数(1)日期函数和字符串函数

文章目录 日期函数函数使用具体使用案例建表插入数据建表插入数据 字符串函数函数使用具体使用案例建表插入数据测试 日期函数 函数使用 获得年月日&#xff1a; 获得时分秒&#xff1a; 获得时间戳&#xff1a; 获得现在的时间&#xff1a; 在日期的基础上加日期&#xf…