1.controller层
// post请求
// 加@RequestBody注解时,前端json传参;不加时,前端以 form 表单传参
//注意: 加@RequestBody注解时,不传,此时userVo=null;当前端传 {} 时,userVo != null
@PostMapping("userList")
public R userList(@RequestBody(required = false) UserVo userVo) {
List<UserVo> userList = iTbUserService.getUserList(userVo);
return R.ok().put("DS", userList);
}
2.mapper层 (以下情况都会在特定情况下出现异常,所以需要定好规范)
注意:当加 @Param注解,xml 写法看 方式一(当前端传{} ,会有异常);
List<UserVo> getUserList(@Param(value = "userVo") UserVo userVo);
注意:当不加 @Param注解,xml 写法看 方式二(当前端不传,此时userVo为null时,会有异常);
List<UserVo> getUserList(UserVo userVo);
方式一:
<select id="getUserList" resultType="org.example.moudle.user.vo.UserVo"
parameterType="org.example.moudle.user.vo.UserVo">
SELECT
tb_user.id,
tb_user.user_name userName,
tb_user.user_pwd userPwd,
tb_user.code,
tb_user.freezing,
tb_org.org_name org
FROM
tb_user
LEFT JOIN tb_org ON tb_user.`code` = tb_org.`code`
where 1=1
<if test="userVo !=null and userVo !=''">
<if test="userVo.userName !=null and userVo.userName !=''">
and tb_user.user_name like CONCAT('%',#{userVo.userName},'%')
</if>
<if test="userVo.freezing == 1 or userVo.freezing == 0">
and tb_user.freezing = #{userVo.freezing}
</if>
<if test="userVo.org !=null and userVo.org !=''">
and tb_user.org_name = #{userVo.org}
</if>
</if>
</select>
方式二:(注意区别)
<select id="getUserList" resultType="org.example.moudle.user.vo.UserVo"
parameterType="org.example.moudle.user.vo.UserVo">
SELECT
tb_user.id,
tb_user.user_name userName,
tb_user.user_pwd userPwd,
tb_user.code,
tb_user.freezing,
tb_org.org_name org
FROM
tb_user
LEFT JOIN tb_org ON tb_user.`code` = tb_org.`code`
where 1=1
<if test="userName !=null and userName !=''">
and tb_user.user_name like CONCAT('%',#{userName},'%')
</if>
<if test="freezing == 1 or freezing == 0">
and tb_user.freezing = #{freezing}
</if>
<if test="org !=null and org !=''">
and tb_user.org_name = #{org}
</if>
</select>