温馨提示×

Mybatis @Select能否返回Map类型

小樊
141
2024-06-14 14:14:29
栏目: 编程语言

在MyBatis中,@Select注解可以返回Map类型。可以通过将查询结果映射到Map对象来返回结果,如下所示:

@Select("SELECT id, name, age FROM user WHERE id = #{id}")
@Results({
    @Result(column = "id", property = "id"),
    @Result(column = "name", property = "name"),
    @Result(column = "age", property = "age")
})
Map<String, Object> getUserById(@Param("id") Long id);

在这个例子中,@Results注解用于指定查询结果的映射关系,将查询结果映射到Map对象中的id、name和age字段。然后通过@Select注解指定查询语句,并在方法参数中传入查询条件。调用该方法后将返回一个Map对象,包含查询结果的id、name和age字段值。

0