Skip to content

Commit 161fb2a

Browse files
author
yulichao
committed
配置默认数据库
1 parent 87633ab commit 161fb2a

File tree

7 files changed

+141
-15
lines changed

7 files changed

+141
-15
lines changed

sharding-sphere/pom.xml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,15 @@
3333
</dependency>
3434

3535
<!-- ShardingSphere 注册配置中心 begin (直接配置文件配置的话不需要引入)-->
36-
<dependency>
37-
<groupId>org.apache.shardingsphere</groupId>
38-
<artifactId>sharding-orchestration-reg-nacos</artifactId>
39-
</dependency>
36+
<!-- <dependency>-->
37+
<!-- <groupId>org.apache.shardingsphere</groupId>-->
38+
<!-- <artifactId>sharding-orchestration-reg-nacos</artifactId>-->
39+
<!-- </dependency>-->
4040

41-
<dependency>
42-
<groupId>com.alibaba.nacos</groupId>
43-
<artifactId>nacos-client</artifactId>
44-
</dependency>
41+
<!-- <dependency>-->
42+
<!-- <groupId>com.alibaba.nacos</groupId>-->
43+
<!-- <artifactId>nacos-client</artifactId>-->
44+
<!-- </dependency>-->
4545
<!-- ShardingSphere 注册配置中心 end -->
4646

4747
<dependency>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package org.ylc.note.shardingsphere.controller;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
import org.springframework.http.ResponseEntity;
5+
import org.springframework.web.bind.annotation.GetMapping;
6+
import org.springframework.web.bind.annotation.RequestMapping;
7+
import org.springframework.web.bind.annotation.RequestParam;
8+
import org.springframework.web.bind.annotation.RestController;
9+
import org.ylc.note.shardingsphere.entity.User;
10+
import org.ylc.note.shardingsphere.mapper.UserMapper;
11+
12+
/**
13+
* 代码千万行,注释第一行,
14+
* 注释不规范,同事泪两行。
15+
*
16+
* @author Yulc
17+
* @date 2020/7/29
18+
*/
19+
@Slf4j
20+
@RequestMapping("/user")
21+
@RestController
22+
public class UserController {
23+
24+
private final UserMapper userMapper;
25+
26+
public UserController(UserMapper userMapper) {
27+
this.userMapper = userMapper;
28+
}
29+
30+
@GetMapping("/search")
31+
public ResponseEntity<User> searchUser(@RequestParam(value = "username") String username) {
32+
User user = userMapper.searchUser(username);
33+
return ResponseEntity.ok(user);
34+
}
35+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package org.ylc.note.shardingsphere.entity;
2+
3+
import lombok.Getter;
4+
import lombok.Setter;
5+
import lombok.ToString;
6+
7+
import java.time.LocalDateTime;
8+
9+
/**
10+
* 代码千万行,注释第一行,
11+
* 注释不规范,同事泪两行。
12+
*
13+
* @author Yulc
14+
* @date 2020/7/29
15+
*/
16+
@Getter
17+
@Setter
18+
@ToString
19+
public class User {
20+
21+
private Long id;
22+
23+
private String name;
24+
25+
private String username;
26+
27+
private String password;
28+
29+
private String status;
30+
31+
private LocalDateTime createTime;
32+
33+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.ylc.note.shardingsphere.mapper;
2+
3+
import org.apache.ibatis.annotations.Param;
4+
import org.springframework.stereotype.Component;
5+
import org.ylc.note.shardingsphere.entity.User;
6+
7+
/**
8+
* 代码千万行,注释第一行,
9+
* 注释不规范,同事泪两行。
10+
*
11+
* @author Yulc
12+
* @date 2020/7/29
13+
*/
14+
@Component
15+
public interface UserMapper {
16+
17+
User searchUser(@Param("username") String username);
18+
}

sharding-sphere/src/main/resources/application.yml

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
spring:
22
shardingsphere:
33
datasource:
4-
names: dcmi, sharding0, sharding1
5-
dcmi:
4+
names: sharding, sharding0, sharding1
5+
sharding:
66
#type: com.alibaba.druid.pool.DruidDataSource
77
type: com.zaxxer.hikari.HikariDataSource
88
driver-class: com.mysql.jdbc.Driver
9-
jdbc-url: jdbc:mysql://localhost:3306/dcmi?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8&autoReconnect=true
9+
jdbc-url: jdbc:mysql://localhost:3306/sharding?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8&autoReconnect=true
1010
username: root
1111
password: root2019!
1212
sharding0:
@@ -23,7 +23,7 @@ spring:
2323
password: root2019!
2424
sharding:
2525
# 默认数据源,不分片数据存放
26-
default-data-source-name: dcmi
26+
default-data-source-name: sharding
2727
# 默认分库策略
2828
default-database-strategy:
2929
# 行表达式
@@ -36,7 +36,11 @@ spring:
3636
tables:
3737
sys_log:
3838
# 真实的数据节点,数据源+表名的形式(临时数据,这里需要通过配置中心来实现动态配置)
39-
actual-data-nodes: sharding$->{0..1}.sys_log_20200$->{7..9}
39+
actual-data-nodes: sharding$->{0..1}.sys_log_$->{[202007, 202008, 202009, 202010]}
40+
database-strategy:
41+
inline:
42+
sharding-column: operation_id
43+
algorithm-expression: sharding$->{operation_id % 2}
4044
table-strategy:
4145
standard:
4246
sharding-column: create_time

sharding-sphere/src/main/resources/db/schemas.sql

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,26 @@
1+
-- 默认数据库
2+
CREATE SCHEMA `sharding` DEFAULT CHARACTER SET utf8 ;
3+
4+
create table t_user(
5+
id int primary key auto_increment,
6+
name varchar(20) not null,
7+
username varchar(20) not null,
8+
password varchar(32) not null,
9+
status char(1) not null,
10+
create_time datetime
11+
)
12+
13+
-- 分库0
114
CREATE SCHEMA `sharding0` DEFAULT CHARACTER SET utf8 ;
215

3-
16+
-- 分库1
417
CREATE SCHEMA `sharding1` DEFAULT CHARACTER SET utf8 ;
518

6-
19+
-- 分库下的分表,每个分库都有
720
CREATE TABLE `sys_log_202007` (
821
`id` int NOT NULL AUTO_INCREMENT,
922
`value` int NOT NULL,
23+
`operation_id` int NOT NULL,
1024
`create_time` datetime DEFAULT NULL,
1125
PRIMARY KEY (`id`)
1226
);
@@ -15,6 +29,7 @@ CREATE TABLE `sys_log_202007` (
1529
CREATE TABLE `sys_log_202008` (
1630
`id` int NOT NULL AUTO_INCREMENT,
1731
`value` int NOT NULL,
32+
`operation_id` int NOT NULL,
1833
`create_time` datetime DEFAULT NULL,
1934
PRIMARY KEY (`id`)
2035
);
@@ -23,6 +38,7 @@ CREATE TABLE `sys_log_202008` (
2338
CREATE TABLE `sys_log_202009` (
2439
`id` int NOT NULL AUTO_INCREMENT,
2540
`value` int NOT NULL,
41+
`operation_id` int NOT NULL,
2642
`create_time` datetime DEFAULT NULL,
2743
PRIMARY KEY (`id`)
2844
);
@@ -31,6 +47,7 @@ CREATE TABLE `sys_log_202009` (
3147
CREATE TABLE `sys_log_202010` (
3248
`id` int NOT NULL AUTO_INCREMENT,
3349
`value` int NOT NULL,
50+
`operation_id` int NOT NULL,
3451
`create_time` datetime DEFAULT NULL,
3552
PRIMARY KEY (`id`)
3653
);
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
3+
<mapper namespace="org.ylc.note.shardingsphere.mapper.UserMapper">
4+
5+
<resultMap id="BaseResultMap" type="org.ylc.note.shardingsphere.entity.User">
6+
<id column="id" jdbcType="INTEGER" property="id"/>
7+
<result column="name" jdbcType="VARCHAR" property="name"/>
8+
<result column="username" jdbcType="VARCHAR" property="username"/>
9+
<result column="password" jdbcType="VARCHAR" property="password"/>
10+
<result column="status" jdbcType="VARCHAR" property="status"/>
11+
<result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
12+
</resultMap>
13+
14+
<select id="searchUser" resultMap="BaseResultMap">
15+
SELECT * FROM t_user WHERE username = #{username}
16+
</select>
17+
18+
19+
</mapper>

0 commit comments

Comments
 (0)