如何用 Fullscreen API 监听全屏切换状态并调整界面 UI
2026/4/22 22:22:13
你想要在SpringBoot项目中规范、高效地整合Sharding-JDBC,掌握其分库分表、读写分离等核心功能的常用API,并遵循行业最佳实践,这份教程会帮你系统落地。
<dependencies><!-- SpringBoot核心 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- SpringBoot数据访问 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></dependency><!-- Sharding-JDBC核心依赖 --><dependency><groupId>org.apache.shardingsphere</groupId><artifactId>shardingsphere-jdbc-core-spring-boot-starter</artifactId><version>5.4.0</version></dependency><!-- MySQL驱动 --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><scope>runtime</scope><version>8.0.33</version></dependency><!-- 数据源(HikariCP,SpringBoot默认,性能最优) --><dependency><groupId>com.zaxxer</groupId><artifactId>HikariCP</artifactId></dependency><!-- 测试 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies>t_order),物理表是t_order_0、t_order_1创建数据库demo_db,并创建2个订单分表:
CREATEDATABASEIFNOTEXISTSdemo_db;USEdemo_db;-- 订单逻辑表t_order的物理表:t_order_0、t_order_1CREATETABLEt_order_0(order_idBIGINTPRIMARYKEY,user_idINTNOTNULL,order_amountDECIMAL(10,2)NOTNULL,create_timeDATETIMEDEFAULTCURRENT_TIMESTAMP);CREATETABLEt_order_1(order_idBIGINTPRIMARYKEY,user_idINTNOTNULL,order_amountDECIMAL(10,2)NOTNULL,create_timeDATETIMEDEFAULTCURRENT_TIMESTAMP);遵循最佳实践:配置分层清晰、策略明确、注释完整
spring:# Sharding-JDBC核心配置shardingsphere:# 数据源配置datasource:names:ds0# 数据源名称(单库)ds0:type:com.zaxxer.hikari.HikariDataSourcedriver-class-name:com.mysql.cj.jdbc.Driverurl:jdbc:mysql://localhost:3306/demo_db?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghaiusername:root# 替换为你的数据库账号password:root# 替换为你的数据库密码# 分库分表规则rules:sharding:# 分片表配置tables:t_order:# 逻辑表名actual-data-nodes:ds0.t_order_${0..1}# 物理表节点(ds0库下的t_order_0/1)# 分片策略:按order_id取模分表table-strategy:standard:sharding-column:order_id# 分片键sharding-algorithm-name:t_order_inline# 分片算法名称# 分片算法配置sharding-algorithms:t_order_inline:type:INLINE# 行内表达式算法(简单高效,适合取模/范围)props:algorithm-expression:t_order_${order_id % 2}# 算法规则:order_id%2决定表后缀# 属性配置:打印SQL(调试必备,生产可关闭)props:sql-show:trueimportlombok.Data;importjavax.persistence.*;importjava.math.BigDecimal;importjava.time.LocalDateTime;@Data@Entity@Table(name="t_order")// 映射逻辑表名publicclassOrder{@IdprivateLongorderId;// 分片键:订单IDprivateIntegeruserId;// 用户IDprivateBigDecimalorderAmount;// 订单金额privateLocalDateTimecreateTime;// 创建时间}importorg.springframework.data.jpa.repository.JpaRepository;importorg.springframework.stereotype.Repository;@RepositorypublicinterfaceOrderRepositoryextendsJpaRepository<Order,Long>{// 继承JpaRepository,无需手写基础CRUD}importorg.springframework.stereotype.Service;importjavax.annotation.Resource;importjava.util.List;@ServicepublicclassOrderService{@ResourceprivateOrderRepositoryorderRepository;// 新增订单(Sharding-JDBC自动路由到对应分表)publicOrdersaveOrder(Orderorder){returnorderRepository.save(order);}// 查询所有订单(Sharding-JDBC自动聚合所有分表数据)publicList<Order>listAllOrders(){returnorderRepository.findAll();}// 根据订单ID查询(Sharding-JDBC自动路由到对应分表)publicOrdergetOrderById(LongorderId){returnorderRepository.findById(orderId).orElse(null);}}importorg.junit.jupiter.api.Test;importorg.springframework.boot.test.context.SpringBootTest;importjavax.annotation.Resource;importjava.math.BigDecimal;importjava.time.LocalDateTime;@SpringBootTestpublicclassShardingJdbcDemoApplicationTests{@ResourceprivateOrderServiceorderService;@TestpublicvoidtestSaveOrder(){// 测试订单1:orderId=1 → 路由到t_order_1Orderorder1=newOrder();order1.setOrderId(1L);order1.setUserId(1001);order1.setOrderAmount(newBigDecimal("99.99"));order1.setCreateTime(LocalDateTime.now());orderService.saveOrder(order1);// 测试订单2:orderId=2 → 路由到t_order_0Orderorder2=newOrder();order2.setOrderId(2L);order2.setUserId(1002);order2.setOrderAmount(newBigDecimal("199.99"));order2.setCreateTime(LocalDateTime.now());orderService.saveOrder(order2);// 查询验证System.out.println("订单1:"+orderService.getOrderById(1L));System.out.println("订单2:"+orderService.getOrderById(2L));System.out.println("所有订单:"+orderService.listAllOrders());}}INSERT INTO t_order_1 (order_id, user_id, order_amount, create_time) VALUES (1, 1001, 99.99, ...)INSERT INTO t_order_0 (order_id, user_id, order_amount, create_time) VALUES (2, 1002, 199.99, ...)t_order_1有订单1,t_order_0有订单2,查询时自动聚合结果。若需按user_id分库、order_id分表,修改配置如下(核心变化):
spring:shardingsphere:datasource:names:ds0,ds1# 两个数据源(分库)ds0:type:com.zaxxer.hikari.HikariDataSourcedriver-class-name:com.mysql.cj.jdbc.Driverurl:jdbc:mysql://localhost:3306/demo_db_0?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghaiusername:rootpassword:rootds1:type:com.zaxxer.hikari.HikariDataSourcedriver-class-name:com.mysql.cj.jdbc.Driverurl:jdbc:mysql://localhost:3306/demo_db_1?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghaiusername:rootpassword:rootrules:sharding:tables:t_order:actual-data-nodes:ds${0..1}.t_order_${0..1}# 两个库,每个库2个表# 分库策略:按user_id取模database-strategy:standard:sharding-column:user_idsharding-algorithm-name:t_order_db_inline# 分表策略:按order_id取模table-strategy:standard:sharding-column:order_idsharding-algorithm-name:t_order_table_inlinesharding-algorithms:t_order_db_inline:type:INLINEprops:algorithm-expression:ds${user_id % 2}t_order_table_inline:type:INLINEprops:algorithm-expression:t_order_${order_id % 2}props:sql-show:truespring:shardingsphere:datasource:names:master,slave0# 主库+从库master:type:com.zaxxer.hikari.HikariDataSourcedriver-class-name:com.mysql.cj.jdbc.Driverurl:jdbc:mysql://localhost:3306/demo_db?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghaiusername:rootpassword:rootslave0:type:com.zaxxer.hikari.HikariDataSourcedriver-class-name:com.mysql.cj.jdbc.Driverurl:jdbc:mysql://localhost:3307/demo_db?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghaiusername:rootpassword:rootrules:readwrite-splitting:data-sources:demo_ds:# 读写分离数据源名称type:STATIC# 静态读写分离(固定主从)props:write-data-source-name:master# 写库read-data-source-names:slave0# 读库(多个用逗号分隔)load-balancer-name:round_robin# 负载均衡算法(轮询)load-balancers:round_robin:type:ROUND_ROBIN# 轮询算法(简单高效)props:sql-show:true若内置算法不满足需求,可自定义分片算法:
importorg.apache.shardingsphere.sharding.api.sharding.standard.PreciseShardingAlgorithm;importorg.apache.shardingsphere.sharding.api.sharding.standard.PreciseShardingValue;importorg.springframework.stereotype.Component;importjava.util.Collection;// 自定义精确分片算法(按order_id范围分表)@ComponentpublicclassCustomOrderShardingAlgorithmimplementsPreciseShardingAlgorithm<Long>{@OverridepublicStringdoSharding(Collection<String>availableTargetNames,PreciseShardingValue<Long>shardingValue){LongorderId=shardingValue.getValue();// 自定义规则:order_id<1000 → t_order_0,否则→t_order_1if(orderId<1000){return"t_order_0";}else{return"t_order_1";}}}配置中替换算法类型为CUSTOM,指定算法类:
sharding-algorithms:t_order_custom:type:CUSTOMprops:sharding-algorithm-class-name:com.example.demo.algorithm.CustomOrderShardingAlgorithmSharding-JDBC内置分布式ID生成器,配置如下:
spring:shardingsphere:rules:sharding:key-generators:snowflake:# 雪花算法(推荐)type:SNOWFLAKEprops:worker-id:1# 工作节点ID(集群需唯一)tables:t_order:key-generate-strategy:column:order_id# 主键字段key-generator-name:snowflake# 关联雪花算法sql-show:避免日志冗余,影响性能。INLINE算法(简单高效),复杂场景自定义算法。