mybatis 字符串转时间类型


有时候前端传过来的时间是字符串类型的,如果直接使用,会报错。
如在XML里这样使用:select * from table where create_time >= #{startTime},其中startTime为前端传过来的时间。
在运行时会报错:

              ERROR: operator does not exist: timestamp without time zone > character varying

解决办法:使用时间转换函数奖字符串转为时间类型:

to_date(#{startTime},'YYYY-MM-DD HH24:MI:SS')
上面是sql就可以写为:
select * from table where create_time >= to_date(#{startTime},'YYYY-MM-DD HH24:MI:SS') 这样就不会报错了

需要注意的是:上面的时间格式为:'YYYY-MM-DD HH24:MI:SS' ,HH24代表使用24小时,如果不加24,就代表使用12小时制,这个时候如果传过来startTime 的值为‘2019-06-15 00:00:00’,
这个时候也会报错:org.postgresql.util.PSQLException: ERROR: hour "0" is invalid for the 12-hour clock

另外不像java的时间格式可以写为 ‘YYYY-MM-dd HH:mm:SS’,mybatis的xml如果写为这样也会报错:
  Cause: org.postgresql.util.PSQLException: ERROR: conflicting values for "MM" field in formatting string

所以记得最好写成: 'YYYY-MM-DD HH24:MI:SS'

end


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM