ew是mapper方法里的@Param(Constants.WRAPPER) Wrapper queryWrapper对象

首先判断ew.emptyOfWhere是否存在where条件,有的话再拼接上去,ew.customSqlSegment是WHERE + sql语句
没有where的时候加上 == false

使用${ew.sqlSegment} 如果是连表查询且查询条件是连表的字段则需在service层拼接查询条件时字段前指定别名

最佳例子1

mapper.xml

 <select id="tableList" resultType="java.util.LinkedHashMap">SELECT${ew.sqlSelect} // 这里拼接select后面的语句FROM${table_name} //如果是单表的话,这里可以写死${ew.customSqlSegment}</select>

mapper

IPage<LinkedHashMap<String,Object>> tableList(@Param("table_name") String table_name,Page page,@Param(Constants.WRAPPER) QueryWrapper queryWrapper);

test

String responseField = "*";
queryWrapper.select(responseField);
// 即 select * ...String responseField = "name";
queryWrapper.select(responseField);
// 即 select name ...

最佳例子2

controller

 public String saveAddress(HttpSession session) {UserVO user1 = (UserVO)session.getAttribute("user");LambdaQueryWrapper<User> lambdaQueryWrapper = Wrappers.<User>lambdaQuery().select(User::getNickName, User::getUserId) // 需要查询的列,即 ${ew.sqlSelect}.eq(User::getUserId, user1.getUserId());// 条件User user = this.userMapper.selectNickNameAndUserId(lambdaQueryWrapper);System.out.println(user);return null;}

mapper

    User selectNickNameAndUserId(@Param(Constants.WRAPPER) Wrapper<User> queryWrapper);

mapper.xml

<select id="selectNickNameAndUserId" resultType="com.example.demo.entity.User">select<if test="ew != null and ew.sqlSelect != null and ew.sqlSelect != ''">${ew.sqlSelect}</if>fromuserwhere is_deleted != 1<if test="ew != null"><if test="ew.nonEmptyOfWhere">AND</if>${ew.sqlSegment}</if></select>
 <select id="selectNickNameAndUserId" resultType="com.example.demo.entity.User">select<if test="ew != null and ew.sqlSelect != null and ew.sqlSelect != ''">${ew.sqlSelect }</if>fromuser${ew.customSqlSegment}</select>

使用${ew.sqlSegment} 如果是联表查询且查询条件是连表的字段则需在service层拼接查询条件时字段前指定别名,而且不能用lambda的查询了

 <select id="selectByRoleId" resultType="com.captain.crewer.mybatis.plus.dto.RolePermsDTO">SELECT tp.id,tp.perm_name,tp.url,tr.role_id   as roleId,tr.role_name as roleNameFROM tb_role trLEFT JOIN tb_perm_role tpr ON tr.role_id = tpr.role_idLEFT JOIN tb_perm tp ON tpr.perm_id = tp.id ${ew.customSqlSegment}</select>

mapper

 List<RolePermsDTO> selectByRoleId(@Param(Constants.WRAPPER) Wrapper<RolePermsDTO> wrapper);
 @Testpublic void test2(){QueryWrapper<RolePermsDTO> wrapper = new QueryWrapper<>();wrapper.eq("tr.role_id", 1);tbPermService.selectByRoleId(wrapper);}

${ew.sqlSet}

LambdaUpdateWrapper<User> wrapper = Wrappers.<User>lambdaUpdate().set(User::getNickName, "1").eq(User::getUserId, 1);this.userMapper.updateUser(wrapper);
int updateUser(@Param(Constants.WRAPPER) Wrapper<User> updateWrapper);
<update id="updateUser">update userset ${ew.sqlSet}where ${ew.sqlSegment}</update>

mybatis plus中的${ew.sqlSegment},${ew.sqlSelect},${ew.customSqlSegment},${ew.sqlSet}使用相关推荐

  1. MyBatis之优化MyBatis配置文件中的配置

    MyBatis之优化MyBatis配置文件中的配置 2017/9/30 MyBatis配置文件很重要,首先我们来看看MyBatis配置文件中的内容和顺序: 文件目录结构如下: 1.<proper ...

  2. mybatis 配置文件中,collection 和 association 的对应关系

    mybatis 配置文件中,collection 和 association 的对应关系  如下图所示:

  3. ❤️Mybatis开发中什么是多对一处理、一对多处理?

    ❤️Mybatis开发中什么是多对一处理.一对多处理? 什么是多对一: 对于学生而言,关联-多个学生关联一个老师(多对一) 对于老师而言,集合-一个老师有很多学生(一对多) SQL: CREATE T ...

  4. mybatis XML 中<if>、<choose>、<when>、<otherwise>等标签的使用?多条件查询该怎么处理?

    mybatis XML 中if.choose.when.otherwise等标签的使用 一般使用在多条查询,虽然也可以通过注解写,我比较菜,我不会. 一般多条查询怎么解决? 1.如果是单表间的多条件查 ...

  5. Mybatis xml中配置一对一关系association一对多关系collection

    Mybatis xml中配置一对一关系association&一对多关系collection 今天在配置一对一关系映射以及一对多关系映射的时候,把collection中应该使用的ofType配 ...

  6. Mybatis.xml中sql语句的转译

    Mybatis.xml中sql语句的转译

  7. mybatis接口中的方法重载_MyBatis的Mapper接口以及Example的实例函数及详解

    一.mapper接口中的方法解析 mapper接口中的函数及方法 方法 功能说明 int countByExample(UserExample example) thorws SQLException ...

  8. mybatis plus中的${ew.sqlSegment},${ew.sqlSelect},${ew.customSqlSegment}使用

    ew是mapper方法里的@Param(Constants.WRAPPER) Wrapper queryWrapper对象 首先判断ew.emptyOfWhere是否存在where条件,有的话再拼接上 ...

  9. ${ew.customSqlSegment}和${ew.sqlSegment}

    ${ew.customSqlSegment}在使用时,相当与 where + queryWrapper内的条件 ${ew.sqlSegment}相当于queryWrapper内的条件 具体使用明天再写

最新文章

  1. 程序员11月书讯 | 硅谷,梦开始的地方
  2. 字符串创建XML文档
  3. Forefront Client Security部署前准备
  4. 取得前九条之后的数据
  5. Java提升篇——equals()方法和“==”运算符
  6. Python学习之路:内置函数
  7. android 摄像头参数,获取Android设备上的详细的摄像头信息
  8. struts2的国际化(即实现网站整体中英文切换)实例
  9. php计算百分比加成_百分比计算器
  10. Linux学习总结(34)——Linux系统目录结构详解
  11. python建模 决策_决策树python建模中的坑 :ValueError: Expected 2D array, got 1D array instead:...
  12. [Java] 蓝桥杯ADV-205 算法提高 拿糖果
  13. LeetCode 51. N-Queens
  14. 教你炒股票15:没有趋势,没有背驰
  15. 《Java解惑》系列——02字符谜题——谜题11:最后的笑声(字符、字符串连接操作)
  16. 在AndroidMenifest动态引用ApplicationId,解决INSTALL_FAILURE_CONFLICTION_PROVIDER
  17. 学生党蓝牙耳机怎么选?四款性价比高的蓝牙耳机推荐
  18. 谷歌浏览器小恐龙作弊无敌代码
  19. 除了中国知网和谷歌文学还有哪些好的有权威的资源站?
  20. ROS+UR机械臂+Moveit+ 仿真与实体机械臂的使用+realsense相机+eye_handeye手眼标定(亲测有效)下

热门文章

  1. 因为计算机丢失uxtheme.dll,解决Win10开机提示uxtheme.dll丢失的方法
  2. 关于磁盘阵列和LVM
  3. 不可不知的“电子邮件”趣味历史!
  4. windows 网管总结
  5. 算法设计 凸多边形的三角剖分
  6. matlab正交余弦变换矩阵,7. matlab图像处理基础——几何变换+正交变换
  7. 罗永浩压倒了王自如,但世界涛声依旧
  8. 积极推进市域治理现代化,全力打造国家治理体系新局面
  9. Datawhale学习笔记-飞桨AI-Task02:头脑风暴:让人拍案叫绝的创意是如何产生的?
  10. 【WaterRemind】用Arduino与SSD1306做一款提醒喝水的杯垫(何同学同款)