mybatis报错:optimize this sql to a count sql has exception:…………………… exception:
null 我的sql语句是这样的:
<select id="getAllProjects" resultType="*******">
(select distinct projects.*,unitUser.uname as unitName,adminUser.uname as userName
from projects
inner join unitUser on projects.unitId=unituser.id
inner join adminProject on adminProject.projectId = projects.id
inner join adminUser on adminUser.id = adminproject.userId
${ew.customSqlSegment} )
UNION
(select distinct projects.*,unitUser.uname as unitName,null as userName
from projects
inner join unitUser on projects.unitId=unituser.id
and projects.id not in (
select projects.id from projects inner join adminProject on adminProject.projectId = projects.id
)
${ew.customSqlSegment} )
</select>
mybatis-plus的版本是4.0以上的,在网上看了博客报错的都是3.4,3.5;
该mapper方法的形参是这样的,ew是我的一个queryWrapper。
报错提示是SOL的解析异常:
${} 解析为SQL时,将形参变量的值直接取出,直接拼接显示在SQL中,
${ew.customSqlSegment}
需要加引号:'${ew.customSqlSegment}'
, 题干中没有说形参变量结构那么猜测${ew.customSqlSegment}
这个可能应该这样:${customSqlSegment}
, 并且出于安全与性能考虑,不建议使用${}
, 而是使用#{}
,#{}
不用加引号