mybatis-plus插件后面的版本没有兼容低版本。即:不存在低版本中EntityWrapper这个类了。而该类采用数据库表真实字段名作查询条件,这样硬编码形式确实不友好,比如如果后面数据库表中字段更名那么所有涉及到的业务都需要去修改,且硬编码形式没有遵循orm映射框架的设计理念。所以mybatis-plus后面的版本已经不支持该类操作了。

  但是这样造成一个问题。我们的一个项目开发设计时架构设计者采用了低版本的插件,但是项目进行一半时想要引入新版插件不兼容。要去一个个修改实在太麻烦,网上又没有这方面的文章。所以只能自己摸索出一个解决方案。

  解决的第一步大家可以参考这篇文章:https://www.cnblogs.com/java-jun-world2099/articles/11114501.html

  本以为就可以大功告成了,但是低高版本的插件内部设计重合度比较高(我并么有一步步去看原理),新的问题一个接一个。最后只能自己重写了BaseMapper和EntityWrapper这两个类。直接看代码好了:

application.yml

server:
port: 8088 spring:
datasource:
url: jdbc:mysql://xx.xx.xx.xxx:3306/ces?useUnicode=true&characterEncoding=utf-8
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
mapper-locations: classpath*:/mapper/*.xml

pom文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/>
</parent>
<groupId>com.bootjar</groupId>
<artifactId>shadeboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>shadeboot</name>
<description>Demo project for Spring Boot</description> <properties>
<java.version>1.8</java.version>
</properties> <dependencies> <dependency>
<groupId>shadejar</groupId>
<artifactId>plusjar</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-core</artifactId>
<version>3.1.1</version>
</dependency>
<!--<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>3.1.1</version>
</dependency>--> <dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatisplus-spring-boot-starter</artifactId>
<version>1.0.5</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>2.2.0</version>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.6</version>
</dependency>
<!--lombok依赖-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.7</version>
</dependency> </dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>

实体类User

@TableName("user")
@Data
public class User {
@TableId
private Long id;
private String userName;
private Integer age;
private String email;
private Date birth; }

UserMapper接口

package com.bootjar.shadeboot.mapper;

import com.bootjar.shadeboot.entity.BaseMapper;
import com.bootjar.shadeboot.entity.User;
import org.apache.ibatis.annotations.Mapper; /**
* @author:shf date:2019/7/1 10:40
*/
@Mapper
public interface UserMapper extends BaseMapper<User> {
}

EntityWrapper

package com.bootjar.shadeboot.entity;

import com.baomidou.mybatisplus.core.conditions.segments.MergeSegments;
import com.baomidou.mybatisplus.core.conditions.segments.NormalSegmentList;
import com.baomidou.mybatisplus.core.metadata.TableFieldInfo;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ReflectionKit;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.TableInfoHelper; import java.util.Objects; /**
* @author:shf date:2019/7/1 14:13
*/
public class EntityWrapper<T> extends com.baomidou.mybatisplus.mapper.EntityWrapper<T> {
public T entity; public MergeSegments expression; public String lastSql = ""; @Override
public T getEntity() {
return this.entity;
} @Override
public String getSqlSelect() {
return null;
} public String getSqlSet() {
return null;
} public MergeSegments getExpression() {
return this.expression;
} public String getCustomSqlSegment() {
MergeSegments expression = this.getExpression();
if (Objects.nonNull(expression)) {
NormalSegmentList normal = expression.getNormal();
String sqlSegment = this.getSqlSegment();
if (StringUtils.isNotEmpty(sqlSegment)) {
if (normal.isEmpty()) {
return sqlSegment;
} return this.concatWhere(sqlSegment);
}
} return "";
} @Override
public String getSqlSegment() {
String sqlSegment = "";
String sqlWhere = "";
if (this.expression != null) {
sqlSegment = this.expression.getSqlSegment();
} else if (this.sql != null) {
sqlWhere = this.sql.toString();
} if (StringUtils.isNotEmpty(sqlSegment)) {
return sqlSegment + this.lastSql;
} else if (com.baomidou.mybatisplus.toolkit.StringUtils.isNotEmpty(sqlWhere)) {
// return this.isWhere != null ? (this.isWhere ? sqlWhere : sqlWhere.replaceFirst("WHERE", this.AND_OR)) : sqlWhere.replaceFirst("WHERE", this.AND_OR);
return sqlWhere;
} else {
return StringUtils.isNotEmpty(this.lastSql) ? this.lastSql : null;
}
} private String concatWhere(String sql) {
return "WHERE " + sql;
} @Override
public boolean isEmptyOfWhere() {
return this.isEmptyOfNormal() && this.isEmptyOfEntity();
} public boolean nonEmptyOfWhere() {
return !this.isEmptyOfWhere();
} public boolean isEmptyOfNormal() {
if(this.getExpression() != null){
return CollectionUtils.isEmpty(this.getExpression().getNormal());
}
return true;
} public boolean nonEmptyOfNormal() {
return !this.isEmptyOfNormal();
} public boolean nonEmptyOfEntity() {
T entity = this.getEntity();
if (entity == null) {
return false;
} else {
TableInfo tableInfo = TableInfoHelper.getTableInfo(entity.getClass());
if (tableInfo == null) {
return false;
} else if (tableInfo.getFieldList().stream().anyMatch((e) -> {
return this.fieldStrategyMatch(entity, e);
})) {
return true;
} else {
return StringUtils.isNotEmpty(tableInfo.getKeyProperty()) ? Objects.nonNull(ReflectionKit.getMethodValue(entity, tableInfo.getKeyProperty())) : false;
}
}
} private boolean fieldStrategyMatch(T entity, TableFieldInfo e) {
switch (e.getFieldStrategy()) {
case NOT_NULL:
return Objects.nonNull(ReflectionKit.getMethodValue(entity, e.getProperty()));
case IGNORED:
return true;
case NOT_EMPTY:
return StringUtils.checkValNotNull(ReflectionKit.getMethodValue(entity, e.getProperty()));
default:
return Objects.nonNull(ReflectionKit.getMethodValue(entity, e.getProperty()));
}
} public boolean isEmptyOfEntity() {
return !this.nonEmptyOfEntity();
} }

