一:MyBatis自关联查询映射文件
1.pojo类
public class Category implements Serializable{ private String cid; //分类主键 private String cname; //分类名 private Category parent; //父分类,是根据pid的自连接对象,一个二级分类只有一个一级分类 private String desc; //描述 private List children; //子分类,一个一级分类可能有多个二级分类,而二级分类无子分类 //setter and getter method... }
2.dao接口
public interface CategoryDao { public Category load(String cid) throws SQLException; public List findByParent(String pid) throws SQLException; public List findParent() throws SQLException; public void add(Category category) throws SQLException; public void edit(Category category) throws SQLException; public int findChildrenByParent(String pid) throws SQLException; public void delete(String cid) throws SQLException; }
3.Mapper映射文件(切记实体类里的toString方法里边去掉类型为List的属性,要不然打印时会出现死循环的)。
xml version="1.0" encoding="UTF-8" ?> <mapper namespace="com.tyust.dao.CategoryDao"> <resultMap id="CategoryResultMap" type="com.tyust.pojo.Category" > <id column="cid" property="cid" jdbcType="VARCHAR" /> <result column="cname" property="cname" jdbcType="VARCHAR" /> <result column="desc" property="desc" jdbcType="VARCHAR" /> <association column