如何将 mybatis mapper 从 Java 迁移到 spring 中的 Kotlin?

How migrate mybatis mapper from Java to Kotlin in spring?

我有 spring-boot 应用程序和 MyBatis ORM。在 java 我写成

@Mapper
@Repository
public interface FooMapper {

    @SelectProvider(type = SqlProviderAdapter.class, method = "select")
    @Results(id = "resultFoo", value = {
            @Result(property = "rowId", column = "ROW_ID"),
            @Result(property = "created", column = "CREATED"),
            @Result(property = "lastUpdated", column = "LAST_UPD"),
            @Result(property = "fooId", column = "FOO_ID"),
    })
    Foo findBy(SelectStatementProvider selectStatement);

    @SelectProvider(type = SqlProviderAdapter.class, method = "select")
    @Results(value = {
            @Result(property = "rowId", column = "ROW_ID"),
            @Result(property = "created", column = "CREATED"),
            @Result(property = "lastUpdated", column = "LAST_UPD"),
            @Result(property = "fooId", column = "FOO_ID"),
    })
    List<Foo> findListBy(SelectStatementProvider selectStatement);

    @SelectProvider(type = SqlProviderAdapter.class, method = "select")
    @Results(value = {
            @Result(property = "fooId", column = "FOO_ID")
    })
    List<String> findFooListBy(SelectStatementProvider selectStatement);

    @InsertProvider(type = SqlProviderAdapter.class, method = "insert")
    @Options(useGeneratedKeys = true, keyProperty = "record.rowId", keyColumn = "row_id")
    int insert(InsertStatementProvider<Foo> insertStatement);

    @UpdateProvider(type = SqlProviderAdapter.class, method = "update")
    int update(UpdateStatementProvider updateStatement);
}

效果很好!

但我需要将我所有的应用程序转换为 Kotlin。 我尝试了以下代码:

@Mapper
@Repository
interface FooMapper {
    @SelectProvider(type = SqlProviderAdapter::class, method = "select")
    @Results(
        id = "resultReferralActivation",
        value = [Result(property = "rowId", column = "ROW_ID"), Result(
            property = "created",
            column = "CREATED"
        ), Result(property = "lastUpdated", column = "LAST_UPD"), Result(
            property = "fooId",
            column = "FOO_ID"
        )]
    )
    fun findBy(selectStatement: SelectStatementProvider?): Foo?

    @SelectProvider(type = SqlProviderAdapter::class, method = "select")
    @Results(
        value = [Result(property = "rowId", column = "ROW_ID"), Result(
            property = "created",
            column = "CREATED"
        ), Result(property = "lastUpdated", column = "LAST_UPD"), Result(
            property = "fooId",
            column = "FOO_ID"
        )]
    )
    fun findListBy(selectStatement: SelectStatementProvider?): List<Foo?>?

    @SelectProvider(type = SqlProviderAdapter::class, method = "select")
    @Results(value = [Result(property = "fooId", column = "FOO_ID")])
    fun findFooListBy(selectStatement: InsertStatementProvider?): List<String?>?

    @InsertProvider(type = SqlProviderAdapter::class, method = "insert")
    @Options(useGeneratedKeys = true, keyProperty = "record.rowId", keyColumn = "row_id")
    fun insert(insertStatement: InsertStatementProvider<Foo?>?): Int

    @UpdateProvider(type = SqlProviderAdapter::class, method = "update")
    fun update(updateStatement: UpdateStatementProvider?): Int
}

但是没用。如果我在 java 或 kotlin class 中尝试使用 awtowired FooMapper,应用程序在启动时失败,消息如下:找不到 bean FooMapper。如果我评论我的自动连接尝试,应用程序启动,但是当我检查 spring-bean-list 时,我看到 bean FooMapper 不存在。

如何将我的 java 映射器转换为 Konlin?

感谢您的帮助!

问题在于“想法”如何创建文件夹。 当我手动创建映射器搜索配置所针对的正确文件夹结构时,一切正常