700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > 注解方式—解决mybatis实体类属性名和数据库字段名不一致问题

注解方式—解决mybatis实体类属性名和数据库字段名不一致问题

时间:2020-01-10 12:32:17

相关推荐

注解方式—解决mybatis实体类属性名和数据库字段名不一致问题

表 tb_brand

实体类 Brand属性名

解决方式一(为表字段取别名)

// 根据字段id查询@Select("select id, brand_name as brandName, company_name as companyName from tb_brand where id = #{id}")Brand selectById(int id);

解决方式二(添加注解@Results)

@Results(id = "brandMapper", value = {@Result(id = true, column = "id", property = "id"),// 这里的id=true是主键@Result(column = "brand_name", property = "brandName"),@Result(column = "company_name", property = "companyName")})@Select("select id, brand_name, company_name from tb_brand where id = #{id}")Brand selectById(int id);

Results注解中有两个常用的参数,一个是id,另一个是value。id为唯一标识,当其他地方需要使用这个Results时,这时我们就可以通过@ResultMap()注解中传入Results注解的参数id来引用Results注解中的内容。

@ResultMap(value = "brandMapper")@Select("select * from tb_brand")List<Brand> selectAll();// 查询所有

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。