myBatis Mapping 时间类型入参新增

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

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

数据库: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>

 

只要注意函数写法,就可以批量新增成功了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值