Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
后端镜像
java镜像
Spring Boot Sharding Sphere
提交
61319ccd
S
Spring Boot Sharding Sphere
项目概览
后端镜像
/
java镜像
/
Spring Boot Sharding Sphere
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
Spring Boot Sharding Sphere
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
61319ccd
编写于
10月 15, 2019
作者:
X
xubo
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
分库分表
上级
4a306535
变更
13
隐藏空白更改
内联
并排
Showing
13 changed file
with
322 addition
and
10 deletion
+322
-10
db-read-write/src/main/resources/application.properties
db-read-write/src/main/resources/application.properties
+2
-2
sub-db-table/src/main/resources/application.properties
sub-db-table/src/main/resources/application.properties
+6
-5
sub-db-table/src/main/resources/mapper/UserMapper.xml
sub-db-table/src/main/resources/mapper/UserMapper.xml
+1
-1
sub-table-read-write/src/main/java/com/oujiong/Application.java
...ble-read-write/src/main/java/com/oujiong/Application.java
+19
-0
sub-table-read-write/src/main/java/com/oujiong/controller/UserController.java
.../src/main/java/com/oujiong/controller/UserController.java
+59
-0
sub-table-read-write/src/main/java/com/oujiong/entity/User.java
...ble-read-write/src/main/java/com/oujiong/entity/User.java
+53
-0
sub-table-read-write/src/main/java/com/oujiong/mapper/UserMapper.java
...ad-write/src/main/java/com/oujiong/mapper/UserMapper.java
+31
-0
sub-table-read-write/src/main/java/com/oujiong/service/UserService.java
...-write/src/main/java/com/oujiong/service/UserService.java
+26
-0
sub-table-read-write/src/main/java/com/oujiong/service/impl/UserServiceImpl.java
...c/main/java/com/oujiong/service/impl/UserServiceImpl.java
+41
-0
sub-table-read-write/src/main/resources/application.properties
...able-read-write/src/main/resources/application.properties
+36
-0
sub-table-read-write/src/main/resources/mapper/UserMapper.xml
...table-read-write/src/main/resources/mapper/UserMapper.xml
+33
-0
sub-table-read-write/src/main/resources/mybatis-config.xml
sub-table-read-write/src/main/resources/mybatis-config.xml
+13
-0
sub-table/src/main/resources/application.properties
sub-table/src/main/resources/application.properties
+2
-2
未找到文件。
db-read-write/src/main/resources/application.properties
浏览文件 @
61319ccd
...
...
@@ -10,14 +10,14 @@ spring.shardingsphere.datasource.master.type=com.alibaba.druid.pool.DruidDataSou
spring.shardingsphere.datasource.master.driver-class-name
=
com.mysql.jdbc.Driver
spring.shardingsphere.datasource.master.url
=
jdbc:mysql://localhost:3306/master?characterEncoding=utf-8
spring.shardingsphere.datasource.master.username
=
root
spring.shardingsphere.datasource.master.password
=
123456
spring.shardingsphere.datasource.master.password
=
root
# 数据源 从库
spring.shardingsphere.datasource.slave0.type
=
com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.slave0.driver-class-name
=
com.mysql.jdbc.Driver
spring.shardingsphere.datasource.slave0.url
=
jdbc:mysql://local:3306/slave?characterEncoding=utf-8
spring.shardingsphere.datasource.slave0.username
=
root
spring.shardingsphere.datasource.slave0.password
=
123456
spring.shardingsphere.datasource.slave0.password
=
root
# 读写分离
spring.shardingsphere.masterslave.load-balance-algorithm-type
=
round_robin
...
...
sub-db-table/src/main/resources/application.properties
浏览文件 @
61319ccd
...
...
@@ -9,19 +9,20 @@ spring.shardingsphere.datasource.names=ds0,ds1
spring.shardingsphere.datasource.ds0.type
=
com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.ds0.driver-class-name
=
com.mysql.jdbc.Driver
spring.shardingsphere.datasource.ds0.url
=
jdbc:mysql://
47.99.203.55
:3306/ds0?characterEncoding=utf-8
spring.shardingsphere.datasource.ds0.url
=
jdbc:mysql://
localhost
:3306/ds0?characterEncoding=utf-8
spring.shardingsphere.datasource.ds0.username
=
root
spring.shardingsphere.datasource.ds0.password
=
123456
spring.shardingsphere.datasource.ds0.password
=
root
spring.shardingsphere.datasource.ds1.type
=
com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.ds1.driver-class-name
=
com.mysql.jdbc.Driver
spring.shardingsphere.datasource.ds1.url
=
jdbc:mysql://
47.99.203.55
:3306/ds1?characterEncoding=utf-8
spring.shardingsphere.datasource.ds1.url
=
jdbc:mysql://
localhost
:3306/ds1?characterEncoding=utf-8
spring.shardingsphere.datasource.ds1.username
=
root
spring.shardingsphere.datasource.ds1.password
=
123456
spring.shardingsphere.datasource.ds1.password
=
root
#根据年龄分库
spring.shardingsphere.sharding.default-database-strategy.inline.sharding-column
=
age
spring.shardingsphere.sharding.default-database-strategy.inline.algorithm-expression
=
ds$->{age % 2}
#根据id分表
spring.shardingsphere.sharding.tables.tab_user.actual-data-nodes
=
ds$->{0..1}.tab_user$->{0..1}
spring.shardingsphere.sharding.tables.tab_user.table-strategy.inline.sharding-column
=
id
spring.shardingsphere.sharding.tables.tab_user.table-strategy.inline.algorithm-expression
=
tab_user$->{id % 2}
...
...
sub-db-table/src/main/resources/mapper/UserMapper.xml
浏览文件 @
61319ccd
...
...
@@ -16,7 +16,7 @@
<select
id=
"selectAll"
resultMap=
"BaseResultMap"
>
select
<include
refid=
"Base_Column_List"
/>
from tab_user
from tab_user
order by age
</select>
<insert
id=
"insertForeach"
parameterType=
"java.util.List"
useGeneratedKeys=
"false"
>
...
...
sub-table-read-write/src/main/java/com/oujiong/Application.java
0 → 100644
浏览文件 @
61319ccd
package
com.oujiong
;
import
org.springframework.boot.SpringApplication
;
import
org.springframework.boot.autoconfigure.SpringBootApplication
;
/**
* @Description: 启动类
*
* @author xub
* @date 2019/10/08 下午6:33
*/
@SpringBootApplication
public
class
Application
{
public
static
void
main
(
String
[]
args
)
{
SpringApplication
.
run
(
Application
.
class
,
args
);
}
}
sub-table-read-write/src/main/java/com/oujiong/controller/UserController.java
0 → 100644
浏览文件 @
61319ccd
package
com.oujiong.controller
;
import
com.google.common.collect.Lists
;
import
com.oujiong.entity.User
;
import
com.oujiong.service.UserService
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.web.bind.annotation.GetMapping
;
import
org.springframework.web.bind.annotation.PostMapping
;
import
org.springframework.web.bind.annotation.RestController
;
import
javax.annotation.PostConstruct
;
import
java.util.List
;
/**
* @Description: 接口测试
*
* @author xub
* @date 2019/8/24 下午6:31
*/
@RestController
public
class
UserController
{
@Autowired
private
UserService
userService
;
/**
* 模拟插入数据
*/
List
<
User
>
userList
=
Lists
.
newArrayList
();
/**
* 初始化插入数据
*/
@PostConstruct
private
void
getData
()
{
userList
.
add
(
new
User
(
100L
,
"小小"
,
"女"
,
3
));
userList
.
add
(
new
User
(
101L
,
"爸爸"
,
"男"
,
30
));
userList
.
add
(
new
User
(
102L
,
"妈妈"
,
"女"
,
28
));
userList
.
add
(
new
User
(
103L
,
"爷爷"
,
"男"
,
64
));
userList
.
add
(
new
User
(
104L
,
"奶奶"
,
"女"
,
62
));
}
/**
* @Description: 批量保存用户
*/
@PostMapping
(
"save-user"
)
public
Object
saveUser
()
{
return
userService
.
insertForeach
(
userList
);
}
/**
* @Description: 获取用户列表
*/
@GetMapping
(
"list-user"
)
public
Object
listUser
()
{
return
userService
.
list
();
}
}
sub-table-read-write/src/main/java/com/oujiong/entity/User.java
0 → 100644
浏览文件 @
61319ccd
package
com.oujiong.entity
;
import
lombok.Data
;
import
java.util.Date
;
/**
* user表
*/
@Data
public
class
User
{
/**
* 主键
*/
private
Long
id
;
/**
* 姓名
*/
private
String
name
;
/**
* 性别
*/
private
String
sex
;
/**
* 年龄
*/
private
Integer
age
;
/**
*
*/
private
Date
createTime
;
/**
*
*/
private
Date
updateTime
;
/**
* 是否删除 1删除 0未删除
*/
private
Integer
status
;
public
User
(
Long
id
,
String
name
,
String
sex
,
Integer
age
)
{
this
.
id
=
id
;
this
.
name
=
name
;
this
.
sex
=
sex
;
this
.
age
=
age
;
}
}
\ No newline at end of file
sub-table-read-write/src/main/java/com/oujiong/mapper/UserMapper.java
0 → 100644
浏览文件 @
61319ccd
package
com.oujiong.mapper
;
import
com.oujiong.entity.User
;
import
org.apache.ibatis.annotations.Mapper
;
import
java.util.List
;
/**
* @Description: 用户mapper
*
* @author xub
* @date 2019/10/8 下午9:23
*/
@Mapper
public
interface
UserMapper
{
/**
* 批量插入
*
* @param list 插入集合
* @return 插入数量
*/
int
insertForeach
(
List
<
User
>
list
);
/**
* 获取所有用户
*/
List
<
User
>
selectAll
();
}
\ No newline at end of file
sub-table-read-write/src/main/java/com/oujiong/service/UserService.java
0 → 100644
浏览文件 @
61319ccd
package
com.oujiong.service
;
import
com.oujiong.entity.User
;
import
java.util.List
;
/**
* @Description: 用户相关接口
*
* @author xub
* @date 2019/8/24 下午6:32
*/
public
interface
UserService
{
/**
* 获取所有用户信息
*/
List
<
User
>
list
();
/**
* 批量 保存用户信息
* @param userVOList
*/
String
insertForeach
(
List
<
User
>
userVOList
);
}
\ No newline at end of file
sub-table-read-write/src/main/java/com/oujiong/service/impl/UserServiceImpl.java
0 → 100644
浏览文件 @
61319ccd
package
com.oujiong.service.impl
;
import
com.oujiong.entity.User
;
import
com.oujiong.mapper.UserMapper
;
import
com.oujiong.service.UserService
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Service
;
import
java.util.Date
;
import
java.util.List
;
/**
* @author xub
* @Description: 用户实现类
* @date 2019/8/8 上午9:13
*/
@Service
public
class
UserServiceImpl
implements
UserService
{
@Autowired
private
UserMapper
userMapper
;
@Override
public
List
<
User
>
list
()
{
List
<
User
>
users
=
userMapper
.
selectAll
();
return
users
;
}
@Override
public
String
insertForeach
(
List
<
User
>
userList
)
{
for
(
User
user
:
userList
)
{
user
.
setCreateTime
(
new
Date
());
user
.
setUpdateTime
(
new
Date
());
user
.
setStatus
(
0
);
}
//批量插入数据
userMapper
.
insertForeach
(
userList
);
return
"保存成功"
;
}
}
sub-table-read-write/src/main/resources/application.properties
0 → 100644
浏览文件 @
61319ccd
server.port
=
8084
#指定mybatis信息
mybatis.config-location
=
classpath:mybatis-config.xml
#打印sql
spring.shardingsphere.props.sql.show
=
true
spring.shardingsphere.datasource.names
=
master0,slave0
spring.shardingsphere.datasource.master0.type
=
com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.master0.driver-class-name
=
com.mysql.jdbc.Driver
spring.shardingsphere.datasource.master0.url
=
jdbc:mysql://localhost:3306/master?characterEncoding=utf-8
spring.shardingsphere.datasource.master0.username
=
root
spring.shardingsphere.datasource.master0.password
=
root
spring.shardingsphere.datasource.slave0.type
=
com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.slave0.driver-class-name
=
com.mysql.jdbc.Driver
spring.shardingsphere.datasource.slave0.url
=
jdbc:mysql://localhost:3306/slave?characterEncoding=utf-8
spring.shardingsphere.datasource.slave0.username
=
root
spring.shardingsphere.datasource.slave0.password
=
root
#数据分表规则
#指定所需分的表
spring.shardingsphere.sharding.tables.tab_user.actual-data-nodes
=
master0.tab_user$->{0..1}
#指定主键
spring.shardingsphere.sharding.tables.tab_user.table-strategy.inline.sharding-column
=
id
#分表规则为主键除以3取模
spring.shardingsphere.sharding.tables.tab_user.table-strategy.inline.algorithm-expression
=
tab_user$->{id % 2}
#spring.shardingsphere.sharding.tables.tab_user.key-generator.column=id
#spring.shardingsphere.sharding.tables.tab_user.key-generator.type=SNOWFLAKE
# 读写分离
#spring.shardingsphere.masterslave.load-balance-algorithm-type=round_robin
spring.shardingsphere.masterslave.name
=
ms
spring.shardingsphere.masterslave.master-data-source-name
=
master0
spring.shardingsphere.masterslave.slave-data-source-names
=
slave0
\ No newline at end of file
sub-table-read-write/src/main/resources/mapper/UserMapper.xml
0 → 100644
浏览文件 @
61319ccd
<?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.oujiong.mapper.UserMapper"
>
<resultMap
id=
"BaseResultMap"
type=
"com.oujiong.entity.User"
>
<id
column=
"id"
jdbcType=
"BIGINT"
property=
"id"
/>
<result
column=
"name"
jdbcType=
"VARCHAR"
property=
"name"
/>
<result
column=
"sex"
jdbcType=
"VARCHAR"
property=
"sex"
/>
<result
column=
"age"
jdbcType=
"INTEGER"
property=
"age"
/>
<result
column=
"create_time"
jdbcType=
"TIMESTAMP"
property=
"createTime"
/>
<result
column=
"update_time"
jdbcType=
"TIMESTAMP"
property=
"updateTime"
/>
<result
column=
"status"
jdbcType=
"INTEGER"
property=
"status"
/>
</resultMap>
<sql
id=
"Base_Column_List"
>
id, name, sex, age, create_time, update_time, status
</sql>
<select
id=
"selectAll"
resultMap=
"BaseResultMap"
>
select
<include
refid=
"Base_Column_List"
/>
from tab_user
</select>
<insert
id=
"insertForeach"
parameterType=
"java.util.List"
useGeneratedKeys=
"false"
>
insert into tab_user (id, name, sex,
age, create_time, update_time,
status)
values
<foreach
collection=
"list"
item=
"item"
index=
"index"
separator=
","
>
(#{item.id,jdbcType=BIGINT}, #{item.name,jdbcType=VARCHAR}, #{item.sex,jdbcType=VARCHAR},
#{item.age,jdbcType=INTEGER}, #{item.createTime,jdbcType=TIMESTAMP}, #{item.updateTime,jdbcType=TIMESTAMP},
#{item.status,jdbcType=INTEGER})
</foreach>
</insert>
</mapper>
\ No newline at end of file
sub-table-read-write/src/main/resources/mybatis-config.xml
0 → 100644
浏览文件 @
61319ccd
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<package
name=
"com.oujiong.entity"
/>
</typeAliases>
<mappers>
<mapper
resource=
"mapper/UserMapper.xml"
/>
</mappers>
</configuration>
sub-table/src/main/resources/application.properties
浏览文件 @
61319ccd
...
...
@@ -8,9 +8,9 @@ spring.shardingsphere.datasource.names=master
# 数据源 主库
spring.shardingsphere.datasource.master.type
=
com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.master.driver-class-name
=
com.mysql.jdbc.Driver
spring.shardingsphere.datasource.master.url
=
jdbc:mysql://
47.99.203.55
:3306/member?characterEncoding=utf-8
spring.shardingsphere.datasource.master.url
=
jdbc:mysql://
localhost
:3306/member?characterEncoding=utf-8
spring.shardingsphere.datasource.master.username
=
root
spring.shardingsphere.datasource.master.password
=
123456
spring.shardingsphere.datasource.master.password
=
root
#数据分表规则
#指定所需分的表
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录