自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Java實(shí)戰(zhàn):教你如何進(jìn)行數(shù)據(jù)庫(kù)分庫(kù)分表

開發(fā) 后端
我們知道,當(dāng)前的應(yīng)用都離不開數(shù)據(jù)庫(kù),隨著數(shù)據(jù)庫(kù)中的數(shù)據(jù)越來越多,單表突破性能上限記錄時(shí),如MySQL單表上線估計(jì)在近千萬(wàn)條內(nèi),當(dāng)記錄數(shù)繼續(xù)增長(zhǎng)時(shí),從性能考慮,則需要進(jìn)行拆分處理。

 

我們知道,當(dāng)前的應(yīng)用都離不開數(shù)據(jù)庫(kù),隨著數(shù)據(jù)庫(kù)中的數(shù)據(jù)越來越多,單表突破性能上限記錄時(shí),如MySQL單表上線估計(jì)在近千萬(wàn)條內(nèi),當(dāng)記錄數(shù)繼續(xù)增長(zhǎng)時(shí),從性能考慮,則需要進(jìn)行拆分處理。而拆分分為橫向拆分和縱向拆分。一般來說,采用橫向拆分較多,這樣的表結(jié)構(gòu)是一致的,只是不同的數(shù)據(jù)存儲(chǔ)在不同的數(shù)據(jù)庫(kù)表中。其中橫向拆分也分為分庫(kù)和分表。

1.示例數(shù)據(jù)庫(kù)準(zhǔn)備

為了說清楚如何用Java語(yǔ)言和相關(guān)框架實(shí)現(xiàn)業(yè)務(wù)表的分庫(kù)和分表處理。這里首先用MySQL數(shù)據(jù)庫(kù)中創(chuàng)建兩個(gè)獨(dú)立的數(shù)據(jù)庫(kù)實(shí)例,名字為mydb和mydb2,此可演示分庫(kù)操作。另外在每個(gè)數(shù)據(jù)庫(kù)實(shí)例中,創(chuàng)建12個(gè)業(yè)務(wù)表,按年月進(jìn)行數(shù)據(jù)拆分。具體的創(chuàng)建表腳本如下:

  1. CREATE TABLE `t_bill_2021_1` ( 
  2.   `order_id` bigint(20) NOT NULL  COMMENT '訂單id'
  3.   `user_id` int(20) NOT NULL COMMENT '用戶id'
  4.   `address_id` bigint(20) NOT NULL COMMENT '地址id'
  5.   `status` char(1) DEFAULT NULL COMMENT '訂單狀態(tài)'
  6.   `create_time` datetime DEFAULT NULL COMMENT '創(chuàng)建時(shí)間'
  7.   PRIMARY KEY (`order_id`) USING BTREE 
  8. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; 
  9.  
  10. CREATE TABLE `t_bill_2021_2` ( 
  11.   `order_id` bigint(20) NOT NULL  COMMENT '訂單id'
  12.   `user_id` int(20) NOT NULL COMMENT '用戶id'
  13.   `address_id` bigint(20) NOT NULL COMMENT '地址id'
  14.   `status` char(1) DEFAULT NULL COMMENT '訂單狀態(tài)'
  15.   `create_time` datetime DEFAULT NULL COMMENT '創(chuàng)建時(shí)間'
  16.   PRIMARY KEY (`order_id`) USING BTREE 
  17. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; 
  18. -- 省略.... 
  19. CREATE TABLE `t_bill_2021_12` ( 
  20.   `order_id` bigint(20) NOT NULL  COMMENT '訂單id'
  21.   `user_id` int(20) NOT NULL COMMENT '用戶id'
  22.   `address_id` bigint(20) NOT NULL COMMENT '地址id'
  23.   `status` char(1) DEFAULT NULL COMMENT '訂單狀態(tài)'
  24.   `create_time` datetime DEFAULT NULL COMMENT '創(chuàng)建時(shí)間'
  25.   PRIMARY KEY (`order_id`) USING BTREE 
  26. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; 

成功執(zhí)行腳本后,在MySQL管理工具中可以看到如下的示例界面:

2.分庫(kù)分表實(shí)現(xiàn)

在Java語(yǔ)言下的框架中,有眾多的開源框架,其中關(guān)于分庫(kù)分表的框架,可以選擇Apache ShardingSphere,其官網(wǎng)介紹說:ShardingSphere 是一套開源的分布式數(shù)據(jù)庫(kù)解決方案組成的生態(tài)圈,它由 JDBC、Proxy 和 Sidecar(規(guī)劃中)這 3 款既能夠獨(dú)立部署,又支持混合部署配合使用的產(chǎn)品組成。 它們均提供標(biāo)準(zhǔn)化的數(shù)據(jù)水平擴(kuò)展、分布式事務(wù)和分布式治理等功能,可適用于如 Java 同構(gòu)、異構(gòu)語(yǔ)言、云原生等各種多樣化的應(yīng)用場(chǎng)景。Apache ShardingSphere 5.x 版本開始致力于可插拔架構(gòu)。 目前,數(shù)據(jù)分片、讀寫分離、數(shù)據(jù)加密、影子庫(kù)壓測(cè)等功能,以及 MySQL、PostgreSQL、SQLServer、Oracle 等 SQL 與協(xié)議的支持,均通過插件的方式織入項(xiàng)目。官網(wǎng)地址為: https://shardingsphere.apache.org/index_zh.html 。

下面的示例采用Spring Boot框架來實(shí)現(xiàn),相關(guān)的庫(kù)通過Maven進(jìn)行管理。首先給出pom.xml配置文件的定義: 

  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  3.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"
  4.     <modelVersion>4.0.0</modelVersion> 
  5.     <parent> 
  6.         <groupId>org.springframework.boot</groupId> 
  7.         <artifactId>spring-boot-starter-parent</artifactId> 
  8.         <version>2.5.3</version> 
  9.         <relativePath/> <!-- lookup parent from repository --> 
  10.     </parent> 
  11.     <groupId>com.example</groupId> 
  12.     <artifactId>wyd</artifactId> 
  13.     <version>0.0.1-SNAPSHOT</version> 
  14.     <name>wyd</name> 
  15.     <description>Demo project for Spring Boot</description> 
  16.     <properties> 
  17.         <java.version>1.8</java.version> 
  18.         <mybatis-plus.version>3.1.1</mybatis-plus.version> 
  19.         <sharding-sphere.version>4.0.0-RC2</sharding-sphere.version> 
  20.         <shardingsphere.version>5.0.0-beta</shardingsphere.version> 
  21.     </properties> 
  22.     <dependencies> 
  23.         <dependency> 
  24.             <groupId>org.springframework.boot</groupId> 
  25.             <artifactId>spring-boot-starter-web</artifactId> 
  26.         </dependency> 
  27.         <dependency> 
  28.             <groupId>org.mybatis.spring.boot</groupId> 
  29.             <artifactId>mybatis-spring-boot-starter</artifactId> 
  30.             <version>2.0.1</version> 
  31.         </dependency> 
  32.         <dependency> 
  33.             <groupId>com.baomidou</groupId> 
  34.             <artifactId>mybatis-plus-boot-starter</artifactId> 
  35.             <version>${mybatis-plus.version}</version> 
  36.         </dependency> 
  37.         <dependency> 
  38.             <groupId>org.projectlombok</groupId> 
  39.             <artifactId>lombok</artifactId> 
  40.             <optional>true</optional> 
  41.         </dependency> 
  42.         <dependency> 
  43.             <groupId>joda-time</groupId> 
  44.             <artifactId>joda-time</artifactId> 
  45.             <version>2.9.8</version> 
  46.         </dependency> 
  47.         <dependency> 
  48.             <groupId>org.apache.shardingsphere</groupId> 
  49.             <artifactId>sharding-jdbc-spring-boot-starter</artifactId> 
  50.             <version>${sharding-sphere.version}</version> 
  51.         </dependency> 
  52.         <dependency> 
  53.             <groupId>org.apache.shardingsphere</groupId> 
  54.             <artifactId>sharding-jdbc-spring-namespace</artifactId> 
  55.             <version>${sharding-sphere.version}</version> 
  56.         </dependency> 
  57.         <dependency> 
  58.             <groupId>mysql</groupId> 
  59.             <artifactId>mysql-connector-java</artifactId> 
  60.             <scope>runtime</scope> 
  61.         </dependency> 
  62.         <dependency> 
  63.             <groupId>org.postgresql</groupId> 
  64.             <artifactId>postgresql</artifactId> 
  65.             <scope>runtime</scope> 
  66.         </dependency> 
  67.         <dependency> 
  68.             <groupId>org.springframework.boot</groupId> 
  69.             <artifactId>spring-boot-starter-test</artifactId> 
  70.             <scope>test</scope> 
  71.         </dependency> 
  72.     </dependencies> 
  73.     <build> 
  74.         <plugins> 
  75.             <plugin> 
  76.                 <groupId>org.springframework.boot</groupId> 
  77.                 <artifactId>spring-boot-maven-plugin</artifactId> 
  78.             </plugin> 
  79.         </plugins> 
  80.     </build> 
  81. </project> 

其次,給出一個(gè)實(shí)體類,它對(duì)應(yīng)于上述創(chuàng)建的數(shù)據(jù)庫(kù)表t_bill,其定義如下:

  1. package com.example.wyd.dao; 
  2. import com.baomidou.mybatisplus.annotation.TableName; 
  3. import lombok.Data; 
  4. import java.util.Date; 
  5. @Data 
  6. @TableName("t_bill"
  7. public class Bill { 
  8.     private Long orderId; 
  9.     private Integer userId; 
  10.     private Long addressId; 
  11.     private String status; 
  12.     private Date createTime; 
  13.     public void setOrderId(Long orderId) { 
  14.         this.orderId = orderId; 
  15.     } 
  16.     public void setUserId(Integer userId) { 
  17.         this.userId = userId; 
  18.     } 
  19.     public void setAddressId(Long addressId) { 
  20.         this.addressId = addressId; 
  21.     } 
  22.     public void setStatus(String status) { 
  23.         this.status = status; 
  24.     } 
  25.     public void setCreateTime(Date createTime) { 
  26.         this.createTime = createTime; 
  27.     } 
  28. 映射類BillMapper定義如下: 
  29.  
  30. package com.example.wyd.mapper; 
  31. import com.baomidou.mybatisplus.core.mapper.BaseMapper; 
  32. import com.example.wyd.dao.Bill; 
  33. public interface BillMapper extends BaseMapper<Bill> { 
  34.  

服務(wù)類接口定義如下:

  1. package com.example.wyd.service; 
  2. import com.baomidou.mybatisplus.extension.service.IService; 
  3. import com.example.wyd.dao.Bill; 
  4. public interface BillService extends IService<Bill> { 
  5.  

服務(wù)類接口的實(shí)現(xiàn)類定義如下:

  1. package com.example.wyd.service; 
  2. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; 
  3. import com.example.wyd.dao.Bill; 
  4. import com.example.wyd.mapper.BillMapper; 
  5. import org.springframework.stereotype.Service; 
  6. @Service 
  7. public class BillServiceImpl extends ServiceImpl<BillMapper, Bill> implements BillService { 
  8.  

這里我們采用了MybatisPlus框架,它可以很方便的進(jìn)行數(shù)據(jù)庫(kù)相關(guān)操作,而無(wú)需過多寫SQL來實(shí)現(xiàn)具體業(yè)務(wù)邏輯。通過上述定義,通過繼承接口的方式,并提供實(shí)體類的定義,MybatisPlus框架會(huì)通過反射機(jī)制來根據(jù)數(shù)據(jù)庫(kù)設(shè)置來生成SQL語(yǔ)句,其中包含增刪改查接口,具體的實(shí)現(xiàn)我們并未具體定義。

下面定義一個(gè)自定義的分庫(kù)算法,具體實(shí)現(xiàn)如下:

  1. package com.example.wyd; 
  2. import org.apache.shardingsphere.api.sharding.standard.PreciseShardingAlgorithm; 
  3. import org.apache.shardingsphere.api.sharding.standard.PreciseShardingValue; 
  4. import java.util.Collection; 
  5. //自定義數(shù)據(jù)庫(kù)分片算法 
  6. public class DBShardingAlgorithm implements PreciseShardingAlgorithm<Long> { 
  7.     @Override 
  8.     public String doSharding(Collection<String> availableTargetNames, PreciseShardingValue<Long> shardingValue) { 
  9.         //真實(shí)數(shù)據(jù)庫(kù)節(jié)點(diǎn) 
  10.         availableTargetNames.stream().forEach((item) -> { 
  11.            System.out.println("actual db:" + item); 
  12.         }); 
  13.         //邏輯表以及分片的字段名 
  14.         System.out.println("logicTable:"+shardingValue.getLogicTableName()+";shardingColumn:"+ shardingValue.getColumnName()); 
  15.         //分片數(shù)據(jù)字段值 
  16.         System.out.println("shardingColumn value:"+ shardingValue.getValue().toString()); 
  17.         //獲取字段值 
  18.         long orderId = shardingValue.getValue(); 
  19.         //分片索引計(jì)算 0 , 1 
  20.         long db_index = orderId & (2 - 1); 
  21.         for (String each : availableTargetNames) { 
  22.             if (each.equals("ds"+db_index)) { 
  23.                 //匹配的話,返回?cái)?shù)據(jù)庫(kù)名 
  24.                 return each; 
  25.             } 
  26.         } 
  27.         throw new IllegalArgumentException(); 
  28.     } 

下面給出數(shù)據(jù)的分表邏輯,這個(gè)定義稍顯復(fù)雜一點(diǎn),就是根據(jù)業(yè)務(wù)數(shù)據(jù)的日期字段值,根據(jù)月份落入對(duì)應(yīng)的物理數(shù)據(jù)表中。實(shí)現(xiàn)示例代碼如下: 

  1. package com.example.wyd; 
  2. import org.apache.shardingsphere.api.sharding.standard.PreciseShardingAlgorithm; 
  3. import org.apache.shardingsphere.api.sharding.standard.PreciseShardingValue; 
  4. import java.util.Collection; 
  5. import java.util.Date; 
  6. //表按日期自定義分片 
  7. public class TableShardingAlgorithm implements PreciseShardingAlgorithm<Date> { 
  8.     @Override 
  9.     public String doSharding(Collection<String> availableTargetNames, PreciseShardingValue<Date> shardingValue) { 
  10.         //真實(shí)數(shù)據(jù)庫(kù)節(jié)點(diǎn) 
  11.         availableTargetNames.stream().forEach((item) -> { 
  12.             System.out.println("actual db:" + item); 
  13.         }); 
  14.         //邏輯表以及分片的字段名 
  15.         System.out.println("logicTable:"+shardingValue.getLogicTableName()+";shardingColumn:"+ shardingValue.getColumnName()); 
  16.         //分片數(shù)據(jù)字段值 
  17.         System.out.println("shardingColumn value:"+ shardingValue.getValue().toString()); 
  18.         //獲取表名前綴 
  19.         String tb_name = shardingValue.getLogicTableName() + "_"
  20.         //根據(jù)日期分表 
  21.         Date date = shardingValue.getValue(); 
  22.         String year = String.format("%tY", date); 
  23.         String mon =String.valueOf(Integer.parseInt(String.format("%tm", date))); 
  24.         //String dat = String.format("%td", date); //也可以安裝年月日來分表 
  25.         // 選擇表 
  26.         tb_name = tb_name + year + "_" + mon; 
  27.         //實(shí)際的表名 
  28.         System.out.println("tb_name:" + tb_name); 
  29.         for (String each : availableTargetNames) { 
  30.             //System.out.println("availableTableName:" + each); 
  31.             if (each.equals(tb_name)) { 
  32.                 //返回物理表名 
  33.                 return each; 
  34.             } 
  35.         } 
  36.         throw new IllegalArgumentException(); 
  37.     } 

數(shù)據(jù)的分庫(kù)分表可以在Spring Boot的屬性配置文件中進(jìn)行設(shè)( application.properties ): 

  1. server.port=8080 
  2. ######################################################################################################### 
  3. # 配置ds0 和ds1兩個(gè)數(shù)據(jù)源 
  4. spring.shardingsphere.datasource.names = ds0,ds1 
  5.  
  6. #ds0 配置 
  7. spring.shardingsphere.datasource.ds0.type = com.zaxxer.hikari.HikariDataSource 
  8. spring.shardingsphere.datasource.ds0.driver-class-name = com.mysql.cj.jdbc.Driver 
  9. spring.shardingsphere.datasource.ds0.jdbc-url = jdbc:mysql://127.0.0.1:3306/mydb?characterEncoding=utf8 
  10. spring.shardingsphere.datasource.ds0.username = uname 
  11. spring.shardingsphere.datasource.ds0.password = pwd 
  12.  
  13. #ds1 配置 
  14. spring.shardingsphere.datasource.ds1.type = com.zaxxer.hikari.HikariDataSource 
  15. spring.shardingsphere.datasource.ds1.driver-class-name = com.mysql.cj.jdbc.Driver 
  16. spring.shardingsphere.datasource.ds1.jdbc-url = jdbc:mysql://127.0.0.1:3306/mydb2characterEncoding=utf8 
  17. spring.shardingsphere.datasource.ds1.username = uname 
  18. spring.shardingsphere.datasource.ds1.password = pwd 
  19. ######################################################################################################### 
  20. # 默認(rèn)的分庫(kù)策略:id取模 
  21. spring.shardingsphere.sharding.default-database-strategy.inline.sharding-column = id 
  22. spring.shardingsphere.sharding.default-database-strategy.inline.algorithm-expression = ds$->{id % 2
  23. ######################################################################################################### 
  24. spring.shardingsphere.sharding.tables.t_bill.actual-data-nodes=ds$->{0..1}.t_bill_$->{2021..2021}_$->{1..12
  25. #數(shù)據(jù)庫(kù)分片字段 
  26. spring.shardingsphere.sharding.tables.t_bill.database-strategy.standard.sharding-column=order_id 
  27. #自定義數(shù)據(jù)庫(kù)分片策略 
  28. spring.shardingsphere.sharding.tables.t_bill.database-strategy.standard.precise-algorithm-class-name=com.example.wyd.DBShardingAlgorithm 
  29. #表分片字段 
  30. spring.shardingsphere.sharding.tables.t_bill.table-strategy.standard.sharding-column=create_time 
  31. #自定義表分片策略 
  32. spring.shardingsphere.sharding.tables.t_bill.table-strategy.standard.precise-algorithm-class-name=com.example.wyd.TableShardingAlgorithm 
  33. ######################################################################################################### 
  34. # 使用SNOWFLAKE算法生成主鍵 
  35. spring.shardingsphere.sharding.tables.t_bill.key-generator.column = order_id 
  36. spring.shardingsphere.sharding.tables.t_bill.key-generator.type = SNOWFLAKE 
  37. spring.shardingsphere.sharding.tables.t_bill.key-generator.props.worker.id=123 
  38. ######################################################################################################### 
  39. spring.shardingsphere.props.sql.show = true 

最后,我們給出一個(gè)定義的Controller類型,來測(cè)試分庫(kù)分表的查詢和保存操作是否正確。HomeController類定義如下:

  1. package com.example.wyd.controller; 
  2. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; 
  3. import com.example.wyd.dao.Bill; 
  4. import com.example.wyd.service.BillService; 
  5. import org.joda.time.DateTime; 
  6. import org.springframework.beans.factory.annotation.Autowired; 
  7. import org.springframework.web.bind.annotation.RequestMapping; 
  8. import org.springframework.web.bind.annotation.RequestParam; 
  9. import org.springframework.web.bind.annotation.RestController; 
  10. import java.text.ParseException; 
  11. import java.text.SimpleDateFormat; 
  12. import java.util.Date; 
  13. import java.util.List; 
  14. @RestController 
  15. @RequestMapping("/api"
  16. public class HomeController { 
  17.     @Autowired 
  18.     private BillService billService; 
  19.     //http://localhost:8080/api/query?start=2021-02-07%2000:00:00&end=2021-03-07%2000:00:00 
  20.     @RequestMapping("/query"
  21.     public List<Bill> queryList(@RequestParam("start") String start, @RequestParam("end") String end) { 
  22.         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
  23.         try { 
  24.             Date date = sdf.parse(start); 
  25.             Date date2 = sdf.parse(end); 
  26.             QueryWrapper<Bill> queryWrapper = new QueryWrapper<>(); 
  27.             queryWrapper.ge("create_time",date) 
  28.                     .and(qw-> qw.le("create_time", date2)).last("limit 1,10"); 
  29.             List<Bill> billIPage = billService.list(queryWrapper); 
  30.             System.out.println(billIPage.size()); 
  31.             billIPage.forEach(System.out::println); 
  32.             return billIPage; 
  33.         } catch (ParseException e) { 
  34.             e.printStackTrace(); 
  35.         } 
  36.         return null
  37.     } 
  38.     //http://localhost:8080/api/save?userid=999&addressId=999&status=M&date=2021-03-07%2000:00:00 
  39.     @RequestMapping("/save"
  40.     public String Save(@RequestParam("userid"int userId, @RequestParam("addressId"long AddressId, 
  41.                        @RequestParam("status") String status 
  42.             ,@RequestParam("date") String strDate) { 
  43.         String ret ="0"
  44.         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
  45.         try { 
  46.             Date date = sdf.parse(strDate); 
  47.             Bill bill = new Bill(); 
  48.             bill.setUserId(userId); 
  49.             bill.setAddressId(AddressId); 
  50.             bill.setStatus(status); 
  51.             bill.setCreateTime(date); 
  52.             boolean isOk = billService.save(bill); 
  53.             if (isOk){ 
  54.                 ret ="1"
  55.             } 
  56.         } catch (ParseException e) { 
  57.             e.printStackTrace(); 
  58.         } 
  59.         return ret; 
  60.     } 

至此,我們可以用測(cè)試類初始化一些數(shù)據(jù),并做一些初步的數(shù)據(jù)操作測(cè)試:

  1. package com.example.wyd; 
  2.  
  3. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; 
  4. import com.example.wyd.dao.Bill; 
  5. import com.example.wyd.dao.Order; 
  6. import com.example.wyd.service.BillService; 
  7. import com.example.wyd.service.OrderService; 
  8. import org.joda.time.DateTime; 
  9. import org.junit.jupiter.api.Test; 
  10. import org.springframework.beans.factory.annotation.Autowired; 
  11.  
  12. import java.text.ParseException; 
  13. import java.text.SimpleDateFormat; 
  14. import java.util.*; 
  15.  
  16. public class OrderServiceImplTest extends WydApplicationTests { 
  17.     @Autowired 
  18.     private BillService billService; 
  19.     @Test 
  20.     public void testBillSave(){ 
  21.         for (int i = 0 ; i< 120 ; i++){ 
  22.             Bill bill = new Bill(); 
  23.             bill.setUserId(i); 
  24.             bill.setAddressId((long)i); 
  25.             bill.setStatus("K"); 
  26.             bill.setCreateTime((new Date(new DateTime(2021,(i % 11)+1,7,0000,00,000).getMillis()))); 
  27.             billService.save(bill); 
  28.         } 
  29.     } 
  30.     @Test 
  31.     public void testGetByOrderId(){ 
  32.         long id = 626038622575374337L; //根據(jù)數(shù)據(jù)修改,無(wú)數(shù)據(jù)會(huì)報(bào)錯(cuò) 
  33.         QueryWrapper<Bill> queryWrapper = new QueryWrapper<>(); 
  34.         queryWrapper.eq("order_id", id); 
  35.         Bill bill = billService.getOne(queryWrapper); 
  36.         System.out.println(bill.toString()); 
  37.     } 
  38.  
  39.     @Test 
  40.     public void testGetByDate(){ 
  41.         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
  42.         try { 
  43.             Date date = sdf.parse("2021-02-07 00:00:00"); 
  44.             QueryWrapper<Bill> queryWrapper = new QueryWrapper<>(); 
  45.             queryWrapper.eq("create_time",date); 
  46.             List<Bill> billIPage = billService.list(queryWrapper); 
  47.             System.out.println(billIPage.size()); 
  48.             System.out.println(billIPage.toString()); 
  49.         } catch (ParseException e) { 
  50.             e.printStackTrace(); 
  51.         } 
  52.  
  53.     } 
  54.  
  55.     @Test 
  56.     public void testGetByDate2(){ 
  57.         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
  58.         try { 
  59.             Date date = sdf.parse("2021-02-07 00:00:00"); 
  60.             Date date2 = sdf.parse("2021-03-07 00:00:00"); 
  61.             QueryWrapper<Bill> queryWrapper = new QueryWrapper<>(); 
  62.             queryWrapper.ge("create_time",date) 
  63.             .and(qw-> qw.le("create_time", date2)); 
  64.             List<Bill> billIPage = billService.list(queryWrapper); 
  65.             System.out.println(billIPage.size()); 
  66.             billIPage.forEach(System.out::println); 
  67.  
  68.         } catch (ParseException e) { 
  69.             e.printStackTrace(); 
  70.         } 
  71.  
  72.     } 

執(zhí)行上述測(cè)試,通過后會(huì)生成測(cè)試數(shù)據(jù)。

3.驗(yàn)證

打開瀏覽器,輸入網(wǎng)址進(jìn)行查詢測(cè)試:http://localhost:8080/api/query?start=2021-02-07%2000:00:00&end=2021-03-07%2000:00:00

輸入如下網(wǎng)址進(jìn)行數(shù)據(jù)新增測(cè)試:http://localhost:8080/api/save?userid=999&addressId=999&status=M&date=2021-03-07%2000:00:00

通過跟蹤分析,此數(shù)據(jù)落入如下的表中,SQL語(yǔ)句如下:

  1. SELECT * FROM mydb2.t_bill_2021_3 LIMIT 01000 

這里還需要注意, ShardingSphere 還支持分布式事務(wù) ,感興趣的可以閱讀官網(wǎng)相關(guān)資料進(jìn)行學(xué)習(xí)。

責(zé)任編輯:張燕妮 來源: 博客園
相關(guān)推薦

2011-05-25 00:00:00

數(shù)據(jù)庫(kù)設(shè)計(jì)

2021-03-17 16:15:55

數(shù)據(jù)MySQL 架構(gòu)

2010-02-02 10:04:58

2024-08-02 15:47:28

數(shù)據(jù)庫(kù)分庫(kù)分表

2019-01-16 14:00:54

數(shù)據(jù)庫(kù)分庫(kù)分表

2019-03-06 14:42:01

數(shù)據(jù)庫(kù)分庫(kù)分表

2021-04-01 05:40:53

分庫(kù)分表數(shù)據(jù)庫(kù)MySQL

2022-06-15 07:32:24

數(shù)據(jù)庫(kù)分庫(kù)分表

2018-06-01 14:00:00

數(shù)據(jù)庫(kù)MySQL分庫(kù)分表

2022-12-05 07:51:24

數(shù)據(jù)庫(kù)分庫(kù)分表讀寫分離

2009-02-02 13:43:19

故障檢測(cè)數(shù)據(jù)庫(kù)

2010-02-04 17:42:15

Android數(shù)據(jù)庫(kù)

2010-03-17 18:21:54

Java多線程靜態(tài)數(shù)據(jù)

2010-05-24 14:57:03

MySQL數(shù)據(jù)庫(kù)表

2024-12-04 13:02:34

數(shù)據(jù)庫(kù)分庫(kù)分表

2019-01-29 15:25:11

阿里巴巴數(shù)據(jù)庫(kù)分庫(kù)分表

2009-07-15 18:01:53

Jython數(shù)據(jù)庫(kù)

2020-01-07 09:40:25

數(shù)據(jù)庫(kù)MySQLRedis

2020-09-07 12:59:10

NoSQL數(shù)據(jù)庫(kù)數(shù)據(jù)

2018-09-17 16:12:03

數(shù)據(jù)庫(kù)數(shù)據(jù)恢復(fù)SQL Server
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)