반응형

오라클에서 BLOB 정보를 가져오는 쿼리를 MyBatis를 이용해서 작성하는 경우 BLOB 정보를 Map 형태로 바로 받으면 안되며, 반드시 ResultMap으로 받아야 한다.

<resultMap type="java.util.Map" id="imgResult">
	<result column="IMG" property="IMG" javaType="[B" jdbcType="BLOB"/>
</resultMap>

Select 절에서는 resultType이 아닌 resultMap을 이용해서 imgResult로 설정해야 한다.

<select id="select_img" parameterType="java.util.Map" resultMap="imgResult">
	select img from img_table where id = #{id}
</select>

이렇게 가져온 이미지를 컨트롤 단에서 사용 방법은 아래와 같다.

Map<String, Object> imgMap = imageService.select_img(map);
byte[] arr = (byte[]) imgMap.get("IMG");

String base64Encode = null;
if (arr != null && arr.length > 0) {
	base64Encode = byteToBase64(arr);
    base64Encode = "data:image/png;base64," + base64Encode;
}

private static String byteToBase64(byte[] arr) {
	String result = "";
    
    try {
    	result = Base64Utils.encodeToString(arr);
    } catch (Exception e) {
    	e.printStackTrace();
    }
    return result;
}

이렇게 가져온 base64Encode 의 값을 JSP 또는 HTML의 img 태그에 src로 할당하면 이미지를 브라우저에서 바로 볼 수 있게 된다.

반응형

'Java 기반 Web > Java' 카테고리의 다른 글

MyBatis 에서 어의 없는 오류..ㅠㅠ  (0) 2022.05.28

+ Recent posts