mybatis 传参实体类遇到的问题(xml非空判断)

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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值