最近用mybatis写一个小程序,涉及到树形结构的关系映射,比如一个分类,本身具有多对一的关系,那么它是如何映射呢?直接贴代码:
Cate.java
@Table(name="cate") public class Cate extends AbstractModel { /** * */ private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private Long id; private String name; private String mark; private String img; private Long pid; public Cate() { super(); } private Date insertTime; private int sortNum; private String intro; private List<Cate> ch = new ArrayList<Cate>(); private Cate parentCate; private int status; public String getImg() { return img; } public void setImg(String img) { this.img = img; } public Date getInsertTime() { return insertTime; } public void setInsertTime(Date insertTime) { this.insertTime = insertTime; } public int getSortNum() { return sortNum; } public void setSortNum(int sortNum) { this.sortNum = sortNum; } public String getIntro() { return intro; } public void setIntro(String intro) { this.intro = intro; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public Cate(String name) { super(); this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getMark() { return mark; } public void setMark(String mark) { this.mark = mark; } public Long getPid() { return pid; } public void setPid(Long pid) { this.pid = pid; } public List<Cate> getCh() { return ch; } public void setCh(List<Cate> ch) { this.ch = ch; } public Cate getParentCate() { return parentCate; } public void setParentCate(Cate parentCate) { this.parentCate = parentCate; } public int getStatus() { return status; } public void setStatus(int status) { this.status = status; } }
Cate.xml
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.cxgt.mvc.gede.dao.CateDao"> <resultMap type="com.cxgt.mvc.gede.pojo.Cate" id="cateResult"> <id column="id" property="id"/> <result column="name" property="name"/> <result column="pid" property="pid"/> <result column="mark" property="mark"/> <result column="img" property="img"/> <result column="insertTime" property="insertTime"/> <result column="intro" property="intro"/> <result column="sortNum" property="sortNum"/> <result column="status" property="status"/> <!-- 多对一的关系 --> <!-- property: 指的是属性的值, javaType:指的是属性的类型--> <association property="parentCate" javaType="com.cxgt.mvc.gede.pojo.Cate" column="pid" select="get"> </association> <collection property="ch" ofType="com.cxgt.mvc.gede.pojo.Cate" column="id" select="getCh"> </collection> </resultMap> <!-- 单行插入 --> <insert id="insert" parameterType="com.cxgt.mvc.gede.pojo.Cate"> INSERT INTO cate ( name, pid, mark, img, insertTime, intro, sortNum, status ) VALUES ( #{name}, #{pid}, #{mark}, #{img}, #{insertTime}, #{intro}, #{sortNum}, #{status} ) </insert> <!-- 删除用户 --> <delete id="delete" parameterType="java.lang.Long"> delete from cate where id=#{id} </delete> <select id="get" resultMap="cateResult"> select * from cate where id=#{id} </select> <select id="findAll" resultMap="cateResult"> select * from cate where pid is null; </select> <select id="getCh" parameterType="java.lang.Long" resultMap="cateResult"><!-- resultType="com.cxgt.mvc.gede.pojo.Cate" --> select * from cate where pid=#{pid}; </select> <update id="update" parameterType="com.cxgt.mvc.gede.pojo.Cate" > update cate set name = #{name,jdbcType=VARCHAR}, pid = #{pid,jdbcType=BIGINT}, mark = #{mark,jdbcType=VARCHAR}, img = #{img,jdbcType=VARCHAR}, insertTime = #{insertTime,jdbcType=DATE}, intro = #{intro,jdbcType=VARCHAR}, sortNum = #{sortNum,jdbcType=INTEGER}, status = #{status,jdbcType=INTEGER} where id = #{id,jdbcType=BIGINT} </update> </mapper>
这样就可以啦,树形结构就可以出现啦~