springboot + mybatis-plus + mysql + sharding-jdbc
部分配置:
pom.xml
xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.xsd">
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.2.0.M3
com.shardingjdbc
shardingjdbc
0.0.1-SNAPSHOT
shardingjdbc
shardingjdbc project for Spring Boot
1.8
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-tomcat
org.springframework.boot
spring-boot-starter-undertow
commons-dbcp
commons-dbcp
1.4
com.baomidou
mybatis-plus
2.1.9
com.baomidou
mybatisplus-spring-boot-starter
1.0.5
mysql
mysql-connector-java
5.1.47
io.shardingsphere
sharding-jdbc-spring-boot-starter
3.0.0.M4
org.springframework.boot
spring-boot-maven-plugin
application.yml
server:
# 端口
port: 1818
undertow:
# 编码
uri-encoding: UTF-8
spring:
main:
allow-bean-definition-overriding: true
resources:
# 静态资源文件夹
static-locations: classpath:/
# 序列化
jackson:
date-format: yyyy-MM-dd HH:mm:ss
time-zone: GMT+8
serialization:
#忽略无法转换的对象
fail_on_empty_beans: false
deserialization:
#允许对象忽略json中不存在的属性
fail_on_unknown_properties: false
#设置空如何序列化
defaultPropertyInclusion: ALWAYS
parser:
#允许出现特殊字符和转义符
allow_unquoted_control_chars: true
#允许出现单引号
allow_single_quotes: true
# 分库
sharding:
jdbc:
datasource:
names: shard1,shard2
shard1:
type: mons.dbcp.BasicDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/shard1
username: root
password: root
shard2:
type: mons.dbcp.BasicDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/shard2
username: root
password: root
config:
sharding:
# 默认数据源(未设置分表策略的表将默认用此数据源)
default-datasource-name: shard1
# 默认分库策略
default-database-strategy:
standard:
sharding-column: id
precise-algorithm-class-name: com.shardingjdbc.config.sharding.DataBasePreciseRule
# 分表策略
tables:
# 分库分表
table_one:
actual-data-nodes: shard$->{1..2}.table_one_$->{1..2}
table-strategy:
standard:
sharding-column: id
precise-algorithm-class-name: com.shardingjdbc.config.sharding.DataTablePreciseRule
# mybatis-plus 的分页插件
pagehelper:
helper-dialect: mysql
params: count=countSql
reasonable: false
support-methods-arguments: true
# mybatis-plus
mybatis-plus:
mapper-locations: /mapper/**/**.xml
typeAliasesPackage: com.shardingjdbc.domain
global-config:
id-type: 3
field-strategy: 2
db-column-underline: true
refresh-mapper: true
configuration:
map-underscore-to-camel-case: true
cache-enabled: false
call-setters-on-nulls: true
其他代码详见git仓库: