MyBatis-Plus 版本要求
需要mybatis-plus版本 >= 3.0.7
用注解
@Select("select * from some_entity ${ew.customSqlSegment}")
List<SomeEntity> getAll(@Param(Constants.WRAPPER) Wrapper wrapper);
以下用法无效,即不能传入多个Wrapper:
@Select("select * from some_entity ${ew.customSqlSegment}")
List<SomeEntity> getAll(@Param(Constants.WRAPPER) Wrapper wrapper, @Param("ew2") Wrapper wrapper2,);
用XML
List<SomeEntity> getAll(@Param(Constants.WRAPPER) Wrapper ew);
<select id="getAll" resultType="com.xxx.yyy.SomeEntity">
SELECT * FROM some_entity ${ew.customSqlSegment}
</select>
高级用法
<select id="search" resultType="com.xxx.yyy.InnerCaseSearchResVo">
SELECT *
FROM inner_case AS parent
WHERE
<!-- 下面是子传导到父的 -->
parent.inner_case_code IN (
SELECT DISTINCT parent_inner_code
FROM inner_case AS child
WHERE
child.parent_inner_code IS NOT NULL
<!-- 下面是页面查询条件 -->
AND ${ew.expression.sqlSegment}
)
OR
<!-- 下面是父命中的 -->
(
parent.parent_inner_code IS NULL
<!-- 下面是页面查询条件 -->
AND ${ew.expression.sqlSegment}
)
</select>
注意对比:
# 打印的SQL中,带where字样
${ew.customSqlSegment}
# 打印的SQL中,不带where字样
${ew.expression.sqlSegment}
资料
- https://mybatis.plus/guide/wrapper.html#%E4%BD%BF%E7%94%A8-wrapper-%E8%87%AA%E5%AE%9A%E4%B9%89sql