mybatis-plus 用注解@select后映射到vo
时间: 2024-05-08 09:20:01 浏览: 9
类时,如何处理结果中字段名与vo类中属性名不一致的情况?
对于这种情况,可以使用 MyBatis-Plus 提供的 @Result 注解来指定该字段映射到 VO 类的哪个属性中。例如:
```java
public class UserVO {
private Long id;
private String name;
private Integer age;
// ... 省略其他属性与 getter/setter 方法
}
@Mapper
public interface UserMapper extends BaseMapper<User> {
@Select("SELECT id, user_name AS name, user_age AS age FROM user WHERE id = #{id}")
@Results({
@Result(column = "user_name", property = "name"),
@Result(column = "user_age", property = "age")
})
UserVO selectUserVO(@Param("id") Long id);
}
```
上述代码中,@Result 注解用于告诉 MyBatis-Plus,查询结果集中的 user_name 字段应该映射到 UserVO 类的 name 属性中,user_age 字段应该映射到 age 属性中。注意,这里的 property 参数指的是 VO 类中的属性名,而 column 参数指的是查询结果集中的列名。这样,即使查询结果中的字段名与 VO 类中的属性名不一致,也能通过 @Result 注解完成正确的映射。