数据库:postgres
字段类型为:DATE 、TIMESTAMP
在新增一条记录时,入参为Date类型,可以顺利新增成功。
但是批量新增时会出现错误:
原因:mybatis 自动将时间类型属性,转换成了 String 类型,所以导致插入失败。
解:
<insert id="insertSchedules" parameterType="HrScheduleRO"> INSERT INTO hr_schedule ( id, union_id, employee_id, dept_id, post_id, schedule_date, the_year, week_of_year, create_by, create_date, update_by, update_date, remarks, del_flag, affiliation, affiliated_department ) <foreach collection="scheduleList" item="al" separator="union"> SELECT #{al.id}, #{al.unionId}, #{al.employeeId}, #{al.deptId}, #{al.postId}, DATE '${al.scheduleDate}',--这里用函数转一下,注:使用函数的入参要注意写法 #{al.theYear}, #{al.weekOfYear}, #{al.createBy}, now(), #{al.updateBy}, now(), #{al.remarks}, '0', #{al.affiliation}, #{al.affiliatedDepartment} </foreach> </insert>
只要注意函数写法,就可以批量新增成功了。