반응형

오라클에서 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
반응형

MyBatis 사용 도중 아래와 같은 오류 메시지 발생.

도대체 뭐가 문제일까?

처음엔 아래와 같은 오류로 인해 상당히 혼란 스러웠다.

뭐가 문제지??

org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database.  Cause: java.lang.NumberFormatException: For input string: "N"
### Cause: java.lang.NumberFormatException: For input string: "N"

 

실제 이 오류를 발생한 xml파일 내의 쿼리는 아래와 같다.

다이나믹 쿼리 수행을 위해 추가한 if 절로 인해 발생하는 것이었다. 문법 상의 문제가 없는지 여러번 살폈으나 별다른 이상을 발견하지 못했고, 구글링 도중 발견한 글 중 하나가 바깥에 표기한 큰따옴표(")를 작은따옴표(')로 변경해 보라는 글을 발견했다. 

<select id="select_CuttingAndNoCutting" resultMap="CuttingResult" parameterType="java.util.Map">
		SELECT  cutting_date, nvl(cut, 0) as cut, nvl(nocut, 0) as nocut 
		  FROM ( select cutting_date, sum(order_length) as order_length, cutting_type 
				   from ( SELECT cutting_date, order_length, cutting_type 
							from cutting_order_circuit
						   where cutting_date like #{YYYYMM,jdbcType=VARCHAR} || '%'
						     and drum_no not like '%결품%' 
							 and drum_no is not null 

							<if test="isPowerCutter == 'Y'">  
							 and (actor = #{actor,jdbcType=VARCHAR}
							  or actor is null 
							 and powerorder = 'on')  
							 and powercomplete is null
							</if>
						
							<if test="isPowerCutter == 'N'">  
							 and actor = #{actor,jdbcType=VARCHAR}
							</if>
						
						) group by cutting_date, cutting_type
			   ) PIVOT ( sum(order_length) FOR cutting_type IN ('절단' as cut, '미절단' as nocut))       
		order by cutting_date
	</select>

혹시나 하는 마음에 큰따옴표와 작은따옴표의 위치를 변경한 결과 아무런 문제 없이 내가 예상했던 대로 쿼리가 수행되는 것이었다.

정말 어의없는 상황..이 문제로 상당한 시간을 허비했거늘...

오늘도 이렇게 어의없이 문제를 깔끔하게(?) 해결한 하루 였다.

내가 바꾼 건 따옴표 밖에는 없었다는..

<select id="select_CuttingAndNoCutting" resultMap="CuttingResult" parameterType="java.util.Map">
		SELECT  cutting_date, nvl(cut, 0) as cut, nvl(nocut, 0) as nocut 
		  FROM ( select cutting_date, sum(order_length) as order_length, cutting_type 
				   from ( SELECT cutting_date, order_length, cutting_type 
							from cutting_order_circuit
						   where cutting_date like #{YYYYMM,jdbcType=VARCHAR} || '%'
						     and drum_no not like '%결품%' 
							 and drum_no is not null 

							<if test='isPowerCutter == "Y"'>  
							 and (actor = #{actor,jdbcType=VARCHAR}
							  or actor is null 
							 and powerorder = 'on')  
							 and powercomplete is null
							</if>
						
							<if test='isPowerCutter == "N"'>
							 and actor = #{actor,jdbcType=VARCHAR}
							</if>
						
						) group by cutting_date, cutting_type
			   ) PIVOT ( sum(order_length) FOR cutting_type IN ('절단' as cut, '미절단' as nocut))       
		order by cutting_date
반응형

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

오라클 DB에서 BLOB 정보를 가져와서 보여주기  (0) 2022.07.20

+ Recent posts