SpringBoot 版本 2.2.2.RELEASE
项目结构
maven依赖
<!--springboot web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--lombok插件-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!--Mybatis-Plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.1.1</version>
</dependency>
<!--shardingsphere start-->
<!-- for spring boot -->
<dependency>
<groupId>io.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
<version>3.1.0</version>
</dependency>
<!-- for spring namespace -->
<dependency>
<groupId>io.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-namespace</artifactId>
<version>3.1.0</version>
</dependency>
<!--shardingsphere end-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--公共api组件-->
<dependency>
<groupId>com.gpdi.wireless</groupId>
<artifactId>wireless-api-commons</artifactId>
<version>${project.version}</version>
</dependency>
<!--alibaba 连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>${druid.verison}</version>
</dependency>
yaml 配置文件
server:
port: 8061
#mybatis-plus映射mapper文件
mybatis-plus:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.gpdi.wireless.domain
sharding:
jdbc:
datasource:
names: master1,salve0 #主从数据源
master1:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver #数据库新的驱动,下面连接url一定要加区时
url: jdbc:mysql://localhost:3306/master1?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowMultiQueries=true
username: root
password: 123456
salve0:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/salve0?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowMultiQueries=true
username: root
password: 123456
config: #读写分离配置
masterslave:
master-data-source-name: master1 #主数据库配置
slave-data-source-names: salve0 #从数据库配置(多个以逗号隔开)
load-balance-algorithm-type: round_robin # 提供轮询与随机(random),这里选择用轮询,
name: ms
props:
sql:
show: true #打印sql日志
spring:
main:
allow-bean-definition-overriding: true #设置为true,表示后发现的bean会覆盖之前相同名称的bean。
application:
name: sharing-sphere-server
MybatisPlusConfig 扫描mapper包
package com.gpdi.wireless.config;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Configuration;
/**
* @Author Lxq
* @Date 2020/5/4 20:59
* @Version 1.0
*/
@Configuration
@MapperScan("com.gpdi.wireless.mapper")
public class MybatisPlusConfig {
}
UserController
package com.gpdi.wireless.controller;
import com.gpdi.wireless.domain.UserInfo;
import com.gpdi.wireless.entities.CommonResult;
import com.gpdi.wireless.entities.Payment;
import com.gpdi.wireless.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* @Author Lxq
* @Date 2020/5/4 20:35
* @Version 1.0
*/
@RestController
public class UserController {
@Autowired
private UserInfoService userService;
@GetMapping("/select")
public List<UserInfo> select() {
return userService.getUserList();
}
@PostMapping("/insert")
public int insert(UserInfo user) {
return userService.save(user);
}
@GetMapping(value = "/getobj/{id}")
public CommonResult<Payment> getStorage(@PathVariable("id") Integer id) {
UserInfo userInfo = userService.selectById(id);
return new CommonResult<>(200, "from serverPort" + userInfo.toString());
}
}
UserInfo
package com.gpdi.wireless.domain;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @Author Lxq
* @Date 2020/5/4 20:34
* @Version 1.0
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName("user_info")
public class UserInfo {
@TableId(value = "id", type = IdType.AUTO)
private int id;
private String userName;
private int age;
}
UserInfoMapper
package com.gpdi.wireless.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.gpdi.wireless.domain.UserInfo;
import org.springframework.stereotype.Repository;
/**
* @Author Lxq
* @Date 2020/5/4 20:38
* @Version 1.0
*/
@Repository
public interface UserInfoMapper extends BaseMapper<UserInfo> {
}
UserInfoService
package com.gpdi.wireless.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.gpdi.wireless.domain.UserInfo;
import java.util.List;
/**
* @Author Lxq
* @Date 2020/5/4 20:36
* @Version 1.0
*/
public interface UserInfoService {
/**
* 保存用户信息
* @param entity
* @return
*/
int save(UserInfo entity);
/**
* 查询所以用户信息
* @return
*/
List<UserInfo> getUserList();
/**
*
* @param id
* @return
*/
UserInfo selectById(Integer id);
}
UserInfoServiceImpl
package com.gpdi.wireless.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gpdi.wireless.domain.UserInfo;
import com.gpdi.wireless.mapper.UserInfoMapper;
import com.gpdi.wireless.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* @Author Lxq
* @Date 2020/5/4 20:37
* @Version 1.0
*/
@Service
public class UserInfoServiceImpl implements UserInfoService {
@Autowired
private UserInfoMapper userInfoMapper;
public int save(UserInfo entity) {
return userInfoMapper.insert(entity);
}
@Override
public List<UserInfo> getUserList() {
QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
queryWrapper.select("*");
return userInfoMapper.selectList(queryWrapper);
}
@Override
public UserInfo selectById(Integer id) {
return userInfoMapper.selectById(id);
}
}
ShardingsphereMain
package com.gpdi.wireless;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @Author Lxq
* @Date 2020/5/4 20:26
* @Version 1.0
*/
@SpringBootApplication
public class ShardingsphereMain {
public static void main(String[] args) {
SpringApplication.run(ShardingsphereMain.class, args);
}
}
数据源初始化成功,用postman 测试,我这里主从没有配置,只是单独用两个数据库来测试,自己可以先配置主从后测试效果更好。