日期數據類型為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>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM