MyBatis Spring Boot自定义类型处理程序 [英] MyBatis Spring Boot custom type handler

查看:118
本文介绍了MyBatis Spring Boot自定义类型处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要有关Spring Boot和MyBatis集成的帮助.我对自定义BaseTypeHandler有问题.我已经创建了一个映射器:

I need help with Spring Boot and MyBatis integration. I have an issue with custom BaseTypeHandler. I've created a mapper:

@MappedTypes({LocalDateTime.class})
public class LocalDateTimeHandler extends BaseTypeHandler<LocalDateTime> {

我添加了一个类型处理程序:

I've added a type handler:

sqlSessionFactory.setTypeHandlers(new LocalDateTimeHandler[]{new LocalDateTimeHandler()});

还有下一个错误:

org.apache.ibatis.executor.ExecutorException: No constructor found in com.some.space.SomeObject matching [java.lang.Integer, java.sql.Timestamp, java.sql.Timestamp]

SomeObject的外观如下:

Where SomeObject looks like:

public class SomeObject {
    private Long id;
    private LocalDateTime created;
    private LocalDateTime updated;

    public SomeObject(Integer id, LocalDateTime created, LocalDateTime updated){
    //..........
    }
}

我使用的是mybatis-spring和spring-boot-starter-web版本1.3.2.

I using mybatis-spring and spring-boot-starter-web version 1.3.2.

有关使用TypeHandlers的所有示例均在XML配置上,但是我需要使用Java配置方式.我在做什么错了?

All examples about working with TypeHandlers are on the XML configuration, but I need to use Java configs way. What I'm doing wrong?

UPD:

我的映射器:

@Component
@Mapper
public interface SomeObjectRepository {

    @Select("SELECT * FROM some_objects")
    @Results(value = {
            @Result(property = "created", column = "created_date", typeHandler = LocalDateTimeTypeHandler.class, jdbcType = JdbcType.TIMESTAMP),
            @Result(property = "updated", column = "updated_date", typeHandler = LocalDateTimeTypeHandler.class, jdbcType = JdbcType.TIMESTAMP)
    })
    List<SomeObject> getAll();
}

推荐答案

您尚未指示mybatis将类型处理程序用于时间戳字段.因此,它将使用该JDBC类型的默认类型处理程序来转换数据库中的时间戳记字段.

You haven't instructed mybatis to use your type handler for timestamp fields. So it converts timestamp fields from the database using default type handler for that JDBC type.

如果只想在某些查询中执行此操作,请对xml映射执行以下操作:

If you want to do this only in some queries do like this for xml mapping:

<result property="created" column="created"
    typeHandler="com.mycompany.myapp.LocalDateTimeHandler"/>

或通过注释:

@Result(property = "created", column = "created",
        typeHandler=LocalDateTimeHandler.class)

如果要使其全局化,并将其用于特定JDBC类型的所有字段,请向您添加@MappedJdbcTypes TypeHandler:

If you want to make it global and use it for all fields of the specific JDBC type add @MappedJdbcTypes to you TypeHandler:

@MappedJdbcTypes({JdbcType.TIMESTAMP})
@MappedTypes({LocalDateTime.class})
public class LocalDateTimeHandler extends BaseTypeHandler<LocalDateTime> {

根据使用的mybatis版本,可能需要在@MappedJdbcTypes注释上设置includeNullJdbcType=true.

Depending on the version of mybatis you are using you may need to set includeNullJdbcType=true on the @MappedJdbcTypes annotation.

有关详细信息,请参见文档.

See documentation for details about this.

这篇关于MyBatis Spring Boot自定义类型处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