本章接讲解MP的条件构造器功能,操作通俗易懂,代码简洁美观。
结合lambda表达式使用,可以增强我们开发的效率,更易于我们后面的维护。
那么,什么是条件构造器呢?简单来说,条件构造器就是用来生成我们查数据库的sql。它可以简化sql代码的编写,灵活、方便且易于维护。我们知道传统的mybatis开发就是通过编写复杂的xml文件,构造最后所需要的、符合业务需求的sql,这种方式代码量会非常大(含有大量的参数判断代码),尤其是业务逻辑复杂的sql,会需要使用到各种各样的标签,需要熟悉各式各样的标签的使用。
MP默认是使用and()来拼接sql条件,当主动调用or()时,下一个方法使用or拼接!
比如:.nested(i->i.eq("NAME",“张三”).lt("AGE",18)).or().ne("NAME",“小明”)
sql如---> (NAME = '张三' and AGE <18 ) or NAME <> '小明'
nested():不携带and或者or的嵌套
该方法可以设置查询返回的字段。
比如:select("NAME","AGE")
sql如---> select NAME,AGE from table
excludeColumns()已从3.0.5版本上移除此方法!
该方法可用于数据库函数 动态入参的params对应前面applySql内部的{0}部分.这样是不会有sql注入风险的,反之会有!
比如:apply("date_format(dateColumn,'%Y-%m-%d') = {0}", "2008-08-08")
sql如---> date_format(dateColumn,'%Y-%m-%d') = '2008-08-08'
这里就不打算所有方法都讲解,主要是引导大家去认识和使用条件构造器的,其实官方说明已经非常地清楚了,大家要善于自己看官方的文档是最好的。下面说下我在使用MP时所遇到的一些问题,给大家分享一下我的经验。
我在做一个分页查询时,需要查询员工姓名、入职时间及其工资,同时可以对姓名进行精确查询,入职时间和工资可以进行区间搜索。
controller层方法:
/**
* 多表查询
* @return
*/
@ApiOperation(value = "分页查询员工及工资信息",notes = "--")
@PostMapping("/listUsers")
public Page<MpsUserRmb> listUsers(@RequestBody MpsUserVO mpsUserVO){
Page<MpsUserRmb> page =new Page<>(mpsUserVO.getPageNum(),mpsUserVO.getPageSize()==0?10:mpsUserVO.getPageSize());
return iMpsUserService.selectPage(page,mpsUserVO);
}
MpsUserServiceImpl.java
package com.mps.mybatisplus.common.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mps.mybatisplus.common.entity.MpsUser;
import com.mps.mybatisplus.common.entity.MpsUserRmb;
import com.mps.mybatisplus.common.vo.MpsUserVO;
import com.mps.mybatisplus.common.mapper.MpsUserMapper;
import com.mps.mybatisplus.common.service.IMpsUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
*
* 员工信息 服务实现类
*
*
* @author ken
* @since 2020-03-31
*/
@Service
public class MpsUserServiceImpl extends ServiceImpl<MpsUserMapper, MpsUser> implements IMpsUserService {
@Autowired
private MpsUserMapper mpsUserMapper;
@Override
public Page<MpsUserRmb> selectPage(Page page, MpsUserVO mpsUserVO){
// QueryWrapper ss =new QueryWrapper();
// ss.eq(StringUtils.isNotEmpty(mpsUserVO.getName()),"NAME",mpsUserVO.getName());
LambdaQueryWrapper<MpsUserRmb> lambdaQueryWrapper = new QueryWrapper<MpsUserRmb>().lambda();
lambdaQueryWrapper.eq(StringUtils.isNotEmpty(mpsUserVO.getName()),MpsUserRmb::getName,mpsUserVO.getName())
.ge(mpsUserVO.getStartCreateDate()!=null,MpsUserRmb::getCreateDate,mpsUserVO.getStartCreateDate())
.le(mpsUserVO.getEndCreateDate()!=null,MpsUserRmb::getCreateDate,mpsUserVO.getEndCreateDate())
.ge(mpsUserVO.getStartMoney()!=null,MpsUserRmb::getMoney,mpsUserVO.getStartMoney())
.le(mpsUserVO.getEndMoney()!=null,MpsUserRmb::getMoney,mpsUserVO.getEndMoney());
return mpsUserMapper.selectPageCustom(page,lambdaQueryWrapper);
}
}
MpsUserRmb.java
package com.mps.mybatisplus.common.entity;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import java.math.BigDecimal;
import java.time.LocalDateTime;
/**
*
* 员工、工资信息
*
*
* @author ken
* @since 2020-03-31
*/
@Data
@Builder
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@ApiModel(value = "员工及工资信息PO")
public class MpsUserRmb {
@ApiModelProperty(name = "id", value = "员工表id")
private String id;
@ApiModelProperty(name = "name", value = "姓名")
private String name;
@ApiModelProperty(name = "createDate", value = "创建时间")
private LocalDateTime createDate;
@ApiModelProperty(name = "startMoney", value = "工资")
private BigDecimal money;
}
MpsUserMapper.java
package com.mps.mybatisplus.common.mapper;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mps.mybatisplus.common.entity.MpsUser;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.mps.mybatisplus.common.entity.MpsUserRmb;
import com.mps.mybatisplus.common.vo.MpsUserVO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Component;
/**
*
* 员工信息 Mapper 接口
*
*
* @author ken
* @since 2020-03-31
*/
@Component
public interface MpsUserMapper extends BaseMapper<MpsUser> {
String querySql = "select * from (SELECT a.*,b.MONEY from MPS_USER a left join MPS_RMB b on a.ID = b.USER_ID) ${ew.customSqlSegment}";
@Select(querySql)
Page<MpsUserRmb> selectPageCustom(Page page, @Param(Constants.WRAPPER) Wrapper wrapper);
}
@Param(Constants.WRAPPER) Wrapper wrapper 和 querySql 里面的 ${ew.customSqlSegment} 是Wrapper 自定义SQL,让我们可以自定义SQL的同时也能使用Wrapper的便利!!!
问题来了,当启动项目开始测试时,发现有以下报错信息:
com.baomidou.mybatisplus.core.exceptions.MybatisPlusException: can not find lambda cache for this property [name] of entity [com.mps.mybatisplus.common.entity.MpsUserRmb]
at com.baomidou.mybatisplus.core.toolkit.ExceptionUtils.mpe(ExceptionUtils.java:49) ~[mybatis-plus-core-3.3.0.jar:3.3.0]
at com.baomidou.mybatisplus.core.toolkit.Assert.isTrue(Assert.java:38) ~[mybatis-plus-core-3.3.0.jar:3.3.0]
at com.baomidou.mybatisplus.core.toolkit.Assert.notNull(Assert.java:72) ~[mybatis-plus-core-3.3.0.jar:3.3.0]
at com.baomidou.mybatisplus.core.conditions.AbstractLambdaWrapper.getColumn(AbstractLambdaWrapper.java:95) ~[mybatis-plus-core-3.3.0.jar:3.3.0]
at com.baomidou.mybatisplus.core.conditions.AbstractLambdaWrapper.columnToString(AbstractLambdaWrapper.java:72) ~[mybatis-plus-core-3.3.0.jar:3.3.0]
at com.baomidou.mybatisplus.core.conditions.AbstractLambdaWrapper.columnToString(AbstractLambdaWrapper.java:68) ~[mybatis-plus-core-3.3.0.jar:3.3.0]
at com.baomidou.mybatisplus.core.conditions.AbstractLambdaWrapper.columnToString(AbstractLambdaWrapper.java:39) ~[mybatis-plus-core-3.3.0.jar:3.3.0]
at com.baomidou.mybatisplus.core.conditions.AbstractWrapper.lambda$addCondition$ac69df92$1(AbstractWrapper.java:332) ~[mybatis-plus-core-3.3.0.jar:3.3.0]
at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193) ~[na:1.8.0_74]
at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1374) ~[na:1.8.0_74]
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481) ~[na:1.8.0_74]
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471) ~[na:1.8.0_74]
at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708) ~[na:1.8.0_74]
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) ~[na:1.8.0_74]
at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499) ~[na:1.8.0_74]
at com.baomidou.mybatisplus.core.conditions.segments.NormalSegmentList.childrenSqlSegment(NormalSegmentList.java:89) ~[mybatis-plus-core-3.3.0.jar:3.3.0]
at com.baomidou.mybatisplus.core.conditions.segments.AbstractISegmentList.getSqlSegment(AbstractISegmentList.java:102) ~[mybatis-plus-core-3.3.0.jar:3.3.0]
at com.baomidou.mybatisplus.core.conditions.segments.MergeSegments.getSqlSegment(MergeSegments.java:72) ~[mybatis-plus-core-3.3.0.jar:3.3.0]
at com.baomidou.mybatisplus.core.conditions.AbstractWrapper.getSqlSegment(AbstractWrapper.java:432) ~[mybatis-plus-core-3.3.0.jar:3.3.0]
at com.baomidou.mybatisplus.core.conditions.Wrapper.getCustomSqlSegment(Wrapper.java:73) ~[mybatis-plus-core-3.3.0.jar:3.3.0]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_74]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_74]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_74]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_74]
at org.apache.ibatis.ognl.OgnlRuntime.invokeMethod(OgnlRuntime.java:881) ~[mybatis-3.5.3.jar:3.5.3]
at org.apache.ibatis.ognl.OgnlRuntime.getMethodValue(OgnlRuntime.java:1691) ~[mybatis-3.5.3.jar:3.5.3]
at org.apache.ibatis.ognl.ObjectPropertyAccessor.getPossibleProperty(ObjectPropertyAccessor.java:60) ~[mybatis-3.5.3.jar:3.5.3]
at org.apache.ibatis.ognl.ObjectPropertyAccessor.getProperty(ObjectPropertyAccessor.java:147) ~[mybatis-3.5.3.jar:3.5.3]
at org.apache.ibatis.ognl.OgnlRuntime.getProperty(OgnlRuntime.java:2719) ~[mybatis-3.5.3.jar:3.5.3]
at org.apache.ibatis.ognl.ASTProperty.getValueBody(ASTProperty.java:114) ~[mybatis-3.5.3.jar:3.5.3]
at org.apache.ibatis.ognl.SimpleNode.evaluateGetValueBody(SimpleNode.java:212) ~[mybatis-3.5.3.jar:3.5.3]
at org.apache.ibatis.ognl.SimpleNode.getValue(SimpleNode.java:258) ~[mybatis-3.5.3.jar:3.5.3]
at org.apache.ibatis.ognl.ASTChain.getValueBody(ASTChain.java:141) ~[mybatis-3.5.3.jar:3.5.3]
at org.apache.ibatis.ognl.SimpleNode.evaluateGetValueBody(SimpleNode.java:212) ~[mybatis-3.5.3.jar:3.5.3]
at org.apache.ibatis.ognl.SimpleNode.getValue(SimpleNode.java:258) ~[mybatis-3.5.3.jar:3.5.3]
at org.apache.ibatis.ognl.Ognl.getValue(Ognl.java:493) ~[mybatis-3.5.3.jar:3.5.3]
at org.apache.ibatis.ognl.Ognl.getValue(Ognl.java:457) ~[mybatis-3.5.3.jar:3.5.3]
at org.apache.ibatis.scripting.xmltags.OgnlCache.getValue(OgnlCache.java:46) ~[mybatis-3.5.3.jar:3.5.3]
at org.apache.ibatis.scripting.xmltags.TextSqlNode$BindingTokenParser.handleToken(TextSqlNode.java:77) ~[mybatis-3.5.3.jar:3.5.3]
at org.apache.ibatis.parsing.GenericTokenParser.parse(GenericTokenParser.java:77) ~[mybatis-3.5.3.jar:3.5.3]
at org.apache.ibatis.scripting.xmltags.TextSqlNode.apply(TextSqlNode.java:51) ~[mybatis-3.5.3.jar:3.5.3]
at org.apache.ibatis.scripting.xmltags.DynamicSqlSource.getBoundSql(DynamicSqlSource.java:39) ~[mybatis-3.5.3.jar:3.5.3]
at org.apache.ibatis.mapping.MappedStatement.getBoundSql(MappedStatement.java:297) ~[mybatis-3.5.3.jar:3.5.3]
at com.baomidou.mybatisplus.core.executor.MybatisCachingExecutor.query(MybatisCachingExecutor.java:88) ~[mybatis-plus-core-3.3.0.jar:3.3.0]
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:147) ~[mybatis-3.5.3.jar:3.5.3]
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:140) ~[mybatis-3.5.3.jar:3.5.3]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_74]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_74]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_74]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_74]
at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:426) ~[mybatis-spring-2.0.3.jar:2.0.3]
at com.sun.proxy.$Proxy85.selectList(Unknown Source) ~[na:na]
at org.mybatis.spring.SqlSessionTemplate.selectList(SqlSessionTemplate.java:223) ~[mybatis-spring-2.0.3.jar:2.0.3]
at com.baomidou.mybatisplus.core.override.MybatisMapperMethod.executeForIPage(MybatisMapperMethod.java:134) ~[mybatis-plus-core-3.3.0.jar:3.3.0]
at com.baomidou.mybatisplus.core.override.MybatisMapperMethod.execute(MybatisMapperMethod.java:96) ~[mybatis-plus-core-3.3.0.jar:3.3.0]
at com.baomidou.mybatisplus.core.override.MybatisMapperProxy.invoke(MybatisMapperProxy.java:96) ~[mybatis-plus-core-3.3.0.jar:3.3.0]
at com.sun.proxy.$Proxy90.selectPageCustom(Unknown Source) ~[na:na]
at com.mps.mybatisplus.common.service.impl.MpsUserServiceImpl.selectPage(MpsUserServiceImpl.java:40) ~[classes/:na]
at com.mps.mybatisplus.common.service.impl.MpsUserServiceImpl$$FastClassBySpringCGLIB$$434ab878.invoke() ~[classes/:na]
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218) ~[spring-core-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:685) ~[spring-aop-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at com.mps.mybatisplus.common.service.impl.MpsUserServiceImpl$$EnhancerBySpringCGLIB$$8111180a.selectPage() ~[classes/:na]
at com.mps.mybatisplus.common.controller.MpsUserController.listUsers(MpsUserController.java:100) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_74]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_74]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_74]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_74]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190) ~[spring-web-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138) ~[spring-web-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:106) ~[spring-webmvc-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:888) ~[spring-webmvc-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:793) ~[spring-webmvc-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040) ~[spring-webmvc-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943) ~[spring-webmvc-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) ~[spring-webmvc-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909) ~[spring-webmvc-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:660) ~[tomcat-embed-core-9.0.29.jar:9.0.29]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) ~[spring-webmvc-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741) ~[tomcat-embed-core-9.0.29.jar:9.0.29]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) ~[tomcat-embed-core-9.0.29.jar:9.0.29]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.29.jar:9.0.29]
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) ~[tomcat-embed-websocket-9.0.29.jar:9.0.29]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.29.jar:9.0.29]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.29.jar:9.0.29]
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) ~[spring-web-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.29.jar:9.0.29]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.29.jar:9.0.29]
at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) ~[spring-web-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.29.jar:9.0.29]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.29.jar:9.0.29]
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) ~[spring-web-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.29.jar:9.0.29]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.29.jar:9.0.29]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202) ~[tomcat-embed-core-9.0.29.jar:9.0.29]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) [tomcat-embed-core-9.0.29.jar:9.0.29]
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:526) [tomcat-embed-core-9.0.29.jar:9.0.29]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139) [tomcat-embed-core-9.0.29.jar:9.0.29]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) [tomcat-embed-core-9.0.29.jar:9.0.29]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74) [tomcat-embed-core-9.0.29.jar:9.0.29]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) [tomcat-embed-core-9.0.29.jar:9.0.29]
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:367) [tomcat-embed-core-9.0.29.jar:9.0.29]
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) [tomcat-embed-core-9.0.29.jar:9.0.29]
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:860) [tomcat-embed-core-9.0.29.jar:9.0.29]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1591) [tomcat-embed-core-9.0.29.jar:9.0.29]
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-9.0.29.jar:9.0.29]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_74]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_74]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-9.0.29.jar:9.0.29]
at java.lang.Thread.run(Thread.java:745) [na:1.8.0_74]
根据报错信息的提示可以看到,说无法在lambda缓存里面找不到 “name” 这个字段,可是我们的 MpsUserRmb.java 确实是有 “name” 这个字段的。经过测试,在 MpsUserServiceImpl.java 里面使用lambda表达式引用 MpsUserRmb.java 里的任何字段,启动项目无报错。但是,你会发现MP的 LambdaQueryWrapper 引用到 MpsUserRmb.java 的字段属性作为查询的筛选条件时,就会出现以上报错信息。
经过后面网上查询,获取方法(AbstractLambdaWrapper.getColumn)有变化,3.1.0版本之前是可以的。只能选择降低MP的版本,或者不使用lambda表达式可以解决以上报错问题。网上说的方法我也没有尝试过,但我自己是不想降低MP的版本的,同时我也不想放弃lambda表达式的使用。
经过不断的努力,我发现MP只缓存了BaseMapper里的 MpsUser 实体。至于是什么时候,怎么缓存的暂时没找到。
既然我们已经找到了原因,那我们就可以再创建一个MpsUserRmbMapper就好了,使得lambda可以获取到缓存字段。
MpsUserRmbMapper.java
package com.mps.mybatisplus.common.mapper;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mps.mybatisplus.common.entity.MpsUserRmb;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Component;
/**
*
* 员工、工资信息 Mapper 接口
*
*
* @author ken
* @since 2020-03-31
*/
@Component
public interface MpsUserRmbMapper extends BaseMapper<MpsUserRmb> {
String querySql = "select * from (SELECT a.*,b.MONEY from MPS_USER a left join MPS_RMB b on a.ID = b.USER_ID) ${ew.customSqlSegment}";
@Select(querySql)
Page<MpsUserRmb> selectPageCustom(Page page, @Param(Constants.WRAPPER) Wrapper wrapper);
}
MpsUserServiceImpl.java改成如下:
package com.mps.mybatisplus.common.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mps.mybatisplus.common.entity.MpsUser;
import com.mps.mybatisplus.common.entity.MpsUserRmb;
import com.mps.mybatisplus.common.mapper.MpsUserRmbMapper;
import com.mps.mybatisplus.common.vo.MpsUserVO;
import com.mps.mybatisplus.common.mapper.MpsUserMapper;
import com.mps.mybatisplus.common.service.IMpsUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
*
* 员工信息 服务实现类
*
*
* @author ken
* @since 2020-03-31
*/
@Service
public class MpsUserServiceImpl extends ServiceImpl<MpsUserMapper, MpsUser> implements IMpsUserService {
@Autowired
private MpsUserMapper mpsUserMapper;
@Autowired
private MpsUserRmbMapper mpsUserRmbMapper;
@Override
public Page<MpsUserRmb> selectPage(Page page, MpsUserVO mpsUserVO){
// QueryWrapper ss =new QueryWrapper();
// ss.eq(StringUtils.isNotEmpty(mpsUserVO.getName()),"NAME",mpsUserVO.getName());
LambdaQueryWrapper<MpsUserRmb> lambdaQueryWrapper = new QueryWrapper<MpsUserRmb>().lambda();
lambdaQueryWrapper.eq(StringUtils.isNotEmpty(mpsUserVO.getName()),MpsUserRmb::getName,mpsUserVO.getName())
.ge(mpsUserVO.getStartCreateDate()!=null,MpsUserRmb::getCreateDate,mpsUserVO.getStartCreateDate())
.le(mpsUserVO.getEndCreateDate()!=null,MpsUserRmb::getCreateDate,mpsUserVO.getEndCreateDate())
.ge(mpsUserVO.getStartMoney()!=null,MpsUserRmb::getMoney,mpsUserVO.getStartMoney())
.le(mpsUserVO.getEndMoney()!=null,MpsUserRmb::getMoney,mpsUserVO.getEndMoney());
// .nested(i -> i.ne(MpsUserRmb::getId,"1").eq(MpsUserRmb::getId,"2"));
return mpsUserRmbMapper.selectPageCustom(page,lambdaQueryWrapper);
}
}
然后进行测试,如下:
大功告成!!!我们可以继续简单快乐地coding~
项目源码git地址:https://github.com/litaijie/mybatisplus.git