【发布时间】:2021-06-04 15:07:50
【问题描述】:
我有带有 MyBatis ORM 的 spring-boot 应用程序。在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 中尝试使用 awtowired FooMapper 或 kotlin 类应用程序失败,并显示如下消息:找不到 bean FooMapper。如果我评论了我的自动装配尝试,应用程序就会启动,但是当我检查 spring-bean-list 时,我发现 bean FooMapper 不存在。
如何将我的 java 映射器转换为 Konlin?
感谢您的帮助!
【问题讨论】:
-
这里有一个在 Kotlin 中使用 MyBatis+Spring 的简单示例:github.com/mybatis/mybatis-dynamic-sql/tree/master/src/test/…。也许您需要在配置类上添加 @MapperScan 注释。
-
你找到解决办法了吗?
标签: java spring-boot kotlin mybatis spring-mybatis