mybatisPlus版本3.2.0
3.0以上默认时间为LocalDateTime ,在使用sql查询的时候会出现
druid版本
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druid-spring-boot-starterartifactId>
<version>1.1.9version>
dependency>
@ApiModelProperty(value = "添加时间")
private LocalDateTime createTime;
/**
* 更新时间
*/
@ApiModelProperty(value = "更新时间")
private LocalDateTime updateTime;
查询最后报错
org.springframework.dao.InvalidDataAccessApiUsageException: Error attempting to get column 'create_time' from result set. Cause: java.sql.SQLFeatureNotSupportedException
; null; nested exception is java.sql.SQLFeatureNotSupportedException
在映射的result里面或者resultMap里面配置
@Result(column="create_time", property="createTime",javaType =
LocalDateTime.class,jdbcType = JdbcType.DATE),
@Result(column="update_time", property="updateTime",javaType =
LocalDateTime.class,jdbcType = JdbcType.DATE),
失败
@Result(column="create_time", property="createTime",javaType = LocalDateTime.class,
jdbcType = JdbcType.DATE,typeHandler = MyLocalDateTimeTypeHandler.class),
@Result(column="update_time", property="updateTime",javaType = LocalDateTime.class,
jdbcType = JdbcType.DATE,typeHandler = MyLocalDateTimeTypeHandler.class),
这里的MyLocalDateTimeTypeHandler是自己的转换类,继承的是mybatis的转换类
public class MyLocalDateTimeTypeHandler extends LocalDateTimeTypeHandler {
@Override
public LocalDateTime getResult(ResultSet rs, String columnName) throws SQLException {
Object object = rs.getObject(columnName);
System.out.println(object);
if(object instanceof java.sql.Timestamp){//在这里强行转换,将sql的时间转换为LocalDateTime
return LocalDateTime//可以根据自己的需要进行转化
.ofInstant(((Timestamp)object).toInstant(), ZoneOffset.ofHours(0));
}
return super.getResult(rs, columnName);
}
}
解决办法可行
在网上搜索的半天,发现是druid的问题,于是把源码拉下来了,自己打包
<dependency>//自己打包的版本
<groupId>com.gitgroupId>
<artifactId>druid-spring-boot-starterartifactId>
<version>1.1.20version>
dependency>
<dependency>
<groupId>com.gitgroupId>
<artifactId>druidartifactId>
<version>1.1.20version>
dependency>
刚准备改进一下,进行rq发现问题已经解决,
于是将版本切换到1.1.20,发现卧槽,还真的有,问题解决
仅此记录。
本文为作者原创,转载请申请!