创建一个新库叫做user_db
/**
* 用户实体表
*/
@Data
public class TUser {
private Long userId;
private String ustatus;
private String username;
}
@Repository
public interface UserMapper extends BaseMapper<TUser> {
}
目前遇到的问题,实体表写的User,连接的时候找不到user,明明数据源配置的没问题。
INTO user ( ustatus,
username ) VALUES ( ?,
? )
后来实体类就改了,其实在上面加@Table的注解用别名也行。
#垂直切分
spring.shardingsphere.datasource.u1.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.u1.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.u1.url=jdbc:mysql://192.168.31.200:3306/user_db?serverTimezone=GMT%2B8
spring.shardingsphere.datasource.u1.username=root
spring.shardingsphere.datasource.u1.password=player3.
#t_user 表策略 u1库的表
spring.shardingsphere.sharding.tables.t_user.actual-data-nodes=u$->{1}.t_user
#主键生成策略
# 指定t_user表 主键user_id 生成策略为 SNOWFLAKE
spring.shardingsphere.sharding.tables.t_user.key-generator.column=user_id
spring.shardingsphere.sharding.tables.t_user.key-generator.type=SNOWFLAKE
# 指定分片策略 约定user_id值是直接添加到t_user种
spring.shardingsphere.sharding.tables.t_user.table-strategy.inline.sharding-column=user_id
spring.shardingsphere.sharding.tables.t_user.table-strategy.inline.algorithm-expression=t_user
/**
* 单条插入
*/
@Test
public void addUser() {
TUser TUser = new TUser();
TUser.setUsername("张三2");
TUser.setUstatus("111132");
userMapper.insert(TUser);
}
@Test
public void findUser() {
QueryWrapper<TUser> wrapper = new QueryWrapper<>();
wrapper.eq("user_id",691448769429897217l);
TUser tUser = userMapper.selectOne(wrapper);
System.out.println(tUser);
//TUser(userId=691448769429897217, ustatus=111132, username=张三2)
}