首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >询问spring4.3.11 4.3.11+mybatie3.2 8 <TypeHandler>

询问spring4.3.11 4.3.11+mybatie3.2 8 <TypeHandler>
EN

Stack Overflow用户
提问于 2018-12-14 02:09:29
回答 1查看 519关注 0票数 0

我有个问题。谢谢你对英语缺乏经验的理解。

在spring环境中,我对使用mybatis的web项目有问题。

问题是这个。在DB建模中,标志值给出为VARCHAR2。在VO.java中,原语类型给出了一个布尔型。但是,在select操作中出现了一个错误。

因此,当我创建我的SqlSessionFactoryBean时,我可以通过将typeHandlers传递给属性来改进它。

所以当我试图对它进行编码时

YesNoBooleanTypeHandler.java

代码语言:javascript
复制
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));
    }

}

SqlSessionFactoryBean设置.

代码语言:javascript
复制
<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

代码语言:javascript
复制
 <result column="MEMBER_ISWITHDRAWAL" property="memberIsWithdrawal" typeHandler="edu.kosta.kdc.util.YesNoBooleanTypeHandler" />

我把它设成这样。

然后

嵌套例外是java.lang.IllegalStateException:无法将“java.lang.String”类型的值转换为属性“typeHandlers”所需的“org.apache.ibatis.type.TypeHandler”类型:没有找到​​匹配的编辑器或转换策略

我无法以错误运行服务器.我需要你的建议。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-12-14 10:42:31

问题是您在SqlSessionFactoryBean中指定了错误的SqlSessionFactoryBeantypeHandlers是一个TypeHandler的数组,但是您提供了一个String值。配置应如下所示:

代码语言:javascript
复制
<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不能实例化bean,因为它不能将字符串转换为TypeHandler

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53772630

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档