Mybatis를 이용해 데이터베이스를 처리하던 중에 날짜 타입을 Date클래스에서 LocalDate클래스로 타입을 변경하니 에러가 발생했다.
birthday 필드의 데이터가 Null이라 데이터베이스에 매핑시키지 못했다.
Mybatis의 버전이 낮아서 LocalDate클래스의 타입을 인식하지 못해서 에러가 발생한 것이었다.
MyBatis는 버전 3.4.5부터 기본적으로 JSR-310(Date and Time API)을 지원한다고 한다.
3.4.5 이상의 버전을 사용하면서 JSR-310 의존성 주입을 하면 자동으로 매핑해준다.
또는 TypeHandler를 정의해야 한다.
3.4.5 이상 버전에서 설정 방법
pom.xml에 의존성 추가
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-typehandlers-jsr310</artifactId>
<version>1.0.2</version>
</dependency>
상위 버전으로 올리고 jsr310 의존성을 추가하면 끝날 줄 알았다.
왜 그런진 모르겠지만 mybatis 3.5.4 버전을 사용하고도 계속 필드를 매핑하지 못했다.
결국 TypeHandler를 정의했다.
TypeHandler 정의하는 방법
mybatis-config.xml 파일을 생성
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"HTTP://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<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" />
<typeHandler handler="org.apache.ibatis.type.YearTypeHandler" />
<typeHandler handler="org.apache.ibatis.type.MonthTypeHandler" />
<typeHandler handler="org.apache.ibatis.type.YearMonthTypeHandler" />
<typeHandler handler="org.apache.ibatis.type.JapaneseDateTypeHandler" />
</typeHandlers>
</configuration>
mybatis를 설정한 context.xml에 property 추가.
<property name="configLocation" value="classpath:/mybatis/mybatis-config.xml"/>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:/mybatis/mybatis-config.xml"/>
<property name="mapperLocations" value="classpath:/mepper/*.xml"/>
</bean>
https://github.com/mybatis/typehandlers-jsr310
mybatis/typehandlers-jsr310
MyBatis type handlers for JSR-310. Contribute to mybatis/typehandlers-jsr310 development by creating an account on GitHub.
github.com
https://stackoverflow.com/questions/25113579/java-8-localdate-mapping-with-mybatis
Java 8 LocalDate mapping with mybatis
I am using java.time.LocalDate (Java 8) to represent some of the member fields in a Java class. class Test { private LocalDate startDate; private LocalDate endDate; //other fields //
stackoverflow.com
https://mybatis.org/mybatis-3/configuration.html#typeHandlers
mybatis – MyBatis 3 | Configuration
Configuration The MyBatis configuration contains settings and properties that have a dramatic effect on how MyBatis behaves. The high level structure of the document is as follows: configuration properties These are externalizable, substitutable properties
mybatis.org
'Spring > Spring Framework' 카테고리의 다른 글
[에러]Class<SpringJUnit4ClassRunner> cannot be resolved to a type- 모듈 추가 (0) | 2020.04.17 |
---|