-
Notifications
You must be signed in to change notification settings - Fork 387
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
尹吉欢
committed
Apr 22, 2019
1 parent
225f7ae
commit e12a083
Showing
12 changed files
with
313 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# maven ignore | ||
target/ | ||
*.jar | ||
*.war | ||
*.zip | ||
*.tar | ||
*.tar.gz | ||
|
||
# eclipse ignore | ||
.settings/ | ||
.project | ||
.classpath | ||
|
||
# idea ignore | ||
.idea/ | ||
*.ipr | ||
*.iml | ||
*.iws | ||
|
||
# temp ignore | ||
*.log | ||
*.cache | ||
*.diff | ||
*.patch | ||
*.tmp | ||
*.java~ | ||
*.properties~ | ||
*.xml~ | ||
|
||
# system ignore | ||
.DS_Store | ||
Thumbs.db |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>com.cxytiandi</groupId> | ||
<artifactId>sjdbc-read-write-springboot</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<packaging>jar</packaging> | ||
|
||
<name>sjdbc-read-write</name> | ||
<url>http://maven.apache.org</url> | ||
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
</properties> | ||
<parent> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-parent</artifactId> | ||
<version>2.0.6.RELEASE</version> | ||
<relativePath /> | ||
</parent> | ||
<dependencies> | ||
|
||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.mybatis.spring.boot</groupId> | ||
<artifactId>mybatis-spring-boot-starter</artifactId> | ||
<version>2.0.1</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>mysql</groupId> | ||
<artifactId>mysql-connector-java</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>com.alibaba</groupId> | ||
<artifactId>druid-spring-boot-starter</artifactId> | ||
<version>1.1.16</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.apache.shardingsphere</groupId> | ||
<artifactId>sharding-jdbc-spring-boot-starter</artifactId> | ||
<version>4.0.0-RC1</version> | ||
</dependency> | ||
<!-- <dependency> | ||
<groupId>org.apache.shardingsphere</groupId> | ||
<artifactId>sharding-jdbc-core</artifactId> | ||
<version>4.0.0-RC1</version> | ||
</dependency> --> | ||
|
||
</dependencies> | ||
</project> |
18 changes: 18 additions & 0 deletions
18
sjdbc-read-write-springboot/src/main/java/com/cxytiandi/sjdbc/ShardingJdbcApplicaiton.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.cxytiandi.sjdbc; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
/** | ||
* Spring Boot版 Sharding JDBC 读写分离示列 | ||
* | ||
* @author yinjihuan | ||
* | ||
* @about http://cxytiandi.com/about | ||
*/ | ||
@SpringBootApplication | ||
public class ShardingJdbcApplicaiton { | ||
public static void main(String[] args) { | ||
SpringApplication.run(ShardingJdbcApplicaiton.class, args); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
sjdbc-read-write-springboot/src/main/java/com/cxytiandi/sjdbc/controller/UserController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.cxytiandi.sjdbc.controller; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import com.cxytiandi.sjdbc.po.User; | ||
import com.cxytiandi.sjdbc.service.UserService; | ||
|
||
@RestController | ||
public class UserController { | ||
|
||
@Autowired | ||
private UserService userService; | ||
|
||
@GetMapping("/users") | ||
public Object list() { | ||
return userService.list(); | ||
} | ||
|
||
@GetMapping("/add") | ||
public Object add() { | ||
User user = new User(); | ||
user.setId(100L); | ||
user.setCity("深圳"); | ||
user.setName("李四"); | ||
return userService.add(user); | ||
} | ||
|
||
} |
40 changes: 40 additions & 0 deletions
40
sjdbc-read-write-springboot/src/main/java/com/cxytiandi/sjdbc/po/User.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.cxytiandi.sjdbc.po; | ||
|
||
import java.io.Serializable; | ||
|
||
public class User implements Serializable { | ||
|
||
private static final long serialVersionUID = -1205226416664488559L; | ||
|
||
private Long id; | ||
|
||
private String city = ""; | ||
|
||
private String name = ""; | ||
|
||
public Long getId() { | ||
return id; | ||
} | ||
|
||
public void setId(Long id) { | ||
this.id = id; | ||
} | ||
|
||
public String getCity() { | ||
return city; | ||
} | ||
|
||
public void setCity(String city) { | ||
this.city = city; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
|
||
} |
16 changes: 16 additions & 0 deletions
16
sjdbc-read-write-springboot/src/main/java/com/cxytiandi/sjdbc/repository/UserRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.cxytiandi.sjdbc.repository; | ||
|
||
import java.util.List; | ||
|
||
import org.apache.ibatis.annotations.Mapper; | ||
|
||
import com.cxytiandi.sjdbc.po.User; | ||
|
||
@Mapper | ||
public interface UserRepository { | ||
|
||
Long addUser(User user); | ||
|
||
List<User> list(); | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
sjdbc-read-write-springboot/src/main/java/com/cxytiandi/sjdbc/service/UserService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.cxytiandi.sjdbc.service; | ||
|
||
import java.util.List; | ||
|
||
import com.cxytiandi.sjdbc.po.User; | ||
|
||
public interface UserService { | ||
|
||
List<User> list(); | ||
|
||
Long add(User user); | ||
|
||
} |
28 changes: 28 additions & 0 deletions
28
sjdbc-read-write-springboot/src/main/java/com/cxytiandi/sjdbc/service/UserServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.cxytiandi.sjdbc.service; | ||
|
||
import java.util.List; | ||
|
||
import org.apache.shardingsphere.api.hint.HintManager; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
|
||
import com.cxytiandi.sjdbc.po.User; | ||
import com.cxytiandi.sjdbc.repository.UserRepository; | ||
|
||
@Service | ||
public class UserServiceImpl implements UserService { | ||
|
||
@Autowired | ||
private UserRepository userRepository; | ||
|
||
public List<User> list() { | ||
// 强制路由主库 | ||
//HintManager.getInstance().setMasterRouteOnly(); | ||
return userRepository.list(); | ||
} | ||
|
||
public Long add(User user) { | ||
return userRepository.addUser(user); | ||
} | ||
|
||
} |
26 changes: 26 additions & 0 deletions
26
sjdbc-read-write-springboot/src/main/resources/META-INF/mappers/User.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?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.cxytiandi.sjdbc.repository.UserRepository"> | ||
|
||
<resultMap id="baseResultMap" type="com.cxytiandi.sjdbc.po.User"> | ||
<result column="id" property="id" jdbcType="INTEGER" /> | ||
<result column="city" property="city" jdbcType="VARCHAR" /> | ||
<result column="name" property="name" jdbcType="VARCHAR" /> | ||
</resultMap> | ||
|
||
<insert id="addUser"> | ||
INSERT INTO user ( | ||
id, city, name | ||
) | ||
VALUES ( | ||
#{id,jdbcType=INTEGER}, | ||
#{city,jdbcType=VARCHAR}, | ||
#{name,jdbcType=VARCHAR} | ||
) | ||
</insert> | ||
|
||
<select id="list" resultMap="baseResultMap"> | ||
SELECT u.* FROM user u | ||
</select> | ||
|
||
</mapper> |
12 changes: 12 additions & 0 deletions
12
sjdbc-read-write-springboot/src/main/resources/META-INF/mybatis-config.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?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.cxytiandi.sjdbc.po"/> | ||
</typeAliases> | ||
<mappers> | ||
<mapper resource="META-INF/mappers/User.xml"/> | ||
</mappers> | ||
</configuration> |
28 changes: 28 additions & 0 deletions
28
sjdbc-read-write-springboot/src/main/resources/application.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
server.port=8084 | ||
|
||
mybatis.config-location=classpath:META-INF/mybatis-config.xml | ||
|
||
spring.shardingsphere.datasource.names=master,slave | ||
|
||
# 主数据源 | ||
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://localhost:3306/ds_0?characterEncoding=utf-8 | ||
spring.shardingsphere.datasource.master.username=root | ||
spring.shardingsphere.datasource.master.password=123456 | ||
|
||
# 从数据源 | ||
spring.shardingsphere.datasource.slave.type=com.alibaba.druid.pool.DruidDataSource | ||
spring.shardingsphere.datasource.slave.driver-class-name=com.mysql.jdbc.Driver | ||
spring.shardingsphere.datasource.slave.url=jdbc:mysql://localhost:3306/ds_1?characterEncoding=utf-8 | ||
spring.shardingsphere.datasource.slave.username=root | ||
spring.shardingsphere.datasource.slave.password=123456 | ||
|
||
# 读写分离配置 | ||
spring.shardingsphere.masterslave.load-balance-algorithm-type=round_robin | ||
spring.shardingsphere.masterslave.name=dataSource | ||
spring.shardingsphere.masterslave.master-data-source-name=master | ||
spring.shardingsphere.masterslave.slave-data-source-names=slave | ||
|
||
# 显示SQL | ||
spring.shardingsphere.props.sql.show=true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
CREATE DATABASE `ds_0` CHARACTER SET 'utf8' COLLATE 'utf8_general_ci'; | ||
CREATE DATABASE `ds_1` CHARACTER SET 'utf8' COLLATE 'utf8_general_ci'; | ||
|
||
CREATE TABLE `user`( | ||
id bigint(64) not null, | ||
city varchar(20) not null, | ||
name varchar(20) not null, | ||
PRIMARY KEY (`id`) | ||
) ENGINE=InnoDB DEFAULT CHARSET=utf8; | ||
|
||
insert into user values(1001,'上海','尹吉欢'); | ||
insert into user values(1002,'北京','张三'); |