MybatisPlus集成baomidou-dynamic,多数据源配置使用、MybatisPlus分页分组等操作示例

news2024/10/6 8:24:11

文章目录

  • pom
  • 配置
  • 示例代码

pom

 <dependencies>
        <!--mybatisPlus集成SpringBoot起步依赖-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
             <version>3.4.2</version>
        </dependency>
        <!--MySQL 驱动依赖-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
             <version>8.0.27</version>
        </dependency>
        <!--druid 数据连接池-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
              <version>1.1.20</version>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>dynamic-datasource-spring-boot-starter</artifactId>
             <version>3.4.1</version>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
             <version>42.3.6</version>
        </dependency>
    </dependencies>

配置

配置文件

mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
spring.datasource.dynamic.primary=mysql
spring.datasource.dynamic.strict=false
spring.datasource.dynamic.datasource.mysql.url=jdbc:mysql://192.168.0.111:3306/database?characterEncoding=UTF-8&useUnicode=true&useSSL=false&tinyInt1isBit=false&currentSchema=public
spring.datasource.dynamic.datasource.mysql.username=root
spring.datasource.dynamic.datasource.mysql.password=123456
spring.datasource.dynamic.datasource.mysql.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.dynamic.datasource.mysql.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.dynamic.datasource.mysql.druid.maxActive=300
spring.datasource.dynamic.datasource.mysql.druid.initialSize=20
spring.datasource.dynamic.datasource.mysql.druid.maxWait=6000
spring.datasource.dynamic.datasource.mysql.druid.minIdle=20
spring.datasource.dynamic.datasource.mysql.druid.timeBetweenEvictionRunsMillis=60000
spring.datasource.dynamic.datasource.mysql.druid.minEvictableIdleTimeMillis=30000
spring.datasource.dynamic.datasource.mysql.druid.validationQuery=select 'x'
spring.datasource.dynamic.datasource.mysql.druid.testWhileIdle=true
spring.datasource.dynamic.datasource.mysql.druid.testOnBorrow=true
spring.datasource.dynamic.datasource.mysql.druid.testOnReturn=false

spring.datasource.dynamic.datasource.postgresql.url= jdbc:postgresql://127.0.0.1:5432/database?characterEncoding=UTF-8&useUnicode=true&useSSL=false&tinyInt1isBit=false&currentSchema=public
spring.datasource.dynamic.datasource.postgresql.username=postgres
spring.datasource.dynamic.datasource.postgresql.password=123456
spring.datasource.dynamic.datasource.postgresql.driverClassName=org.postgresql.Driver
spring.datasource.dynamic.datasource.postgresql.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.dynamic.datasource.postgresql.druid.maxActive=300
spring.datasource.dynamic.datasource.postgresql.druid.initialSize=20
spring.datasource.dynamic.datasource.postgresql.druid.maxWait=6000
spring.datasource.dynamic.datasource.postgresql.druid.minIdle=20
spring.datasource.dynamic.datasource.postgresql.druid.timeBetweenEvictionRunsMillis=60000
spring.datasource.dynamic.datasource.postgresql.druid.minEvictableIdleTimeMillis=30000
spring.datasource.dynamic.datasource.postgresql.druid.validationQuery=select 'x'
spring.datasource.dynamic.datasource.postgresql.druid.testWhileIdle=true
spring.datasource.dynamic.datasource.postgresql.druid.testOnBorrow=true
spring.datasource.dynamic.datasource.postgresql.druid.testOnReturn=false

配置类

解决分页失效问题

import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@MapperScan("com.ais.**.mapper.**")
public class MybatisPlusConfig {
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
        return interceptor;
    }
}

示例代码

实体类
@TableName(“tableName”)
@TableField
@TableId
在这里插入图片描述
Mapper
在这里插入图片描述
参考代码

