ruoyi框架在yml中配置Sharding分表不生效的原因。并举个在若依框架中正确使用sharding的案例
ruoyi框架在yml中配置Sharding分表不生效的可能原因有以下几点:
-
配置文件格式错误:ShardingSphere的配置文件格式有一定的要求,如果格式不正确,可能会导致配置不生效。需要检查配置文件是否符合ShardingSphere的要求。
-
配置项错误:ShardingSphere的配置项较多,如果配置项填写错误或漏填,也会导致配置不生效。需要仔细检查每个配置项的名称、格式和值。
-
依赖版本不兼容:ShardingSphere的各个组件之间有一定的版本依赖关系,如果版本不兼容,也会导致配置不生效。需要检查各个组件的版本是否兼容。
以下是若依框架中正确使用sharding的案例:
在若依框架中使用ShardingSphere进行分表,需要在数据源配置文件中添加如下配置:
spring:
shardingsphere:
datasource:
names: ds0, ds1
ds0:
# ...
ds1:
# ...
sharding:
tables:
user:
actualDataNodes: ds${0..1}.user_${0..1}
tableStrategy:
standard:
shardingColumn: user_id
shardingAlgorithmName: userShardingAlgorithm
keyGenerateion:
column: user_id
type: SNOWFLAKE
shardingAlgorithms:
userShardingAlgorithm:
type: INLINE
props:
algorithm-expression: user_${user_id % 2}
其中,ds0和ds1分别代表两个数据源,user表根据user_id进行分表,分成四个表:ds0.user_0、ds0.user_1、ds1.user_0、ds1.user_1。
在代码中使用时,可以像使用普通数据源一样使用分表数据源,例如:
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public List<User> listUsers() {
return userMapper.listUsers();
}
// ...
}
在mapper.xml中,可以像普通SQL一样操作分表,例如:
<select id="listUsers" resultType="com.ruoyi.system.domain.User">
SELECT * FROM user WHERE user_id = #{userId}
</select>
``
原文地址: https://www.cveoy.top/t/topic/fhzv 著作权归作者所有。请勿转载和采集!