多对一通过注解查询数据
案例:获取所有的产品信息和所属分类名
1.实体类:
public class Product { private int id; private String name; private float price; private Category category ; } public class Category { private int id; private String name; private List<Product> products; }
2.dao层:
public interface ProductMapper {
@Select(" select * from product_ ") @Results({
//column=参数 @Result(property="category",column="cid",one=@One(select="com.mapper.CategoryMapper.get")) }) public List<Product> list(); }
public interface CategoryMapper {
@Select(" select * from category_ where id = #{id}") public Category get(int id); }
3.测试效果图: