Skip to content

Commit

Permalink
Spring Boot版 Sharding JDBC 读写分离示列
Browse files Browse the repository at this point in the history
  • Loading branch information
尹吉欢 committed Aug 30, 2018
1 parent 1918942 commit e0a26b5
Show file tree
Hide file tree
Showing 11 changed files with 272 additions and 0 deletions.
53 changes: 53 additions & 0 deletions fangjia-sjdbc-read-write-springboot/pom.xml
@@ -0,0 +1,53 @@
<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.fangjia</groupId>
<artifactId>fangjia-sjdbc-read-write-springboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>fangjia-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>1.5.4.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>1.3.2</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.2</version>
</dependency>

<dependency>
<groupId>io.shardingjdbc</groupId>
<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
<version>2.0.0.M3</version>
</dependency>

</dependencies>
</project>
@@ -0,0 +1,18 @@
package com.fangjia.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);
}
}
@@ -0,0 +1,29 @@
package com.fangjia.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.fangjia.sjdbc.po.User;
import com.fangjia.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);
}

}
@@ -0,0 +1,40 @@
package com.fangjia.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;
}


}
@@ -0,0 +1,16 @@
package com.fangjia.sjdbc.repository;

import java.util.List;

import org.apache.ibatis.annotations.Mapper;

import com.fangjia.sjdbc.po.User;

@Mapper
public interface UserRepository {

Long addUser(User user);

List<User> list();

}
@@ -0,0 +1,12 @@
package com.fangjia.sjdbc.service;

import java.util.List;
import com.fangjia.sjdbc.po.User;

public interface UserService {

List<User> list();

Long add(User user);

}
@@ -0,0 +1,29 @@
package com.fangjia.sjdbc.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fangjia.sjdbc.po.User;
import com.fangjia.sjdbc.repository.UserRepository;

import io.shardingjdbc.core.api.HintManager;

@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);
}

}
@@ -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.fangjia.sjdbc.repository.UserRepository">

<resultMap id="baseResultMap" type="com.fangjia.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>
@@ -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.fangjia.sjdbc.po"/>
</typeAliases>
<mappers>
<mapper resource="META-INF/mappers/User.xml"/>
</mappers>
</configuration>
@@ -0,0 +1,25 @@
server.port=8084

mybatis.config-location=classpath:META-INF/mybatis-config.xml

sharding.jdbc.datasource.names=ds_master,ds_slave

# 主数据源
sharding.jdbc.datasource.ds_master.type=com.alibaba.druid.pool.DruidDataSource
sharding.jdbc.datasource.ds_master.driver-class-name=com.mysql.jdbc.Driver
sharding.jdbc.datasource.ds_master.url=jdbc:mysql://localhost:3306/ds_0?characterEncoding=utf-8
sharding.jdbc.datasource.ds_master.username=root
sharding.jdbc.datasource.ds_master.password=123456

# 从数据源
sharding.jdbc.datasource.ds_slave.type=com.alibaba.druid.pool.DruidDataSource
sharding.jdbc.datasource.ds_slave.driver-class-name=com.mysql.jdbc.Driver
sharding.jdbc.datasource.ds_slave.url=jdbc:mysql://localhost:3306/ds_1?characterEncoding=utf-8
sharding.jdbc.datasource.ds_slave.username=root
sharding.jdbc.datasource.ds_slave.password=123456

# 读写分离配置
sharding.jdbc.config.masterslave.load-balance-algorithm-type=round_robin
sharding.jdbc.config.masterslave.name=dataSource
sharding.jdbc.config.masterslave.master-data-source-name=ds_master
sharding.jdbc.config.masterslave.slave-data-source-names=ds_slave
12 changes: 12 additions & 0 deletions fangjia-sjdbc-read-write-springboot/src/main/resources/user.sql
@@ -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,'北京','张三');

0 comments on commit e0a26b5

Please sign in to comment.