VUE + ElementUI传入后端的值为null或者“ “(空值)时后端怎么处理--java后端

1.在java开发中前端传入后端的值为空时常常出现空指针异常,这种情况往往是前端传入控制,后端使用toString()进行转换时出现空指针异常
2.如果使用String.valueOf()进行转换时得到的结果是“null”,所以在判断的时候需要进行判断,具体请看如下代码:

if (String.valueOf(params.get("pId")) != "null" && String.valueOf(params.get("pId")) != "") {
     //String pId = String.valueOf(params.get("pId"));
     String pId = params.get("pId").toString();
     dyFormVO.setpId(pId);
 }

 if (String.valueOf(params.get("applicant")) != "null" && String.valueOf(params.get("applicant")) != "") {
     //String applicant = String.valueOf(params.get("applicant"));
     String applicant = params.get("applicant").toString();
     dyFormVO.setApplicant(applicant);
 }

但是传入后端保存到数据库是存的值可能是字符串“null”,在后端处理后,但是还有有存入“null”的情况。
这是是添加或者更新时需要注意的
我采用的方法是在mybatis中加入条件判断,insert标签中需要使用if条件判断

<insert id="insert" useGeneratedKeys="true" keyProperty="id">
    insert into sys_user(
    user_name,user_password,
    <if test="userEmail != null and userEmail !=''">
        user_email,
    </if>
    user_info,head_img,create_time)
    values(#{userName},#{userPassword},
    <if test="userEmail != null and userEmail !=''">
        #{userEmail},
    </if>
    #{userInfo},#{headImg},#{createTime})
</insert>

上面的表示的是:如果插入的字段为空值时需要在两个地方加入判断条件,而且需要对应。

表:
sys_user(
    user_name,user_password,
    <if test="userEmail != null and userEmail !=''">
        user_email,
    </if>
    user_info,head_img,create_time
    )
values:
values(#{userName},#{userPassword},
    <if test="userEmail != null and userEmail !=''">
        #{userEmail},
    </if>
    #{userInfo},#{headImg},#{createTime})

如果不太理解可以参考mysql的插入语法。
下面是我的程序代码,请参考,有意见请反馈!

<insert id="saveFormData" parameterType="com.pdm.dproject.domain.DyFormVO" useGeneratedKeys="true" keyProperty="id">
      insert into
        dproject_figurenum
	     (
			<if test="module != null and module != 'null' and module != ''">
			  module,
			</if>
			<if test="subType != null and subType != 'null' and subType != ''">
			  sub_type,
			</if>
			<if test="card != null and card != 'null' and card != ''">
				card,
			</if>
			pro_name,
			<if test="pId != null and pId != 'null' and pId != ''">
				p_id,
			</if>
			<if test="applicant != null and applicant != 'null' and applicant != ''">
				applicant,
			</if>
			<if test="applyDate != null and applyDate != 'null' and applyDate != ''">
				apply_date,
			</if>
			fig_num
	    )
	    values
	      (
			<if test="module != null and module != 'null' and module != ''">
				#{module},
			</if>
			<if test="subType != null and subType != 'null' and subType != ''">
				#{subType},
			</if>
			<if test="card != null and card != 'null' and card != ''">
				#{card},
			</if>
			#{pName},
			<if test="pId != null and pId != 'null' and pId != ''">
				#{pId, jdbcType=VARCHAR},
			</if>
			<if test="applicant != null and applicant != 'null' and applicant != ''">
				#{applicant, jdbcType=VARCHAR},
			</if>
			<if test="applyDate != null and applyDate != 'null' and applyDate != ''">
				#{applyDate},
			</if>
		    #{figNum}
	      )
</insert>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值