shardingsphere mybatisplus properties和yml配置实现

news2024/10/7 14:31:41

shardingsphere mybatisplus properties和yml配置实现

目录结构
在这里插入图片描述
model

package com.oujiong.entity;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import java.util.Date;

/**
 * user表
 */
@TableName("user")
@Data
public class User {
    /**
     * 主键
     */
    private Long id;

    /**
     * 姓名
     */
    private String name;

    /**
     * 性别
     */
    private String sex;

    /**
     * 年龄
     */
    private Integer age;

    /**
     *
     */
    private Date createTime;

    /**
     *
     */
    private Date updateTime;

    /**
     * 是否删除 1删除 0未删除
     */
    private Integer status;

    public User(Long id, String name, String sex, Integer age) {
        this.id = id;
        this.name = name;
        this.sex = sex;
        this.age = age;
    }
}

mapper

package com.oujiong.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.oujiong.entity.User;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
/**
 * @Description: 用户mapper
 */
@Mapper
public interface UserMapper extends BaseMapper<User> {

    /**
     * 批量插入
     *
     * @param list 插入集合
     * @return 插入数量
     */
    int insertForeach(List<User> list);

    /**
     * 获取所有用户
     */
    List<User> selectAll();

}

servie

package com.oujiong.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.oujiong.entity.User;
import com.oujiong.mapper.UserMapper;
import java.util.List;

/**
 * @Description: 用户相关接口
 *
 */
public interface UserService  {

    /**
     *  获取所有用户信息
     */
    List<User>  list();

    /**
     *  批量 保存用户信息
     * @param userVOList
     */
    String  insertForeach(List<User> userVOList);

}

seviceImp

package com.oujiong.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.oujiong.entity.User;
import com.oujiong.mapper.UserMapper;
import com.oujiong.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author xub
 * @Description: 用户实现类
 * @date 2019/8/8 上午9:13
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public  List<User> list() {
        List<User> users =new ArrayList<>();
        User user = this.getById(102);
//        User user = userMapper.selectById(102);
        users.add(user);
//        List<User> users = userMapper.selectAll();
        return users;
    }

    @Override
    public String insertForeach(List<User> userList) {
        for (User user : userList) {
            user.setCreateTime(new Date());
            user.setUpdateTime(new Date());
            user.setStatus(0);
        }
        //批量插入数据
        userMapper.insertForeach(userList);
        return "保存成功";
    }
}

Application

package com.oujiong;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @Description: 启动类
 *
 * @author xub
 * @date 2019/10/08 下午6:33
 */
@MapperScan("com.oujiong.mapper.**")
@SpringBootApplication
public class Application {

	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}

}

mapper.xml

