CustomTypeHandler.java

마이바티스에서 VO를 이용해 결과를 받을떄 Boolean 타입에 대한 설정

 

 

package com.cheezred.common.mybatis;

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

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

public class CustomTypeHandler implements TypeHandler<Boolean> {

   public Boolean getResult(ResultSet rs, String columnName) throws SQLException {
        String s = rs.getString(columnName);

        return parseBoolean(s);
    }

    public Boolean getResult(ResultSet rs, int columnIndex) throws SQLException {
        String s = rs.getString(columnIndex);

        return parseBoolean(s);
    }

    public Boolean getResult(CallableStatement cs, int columnIndex)
        throws SQLException {
        String s = cs.getString(columnIndex);

        return parseBoolean(s);
    }

    public void setParameter(PreparedStatement ps, int i, Boolean bool,
        JdbcType jdbcType) throws SQLException {

        ps.setString(i, parseString(bool));
    }

    private boolean parseBoolean(String s) {
        if (s == null) {
            return false;
        }

        s = s.trim().toUpperCase();

        if (s.length() == 0) {
            return false;
        }

        return "Y".equals(s);
    }

    private String parseString(Boolean bool) {
        return (bool != null && bool == true) ? "Y" : "N";
    }

}

 

 

클래스를 만든후

쿼리가 들어있는 XML에서

 

<resultMap type="com.cheezred.common.OutputVo"   id="OutputVo">
      <result property="trueFalseValue" column="trueFalseValue" typeHandler="com.cheezred.common.mybatis" />

 select

      ....

....

....

 

 

 

이런 형식으로 사용하면 됩니다.

+ Recent posts