当前使用版本(必填,否则不予处理)

3.3.1

该问题是如何引起的?(确定最新版也有问题再提!!!)

代码中写的查询条件是 is_delete = 0,一般实体类的属性都是包装数据类型,由于同事编写代码有一个类的属性使用的是int类型,int类型默认值为0。导致查询会默认携带上这个查询条件,导致查询返回数据不正确。使用最新版本我也进行验证了,也是这样的。个人感觉在编写实体类的时候属性直接定义成包装类型就可以了。

重现步骤(如果有就写完整)

MyBatis-Plus com.baomidou.mybatisplus.core.toolkit.Wrappers#lambdaQuery(T)查询问题

MyBatis-Plus com.baomidou.mybatisplus.core.toolkit.Wrappers#lambdaQuery(T)查询问题

MyBatis-Plus com.baomidou.mybatisplus.core.toolkit.Wrappers#lambdaQuery(T)查询问题

Comment From: mabyoung

把Wrappers.lambdaQuery(new User()).eq(BaseEntity::getIsDelete, LogicDeleteEnum.NOT_DELETED.getValue())
改成 Wrappers.<User>lambdaQuery().eq(BaseEntity::getIsDelete, LogicDeleteEnum.NOT_DELETED.getValue())
试试

Comment From: cxhello

把Wrappers.lambdaQuery(new User()).eq(BaseEntity::getIsDelete, LogicDeleteEnum.NOT_DELETED.getValue()) 改成 Wrappers.<User>lambdaQuery().eq(BaseEntity::getIsDelete, LogicDeleteEnum.NOT_DELETED.getValue()) 试试

改成这样可以,能讲解一下具体是为什么吗?

Comment From: Bjergsenj

把Wrappers.lambdaQuery(new User()).eq(BaseEntity::getIsDelete, LogicDeleteEnum.NOT_DELETED.getValue()) 改成 Wrappers.<User>lambdaQuery().eq(BaseEntity::getIsDelete, LogicDeleteEnum.NOT_DELETED.getValue()) 试试

改成这样可以,能讲解一下具体是为什么吗?

个人理解, 构造器不同,Wrappers.lambdaQuery(new User())传入的实体,Wrappers.lambdaQuery()在构造时实体为null。 com.baomidou.mybatisplus.core.injector.methods.SelectPage 类中生成SQL时,会产生各种if标签, 当你new User()的时候,基本数据类型会有默认值,然后满足了下面的if条件就会拼上sql

里面生成后的where大概如下

<where>
<if test="ew.entity != null">
<if test="ew.entity.id != null">id=#{ew.entity.id}</if>
<if test="ew.entity['name'] != null"> AND name=#{ew.entity.name}</if>
</if>
<if test="ew.sqlSegment != null and ew.sqlSegment != '' and ew.nonEmptyOfWhere">
<if test="ew.nonEmptyOfEntity and ew.nonEmptyOfNormal"> AND</if> ${ew.sqlSegment}
</if>
</where>

Comment From: cxhello

把Wrappers.lambdaQuery(new User()).eq(BaseEntity::getIsDelete, LogicDeleteEnum.NOT_DELETED.getValue()) 改成 Wrappers.<User>lambdaQuery().eq(BaseEntity::getIsDelete, LogicDeleteEnum.NOT_DELETED.getValue()) 试试

改成这样可以,能讲解一下具体是为什么吗?

个人理解, 构造器不同,Wrappers.lambdaQuery(new User())传入的实体,Wrappers.lambdaQuery()在构造时实体为null。 com.baomidou.mybatisplus.core.injector.methods.SelectPage 类中生成SQL时,会产生各种if标签, 当你new User()的时候,基本数据类型会有默认值,然后满足了下面的if条件就会拼上sql

里面生成后的where大概如下

<where> <if test="ew.entity != null"> <if test="ew.entity.id != null">id=#{ew.entity.id}</if> <if test="ew.entity['name'] != null"> AND name=#{ew.entity.name}</if> </if> <if test="ew.sqlSegment != null and ew.sqlSegment != '' and ew.nonEmptyOfWhere"> <if test="ew.nonEmptyOfEntity and ew.nonEmptyOfNormal"> AND</if> ${ew.sqlSegment} </if> </where>

我大致想的也是这样,实体类属性如果是基本数据类型int的话默认值是0,直接就带过去了。

Comment From: showlist

不建议直接传入实体进行查询

Comment From: qmdx

MP 建议都是包装类