日期数据类型为Date ,前台传递喂String的后台处理

方法一:

在实体类里面将set方法里面将数据类型转为Date

 public void setBirth(String birth) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            this.birth = sdf.parse(birth);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

这样就可以了

注意在修改是判断是否为空的时候不能写birth!=“”,否则报java.util.Date and java.lang.String 的错

<if test="birth!= null">
                birth = #{birth},
            </if>

 

方法二:

实体类改为String类型,在xml里面改数据类型

<insert id="insert" parameterType="实体类">
        INSERT INTO
        table(id,idtype,idno,name,sex,phone,birth,nation,degree,native_type,native_place,address,first_job_year,emp_type,emp_form,start_date)
            VALUES(#{id},#{idtype},#{idno},#{name},#{sex},#{phone},DATE_FORMAT(#{birth},'%Y-%m-%d'),#{nation},#{degree},#{nativeType},#{nativePlace},#{address},#{firstJobYear},#{empType},#{empForm},DATE_FORMAT(#{startDate},'%Y-%m-%d'))
    </insert>-->
<update id="update" parameterType="实体类">
        update table
        <trim prefix="SET" suffixOverrides="," suffix="WHERE id = #{id}" >
        <if test="idtype!= null and idtype != ''">
                idtype = #{idtype},
            </if>
            <if test="idno!= null and idno != ''">
                idno = #{idno},
            </if>
            <if test="name!= null and name != ''">
                name = #{name},
            </if>
            <if test="sex!= null and sex != ''">
                sex = #{sex},
            </if>
            <if test="phone!= null and phone != ''">
                phone = #{phone},
            </if>
            <if test="birth!= null and birth != ''">
                birth = DATE_FORMAT(#{birth},'%Y-%m-%d'),
            </if>
            <if test="nation != null and nation != ''">
                nation = #{nation},
            </if>
            <if test="degree!= null and degree != ''">
                degree = #{degree},
            </if>
            <if test="nativeType!= null and nativeType != ''">
                native_type = #{nativeType},
            </if>
            <if test="nativePlace!= null and nativePlace != ''">
                native_place = #{nativePlace},
            </if>
            <if test="address!= null and address != ''">
                address = #{address},
            </if>
            <if test="firstJobYear!= null and firstJobYear != ''">
                first_job_year = #{firstJobYear},
            </if>
            <if test="empType!= null and empType != ''">
                emp_type = #{empType},
            </if>
            <if test="empForm!= null and empForm != ''">
                emp_form = #{empForm},
            </if>
            <if test="startDate!= null and startDate != ''">
                start_date = DATE_FORMAT(#{startDate},'%Y-%m-%d'),
            </if>
        </trim>
    </update>

 

转载于:https://www.cnblogs.com/NCL--/p/9273960.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值