javaspringmybatistypehandler

Ask about spring4.3.11 + mybatis 3.2 8 <TypeHandler>


I have a question. Thank you for your understanding that English is inexperienced.

I have a problem with my web project using mybatis in spring web mvc environment.

The problem is this. In DB modeling, the flag value is given as VARCHAR2. In VO.java, the primitive type is given as a boolean. However, there was an error during the select operation.

So, when I created my SqlSessionFactoryBean, I could improve it by passing typeHandlers to the property.

So when I try to code it

YesNoBooleanTypeHandler.java

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

import javax.servlet.annotation.HandlesTypes;

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

@MappedJdbcTypes(value = JdbcType.VARCHAR)
@MappedTypes(Boolean.class)
public class YesNoBooleanTypeHandler extends BaseTypeHandler<Boolean> {

    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, Boolean parameter, JdbcType jdbcType)
            throws SQLException {
        ps.setString(i, parameter ? "true" : "false");

    }

    @Override
    public Boolean getNullableResult(ResultSet rs, String columnName) throws SQLException {

        return rs.getString(columnName) != null && "true".equalsIgnoreCase(rs.getString(columnName));
    }

    @Override
    public Boolean getNullableResult(ResultSet rs, int columnIndex) throws SQLException {

        return rs.getString(columnIndex) != null && "true".equalsIgnoreCase(rs.getString(columnIndex));
    }

    @Override
    public Boolean getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {

        return cs.getString(columnIndex) != null && "true".equalsIgnoreCase(cs.getString(columnIndex));
    }

}

The SqlSessionFactoryBean setting.

<beans:bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <beans:property name="dataSource" ref="dataSource" />
    <beans:property name="mapperLocations" value="classpath:edu/kosta/kdc/mapper/*Mapper.xml" />
    <beans:property name="typeAliasesPackage" value="edu.kosta.kdc.model.dto" />
    <beans:property name="typeHandlers" value="edu.kosta.kdc.util.YesNoBooleanTypeHandler"/>
</beans:bean>

ResultMap config

 <result column="MEMBER_ISWITHDRAWAL" property="memberIsWithdrawal" typeHandler="edu.kosta.kdc.util.YesNoBooleanTypeHandler" />

I set it up like this.

Then

nested exception is java.lang.IllegalStateException: Can not convert value of type 'java.lang.String' to required type 'org.apache.ibatis.type.TypeHandler' for property 'typeHandlers [0]': no ​​matching editors or conversion strategy found

I can not run the server with an error .............. I need your advice.


Solution

  • The problem is that you specified typeHandlers in the SqlSessionFactoryBean incorrectly. The typeHandlers is an array of TypeHandlers but you have provides a String value. The configuration should be like this:

    <beans:bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <beans:property name="dataSource" ref="dataSource" />
        <beans:property name="mapperLocations" value="classpath:edu/kosta/kdc/mapper/*Mapper.xml" />
        <beans:property name="typeAliasesPackage" value="edu.kosta.kdc.model.dto" />
        <beans:property name="typeHandlers">
                <array>
                    <beans:bean class="edu.kosta.kdc.util.YesNoBooleanTypeHandler" />
                </array>
        </beans:property>
    </beans:bean>
    

    Spring cannot instantiate the bean because it cannot convert the string to TypeHandler.