SpringBoot 整合shardingsphere + mybatisPlus 实现读写分离

SpringBoot 版本 2.2.2.RELEASE

项目结构

SpringBoot 整合shardingsphere + mybatisPlus 实现读写分离_第1张图片

maven依赖

 
        
            org.springframework.boot
            spring-boot-starter-web
        
        
        
            org.projectlombok
            lombok
            true
        
        
        
            mysql
            mysql-connector-java
            runtime
        
        
        
            com.baomidou
            mybatis-plus-boot-starter
            3.1.1
        
        
        
        
            io.shardingsphere
            sharding-jdbc-spring-boot-starter
            3.1.0
        
        
        
            io.shardingsphere
            sharding-jdbc-spring-namespace
            3.1.0
        
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
        
            com.gpdi.wireless
            wireless-api-commons
            ${project.version}
        
        
        
            com.alibaba
            druid-spring-boot-starter
            ${druid.verison}
        

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 select() {
        return userService.getUserList();
    }

    @PostMapping("/insert")
    public int insert(UserInfo user) {
        return userService.save(user);
    }

    @GetMapping(value = "/getobj/{id}")
    public CommonResult 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 {
    
}
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 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 getUserList() {
        QueryWrapper 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);
    }
}

SpringBoot 整合shardingsphere + mybatisPlus 实现读写分离_第2张图片

 数据源初始化成功,用postman 测试,我这里主从没有配置,只是单独用两个数据库来测试,自己可以先配置主从后测试效果更好。

 SpringBoot 整合shardingsphere + mybatisPlus 实现读写分离_第3张图片

SpringBoot 整合shardingsphere + mybatisPlus 实现读写分离_第4张图片

你可能感兴趣的:(spring,boot)