问题:数据库中变量类型为datetime,实体类中变量类型为localdatetime,在进行查询数据库操作时,查询结果不能进行自动映射,报错信息如下:
2022-07-29 14:43:08.705 ERROR [http-nio-83-exec-10] o.a.c.c.C.[.[localhost].[/].[dispatcherServlet] >> Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessApiUsageException: Error attempting to get column 'reportDate' from result set. Cause: java.sql.SQLFeatureNotSupportedException: getObject with type
; getObject with type; nested exception is java.sql.SQLFeatureNotSupportedException: getObject with type] with root cause
java.sql.SQLFeatureNotSupportedException: getObject with type
解决方案:
-
pom.xml文件中添加依赖
<dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-typehandlers-jsr310</artifactId> <version>1.0.1</version> </dependency>
-
对应实体类中添加注解
之后再进行数据库查询,可以将数据库中的datetime类型数据映射为实体类中localdatetime类型,但在进行查询结果返回时出错,报错信息如下:
2022-07-29 14:50:33.204 ERROR [http-nio-83-exec-6] com.**.**.**.util.Utils >> toJson
com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Java 8 date/time type `java.time.LocalDateTime` not supported by default: add Module "com.fasterxml.jackson.datatype:jackson-datatype-jsr310" to enable handling (through reference chain: java.util.ArrayList[0]->com.**.**.entity.Statisticv2Apps["reportdate"])
解决方法:
-
pom.xml文件中添加依赖
<dependency> <groupId>com.fasterxml.jackson.datatype</groupId> <artifactId>jackson-datatype-jsr310</artifactId> <version>2.13.0</version> </dependency>
-
进行模块注册
private static final ObjectMapper objectMapper = new ObjectMapper(); public static String toJson(Object data){ try { //模块注册 objectMapper.registerModule(new JavaTimeModule()); return data == null ? null : objectMapper.writeValueAsString(data); } catch (JsonProcessingException e) { LOGGER.error("toJson",e); return null; } }
更多推荐