数据库: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>
只要注意函数写法,就可以批量新增成功了。
本文详细解析了在使用MyBatis进行数据库操作时,遇到的批量插入日期类型字段的问题及其解决方案。通过调整SQL语句中的函数写法,成功解决了mybatis将时间类型属性自动转换为String类型的问题,实现了批量新增记录的功能。
645

被折叠的 条评论
为什么被折叠?



