똑같은 삽질은 2번 하지 말자

Java(Enum) → DB(Int) 본문

Spring

Java(Enum) → DB(Int)

곽빵 2020. 6. 16. 16:08
<mapper namespace="com.board.mapper.BoardMapper">
	<resultMap type="com.board.dto.BoardVO" id="board">
		<result column="b_kind" property="b_kind" typeHandler="org.apache.ibatis.type.EnumOrdinalTypeHandler"/>
		<result column="b_code" property="b_code"/>
		<result column="b_title" property="b_title"/>
		<result column="b_content" property="b_content"/>
		<result column="b_indate" property="b_indate"/>
		<result column="b_hit" property="b_hit"/>
		<result column="b_like" property="b_like"/>
		<result column="b_writer" property="b_writer"/>
		
	</resultMap>

	<select id="getAllBoardList" resultMap="board">
		select * from board
	</select>
	
	<select id="getPagingBoardList" resultMap="board" parameterType="utils.CriteriaVO">
		SELECT P.* 
		FROM (SELECT * FROM BOARD 
		         WHERE B_TITLE LIKE CONCAT('%',#{searchKeyword},'%') 
		         ORDER BY B_CODE ASC) AS P
		LIMIT #{perPageNum} OFFSET #{pageStart}
	</select>
	
	<select id="getTotalRecord" resultType="Integer">
		select count(*) from board
	</select>
	
	<insert id="save" parameterType="com.board.dto.BoardVO">
		INSERT INTO BOARD(b_title,b_content,b_writer,b_kind) 
		VALUES(
			#{b_title},
			#{b_content},
			#{b_writer},
			#{b_kind, typeHandler = org.apache.ibatis.type.EnumOrdinalTypeHandler}
		)
	</insert>
	
	<delete id="delete" parameterType="int">
		DELETE FROM BOARD WHERE B_CODE = #{b_code}
	</delete>
	
	<select id="findOne" parameterType="int" resultMap="board">
		SELECT * FROM BOARD WHERE B_CODE = #{b_code}
	</select>
	
	<update id="updateHit" parameterType = "int" >
		UPDATE BOARD SET B_HIT = B_HIT + 1 WHERE B_CODE = #{b_code}
	</update>

</mapper>​
public enum BoardKind {
	FreeBoard(1),
	MemberBoard(2),
	AdminBoard(3);
	
	private final int kind;
	
	BoardKind(int kind){
		this.kind = kind;
	}
	
	public int getKind() {
		return kind;
	}
	public static BoardKind valueOf(int kind) {
		switch(kind) {
		case 1: 
			return FreeBoard;
		case 2: 
			return MemberBoard;
		case 3: 
			return AdminBoard;
		default :
			throw new AssertionError("Unknown kind" + kind);
		}
	}
}

DBとJavaのIndexの番号が合わなくてエラーができました。

 

 

 

Comments