<?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.oujiong.mapper.UserMapper">
  <resultMap id="BaseResultMap" type="com.oujiong.entity.User">
    <id column="id" jdbcType="BIGINT" property="id" />
    <result column="name" jdbcType="VARCHAR" property="name" />
    <result column="sex" jdbcType="VARCHAR" property="sex" />
    <result column="age" jdbcType="INTEGER" property="age" />
    <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
    <result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
    <result column="status" jdbcType="INTEGER" property="status" />
  </resultMap>
  <sql id="Base_Column_List">
    id, name, sex, age, create_time, update_time, status
  </sql>
  <select id="selectAll"  resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List" />
    from tab_user
  </select>

  <insert id="insertForeach" parameterType="java.util.List" useGeneratedKeys="false">
    insert into tab_user (id, name, sex,
    age, create_time, update_time,
    status)
    values
    <foreach collection="list" item="item" index="index" separator=",">
      (#{item.id,jdbcType=BIGINT}, #{item.name,jdbcType=VARCHAR}, #{item.sex,jdbcType=VARCHAR},
      #{item.age,jdbcType=INTEGER}, #{item.createTime,jdbcType=TIMESTAMP}, #{item.updateTime,jdbcType=TIMESTAMP},
      #{item.status,jdbcType=INTEGER})
    </foreach>
  </insert>
</mapper>

properties和yml配置 目录结构和java文件配置都一致

properties

server.port=8088

#指定mybatis信息
mybatis.config-location=classpath:mybatis-config.xml

## 一个实体类对应多张表,覆盖
spring.main.allow-bean-definition-overriding=true

#数据库
spring.shardingsphere.datasource.names=master0,slave0

spring.shardingsphere.datasource.master0.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.master0.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.master0.url=jdbc:mysql://139.155.6.193:3306/sharding_test_1?characterEncoding=utf-8
spring.shardingsphere.datasource.master0.username=root
spring.shardingsphere.datasource.master0.password=MyNewPass4!

spring.shardingsphere.datasource.slave0.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.slave0.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.slave0.url=jdbc:mysql://139.155.6.193:3306/sharding_test_2?characterEncoding=utf-8
spring.shardingsphere.datasource.slave0.username=root
spring.shardingsphere.datasource.slave0.password=MyNewPass4!

#数据分表规则
#指定所需分的表(spring.shardingsphere.sharding.tables.user注意user)
spring.shardingsphere.sharding.tables.user.actual-data-nodes=master0.tab_user$->{0..1}
#指定主键
spring.shardingsphere.sharding.tables.user.table-strategy.inline.sharding-column=id
#分表规则为主键除以2取模
spring.shardingsphere.sharding.tables.user.table-strategy.inline.algorithm-expression=tab_user$->{id % 2}

# 读写分离
spring.shardingsphere.masterslave.load-balance-algorithm-type=round_robin
spring.shardingsphere.masterslave.name=ms
#这里配置读写分离的时候一定要记得添加主库的数据源名称 这里为master0
spring.shardingsphere.sharding.master-slave-rules.master0.master-data-source-name=master0
spring.shardingsphere.sharding.master-slave-rules.master0.slave-data-source-names=slave0

#spring.shardingsphere.sharding.binding-tables[0]=user
#spring.shardingsphere.mode.type=Memory
#spring.shardingsphere.mode.repository.type=File
#spring.shardingsphere.mode.overwrite=true

#打印sql
spring.shardingsphere.props.sql.show=true

spring.shardingsphere.sharding.tables.user注意user与实体类、xml相对应

在这里插入图片描述

yml

#服务器设置
server:
  port: 8090

spring:
  # 文件上传需要
  main:
    allow-bean-definition-overriding: true
  shardingsphere:
    #    数据库名称
    datasource:
      names: master0,slave0
      master0:
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.jdbc.Driver
        url: jdbc:mysql://139.155.6.193:3306/sharding_test_1?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8
        username: root
        password: MyNewPass4!
      slave0:
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.jdbc.Driver
        url: jdbc:mysql://139.155.6.193:3306/sharding_test_2?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8
        username: root
        password: MyNewPass4!
        #      配置主从规则
  # ====================== ↓↓↓↓↓↓ 读写分离配置 ↓↓↓↓↓↓ ======================
    master-slave-rules:
      master0:
        # 主库
        masterDataSourceName: master0
        # 从库
        slaveDataSourceNames:
          - slave0
        # 从库查询数据的负载均衡算法 目前有2种算法 round_robin(轮询)和 random(随机)
        # 算法接口 org.apache.shardingsphere.spi.masterslave.MasterSlaveLoadBalanceAlgorithm
        # 实现类 RandomMasterSlaveLoadBalanceAlgorithm 和 RoundRobinMasterSlaveLoadBalanceAlgorithm
        loadBalanceAlgorithmType: ROUND_ROBIN
    #  配置分片规则
    sharding:
      tables:
        user:
          logicTable: tab_user
          actual-data-nodes: master0.tab_user$->{0..1}
#          database-strategy:
#            inline:
#              sharding-column: id
#              algorithm-expression: master0
          table-strategy:
            inline:
              sharding-column: id
              algorithm-expression: tab_user$->{id % 2}


    props:
      sql:
        show: true                                          # 打印SQL




本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/750916.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

CSS整段文字缩进(一段多行文字中首列位置相对应)

<style>p {text-align: justify;padding-left: 2em;} </style>

webpack5性能优化

webpack构建速度 一、优化babel-loader 注意&#xff1a;开启缓存,配置后打包是就能缓存babel webpack.common.js文件命中缓存cacheDirectory {test: /\.js$/,use: [babel-loader?cacheDirectory],include: srcPath,exclude: /node_modules/ }, 测试&#xff1a; 打包后的…

ChatGLM-6B+LangChain实战

目标&#xff1a;原始使用ChatGLM-6B可接受的文字长度有限&#xff0c;打算结合LangChain实现长文本生成摘要. 方法&#xff1a; step1&#xff1a;自定义一个GLM继承LangChain中的langchain.llms.base.LLM&#xff0c;load自己的模型. step2&#xff1a;使用LangChain的mapred…

Web前端 Day 5

js初体验 使得代码可以具有某些行为 <body><button>点击我变成粉色</button><script>const btn document.querySelector(button)btn.addEventListener(click, () > {btn.style.backgroundColor pink ​})</script> </body> 效果图…

Maven详见及在Idea中的使用方法[保姆级包学包会]

文章目录 Maven详解1.1 目标1.2 Maven概括1.3 多模块开发1.3.1 pom.xml1.3.2 生命周期1.3.3 依赖特性(多模块1)1.3.4 继承特性(多模块2)1.3.5 dependencyManagement标签1.3.6 Maven-聚合(多模块3)聚合 1.3.6.1聚合总结 Maven详解 1.1 目标 maven是什么?maven能干什么?maven…

java并发编程 10:AQS

目录 什么是AQS原理 什么是AQS juc包的结构如下图&#xff1a; AQS就是AbstractQueuedSynchronizer&#xff0c;是个抽象类&#xff0c;实现了自己的一些方法。它是阻塞式锁和相关的同步器工具的框架。很多并发类都是基于它实现的&#xff0c;如&#xff1a;ReentrantLock、Co…

【力扣刷题 | 第十八天】

目录 前言&#xff1a; 1005. K 次取反后最大化的数组和 - 力扣&#xff08;LeetCode&#xff09; 134. 加油站 - 力扣&#xff08;LeetCode&#xff09; 总结&#xff1a; 前言&#xff1a; 今天随机刷题&#xff0c;不对题型做具体的要求 1005. K 次取反后最大化的数组和 …

Spring5学习笔记--Maven

Spring5学习笔记--Maven Maven高级1 分模块开发1.1 分模块开发设计1.2 分模块开发实现1.2.1 环境准备1.2.2 抽取domain层步骤1:创建新模块步骤2:项目中创建domain包步骤3:删除原项目中的domain包步骤4:建立依赖关系步骤5:编译maven_02_ssm项目步骤6:将项目安装本地仓库 1.2.3 抽…

揭秘GPT-4;Adobe Firefly AI 扩大测试规模

&#x1f989; AI新闻 &#x1f680; Adobe Firefly AI 扩大测试规模&#xff0c;支持100多种语言的输入 摘要&#xff1a;Adobe宣布扩大测试规模&#xff0c;Adobe Firefly AI现在支持100多种语言的 prompts 输入。网页测试版Firefly已经扩充了罗马尼亚语等多种语言&#xf…

layui选项卡演示

layui选项卡演示 .1 引入layui2. 选项卡演示实列3.js分离的代码4运行结果 在前端开发中&#xff0c;选项卡常用于展示多个内容模块&#xff0c;提供用户友好的界面交互方式。layui作为一款简洁易用的前端框架&#xff0c;提供了丰富的组件库&#xff0c;其中包括了强大且易用的…

Html基础知识学习——css精灵

这里写自定义目录标题 定义示例一示例二 定义 将网页用到的图片放在一张图片上&#xff0c;进行定位展示 优点:防止网页http请求次数过多&#xff0c;从而提高页面性能 缺点&#xff1a;降低开发效率。维护难度加大 示例一 使用图 网页制作图 <!DOCTYPE html> <…

AcWing 1273. 天才的记忆—RMQ

题目链接: AcWing 1273. 天才的记忆 问题描述 RMQ是用来求解静态区间最大/小值的算法&#xff0c;静态空间就是数组里的数不会变&#xff0c;动态空间最大/小值可以用线段树或者树状数组来求解。 RMQ算法有点类似与区间DP&#xff0c;RMQ算法的时间复杂度为 O ( n l o g n ) …

三菱 FX三菱PLC以太网通信程序

捷米特三菱FX转以太网通讯处理器是一款经济型的以太网通讯处理器&#xff0c;是为满足日益增多的工厂设备信息化需求&#xff08;设备网络监控和生产管理&#xff09;而设计&#xff0c;用于三菱FX1S/1N/2N/3S/3G/3GA/3GC/3U/3UC系列、汇川、士林AX、禾川、维控等PLC以太网数据…

【算法和数据结构】347、LeetCode前 K 个高频元素

文章目录 一、题目二、解法三、完整代码 所有的LeetCode题解索引&#xff0c;可以看这篇文章——【算法和数据结构】LeetCode题解。 一、题目 二、解法 思路分析&#xff1a;首先我们使用一个哈希表记录每个元素出现的频率。再设置一个优先队列&#xff0c;并将数组中元素出现的…

C语言实现扫雷【经典】

前言   本篇文章要实现的是扫雷游戏&#xff0c;其代码实现与上一篇的三子棋游戏类同&#xff0c;都是在棋盘的基础上&#xff0c;与电脑进行对抗&#xff0c;不同的是&#xff0c;扫雷游戏一开始电脑就已经随机布置好了所有“雷”。 请戳 --->三子棋 扫雷游戏 1. 扫雷游…

【Visual Studio】VTK 显示小球例子,在 Windows 上使用 Visual Studio 配合 Qt 构建 VTK

知识不是单独的&#xff0c;一定是成体系的。更多我的个人总结和相关经验可查阅这个专栏&#xff1a;Visual Studio。 关于更多此例子的资料&#xff0c;可以参考&#xff1a;【Visual Studio】在 Windows 上使用 Visual Studio 配合 Qt 构建 VTK。 文章目录 版本环境VTKTest.…

Nginx upstream 负载均衡配置

[toc] ## 问题: 自7/4 以来, 所有设备同时出现 Network Error, 导致业务无法正常进行, 频率 3次/每分钟; ## 现场情况及原因分析: 3楼: 8条产线 4楼: 20条产线 5楼: 5条产线 点数: 33条线 * 平均 (5台工位 1台电视看板 3台测试仪 ) ≈ 300 - Nginx 日志占用: access 日志 …

【MySQL备份与还原、索引、视图】练习

一、备份与还原 /***************************样例表***************************/CREATE DATABASE booksDB;use booksDB;CREATE TABLE books(bk_id INT NOT NULL PRIMARY KEY,bk_title VARCHAR(50) NOT NULL,copyright YEAR NOT NULL);INSERT INTO booksVALUES (11078, Lear…

UNIX网络编程卷一 学习笔记 第二十三章 高级SCTP套接字编程

SCTP是一个面向消息的协议&#xff0c;递送给用户的是部分的或完整的消息。只有当发送大消息时&#xff0c;在对端才会递送部分的消息。部分消息被递送给应用后&#xff0c;多个部分消息组合成单个完整消息不由SCTP负责。在SCTP应用进程看来&#xff0c;一个消息既可由单个输入…

线性代数的一些小细节

1 .矩阵的满足结合律&#xff0c;但不满足交换律 验证和证明如下图&#xff1a; 如下&#xff0c;UWQ三个矩阵的2种结合&#xff0c;证明矩阵乘法满足结合律 下图中&#xff0c;AB 和BA的值可能是不同的&#xff08;相同的条件是图中相互对应的4项相同&#xff0c;即对称矩阵…