9 changed files with 375 additions and 0 deletions
@ -0,0 +1,127 @@ |
|||
<?xml version="1.0" encoding="UTF-8"?> |
|||
<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.woniu</groupId> |
|||
<artifactId>springboot-multisource</artifactId> |
|||
<version>1.0.0-SNAPSHOT</version> |
|||
<packaging>jar</packaging> |
|||
|
|||
<name>springboot-multisource</name> |
|||
<description>SpringBoot配置多数据源</description> |
|||
|
|||
<parent> |
|||
<groupId>org.springframework.boot</groupId> |
|||
<artifactId>spring-boot-starter-parent</artifactId> |
|||
<version>2.0.4.RELEASE</version> |
|||
</parent> |
|||
|
|||
<properties> |
|||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> |
|||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> |
|||
<java.version>1.8</java.version> |
|||
<druid.version>1.1.2</druid.version> |
|||
<mysql-connector.version>8.0.26</mysql-connector.version> |
|||
<mybatis-plus.version>2.1.8</mybatis-plus.version> |
|||
<mybatisplus-spring-boot-starter.version>1.0.5</mybatisplus-spring-boot-starter.version> |
|||
</properties> |
|||
|
|||
<dependencies> |
|||
<dependency> |
|||
<groupId>org.springframework.boot</groupId> |
|||
<artifactId>spring-boot-starter-aop</artifactId> |
|||
</dependency> |
|||
<dependency> |
|||
<groupId>mysql</groupId> |
|||
<artifactId>mysql-connector-java</artifactId> |
|||
<version>${mysql-connector.version}</version> |
|||
<scope>runtime</scope> |
|||
</dependency> |
|||
<dependency> |
|||
<groupId>com.alibaba</groupId> |
|||
<artifactId>druid</artifactId> |
|||
<version>${druid.version}</version> |
|||
</dependency> |
|||
<!-- MyBatis plus增强和springboot的集成--> |
|||
<dependency> |
|||
<groupId>com.baomidou</groupId> |
|||
<artifactId>mybatis-plus</artifactId> |
|||
<version>${mybatis-plus.version}</version> |
|||
</dependency> |
|||
<dependency> |
|||
<groupId>com.baomidou</groupId> |
|||
<artifactId>mybatisplus-spring-boot-starter</artifactId> |
|||
<version>${mybatisplus-spring-boot-starter.version}</version> |
|||
</dependency> |
|||
|
|||
<dependency> |
|||
<groupId>io.shardingjdbc</groupId> |
|||
<artifactId>sharding-jdbc-core</artifactId> |
|||
<version>2.0.3</version> |
|||
</dependency> |
|||
|
|||
<dependency> |
|||
<groupId>org.springframework.boot</groupId> |
|||
<artifactId>spring-boot-starter-test</artifactId> |
|||
<scope>test</scope> |
|||
</dependency> |
|||
<dependency> |
|||
<groupId>org.hamcrest</groupId> |
|||
<artifactId>hamcrest-all</artifactId> |
|||
<version>1.3</version> |
|||
<scope>test</scope> |
|||
</dependency> |
|||
<dependency> |
|||
<groupId>org.projectlombok</groupId> |
|||
<artifactId>lombok</artifactId> |
|||
</dependency> |
|||
<dependency> |
|||
<groupId>org.springframework.boot</groupId> |
|||
<artifactId>spring-boot-starter-web</artifactId> |
|||
</dependency> |
|||
</dependencies> |
|||
|
|||
<build> |
|||
<plugins> |
|||
<plugin> |
|||
<groupId>org.apache.maven.plugins</groupId> |
|||
<artifactId>maven-compiler-plugin</artifactId> |
|||
<version>3.6.1</version> |
|||
<configuration> |
|||
<!--<proc>none</proc>--> |
|||
<source>1.8</source> |
|||
<target>1.8</target> |
|||
</configuration> |
|||
</plugin> |
|||
<plugin> |
|||
<groupId>org.apache.maven.plugins</groupId> |
|||
<artifactId>maven-surefire-plugin</artifactId> |
|||
<version>2.20</version> |
|||
<configuration> |
|||
<skip>true</skip> |
|||
</configuration> |
|||
</plugin> |
|||
<plugin> |
|||
<groupId>org.springframework.boot</groupId> |
|||
<artifactId>spring-boot-maven-plugin</artifactId> |
|||
<executions> |
|||
</executions> |
|||
</plugin> |
|||
</plugins> |
|||
|
|||
<resources> |
|||
<resource> |
|||
<directory>src/main/resources</directory> |
|||
</resource> |
|||
<resource> |
|||
<directory>src/main/java</directory> |
|||
<includes> |
|||
<include>**/*.xml</include> |
|||
</includes> |
|||
</resource> |
|||
</resources> |
|||
</build> |
|||
|
|||
</project> |
@ -0,0 +1,12 @@ |
|||
package com.woniu.pos; |
|||
|
|||
import org.springframework.boot.SpringApplication; |
|||
import org.springframework.boot.autoconfigure.SpringBootApplication; |
|||
|
|||
@SpringBootApplication |
|||
public class Application { |
|||
public static void main(String[] args) { |
|||
SpringApplication.run(Application.class, args); |
|||
} |
|||
|
|||
} |
@ -0,0 +1,32 @@ |
|||
package com.woniu.pos.common.dao.entity; |
|||
|
|||
|
|||
import com.baomidou.mybatisplus.annotations.TableName; |
|||
import com.baomidou.mybatisplus.enums.IdType; |
|||
import com.baomidou.mybatisplus.annotations.TableId; |
|||
import lombok.Data; |
|||
|
|||
import java.io.Serializable; |
|||
|
|||
/** |
|||
* @author 蜗牛 |
|||
*/ |
|||
@TableName(value = "t_user") |
|||
@Data |
|||
public class User implements Serializable { |
|||
|
|||
private static final long serialVersionUID = 1L; |
|||
|
|||
/** |
|||
* 主键ID |
|||
*/ |
|||
@TableId(value = "id", type = IdType.AUTO) |
|||
private Integer id; |
|||
|
|||
/** |
|||
* 名字 |
|||
*/ |
|||
private String name; |
|||
|
|||
|
|||
} |
@ -0,0 +1,13 @@ |
|||
package com.woniu.pos.common.dao.repository; |
|||
|
|||
import com.woniu.pos.common.dao.entity.User; |
|||
import com.baomidou.mybatisplus.mapper.BaseMapper; |
|||
import org.apache.ibatis.annotations.Mapper; |
|||
|
|||
/** |
|||
* @author 蜗牛 |
|||
*/ |
|||
@Mapper |
|||
public interface UserMapper extends BaseMapper<User> { |
|||
|
|||
} |
@ -0,0 +1,46 @@ |
|||
package com.woniu.pos.config; |
|||
|
|||
|
|||
import com.google.common.collect.Maps; |
|||
import io.shardingjdbc.core.api.MasterSlaveDataSourceFactory; |
|||
import io.shardingjdbc.core.api.config.MasterSlaveRuleConfiguration; |
|||
import lombok.extern.log4j.Log4j2; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
|||
import org.springframework.boot.context.properties.EnableConfigurationProperties; |
|||
import org.springframework.context.annotation.Bean; |
|||
import org.springframework.context.annotation.Configuration; |
|||
|
|||
import javax.sql.DataSource; |
|||
import java.sql.SQLException; |
|||
import java.util.Map; |
|||
|
|||
@Configuration |
|||
@EnableConfigurationProperties(ShardingMasterSlaveConfig.class) |
|||
@Slf4j |
|||
// 读取ds_master主数据源和读写分离配置
|
|||
@ConditionalOnProperty({ |
|||
"sharding.jdbc.data-sources.ds_master.jdbc-url", |
|||
"sharding.jdbc.master-slave-rule.master-data-source-name" |
|||
}) |
|||
public class ShardingDataSourceConfig { |
|||
|
|||
@Autowired |
|||
private ShardingMasterSlaveConfig shardingMasterSlaveConfig; |
|||
|
|||
@Bean |
|||
public DataSource masterSlaveDataSource() throws SQLException { |
|||
final Map<String, DataSource> dataSourceMap = Maps.newHashMap(); |
|||
dataSourceMap.putAll(shardingMasterSlaveConfig.getDataSources()); |
|||
// 拿到主从配置
|
|||
MasterSlaveRuleConfiguration masterSlaveRule = shardingMasterSlaveConfig.getMasterSlaveRule(); |
|||
final Map<String, Object> newHashMap = Maps.newHashMap(); |
|||
// 创建 MasterSlave数据源
|
|||
DataSource dataSource = MasterSlaveDataSourceFactory.createDataSource(dataSourceMap, |
|||
masterSlaveRule, newHashMap); |
|||
log.info("masterSlaveDataSource config complete"); |
|||
return dataSource; |
|||
} |
|||
|
|||
} |
@ -0,0 +1,21 @@ |
|||
package com.woniu.pos.config; |
|||
|
|||
import com.zaxxer.hikari.HikariDataSource; |
|||
import io.shardingjdbc.core.api.config.MasterSlaveRuleConfiguration; |
|||
import lombok.Data; |
|||
import org.springframework.boot.context.properties.ConfigurationProperties; |
|||
|
|||
import java.util.HashMap; |
|||
import java.util.Map; |
|||
|
|||
|
|||
@Data |
|||
@ConfigurationProperties(prefix = "sharding.jdbc") |
|||
public class ShardingMasterSlaveConfig { |
|||
|
|||
// 存放本地多个数据源
|
|||
private Map<String, HikariDataSource> dataSources = new HashMap<>(); |
|||
|
|||
private MasterSlaveRuleConfiguration masterSlaveRule; |
|||
|
|||
} |
@ -0,0 +1,30 @@ |
|||
package com.woniu.pos.controller; |
|||
|
|||
import com.woniu.pos.common.dao.entity.User; |
|||
import com.woniu.pos.common.dao.repository.UserMapper; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.web.bind.annotation.*; |
|||
|
|||
import java.util.List; |
|||
|
|||
/** |
|||
* 3分钟搞定读写分离 |
|||
*/ |
|||
@RestController |
|||
@RequestMapping("/user") |
|||
public class TbUserController { |
|||
|
|||
@Autowired |
|||
private UserMapper userMapper; |
|||
|
|||
@GetMapping("/userList") |
|||
public List<User> selectUsers() { |
|||
return userMapper.selectList(null); |
|||
} |
|||
|
|||
@PostMapping("/insertUser") |
|||
public void insertUser(@RequestBody User user) { |
|||
userMapper.insert(user); |
|||
} |
|||
|
|||
} |
@ -0,0 +1,63 @@ |
|||
package com.woniu.pos.service; |
|||
|
|||
import com.woniu.pos.common.dao.entity.User; |
|||
import com.woniu.pos.common.dao.repository.UserMapper; |
|||
import org.springframework.stereotype.Service; |
|||
import org.springframework.transaction.annotation.Transactional; |
|||
|
|||
import javax.annotation.Resource; |
|||
|
|||
/** |
|||
* 后台用户管理 |
|||
*/ |
|||
|
|||
@Service |
|||
@Transactional |
|||
public class UserService { |
|||
|
|||
@Resource |
|||
private UserMapper userMapper; |
|||
|
|||
/** |
|||
* 通过ID查找用户 |
|||
* @param id |
|||
* @return |
|||
*/ |
|||
public User findById(Integer id) { |
|||
return userMapper.selectById(id); |
|||
} |
|||
|
|||
/** |
|||
* 通过ID查找用户 |
|||
* @param id |
|||
* @return |
|||
*/ |
|||
public User findById1(Integer id) { |
|||
return userMapper.selectById(id); |
|||
} |
|||
|
|||
/** |
|||
* 新增用户 |
|||
* @param user |
|||
*/ |
|||
public void insertUser(User user) { |
|||
userMapper.insert(user); |
|||
} |
|||
|
|||
/** |
|||
* 修改用户 |
|||
* @param user |
|||
*/ |
|||
public void updateUser(User user) { |
|||
userMapper.updateById(user); |
|||
} |
|||
|
|||
/** |
|||
* 删除用户 |
|||
* @param id |
|||
*/ |
|||
public void deleteUser(Integer id) { |
|||
userMapper.deleteById(id); |
|||
} |
|||
|
|||
} |
@ -0,0 +1,31 @@ |
|||
server: |
|||
port: 8080 |
|||
mybatis-plus: |
|||
mapper-locations: classpath*:/mapper/*.xml |
|||
|
|||
#shardingjdbc配置 |
|||
sharding: |
|||
jdbc: |
|||
data-sources: |
|||
###配置第一个从数据库 |
|||
ds_slave_0: |
|||
password: 123456 |
|||
jdbc-url: jdbc:mysql://127.0.0.1:3306/ds2database?useUnicode=true&characterEncoding=utf-8&useSSL=false |
|||
driver-class-name: com.mysql.jdbc.Driver |
|||
username: root |
|||
###主数据库配置 |
|||
ds_master: |
|||
password: 123456 |
|||
jdbc-url: jdbc:mysql://127.0.0.1:3306/ds1database?useUnicode=true&characterEncoding=utf-8&useSSL=false |
|||
driver-class-name: com.mysql.jdbc.Driver |
|||
username: root |
|||
###配置读写分离 |
|||
master-slave-rule: |
|||
###配置从库选择策略,提供轮询与随机,这里选择用轮询 |
|||
load-balance-algorithm-type: round_robin |
|||
####指定主数据库 |
|||
master-data-source-name: ds_master |
|||
####指定从数据库 |
|||
slave-data-source-names: ds_slave_0 |
|||
####读写分离名称,随便起。 |
|||
name: ds_ms |
Loading…
Reference in new issue