据我所知,我没有做任何不寻常的事。我有一个spring引导应用程序,它使用mybatis:
implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.1.1'我有一个application.properties配置,这个配置非常简单:
## MyBatis ##
mybatis.configuration.map-underscore-to-camel-case=true
mybatis.configuration.default-statement-timeout=30我的数据库表如下所示:
CREATE TABLE workspace_external_references (
id CHAR(36) PRIMARY KEY,
workspace_id CHAR(36) NOT NULL,
site VARCHAR(255) NOT NULL,
external_id VARCHAR(255) NOT NULL,
created_at DATETIME(6) NOT NULL DEFAULT NOW(6),
updated_at DATETIME(6) NOT NULL DEFAULT NOW(6),
FOREIGN KEY (workspace_id) REFERENCES workspaces (id) ON DELETE CASCADE
)只有一个像这样的条目:
'a907c0af-216a-41e0-b16d-42107a7af05f', 'e99e4ab4-839e-405a-982b-08e00fbfb2d4', 'ABC', '6', '2020-06-09 00:19:20.135822', '2020-06-09 00:19:20.135822'在mapper文件中,我选择了如下所示的所有引用:
@Select("SELECT * FROM workspace_external_references WHERE workspace_id = #{workspaceId}")
List<WorkspaceExternalReference> findByWorkspace(@Param("workspaceId") final UUID workspaceId);它应该映射到的java对象如下所示:
public class WorkspaceExternalReference {
private UUID id;
private UUID workspaceId;
private Sites site;
private String externalId;
private LocalDateTime createdAt;
private LocalDateTime updatedAt;
public WorkspaceExternalReference(
final Sites site,
final UUID workspaceId,
final String externalId) {
this.site = site;
this.workspaceId = workspaceId;
this.externalId = externalId;
}
}
public enum Sites {
ABC, XYZ;
}Sooooo为什么不能工作?,我得到了这个错误:
Caused by: org.apache.ibatis.executor.result.ResultMapException: Error attempting to get column 'id' from result set. Cause: java.lang.IllegalArgumentException: No enum constant com.acme.Sites.a907c0af-216a-41e0-b16d-42107a7af05f发布于 2020-06-09 03:39:30
当没有默认构造函数时,您需要让MyBatis知道哪些列要显式地传递给构造函数(在大多数情况下)。
有了注释,它将如下所示。
您可以在XML中使用<resultMap>和<constructor>。
@ConstructorArgs({
@Arg(column = "site", javaType = Sites.class),
@Arg(column = "workspace_id", javaType = UUID.class),
@Arg(column = "external_id", javaType = String.class)
})
@Select("SELECT * FROM workspace_external_references WHERE workspace_id = #{workspaceId}")
List<WorkspaceExternalReference> findByWorkspace(@Param("workspaceId") final UUID workspaceId);其他列(即id、created_at、updated_at)将通过设置器(如果有)或反射自动映射。
或者,只需将默认构造函数(no-arg)添加到WorkspaceExternalReference类。然后,所有列都将在类实例化后自动映射。
注意:要使其正常工作,需要为UUID注册一个类型处理程序,但您似乎已经完成了(否则参数映射将无法工作)。
https://stackoverflow.com/questions/62274210
复制相似问题