mybatis-timestamp为空比较问题

问题描述

现象

### Error querying database.  Cause: java.lang.IllegalArgumentException: invalid comparison: java.util.Date and java.lang.String
### Cause: java.lang.IllegalArgumentException: invalid comparison: java.util.Date and java.lang.String

原因

在myBatis的xml文件中 写的 if 为空判断类型出错,把日期类型与空串作比较

<if test="startTime != null and startTime!=''">
	and equip.add_time >= #{startTime}
</if>
<if test="endTime != null and endTime!=''">
	and equip.add_time <= #{endTime}
</if>

在mybatis xml文件中进行if比较时时间类型或者数字型的可以为null判断即可,无需进行**’’**判断

解决方案

去掉 if判读中的 endtime!=’'即可

<if test="startTime != null">
	and equip.add_time >= #{startTime}
</if>
<if test="endTime != null">
	and equip.add_time <= #{endTime}
</if>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MyBatis提供了TypeHandler的机制来处理Java对象和数据库字段之间的转换。在mybatis-config.xml中配置TypeHandler可以通过以下步骤完成: 1. 定义自定义TypeHandler类,实现org.apache.ibatis.type.TypeHandler接口,并在类上使用@MappedTypes和@MappedJdbcTypes注解指定Java类型和对应的JDBC类型。 例如,定义一个将Java的LocalDateTime类型转换为数据库的TIMESTAMP类型的TypeHandler: ``` @MappedTypes(LocalDateTime.class) @MappedJdbcTypes(JdbcType.TIMESTAMP) public class LocalDateTimeTypeHandler implements TypeHandler<LocalDateTime> { @Override public void setParameter(PreparedStatement ps, int i, LocalDateTime parameter, JdbcType jdbcType) throws SQLException { ps.setTimestamp(i, Timestamp.valueOf(parameter)); } @Override public LocalDateTime getResult(ResultSet rs, String columnName) throws SQLException { Timestamp timestamp = rs.getTimestamp(columnName); return timestamp != null ? timestamp.toLocalDateTime() : null; } @Override public LocalDateTime getResult(ResultSet rs, int columnIndex) throws SQLException { Timestamp timestamp = rs.getTimestamp(columnIndex); return timestamp != null ? timestamp.toLocalDateTime() : null; } @Override public LocalDateTime getResult(CallableStatement cs, int columnIndex) throws SQLException { Timestamp timestamp = cs.getTimestamp(columnIndex); return timestamp != null ? timestamp.toLocalDateTime() : null; } } ``` 2. 在mybatis-config.xml中配置TypeHandler。 例如,将上面定义的LocalDateTimeTypeHandler配置为全局TypeHandler: ``` <configuration> <typeHandlers> <typeHandler handler="com.example.LocalDateTimeTypeHandler"/> </typeHandlers> </configuration> ``` 或者,将LocalDateTimeTypeHandler配置为特定字段的TypeHandler: ``` <resultMap id="orderResultMap" type="Order"> <id property="id" column="id"/> <result property="createTime" column="create_time" typeHandler="com.example.LocalDateTimeTypeHandler"/> </resultMap> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值