BaseMapper

package com.bootjar.shadeboot.entity;

import com.baomidou.mybatisplus.mapper.Wrapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.session.RowBounds; import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Map; /**
* @author:shf date:2019/7/1 13:17
*/
public interface BaseMapper<T> extends com.baomidou.mybatisplus.core.mapper.BaseMapper<T> {
int insert(T var1); Integer insertAllColumn(T var1); int deleteById(Serializable var1); int deleteByMap(@Param("cm") Map<String, Object> var1); Integer delete(@Param("ew") Wrapper<T> var1); int deleteBatchIds(@Param("coll") Collection<? extends Serializable> var1); int updateById(@Param("et") T var1); Integer updateAllColumnById(@Param("et") T var1); Integer update(@Param("et") T var1, @Param("ew") Wrapper<T> var2); T selectById(Serializable var1); List<T> selectBatchIds(@Param("coll") Collection<? extends Serializable> var1); List<T> selectByMap(@Param("cm") Map<String, Object> var1); T selectOne(@Param("ew") T var1); Integer selectCount(@Param("ew") Wrapper<T> var1); List<T> selectList(@Param("ew") Wrapper<T> var1); List<Map<String, Object>> selectMaps(@Param("ew") Wrapper<T> var1); List<Object> selectObjs(@Param("ew") Wrapper<T> var1); List<T> selectPage(RowBounds var1, @Param("ew") Wrapper<T> var2); List<Map<String, Object>> selectMapsPage(RowBounds var1, @Param("ew") Wrapper<T> var2); }

测试类ShadebootApplicationTests

package com.bootjar.shadeboot;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bootjar.shadeboot.entity.EntityWrapper;
import com.bootjar.shadeboot.entity.User;
import com.bootjar.shadeboot.mapper.UserMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner; import java.util.HashMap;
import java.util.List;
import java.util.Map; @RunWith(SpringRunner.class)
@SpringBootTest
public class ShadebootApplicationTests { @Autowired
private UserMapper userMapper; //@Autowired
//private UserCopyMapper userCopyMapper; @Test
public void contextLoads() {
User user = userMapper.selectById("1");
System.out.println("--------------------user:" + user);
EntityWrapper<User> itemWrapper = new EntityWrapper<>(); itemWrapper.where("user_name = {0}", "Jack").eq("age", "20");
List<User> itemsEntities = userMapper.selectList(itemWrapper);
System.out.println("----------------itemsEntities :" + itemsEntities); LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<User>()
.eq(User::getUserName, "Jack").last("ORDER BY id DESC");
List<User> userList = userMapper.selectList(queryWrapper);
System.out.println("----------------userList:" + userList);
} @Test
public void insertTest() {
User user = new User();
user.setUserName("dada");
userMapper.insert(user);
} @Test
public void MapTest() {
Map map = new HashMap();
map.put("user_name", "1q");
map.put("age", 18);
List<User> MapList = userMapper.selectByMap(map);
System.out.println("--------------------------MapList:" + MapList);
} }

