标签: MybatisPlus

阿超 | 2022-09-06 | JavaMybatisPlus

Java类型描述符,与LambdaWrapper源码底层探究

年轻人在科学的进程中要有冲刺力,当你老了,就会越来越胆小——杨振宁 在 java 中,由于历史原因,出现在类文件结构中的二进制名称语法与我们常用的类名不同,通常使用(正斜杠) / 替换了原本的包名间隔(句号) . 例如 Thread 的类名叫 java.lang.Thread ,但是在 class 文件格式的描述符中使用的内部格式,对 Thread 类名称 utf8 的引用却是: java/lang/Thread 不信我们随便打开一个 class 文件 ![image-20220607130246228](https://waibi.oss-cn-chengdu.aliyuncs.com/picGo/image-20220607130246228.png) 可以看到类似的描述符 那如何获取类的描述符呢?它的规则又是如何呢? 首先,基本类型描述符,都是以 ASCII 字符表示,例如 L 正斜杠类名; 表示对象类型, [ 表示数组类型 我们可以在 sun.invoke.util.Wrapper 下看到对应枚举常量 ![image-2022060712494673...

 552 |  1 |  0 JavaMybatisPlus

老马 | 2022-09-05 | SpringBootMybatisPlus

搞懂幂等性

什么是幂等性 这个概念来源于一个数学公式: <img src="http://img.mayuanfei.com/typora-images/image-20220905160845723.png" alt="image-20220905160845723" style="zoom:50%;" / 简言之就是:任意多次的执行,对资源本身所产生的影响均与一次执行的影响相同。 什么情况下需要保证幂等性 我们以sql为例,来看看什么情况下有幂等性的问题。这里就以 stock (库存表为例) | 字段名 | 字段含义 | | ----------- | ------------- | | id | 雪花算法的id值 | | product_id | 产品id | | product_name | 产品名称 | | balance | 剩余数量 | 1.查询语句 sql select from stock where id = ?

 439 |  1 |  0 SpringBootMybatisPlus

青苗 | 2022-09-03 | MybatisPlus

Mybatis源码深度解析之#{}参数

mybatis中的 {}参数我们最常用的特性,在mybatis中 {}参数最终会作为编译参数来处理,也就是会被替换为‘?’,然后使用PreparedStatement的setXXX方法设置参数值,所以使用 {}参数没有sql注入的风险。 我们先简单回顾一下JDBC预编译语句的使用: java // 加载驱动 Class.forName(driver); // 获取db连接 Connection connection = DriverManager.getConnection(url, user, password); // 创建预编译语句 PreparedStatement ps = connection.prepareStatement("select name from user where id = ?"); // 设置预编译语句参数值 ps.setInt(1, 1); // 执行 ResultSet resultSet = ps.executeQuery(); // 获取结果 while (resultSet.next()) { System.out.pr

 1300 |  1 |  0 MybatisPlus

阿超 | 2022-09-02 | JavaMybatisPlusSQL

MP在h2下update entity 和updateWrapper字段重复处理拦截器

慷慨是友谊的精华——王尔德 今天发现 Mybatis-Plus 在 h2 下,同时使用 UpdateWrapper 和 entity 会出现 update 表名 set 字段1=xxx,字段1=xxx 这样的 sql ,在 mysql 下是正确的语法, h2 会抛出异常 所以写了个 mybatis 拦截器,放在了 streampark 里: pr 地址:https://github.com/streamxhub/streampark/pull/1493 源码: java / Copyright 2019 The StreamX Project Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apac

 551 |  1 |  0 JavaMybatisPlus

青苗 | 2022-08-24 | MybatisPlus

Mapper 接口无实现类动态代理原理

关于 Mybatis 核心 mapper 无具体实现如何执行 SQL 这部分相信很多人非常好奇,本文主要通过一个例子讲清楚这部分的原理实现。 核心原理 JDK InvocationHandler 动态代理,这部分我们需要熟悉 Proxy.newProxyInstance 如何生成代理类,这里就不逐一说明自行查找资料补全这部分知识点。 本文基于 JDK 18 演示例子 Mapper 代理类 MapperProxy java import java.io.Serializable; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.util.Map; public record MapperProxy<T (Map<Method, MapperMethod methodCache) implements InvocationHandler, Serializable { @Override public Objec

 3597 |  3 |  2 MybatisPlus

joert | 2022-08-12 | SpringBootMybatisPlus

SpringBoot 3.0 M3 集成 MybatisPlus 3.5.2 解决打包运行报错NestIOException问题

SpringBoot 3.0 M3集成MybatisPlus后打包运行出现报错NotFoundClass NestIOException. 因为Spring6中将NestIOException去掉了,Mybatis的MybatisSqlSessionFactoryBean用到了NestIOException。 解决办法: src.main.java目录下创建com.baomidou.mybatisplus.extension.spring包, 重写MybatisSqlSessionFactoryBean将NestIOException换成IOException即可。

 417 |  2 |  0 SpringBootMybatisPlus

王磊 | 2022-07-29 | MybatisPlus

大数据表 分页查询终极设计交互优化方案(附MP实现方案)

1、遇到的问题 去年我们做了一套停车系统,车辆的出入场记录数据量非常大,每次列表加载都很慢。定位问题后发现是COUNT语句执行效率较低。 2、初步处理 当然是加索引,建分区,前端自动设置当天起止时间一波操作 3、终极解决方案 和甲方商量,他们反馈说总数他们很少去关心100次使用这个功能需要看总数的次数不超过10次。于是前端加了一个可选项,是否显示总数,默认是不显示的,在分页插件那里checkbox选中才去走count,否则不走count。 4、前端逻辑 如果一页10条,本次返回了不满十条,前端禁用下一页,满十条启用下一页。 如果下一页数据是空的,则不替换列表数据,只提示已经是最后一页,然后禁用掉下一页按钮。 5、MP相关API 分页接口IPage提供了searchCount方法,默认返回true,咱们可以提供过一个setSearchCount方法来改变次方法的返回值控制本次是否只是走limit,不走count。

 635 |  1 |  4 MybatisPlus

老马 | 2022-07-29 | MybatisPlus

7.MP代码生成

目的 咱们在代码中要生成与数据库对应的实体类和mapper,甚至是服务相关的接口和实现类。MP支持这些代码的自动生成。 MP提供两种生成代码的方式: 代码生成器 idea插件方式 代码生成器 1.加入依赖 xml <!- MP代码生成器最新版本 - <dependency <groupId com.baomidou</groupId <artifactId mybatis-plus-generator</artifactId <version 3.5.3</version </dependency <!- freemarker - <dependency <groupId org.freemarker</groupId <artifactId freemarker</artifactId <version 2.3.31</version </dependency 2.生成代码 java public class MpGeneratorTest

 481 |  0 |  2 MybatisPlus

老马 | 2022-07-29 | MybatisPlus

6.MP多数据源

多数据源 dynamic-datasource-spring-boot-starter 是一个基于springboot的快速集成多数据源的启动器。这是一个第三方 mybatis 扩展库,与 mybatis-plus 本身无关,属于参与者小锅盖个人发起的项目。和MP结合的很好,而且操作多数据源时使用很方便。所以这里来介绍下使用。 DS文档地址:https://www.kancloud.cn/tracy5546/dynamic-datasource/2398947 文档是收费,基本使用本文档介绍的内容即可。 @DS的使用 1.springboot三板斧之添加依赖 xml <!- 动态数据源最新版本 - <dependency <groupId com.baomidou</groupId <artifactId dynamic-datasource-spring-boot-starter</artifactId <version 3.5.1</version </dependency 2.springbo

 762 |  0 |  0 MybatisPlus

老马 | 2022-07-29 | MybatisPlus

5.MP中内置的插件

内置插件 目前MP已经存在的内部插件包括如下: | 插件类名 | 作用 | | ------------------------------- | ------------------------------------------- | | PaginationInnerInterceptor | 分页插件。可以代替以前的PageHelper | | OptimisticLockerInnerInterceptor | 乐观锁插件。用于幂等性操作,采用版本更新记录 | | DynamicTableNameInnerInterceptor | 动态表名 | | TenantLineInnerInterceptor | 多租户 | | IllegalSQLInn

 471 |  0 |  0 MybatisPlus

老马 | 2022-07-29 | MybatisPlus

4.MP中Wrapper的使用

Wrapper简介 <img src="http://img.mayuanfei.com/typora-images/image-20220729134743697.png" alt="image-20220729134743697" style="zoom:50%;" / 注意: 查询用QueryWrapper和LambdaQueryWrapper来封装 updateWrapper和LambdaUPdateWrapper不但能封装查询还能更改要更新的对象。 QueryWrapper的使用 QueryWrapper中的很多条件限定都是见名知其意的。下表列出来几个常用的: <img src="http://img.mayuanfei.com/typora-images/image-20220729134854387.png" alt="image-20220729134854387" style="zoom:50%;" / 1.多条件进行查询 java @Test public void test01() { //查询用...

 365 |  0 |  0 MybatisPlus

老马 | 2022-07-29 | MybatisPlus

3.MP常用注解

常用注解 | 注解 | 含义 | 应用场景 | | ----------------------------------------------------------- | --------------------------- | ------------------------- | | [@TableName](https://baomidou.com/pages/223848/ tablename) | 表名注解,标识实体类对应的表 | 表名和实体类名称不一致 | | [@TableId](https://baomidou.com/pages/223848/ tableid) | 主键注解,标识实体类的主键 | 主键需要指定自增长 | | [@TableField](https://baomidou.com/pages/2238

 473 |  1 |  0 MybatisPlus

没有更多啦~