表格
商品、分类、品牌
分类实体类
public class Category implements Serializable {
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
private Long id;
private String name;
private Long parentId;
private Boolean isParent;
/**
* 排序指数,越小越靠前
*/
private Integer sort;
通用mapper
使用通用Mapper来简化开发:
一定要加上注解
@org.apache.ibatis.annotations.Mapper
public interface CategoryMapper extends Mapper<Category> {
}
然后在业务层使用categoryMapper
@Service
public class CategoryServiceImpl implements CategoryService {
@Autowired
private CategoryMapper categoryMapper;
@Override
public List<Category> queryCategoryByPid(Long pid) {
Category t=new Category();
t.setParentId(pid);
return this.categoryMapper.select(t);
}
}
问题
通用mapper只能单表查询。
解决
自己拼sql字符串
商品查询之级联查询
* 根据category id查询brand,左连接
* @param cid
* @return
*/
@Select("SELECT b.* FROM tb_brand b LEFT JOIN tb_category_brand cb ON b.id=cb.brand_id WHERE cb.category_id=#{cid}")
List<Brand> queryBrandByCategoryId(Long cid);
【注意】:如果在mapper中自己查询的话就无需继承mapper了。