VUE + ElementUI传入后端的值为null或者“ “(空值)时后端怎么处理--java后端
1.在java开发中前端传入后端的值为空时常常出现空指针异常,这种情况往往是前端传入控制,后端使用toString()进行转换时出现空指针异常2.如果使用String.valueOf()进行转换时得到的结果是“null”,所以在判断的时候需要进行判断,具体请看如下代码:if (String.valueOf(params.get("pId")) != "null" && String
·
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>
更多推荐
已为社区贡献3条内容
所有评论(0)