This article mainly introduces to you the relevant information about how mybatis uses the date LocalDate and LocalDateTime of Java8. The article introduces it in detail through the sample code. It has certain reference learning value for everyone's study or work. Friends in need Let’s learn together with the editor below.
Preface
I believe everyone should know that in the entity Entity, you can use java.sql.Date, java.sql.Timestamp, java.util.Date to map to the date, timestamp, datetime and other fields of the database
However, the classes java.sql.Date, java.sql.Timestamp, and java.util.Date are not good Used, many methods are outdated.
There are some new APIs in Java8, LocalDate, LocalTime, LocalDateTime are very easy to use
By default, the time and date of java8 are not supported in mybatis. If used directly, the following error will be reported
Caused by: java.lang.IllegalStateException: No typehandler found for property createTime at org.apache.ibatis.mapping.ResultMapping$Builder.validate(ResultMapping.java:151) at org.apache.ibatis.mapping.ResultMapping$Builder.build(ResultMapping.java:140) at org.apache.ibatis.builder.MapperBuilderAssistant.buildResultMapping(MapperBuilderAssistant.java:382) at org.apache.ibatis.builder.xml.XMLMapperBuilder.buildResultMappingFromContext(XMLMapperBuilder.java:378) at org.apache.ibatis.builder.xml.XMLMapperBuilder.resultMapElement(XMLMapperBuilder.java:280) at org.apache.ibatis.builder.xml.XMLMapperBuilder.resultMapElement(XMLMapperBuilder.java:252) at org.apache.ibatis.builder.xml.XMLMapperBuilder.resultMapElements(XMLMapperBuilder.java:244) at org.apache.ibatis.builder.xml.XMLMapperBuilder.configurationElement(XMLMapperBuilder.java:116) ... 81 common frames omitted
The solution is as follows:
Add the following dependency directly
<dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-typehandlers-jsr310</artifactId> <version>1.0.1</version> </dependency>
After configuring this dependency, you can replace the Date in the Entity with LocalDate and LocalDateTime. Others do not need to be changed.
public class User { private Integer id; private String name; private LocalDate createDate; private LocalDateTime createTime; }
The above is only valid for testing in mybatis version 3.4.0
If used If the mybatis version is lower than 3.4.0, you also need to configure the following
<typeHandlers> <typeHandler handler="org.apache.ibatis.type.InstantTypeHandler" /> <typeHandler handler="org.apache.ibatis.type.LocalDateTimeTypeHandler" /> <typeHandler handler="org.apache.ibatis.type.LocalDateTypeHandler" /> <typeHandler handler="org.apache.ibatis.type.LocalTimeTypeHandler" /> <typeHandler handler="org.apache.ibatis.type.OffsetDateTimeTypeHandler" /> <typeHandler handler="org.apache.ibatis.type.OffsetTimeTypeHandler" /> <typeHandler handler="org.apache.ibatis.type.ZonedDateTimeTypeHandler" /> </typeHandlers>
Summary
The above is the detailed content of Examples of mybatis using Java8 dates LocalDate and LocalDateTime. For more information, please follow other related articles on the PHP Chinese website!