第一步、引入扩展pom.xml
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-jdbc-core-spring-boot-starter</artifactId>
<version>5.1.1</version>
</dependency>
第二步、配置规则
shardingsphere:
mode:
type: Memory
props:
sql:
show: true
datasource:
names: ims0
ims0:
type: com.zaxxer.hikari.HikariDataSource
driver-class-name: com.mysql.jdbc.Driver
url: ${spring.datasource.url}
username: ${spring.datasource.username}
password: ${spring.datasource.password}
rules:
sharding:
tables:
ims_message:
actual-data-nodes: ims0.lego_ims_message$->{0..1}
table-strategy:
standard:
sharding-column: message_id
sharding-algorithm-name: sess_id_mod
sharding-algorithms:
sess_id_mod:
type: MOD
props:
sharding-count: 2
第三步、自行生成实体类,唯一区别将实体类的@TableName(value =”ims_message”) 改成逻辑表名,其他所有都不变。
package com.kairuidata.ims.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import java.io.Serializable;
import java.util.Date;
import lombok.Data;
/**
* 消息表
* @TableName lego_ims_message
*/
@TableName(value ="ims_message")
@Data
public class ImsMessage implements Serializable {
/**
* 主键id
*/
@TableId(value = "message_id")
private Long messageId;
/**
* 会话id
*/
@TableField(value = "sess_id")
private Integer sessId;
/**
* 会话状态 1私聊 2群聊
*/
@TableField(value = "type")
private Integer type;
/**
* 删除状态 1合法
*/
@TableField(value = "status")
private Integer status;
/**
* 消息文本
*/
@TableField(value = "msg")
private String msg;
/**
* 消息文本
*/
@TableField(value = "extra")
private String extra;
/**
* 创建时间
*/
@TableField(value = "create_date")
private Date createDate;
/**
* 修改时间
*/
@TableField(value = "update_date")
private Date updateDate;
@TableField(exist = false)
private static final long serialVersionUID = 1L;
}