一对多通过注解查询数据
案例: 获取分类信息和该分类下的所有产品
1.实体类:
public class Product { private int id; private String name; private float price; } public class Category { private int id; private String name; List<Product> products; }
2.dao层:
public interface CategoryMapper { // 获取分类信息和该分类下的所有产品 @Select(" select * from category_ ") @Results({ @Result(property = "id", column = "id"), //property="实体类中的字段", javaType=结果返回类型 ,column=参数 ,many=@Many("集合查询接口") @Result(property = "products", javaType = List.class, column = "id", many = @Many(select = "com.mapper.ProductMapper.listByCategory") ) }) public List<Category> list(); }
public interface ProductMapper { // 根据分类id查询产品列表 @Select(" select * from product_ where cid = #{cid}") public List<Product> listByCategory(int cid); }
3.效果图