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. 匹马行天下之思维决定高度篇——道道道,学习Java之道

    致Java星球的程序员兄弟们的一封信 亲爱的Java星球的程序员兄弟们: 你们好!我是来自地球的一名Java程序员,首先我代表地球人对贵星球的高司令来到地球传授Java语言,造福了全人类,造福了整个地 ...

  2. 九大Java性能调试工具,必备至少一款

    九款Java性能调试工具,有什么更好.更多的工具,欢迎补充. NetBeans Profiler NetBeans中可以找到NetBeans Profiler. NetBeans分析器是NetBean ...

  3. ArcGIS Engine连接ArcSDE SQL Server(获得所有SDE图层)

    ArcSDE是ESRI公司推出的基于SDE技术的空间数据库解决方案,它是在现有的关系或对象关系型数据库管理系统的基础上进行应用扩展,可以将空间数据和非空间数据存储在目前绝大多数商用DBMS中,享受商用 ...

  4. Java虚拟机之垃圾回收

    简述 Java与那些较传统的语言比如C++有个很大不同就是垃圾回收策略了.前者通常是虚拟机自动帮我们做了,而后者就需要我们手动来完成. Java虚拟机帮我们完成了垃圾回收,是不是意味着我们就不用完全去 ...

  5. Pandas文本数据处理

    先初始化数据 import pandas as pd import numpy as np index = pd.Index(data=["Tom", "Bob" ...

  6. 19.7.29 NOIP模拟10

    话说这次三道考试题直接可以连成一个段子:我一个辣鸡,连模板都不会打,只能跪倒在大佬面前; T1 辣鸡 但是我实在是太辣鸡了,最后干的T1,时间不够用,连暴力都没打对,无奈之下交了一个qj程序,60分( ...

  7. C语言程序设计100例之(4):水仙花数

    例4    水仙花数 题目描述 一个三位整数(100-999),若各位数的立方和等于该数自身,则称其为“水仙花数”(如:153=13+53+33),找出所有的这种数. 输入格式 没有输入 输出格式 若 ...

  8. mysql用find_in_set代替like搜索提高性能

    mysql用find_in_set代替like搜索提高性能 <pre>SELECT * from mobantestinfo1 where find_in_set('33',info2); ...

  9. PHP对象继承

    PHP对象继承<?php class foo{ public function printItem($string) { echo 'Foo: ' . $string . PHP_EOL; } ...

  10. WIN7安装Docker Toolbox、制作镜像并发到阿里云

    一.安装Docker Toolbox,并配置国内源加速 WIndows7不支持Hyper-v,所以只能采用Docker Toolbox的方式使用Docker.传送门:http://mirrors.al ...