feat: 分库分表

This commit is contained in:
胥员员
2022-11-05 21:39:29 +08:00
parent 35f88a0937
commit e0d76a3bf9
13 changed files with 131 additions and 688 deletions

View File

@@ -1,34 +1,48 @@
spring:
application:
name: springboot-init
# DataSource Config
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/my_db
username: root
password: 123456
mvc:
pathmatch:
matching-strategy: ANT_PATH_MATCHER
# session 失效时间(分钟)
session:
timeout: 86400
store-type: redis
# redis 配置
redis:
port: 6379
host: localhost
database: 0
# ShardingSphere 配置项
shardingsphere:
datasource:
# 所有数据源的名字
names: db0,db1
# 订单 orders 数据源配置 00
# 订单 orders 数据源配置 01
db0:
type: com.zaxxer.hikari.HikariDataSource # 使用 Hikari 数据库连接池
driver-class-name: com.mysql.jdbc.Driver
jdbc-url: jdbc:mysql://120.26.194.135:3306/db0?useSSL=false&useUnicode=true&characterEncoding=UTF-8
username: db0
password: xyy
db1:
type: com.zaxxer.hikari.HikariDataSource # 使用 Hikari 数据库连接池
driver-class-name: com.mysql.jdbc.Driver
jdbc-url: jdbc:mysql://120.26.194.135:3306/db1?useSSL=false&useUnicode=true&characterEncoding=UTF-8
username: db1
password: xyy
# 分片规则
sharding:
tables:
# orders 表配置
orders:
actualDataNodes: db${0..1}.orders_${0..7} # 映射到 ds-orders-0 和 ds-orders-1 数据源的 orders 表们
databaseStrategy:
inline:
sharding-column: user_id
algorithm-expression: db${user_id % 2}
tableStrategy:
inline:
sharding-column: user_id
algorithm-expression: orders_${user_id % 8}
# 拓展属性配置
props:
sql:
show: true # 打印 SQL
server:
port: 7529
servlet:
context-path: /api
mybatis-plus:
configuration:
map-underscore-to-camel-case: false
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
global-config:
db-config:
logic-delete-field: isDelete # 全局逻辑删除的实体字段名(since 3.3.0,配置后可以忽略不配置步骤2)
logic-delete-value: 1 # 逻辑已删除值(默认为 1)
logic-not-delete-value: 0 # 逻辑未删除值(默认为 0)
mybatis:
mapper-locations: classpath*:src/main/resources/mapper/*.xml
type-aliases-package: com.yupi.project.model.entity

View File

@@ -1 +0,0 @@
我的项目 by 程序员鱼皮 https://github.com/liyupi

View File

@@ -1,30 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.yupi.project.mapper.UserMapper">
<resultMap id="BaseResultMap" type="com.yupi.project.model.entity.User">
<id property="id" column="id" jdbcType="BIGINT"/>
<result property="username" column="username" jdbcType="VARCHAR"/>
<result property="userAccount" column="userAccount" jdbcType="VARCHAR"/>
<result property="avatarUrl" column="avatarUrl" jdbcType="VARCHAR"/>
<result property="gender" column="gender" jdbcType="TINYINT"/>
<result property="userPassword" column="userPassword" jdbcType="VARCHAR"/>
<result property="phone" column="phone" jdbcType="VARCHAR"/>
<result property="email" column="email" jdbcType="VARCHAR"/>
<result property="userStatus" column="userStatus" jdbcType="INTEGER"/>
<result property="createTime" column="createTime" jdbcType="TIMESTAMP"/>
<result property="updateTime" column="updateTime" jdbcType="TIMESTAMP"/>
<result property="isDelete" column="isDelete" jdbcType="TINYINT"/>
<result property="userRole" column="userRole" jdbcType="INTEGER"/>
</resultMap>
<sql id="Base_Column_List">
id,username,userAccount,
avatarUrl,gender,userPassword,
phone,email,userStatus,
createTime,updateTime,isDelete,
userRole
</sql>
</mapper>