mapper中select后面的返回参数统一加上相同的前缀代码
在Mapper接口中,可以通过使用注解@ResultMap和@Results来定义返回结果集的映射关系,其中通过@Result注解可以为返回的属性指定前缀。
下面是一个示例代码:
public interface UserMapper {
@Results(id = "userResultMap", value = {
@Result(property = "userId", column = "user_id"),
@Result(property = "userName", column = "user_name"),
@Result(property = "userAge", column = "user_age")
})
@Select("SELECT * FROM user")
List<User> getAllUsers();
@ResultMap("userResultMap")
@Select("SELECT * FROM user WHERE user_id = #{userId}")
User getUserById(Long userId);
}
在上述代码中,@Results注解用于定义返回结果集的映射关系,通过@Result注解为返回的属性指定了前缀。在@Result注解中,property属性指定了返回对象的属性名,column属性指定了结果集中对应的列名。
在@Select注解中,可以通过@ResultMap注解来使用之前定义的结果映射关系。在此示例中,@ResultMap注解引用了userResultMap,即使用了之前定义的映射关系。
通过以上代码,返回的User对象的属性名称会添加前缀,例如user_id会映射到userId属性,user_name会映射到userName属性,以此类推。
希望以上代码能够帮助到你
原文地址: http://www.cveoy.top/t/topic/iXbj 著作权归作者所有。请勿转载和采集!