以上测试方法均通过,这是contextLoads方法的测试结果:

JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@7718a40f] will not be managed by Spring
==> Preparing: SELECT id,user_name,age,email,birth FROM user WHERE id=?
==> Parameters: 1(String)
<== Columns: id, user_name, age, email, birth
<== Row: 1, 1q, 18, test1@baomidou.com, 2019-01-01
<== Total: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@397ef2]
--------------------user:User(id=1, userName=1q, age=18, email=test1@baomidou.com, birth=Tue Jan 01 14:00:00 CST 2019)
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1229a2b7] was not registered for synchronization because synchronization is not active
JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@7718a40f] will not be managed by Spring
==> Preparing: SELECT id,user_name,age,email,birth FROM user WHERE (user_name = ? OR age = ?)
==> Parameters: Jack(String), 20(String)
<== Columns: id, user_name, age, email, birth
<== Row: 2, Jack, 20, test2@baomidou.com, 2019-01-01
<== Row: 7, Jack, 24, test5@baomidou.com, 2019-01-01
<== Row: 8, Jack, 24, test5@baomidou.com, 2019-01-01
<== Row: 9, Jack, 24, test5@baomidou.com, 2019-01-01
<== Total: 4
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1229a2b7]
----------------itemsEntities :[User(id=2, userName=Jack, age=20, email=test2@baomidou.com, birth=Tue Jan 01 14:00:00 CST 2019), User(id=7, userName=Jack, age=24, email=test5@baomidou.com, birth=Tue Jan 01 14:00:00 CST 2019), User(id=8, userName=Jack, age=24, email=test5@baomidou.com, birth=Tue Jan 01 14:00:00 CST 2019), User(id=9, userName=Jack, age=24, email=test5@baomidou.com, birth=Tue Jan 01 14:00:00 CST 2019)]
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4d95a72e] was not registered for synchronization because synchronization is not active
JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@7718a40f] will not be managed by Spring
==> Preparing: SELECT id,user_name,age,email,birth FROM user WHERE user_name = ? AND ( age LIKE ? ) ORDER BY id DESC
==> Parameters: Jack(String), %24%(String)
<== Columns: id, user_name, age, email, birth
<== Row: 9, Jack, 24, test5@baomidou.com, 2019-01-01
<== Row: 8, Jack, 24, test5@baomidou.com, 2019-01-01
<== Row: 7, Jack, 24, test5@baomidou.com, 2019-01-01
<== Total: 3
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4d95a72e]
----------------userList:[User(id=9, userName=Jack, age=24, email=test5@baomidou.com, birth=Tue Jan 01 14:00:00 CST 2019), User(id=8, userName=Jack, age=24, email=test5@baomidou.com, birth=Tue Jan 01 14:00:00 CST 2019), User(id=7, userName=Jack, age=24, email=test5@baomidou.com, birth=Tue Jan 01 14:00:00 CST 2019)]
2019-07-01 16:12:07.160 INFO 17744 --- [ Thread-2] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor'
2019-07-01 16:12:07.164 INFO 17744 --- [ Thread-2] com.alibaba.druid.pool.DruidDataSource : {dataSource-1} closed Process finished with exit code 0

解决Mybatis-plus高版本不向后兼容的问题的更多相关文章

  1. 解决linux 升级高版本python3.7后yum不能使用的问题

    我们linux系统一般自带python2.7 版本,但是最近项目需求必须要上python3以上,对于用惯了python2的我来说,只能硬着头皮上了.下面是我的解决办法 which yum => ...

  2. 解决jmeter5.1高版本linux CPU,IO,Memory监控性能测试 java.lang.NoSuchMethodError: org.apache.jmeter.samplers.SampleSaveConfiguration.setFormatter(Ljava/t

    jmeter中也可以监控服务器的CPU和内存使用情况,但是需要安装一些插件还需要在被监测服务器上开启服务. 安装性能监控插件(jmeter-plugins)后报如下错误,是由于jmeter版本过高jm ...

  3. 修改VS解决方案及工程名,解决如何打开高/版本VS项目

    对于VS2008等低版本与高版本VS之间的转换问题: 对照下面2个版本的不同点自由修改,切换到相应的版本文件(红字修改,灰色删除) ---------------------------------- ...

  4. 关于SQLServer2008数据如何导入SQL2005的解决办法,高版本数据导入低版本中。

    最近需要把SqlServer2008 的数据库导入sqlserver2005 中.直接备份还原肯定不行.后来想到可以生成脚本执行sql语句,并选择数据可以一同进行执行. 点击右键--->任务-- ...

  5. Subclipse和TortoiseSVN版本不一致导致升到高版本的project后,低版本svn客户端无法使用。

  6. 如何解决MySQL在高版本需要指明是否进行SSL连接问题

    WARN: Establishing SSL connection without server's identity verification is not recommended. Accordi ...

  7. sqlserver 高版本迁移到低版本

    奇葩事不少,  这不, 得把 sqlserver 2014 迁移到 2012 开始以为用备份再还原的方法就可以, 谁知道最终兼容性的问题无法解决(低版本不兼容高版本备份的文件, 即便在高版本中选择了兼 ...

  8. 解决redis集群版本不一致导致RDB同步失败的问题

    某天,运维反馈某两个机房的出口流量和入口流量过大,并且持续了好一段时间. 再仔细排查后发现是 redis 集群的几台服流量问题,于是开始查日志. 在日志中发现出现大量的 Can't handle RD ...

  9. html5调用本机摄像头兼容谷歌浏览器高版本,谷歌浏览器低版本,火狐浏览器

    做这个功能的时候在网上查了一些资料,代码如下,在这个代码在谷歌浏览器46版本是没问题的,在火狐浏览器也行,但是在谷歌浏览器高版本下是不兼容的 <div id="body"&g ...

随机推荐

  1. 在k8s上安装Jenkins及常见问题

    持续集成和部署是DevOps的重要组成部分,Jenkins是一款非常流行的持续集成和部署工具,最近试验了一下Jenkins,发现它是我一段时间以来用过的工具中最复杂的.一个可能的原因是它需要与各种其它 ...

  2. QHDYZ模拟赛20191012

    今天信息处老师(并不是教练,基本等于机房看门大爷) (好吧老师其实很犇,软件什么的厉害的一批,只是不能带oi--) 跟我说:"xxj,过两天月考完了,可以在初赛前再整一次模拟赛,一天,三道题 ...

  3. 使用“反向传播”迭代法求解y=√10

    X=√10,求X,也就是求Y=10 =X2 , X是多少. *重要的思想是,如何转化为可迭代求解的算法问题. *解数学问题,第一时间画图,求导,“直线化”. Y = X2 假如已知Y = 10 ,要求 ...

  4. 水管局长数据加强版:lct,时光倒流,最小生成树,边化点

    Description: SC省MY市有着庞大的地下水管网络,嘟嘟是MY市的水管局长(就是管水管的啦),嘟嘟作为水管局长的工作就是:每天供水公司可能要将一定量的水从x处送往y处,嘟嘟需要为供水公司找到 ...

  5. 20190630模拟赛B(单调队列优化dp)

    .dp无疑了其实. 在考场上,我写了一个错解,但是数据小都能过,只是会爆空间,考场上想着怎么用滚动数组优化来着....把错解的方程列出来吧 ;i<=n;i++) { ;j<=k;j++) ...

  6. Java高性能反射工具包ReflectASM

    ReflectASM 使用字节码生成的方式实现了更为高效的反射机制.执行时会生成一个存取类来 set/get 字段,访问方法或创建实例.一看到 ASM 就能领悟到 ReflectASM 会用字节码生成 ...

  7. python手册学习笔记2

    笔记2 > http://www.pythondoc.com/pythontutorial3/datastructures.html 列表操作 list.append(x) 把一个元素添加到列表 ...

  8. Uber Go 语言编码规范

    Uber Go 语言编码规范 Uber 是一家美国硅谷的科技公司,也是 Go 语言的早期 adopter.其开源了很多 golang 项目,诸如被 Gopher 圈熟知的 zap.jaeger 等.2 ...

  9. linux内核崩溃之kdump机制

    kdump相关概念 standard(production) kernel   生产内核    ,是指我们正在使用的kernel. Crash(capture)kernel             捕 ...

  10. 更改微信小程序的组件默认样式

    checkbox /*checkbox 整体大小  */ .checkbox {      width: 12%;      /* height: 240rpx; */ } /*checkbox 选项 ...