资讯详情

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

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");          LambdaQueryrapper<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>
        from
            user
        where 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>
        from
            user
        ${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 roleName
        FROM tb_role tr
                     LEFT JOIN tb_perm_role tpr ON tr.role_id = tpr.role_id
                     LEFT JOIN tb_perm tp ON tpr.perm_id = tp.id ${ew.customSqlSegment}
    </select>

mapper

 List<RolePermsDTO> selectByRoleId(@Param(Constants.WRAPPER) Wrapper<RolePermsDTO> wrapper);
 @Test
    public 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 user
		set ${ 
        ew.sqlSet}
		where ${ 
        ew.sqlSegment}
    </update>

标签: 1ew丝印三极管

锐单商城拥有海量元器件数据手册IC替代型号,打造 电子元器件IC百科大全!

 锐单商城 - 一站式电子元器件采购平台  

 深圳锐单电子有限公司