我有个问题。谢谢你对英语缺乏经验的理解。
在spring环境中,我对使用mybatis的web项目有问题。
问题是这个。在DB建模中,标志值给出为VARCHAR2。在VO.java中,原语类型给出了一个布尔型。但是,在select操作中出现了一个错误。
因此,当我创建我的SqlSessionFactoryBean时,我可以通过将typeHandlers传递给属性来改进它。
所以当我试图对它进行编码时
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));
}
}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
<result column="MEMBER_ISWITHDRAWAL" property="memberIsWithdrawal" typeHandler="edu.kosta.kdc.util.YesNoBooleanTypeHandler" />我把它设成这样。
然后
嵌套例外是java.lang.IllegalStateException:无法将“java.lang.String”类型的值转换为属性“typeHandlers”所需的“org.apache.ibatis.type.TypeHandler”类型:没有找到匹配的编辑器或转换策略
我无法以错误运行服务器.我需要你的建议。
发布于 2018-12-14 10:42:31
问题是您在SqlSessionFactoryBean中指定了错误的SqlSessionFactoryBean。typeHandlers是一个TypeHandler的数组,但是您提供了一个String值。配置应如下所示:
<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。
https://stackoverflow.com/questions/53772630
复制相似问题