简体   繁体   English

询问spring4.3.11 + mybatis 3.2 8 <TypeHandler>

[英]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. 我在Spring Web MVC环境中使用mybatis的Web项目出现问题。

The problem is this. 问题是这样的。 In DB modeling, the flag value is given as VARCHAR2. 在数据库建模中,标志值指定为VARCHAR2。 In VO.java, the primitive type is given as a boolean. 在VO.java中,原始类型以布尔值给出。 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. 因此,当我创建SqlSessionFactoryBean时,可以通过将typeHandlers传递给属性来改进它。

So when I try to code it 所以当我尝试编码时

YesNoBooleanTypeHandler.java 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. SqlSessionFactoryBean设置。

<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 ResultMap配置

 <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 嵌套异常为java.lang.IllegalStateException:无法将属性'typeHandlers [0]'的'java.lang.String'类型的值转换为所需的类型'org.apache.ibatis.type.TypeHandler':没有匹配的编辑器或找到转化策略

I can not run the server with an error .............. I need your advice. 我无法运行服务器时出现错误..............我需要您的建议。

The problem is that you specified typeHandlers in the SqlSessionFactoryBean incorrectly. 问题是您在SqlSessionFactoryBean指定的typeHandlers错误。 The typeHandlers is an array of TypeHandler s but you have provides a String value. typeHandlers是数组TypeHandler秒,但你必须提供一个String值。 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 . Spring无法实例化bean,因为它无法将字符串转换为TypeHandler

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM