본문 바로가기
프로그래밍/Spring boot

Mybatis Enum 사용

by oedpus 2019. 11. 15.

Mybatis 에 Enum을 사용 중 아래와 같은 에러를 만남.

 

nested exception is org.apache.ibatis.executor.result.ResultMapException: Error attempting to get column 'board_type' from result set. Cause: java.lang.IllegalArgumentException: No enum constant com.oedpus.enums.BoardType.1

org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.executor.result.ResultMapException: Error attempting to get column 'board_type' from result set. Cause: java.lang.IllegalArgumentException: No enum constant com.oedpus.enums.BoardType.1

 

에러 코드 상황.

 

Board.java (도메인 클래스)

package com.oedpus.domain;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import lombok.Builder;

import java.io.Serializable;
import java.time.LocalDateTime;

import com.oedpus.enums.BoardType;

@Setter
@Getter
@ToString
public class Board implements Serializable {
	private static final long serialVersionUID = 5611447741477129635L;

	private Long idx;
	private String title;
	private String sub_title;
	private String content;
	private BoardType board_type; // Enum
	private LocalDateTime created_date;
	private LocalDateTime updated_date;
	private String name;

	@Builder
	public Board(Long idx,String title, String sub_title, String content, BoardType board_type,
			LocalDateTime created_date, LocalDateTime updated_date, String name) {
		this.idx = idx;
		this.title = title;
		this.sub_title = sub_title;
		this.content = content;
		this.board_type = board_type;
		this.created_date = created_date;
		this.updated_date = updated_date;
		this.name = name;
	}
}

 

BoardType.java (Enum 타입)

package com.oedpus.enums;

public enum BoardType {
	NOTICE(1),
	FREE(2);
	
	private int value;
	
	BoardType(int value){
		this.value = value;
	}
	
	public int getValue() {
		return this.value;
	}
	
	public static BoardType valueOf(int value) {
		switch(value) {
			case 1: return BoardType.NOTICE;
			case 2: return BoardType.FREE;
			default:
				throw new AssertionError("unknowns value : " + value);
		}
	}
}

 

BoardMapper.xml (매퍼 파일)

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.oedpus.dao.BoardMapper">
	<resultMap type="com.oedpus.domain.Board" id="boardResultMap">
	 	<result column="idx" property="idx" />
	 	<result column="title" property="title" />
	 	<result column="sub_title" property="sub_title" />
	 	<result column="content" property="content" />
	 	<result column="board_type" property="board_type" />
	 	<result column="created_date" property="created_date" />
	 	<result column="updated_date" property="updated_date" />
	 	<result column="name" property="name" />
	</resultMap>
	<select id="selectBoard" resultMap="boardResultMap" parameterType="HashMap">
		select 
			t1.idx
			, title
			, sub_title
			, content
			, board_type
			, t1.created_date
			, t1.updated_date
			, t2.name
		from board t1
		inner join user t2
		on t1.user_idx = t2.idx
		<if test="searchTitle != null and searchTitle != ''">
			where title LIKE CONCAT('%', #{searchTitle}, '%')
		</if>
		order by idx desc
		LIMIT #{startItem}, #{pagePerItemCount};
	</select>
</mapper>

 

위와 같이 구현하였는데 에러 발생.

 

타입핸들러를 상속받아 구현해 주어야 함.

 

BoardTypeHandler.java

package com.oedpus.handlers;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;

import com.oedpus.enums.BoardType;

public class BoardTypeHandler extends BaseTypeHandler<BoardType>{

	@Override
	public void setNonNullParameter(PreparedStatement ps, int i, BoardType parameter, JdbcType jdbcType)
			throws SQLException {
		ps.setInt(i, parameter.getValue());
	}

	@Override
	public BoardType getNullableResult(ResultSet rs, String columnName) throws SQLException {
		return BoardType.valueOf(rs.getInt(columnName));
	}

	@Override
	public BoardType getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
		return BoardType.valueOf(rs.getInt(columnIndex));
	}

	@Override
	public BoardType getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
		return BoardType.valueOf(cs.getInt(columnIndex));
	}

}

 

mybatis 설정 파일 수정

application.yml

mybatis:
  type-handlers-package: com.oedpus.handlers

 

이후 정상적으로 데이터 출력.

 

'프로그래밍 > Spring boot' 카테고리의 다른 글

Spring boot + JSP  (0) 2019.09.20
Spring boot 에러 페이지 설정  (0) 2019.09.06
Spring boot + Mybatis  (0) 2019.09.04

댓글