import com.UserEntity;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class MybatisTest {
    @Resource
    private UserMapper mapper;

    /**
     * 分页操作 Mysql  MybatisPlus
     */
    @Test
    public void page() {
        IPage page = new Page(2, 2);
        IPage iPage = mapper.selectPage(page, null);
        System.out.println(iPage.getRecords());

    }

    /**
     * PGSQL 自定义SQL分页
     */
    @Test
    public void pagePg() {
        IPage page = new Page(1, 1);
        IPage iPage = mapper.selectAll(page);
        System.out.println(iPage.getRecords());
    }

    /**
     * 分页加排序降序
     */
    @Test
    public void sort() {
        IPage page = new Page(1, 10);
        QueryWrapper<Entity> queryWrapper = new QueryWrapper<>();
        //降序
        queryWrapper.orderByDesc("center_Id");
        //升序
//        queryWrapper.orderByAsc("center_Id");
        IPage iPage = mapper.selectPage(page, queryWrapper);
        System.out.println(iPage.getRecords());
    }

    /**
     * 分页加排序降序 lambda 表达式
     */
    @Test
    public void lambdaSort() {
        IPage page = new Page(1, 10);
        LambdaQueryWrapper<Entity> queryWrapper = new LambdaQueryWrapper<>();
        //降序
        queryWrapper.orderByDesc(Entity::getCenterId);
        //升序
//        queryWrapper.orderByAsc(Entity::getCenterId);
        IPage iPage = mapper.selectPage(page, queryWrapper);
        System.out.println(iPage.getRecords());
    }

    /**
     * in条件过滤 lambda 表达式
     */
    @Test
    public void selectIn() {
        IPage page = new Page(1, 10);
        LambdaQueryWrapper<Entity> queryWrapper = new LambdaQueryWrapper<>();
        List<Long> ids = new ArrayList<>();
        ids.add(1l);
        ids.add(2l);
        ids.add(3l);
        ids.add(4l);
        queryWrapper.in(Entity::getCenterId, ids);
//        queryWrapper.notIn(Entity::getCenterId, ids);
        IPage iPage = mapper.selectPage(page, queryWrapper);
        System.out.println(iPage.getRecords());
    }


    /**
     * in条件过滤 lambda 表达式
     */
    @Test
    public void selectInSql() {
        IPage page = new Page(1, 10);
        LambdaQueryWrapper<Entity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.inSql(Entity::getCenterId, "select center_Id from center where center_Id>5");
//        queryWrapper.notIn(Entity::getCenterId, ids);
        IPage iPage = mapper.selectPage(page, queryWrapper);
        System.out.println(iPage.getRecords());
    }

    @Test
    public void select() {
        IPage page = new Page(1, 10);
        LambdaQueryWrapper<Entity> queryWrapper = new LambdaQueryWrapper<>();
        IPage iPage = mapper.selectPage(page, queryWrapper);
        System.out.println(iPage.getRecords());
    }

    /**
     * max 加分组
     */
    @Test
    public void selectMax() {
        IPage page = new Page(1, 10);
        QueryWrapper<Entity> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("max(center_id) as center_id").groupBy("created_By");
        IPage iPage = mapper.selectPage(page, queryWrapper);
        System.out.println(iPage.getRecords());
    }

    /**
     * count 加分组
     */
    @Test
    public void selectCount() {
        IPage page = new Page(1, 10);
        QueryWrapper<Entity> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("count(center_id) as count").groupBy("created_By");
        mapper.selectPage(page, queryWrapper);
    }
}

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

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

相关文章

荆涛演唱歌曲《老板的孤独》:孤独中的坚韧与担当

歌手荆涛演唱的《老板的孤独》不仅是一首歌&#xff0c;更是一种情感的宣泄和表达。歌曲中表达了老板们在面对压力、孤独和困难时&#xff0c;依然坚持、积极向前的坚韧精神。每一句歌词都充满了对生活的深刻理解和感悟&#xff0c;以及对团队、家人的深深牵挂。 一、欣喜时要h…

【Netty专题】Netty调优及网络编程中一些问题补充(面向面试学习)

目录 前言阅读对象阅读导航笔记正文一、如何选择序列化框架1.1 基本介绍1.2 在网络编程中如何选择序列化框架1.3 常用Java序列化框架比较 二、Netty调优2.1 CONNECT_TIMEOUT_MILLIS&#xff1a;客户端连接时间2.2 SO_BACKLOG&#xff1a;最大同时连接数2.3 TCP_NODELAY&#xf…

