项目需求场景:
1.基于mybatis-plus集成sharding-jdbc只分表不分库
2.解决sharding-jdbc 出现 java.sql.SQLFeatureNotSupportedException: getObject with type异常
pom.xml配置
<!--mybatis-plus 依赖-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-extension</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-typehandlers-jsr310</artifactId>
<version>1.0.2</version>
</dependency>
<!--sharding jdbc springboot-->
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
<version>4.0.0-RC1</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.21</version>
</dependency>
yml配置
spring:
main:
allow-bean-definition-overriding: true
shardingsphere:
datasource:
names: test
copyright:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://192.168.67.10:test?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8
username: test
password: test
initialization-mode: always
sharding:
# 默认数据库策略 如果不想分库,可不设此配置
defaultDatabaseStrategy:
# 分库
standard:
# 分片键 zone 表示根据 数据zone这一列的值与自定义的算法进行分片
shardingColumn: zone
# 精准分片算法
preciseAlgorithmClassName: com.leilei.algorithm.SubDataBasePreciseAlgorithm
# 范围分片算法
rangeAlgorithmClassName: com.leilei.algorithm.SubDataBaseRangeAlgorithm
#分表
tables:
user_message:
# 分表节点
actualDataNodes: test.user_message_$->{0..7}
tableStrategy:
inline:
# 根据哪列分表
shardingColumn: uid
# 分表算法
algorithmExpression: user_message_$->{uid % 8}
# 分表后,sharding-jdbc的全局id生成策略
keyGenerator:
type: SNOWFLAKE
# 对id列采用 sharding-jdbc的全局id生成策略
column: id
# 问题描述:
<font color=#999AAA >提示:这里描述项目中遇到的问题:
例如:数据传输过程中数据不时出现丢失的情况,偶尔会丢失一部分数据
APP 中接收数据代码:
```c
@Override
public void run() {
bytes = mmInStream.read(buffer);
mHandler.obtainMessage(READ_DATA, bytes, -1, buffer).sendToTarget();
}
sharding-jdbc 出现 java.sql.SQLFeatureNotSupportedException: getObject with type的原因
mp3.1.1+使用了新版jdbc,LocalDateTime等新日期类型处理方式升级,但druid在1.1.21版本之前不支持解决方案
一、官网提供的解决方案:- 升级druid到1.1.21解决这个问题;
- 保持mp版本3.1.0;
3.紧跟mp版本,换掉druid数据源
由于项目中很多东西用了mybatis-plus3.4.1的东西 这里降低版本不太现实
二、改写LocalDateTimeTypeHandler
1.自定义两个类进行改写
参考这位老哥的改写: https://blog.csdn.net/weixin_43356458/article/details/108101896
但是改写之后 不知道为啥还是走mybatis源码的类(求大佬指点)
2.最后我的解决方案是在项目下创建LocalDateTimeTypeHandler 包名必须定义和源码包名相同