ew是mapper方法里的@Param() Wrapper queryWrapper对象
首先判断是否存在where条件,有的话再拼接上去,是WHERE + sql语句
没有where的时候加上 == false
使用${} 如果是连表查询且查询条件是连表的字段则需在service层拼接查询条件时字段前指定别名
最佳例子1
<select resultType="">
SELECT
${} // 这里拼接select后面的语句
FROM
${table_name} //如果是单表的话,这里可以写死
${}
</select>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
mapper
IPage<LinkedHashMap<String,Object>> tableList(@Param("table_name") String table_name,
Page page,
@Param() QueryWrapper queryWrapper);
- 1
- 2
- 3
test
String responseField = "*";
(responseField);
// 即 select * ...
String responseField = "name";
(responseField);
// 即 select name ...
- 1
- 2
- 3
- 4
- 5
- 6
- 7
最佳例子2
controller
public String saveAddress(HttpSession session) {
UserVO user1 = (UserVO)("user");
LambdaQueryWrapper<User> lambdaQueryWrapper = Wrappers.<User>lambdaQuery()
.select(User::getNickName, User::getUserId) // 需要查询的列,即 ${}
.eq(User::getUserId, ());// 条件
User user = (lambdaQueryWrapper);
(user);
return null;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
mapper
User selectNickNameAndUserId(@Param() LambdaQueryWrapper<User> queryWrapper);
- 1
<select resultType="">
select
<if test="ew != null and != null and != ''">
${}
</if>
from
user
where is_deleted != 1
<if test="ew != null">
<if test="">
AND
</if>
${}
</if>
</select>
<select resultType="">
select
<if test="ew != null and != null and != ''">
${}
</if>
from
user
${}
</select>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
使用${} 如果是联表查询且查询条件是连表的字段则需在service层拼接查询条件时字段前指定别名,而且不能用lambda的查询了
<select resultType="">
SELECT ,
tp.perm_name,
,
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 = ${}
</select>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
mapper
List<RolePermsDTO> selectByRoleId(@Param() Wrapper<RolePermsDTO> wrapper);
@Test
public void test2(){
QueryWrapper<RolePermsDTO> wrapper = new QueryWrapper<>();
("tr.role_id", 1);
(wrapper);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10