gpt4 book ai didi

mybatis - 如何为 mybatis-spring-boot 映射 EnumOrdinalTypeHandler?

转载 作者:行者123 更新时间:2023-12-05 04:10:21 25 4
gpt4 key购买 nike

我现在使用 Mybatis 和 spring-boot。我没有添加mybatis-config.xml。我根据说明通过 application.properties 为数据源和 mybatis 进行所有配置 mybatis-spring-boot-autoconfigure如下

### Database Configuration
spring.datasource.url=jdbc:sqlserver://localhost;databaseName=mywebsite;catalogName=mywebsite
spring.datasource.username=sa
spring.datasource.password=root
spring.datasource.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver

### Mybatis Configurations ###
mybatis.type-aliases-package=com.mycom.myproducts.mywebsite.config.bean
mybatis.type-handlers-package=org.apache.ibatis.type.EnumOrdinalTypeHandler
mybatis.mapperLocations=classpath:mybatis/mapper/**/*.xml
mybatis.configuration.default-fetch-size=100
mybatis.configuration.default-statement-timeout=30

问题是mybatis can't map for my enum types and the error show

Caused by: org.apache.ibatis.executor.result.ResultMapException: Error attempting to get column 'gender' from result set.  
Cause: java.lang.IllegalArgumentException: No enum constant com.mycom.myproducts.mywebsite.config.bean.config.UserBean.Gender.0

这可以通过 mybatis-config.xml

    <typeHandlers>
<typeHandler handler="org.apache.ibatis.type.EnumOrdinalTypeHandler" javaType="com.mycom.myproducts.mywebsite.config.bean.config.UserBean$Gender"/>
</typeHandlers>

但我不知道如何通过 application.properties 文件来完成?

最佳答案

试试这个

package com.example.typehandler;
@MappedTypes({Gender.class})
public class GenderTypeHandler extends EnumOrdinalTypeHandler {}

mybatis.type-handlers-package=com.example.typehandler

    @Select(...)
@Results(value = {
@Result(property="gender", column="gender", typeHandler="com.example.typehandler.Gender"),
...

如果您使用@MappedTypes@Result 中的typeHandler 可能是不必要的。

关于mybatis - 如何为 mybatis-spring-boot 映射 EnumOrdinalTypeHandler?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44690789/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com