spring-framework-5.2.25.RELEASE源码环境搭建

环境准备 spring-framework-5.2.25.RELEASEIntelliJ IDEA 2022.3.1java version “11.0.20” 2023-07-18 LTSGradle 5.6.4java version “1.8.0_301” 下载spring-framework-5.2.25.RELEASE源码 git clone https://gitee.com/QQ952051088/spring.git cd spring gradlew buil…

车载通信架构 —— 传统车内通信网络FlexRay(较高速度高容错、较灵活拓扑结构)

车载通信架构 —— 传统车内通信网络FlexRay(较高速度高容错、较灵活拓扑结构) 我是穿拖鞋的汉子,魔都中坚持长期主义的汽车电子工程师。 老规矩,分享一段喜欢的文字,避免自己成为高知识低文化的工程师: 屏蔽力是信息过载时代一个人的特殊竞争力,任何消耗你的人和事,…

Mysql 解决Invalid default value for ‘created_at‘

在mysql版本 8.0 和 5.* 之间数据互导的过程中&#xff0c;老是会出现各种错误&#xff0c;比如 这个created_at 一定要有一个默认值&#xff0c; 但是我加了 default null 还是会报错&#xff0c;于是对照了其他的DDL 发现&#xff0c;需要再加 null default null 才行&#…

Element-UI Upload 手动上传文件的实现与优化

文章目录 引言第一部分&#xff1a;Element-UI Upload 基本用法1.1 安装 Element-UI1.2 使用 <el-upload> 组件 第二部分&#xff1a;手动上传文件2.1 手动触发上传2.2 手动上传时的文件处理 第三部分&#xff1a;性能优化3.1 并发上传3.2 文件上传限制 结语 &#x1f38…

软件测试 | 解决‘pip‘ 不是内部或外部命令,也不是可运行的程序或批处理文件

&#x1f4e2;专注于分享软件测试干货内容&#xff0c;欢迎点赞 &#x1f44d; 收藏 ⭐留言 &#x1f4dd; 如有错误敬请指正&#xff01;&#x1f4e2;交流讨论&#xff1a;欢迎加入我们一起学习&#xff01;&#x1f4e2;资源分享&#xff1a;耗时200小时精选的「软件测试」资…

车载通信架构 —— 传统车内通信网络LIN总线(低成本覆盖低速场景)

车载通信架构 —— 传统车内通信网络LIN总线(低成本覆盖低速场景) 我是穿拖鞋的汉子,魔都中坚持长期主义的汽车电子工程师。 老规矩,分享一段喜欢的文字,避免自己成为高知识低文化的工程师: 屏蔽力是信息过载时代一个人的特殊竞争力,任何消耗你的人和事,多看一眼都是…

前缀和+哈希表——974. 和可被 K 整除的子数组

文章目录 &#x1fa81;1. 题目&#x1f3a3;2. 算法原理&#x1fa84;解法一&#xff1a;暴力枚举&#x1fa84;解法二&#xff1a;前缀和 哈希表 ⛳3. 代码实现 &#x1fa81;1. 题目 题目链接&#xff1a;974. 和可被 K 整除的子数组 - 力扣&#xff08;LeetCode&#xff0…

【室内定位系统源码】UWB超宽带定位技术的特点和应用前景

uwb人员、物品定位系统源码&#xff0c;智慧工厂人员安全管理定位&#xff0c;高精度定位系统源码 UWB超宽带定位技术概念&#xff1a; 超宽带无线通信技术&#xff08;UWB&#xff09;是一种无载波通信技术&#xff0c;UWB不使用载波&#xff0c;而是使用短的能量脉冲序…

QEMU Guest Agent本地提权漏洞处理(CVE-2023-0664)

一、漏洞描述 QEMU Guest Agent&#xff08;qga&#xff09;类似于vmware中的 vmtools&#xff0c;相关安全报告显示它的Windows版本安装程序存在本地提权高危漏洞&#xff08;CVE-2023-0664&#xff09;&#xff0c;攻击者可利用该漏洞进行本地权限提升&#xff0c;获得SYSTE…

【图数据库实战】图数据库基本概念

1、图数据库的概念 维基百科图书库的概念&#xff1a; 在计算机科学中&#xff0c;图数据库&#xff08;英语&#xff1a;graph database&#xff0c;GDB&#xff09;是一个使用图结构进行语义查询的数据库&#xff0c;它使用节点、边和属性来表示和存储数据。该系统的关键概念…

2024年天津天狮学院专升本护理学专业《内外科护理学》考试大纲

天津天狮学院2024年护理学专业高职升本入学考试《内外科护理学》考试大纲 一、考试性质 《内外科护理学》专业课程考试是天津天狮学院护理专业高职升本入学考试的必考科目之一&#xff0c;其性质是考核学生是否达到了升入本科继续学习的要求而进行的选拔性考试。《内外科护理学…

142.【Nginx负载均衡-01】

Nginx_基础篇 (一)、Nginx 简介1.背景介绍(1).http和三大邮局协议(2).反向代理与正向代理 2.常见服务器对比(1).公司介绍(2).lls 服务器(3).Tomcat 服务器(4).Apache 服务器(5).Lighttpd 服务器(6).其他的服务器 3.Nginx的优点(1).速度更快、并发更高(2).配置简单&#xff0c;扩…

apollo云实验:借道绕行场景仿真调试(9.0版)

借道绕行场景仿真调试&#xff08;9.0版&#xff09; 概述仿真目标与需求模型构建与数据准备仿真实验与结果分析 启动仿真环境实现任务功能修改全局配置参数 福利活动 主页传送门&#xff1a;&#x1f4c0; 传送 概述 在现代交通系统中&#xff0c;借道绕行是一种常见的交通管…

2021年全国硕士研究生入学统一考试管理类专业学位联考数学试题——解析版

文章目录 2021 年 1 月份管综初数真题一、问题求解&#xff08;本大题共 5 小题&#xff0c;每小题 3 分&#xff0c;共 45 分&#xff09;下列每题给出 5 个选项中&#xff0c;只有一个是符合要求的&#xff0c;请在答题卡上将所选择的字母涂黑。真题&#xff08;2014-01&…

应用可观测性OpenTelemetry简介

应用可观测性OpenTelemetry简介 OpenTelmetry遥测方案可观测性三支柱日志 Logs指标跟踪 什么是OpenTelemetryOpenTelemetry架构和组件OpenTelemetry与OpenCensus、OpenTracing是什么关系 OpenTelmetry遥测方案 可观测性三支柱 日志 Logs 日志是特定事件在特定时间点发生的文本…

【源码】智慧工地系统:让工地管理可视化、数字化、智能化

智慧工地是指运用信息化手段&#xff0c;围绕施工过程管理&#xff0c;建立互联协同、智能生产、科学管理的施工项目信息化生态圈&#xff0c;并将此数据在虚拟现实环境下与物联网采集到的工程信息进行数据挖掘分析&#xff0c;提供过程趋势预测及专家预案&#xff0c;实现工程…

matlab不用sawtooth,自己写代码实现锯齿波/三角波

matlab自己写代码实现锯齿波/三角波 为什么要自己写代码&#xff0c;不用现成的函数sawtooth&#xff1f; 函数sawtooth的采样频率是固定的&#xff0c;也就是给定一个时间段&#xff0c;只能按照固定的频率取点。比如10s内&#xff0c;每1s取一个点。这样就得到了1s 2s 3s……

Word打印模板,打印效果更出众丨三叠云

Word打印模板 路径 表单设置 >> 打印设置 功能简介 新增「Word打印模板」(beta版)。 Word 打印模板是指&#xff0c;在 Word 文档的基础上插入表单中的字段代码&#xff0c;打印时即可根据 Word 文档的格式&#xff0c;对表单数据进行个性化打印。 Word 打印模板能…