mybatis映射配置文件:
主要:
<collection property="subCategories" column="category_id" ofType="com.school.domain.Category" resultMap="subCategories" />
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace="com.school.dao.CategoryBeanMapper" > <resultMap id="BaseResultMap" type="com.school.domain.CategoryBean" > <id column="category_id" property="categoryId" jdbcType="VARCHAR" /> <result column="parent_id" property="parentId" jdbcType="VARCHAR" /> <result column="grade_id" property="gradeId" jdbcType="VARCHAR" /> <result column="school_id" property="schoolId" jdbcType="VARCHAR" /> <result column="name" property="name" jdbcType="VARCHAR" /> <result column="description" property="description" jdbcType="VARCHAR" /> <result column="link" property="link" jdbcType="VARCHAR" /> <result column="icon" property="icon" jdbcType="VARCHAR" /> <result column="ordered" property="ordered" jdbcType="INTEGER" /> <result column="date_added" property="dateAdded" jdbcType="TIMESTAMP" /> <result column="last_modified" property="lastModified" jdbcType="TIMESTAMP" /> <result column="last_user_id" property="lastUserId" jdbcType="VARCHAR" /> <result column="is_open" property="isOpen" jdbcType="VARCHAR" /> <result column="status" property="status" jdbcType="VARCHAR" /> <result column="category_type" property="categoryType" jdbcType="VARCHAR" /> <collection property="subCategories" column="category_id" ofType="com.school.domain.Category" resultMap="subCategories" /> </resultMap> <resultMap id="subCategories" type="com.school.domain.Category" > <id column="category_categoryId" property="categoryId" jdbcType="VARCHAR" /> <result column="category_parentId" property="parentId" jdbcType="VARCHAR" /> <result column="category_gradeId" property="gradeId" jdbcType="VARCHAR" /> <result column="category_schoolId" property="schoolId" jdbcType="VARCHAR" /> <result column="category_name" property="name" jdbcType="VARCHAR" /> <result column="category_description" property="description" jdbcType="VARCHAR" /> <result column="category_link" property="link" jdbcType="VARCHAR" /> <result column="category_icon" property="icon" jdbcType="VARCHAR" /> <result column="category_ordered" property="ordered" jdbcType="INTEGER" /> <result column="category_date_added" property="dateAdded" jdbcType="TIMESTAMP" /> <result column="category_last_modified" property="lastModified" jdbcType="TIMESTAMP" /> <result column="category_last_user_id" property="lastUserId" jdbcType="VARCHAR" /> <result column="category_is_open" property="isOpen" jdbcType="VARCHAR" /> <result column="category_status" property="status" jdbcType="VARCHAR" /> <result column="category_category_type" property="categoryType" jdbcType="VARCHAR" /> </resultMap> <!-- 查询所用专题目录 --> <select id="selectByPrimaryKey" resultMap="BaseResultMap"> SELECT c.category_id, c.parent_id, c.grade_id, c.school_id, c.name, c.description, c.link, c.icon, c.ordered, c.date_added, c.last_modified, c.last_user_id, c.category_type, sub.category_id AS category_categoryId, sub.parent_id AS category_parentId, sub.grade_id AS category_gradeId, sub.school_id AS category_schoolId, sub.name AS category_name, sub.description AS category_description, sub.link AS category_link, sub.icon AS category_icon, sub.ordered AS category_ordered, sub.date_added AS category_dateAdded, sub.last_modified AS category_lastModified, sub.last_user_id AS category_lastUserId, sub.category_type AS category_category_type FROM category AS c LEFT JOIN category AS sub ON(c.category_id = sub.parent_id) WHERE c.grade_id = 1 <if test="categoryId != null"> AND c.category_id=#{categoryId,jdbcType=VARCHAR} </if> <if test="schoolId != null"> AND c.school_id=#{schoolId,jdbcType=VARCHAR} </if> </select> <select id="selectByCategoryType" resultMap="BaseResultMap"> SELECT c.category_id, c.parent_id, c.grade_id, c.school_id, c.name, c.description, c.link, c.icon, c.ordered, c.date_added, c.last_modified, c.last_user_id, c.category_type, sub.category_id AS category_categoryId, sub.parent_id AS category_parentId, sub.grade_id AS category_gradeId, sub.school_id AS category_schoolId, sub.name AS category_name, sub.description AS category_description, sub.link AS category_link, sub.icon AS category_icon, sub.ordered AS category_ordered, sub.date_added AS category_dateAdded, sub.last_modified AS category_lastModified, sub.last_user_id AS category_lastUserId, sub.category_type AS category_category_type FROM category AS c LEFT JOIN category AS sub ON(c.category_id = sub.parent_id) WHERE c.grade_id = 1 <if test="categoryType != null"> AND c.category_type=#{categoryType,jdbcType=VARCHAR} </if> <if test="schoolId != null"> AND c.school_id=#{schoolId,jdbcType=VARCHAR} </if> </select> </mapper>
package com.school.domain; import java.util.List; /** * 一级栏目及其二级栏目 * @author FeiFan.Lin */ public class CategoryBean extends Category{ private List<Category> subCategories; public List<Category> getSubCategories() { return subCategories; } public void setSubCategories(List<Category> subCategories) { this.subCategories = subCategories; } }
package com.school.domain; import java.util.Date; public class Category { private String categoryId; private String parentId; private String gradeId; private String schoolId; private String name; private String description; private String link; private String icon; private Integer ordered; private Date dateAdded; private Date lastModified; private String lastUserId; private String isOpen; private String status; private String categoryType; public String getCategoryId() { return categoryId; } public void setCategoryId(String categoryId) { this.categoryId = categoryId == null ? null : categoryId.trim(); } public String getParentId() { return parentId; } public void setParentId(String parentId) { this.parentId = parentId == null ? null : parentId.trim(); } public String getGradeId() { return gradeId; } public void setGradeId(String gradeId) { this.gradeId = gradeId == null ? null : gradeId.trim(); } public String getSchoolId() { return schoolId; } public void setSchoolId(String schoolId) { this.schoolId = schoolId == null ? null : schoolId.trim(); } public String getName() { return name; } public void setName(String name) { this.name = name == null ? null : name.trim(); } public String getDescription() { return description; } public void setDescription(String description) { this.description = description == null ? null : description.trim(); } public String getLink() { return link; } public void setLink(String link) { this.link = link == null ? null : link.trim(); } public String getIcon() { return icon; } public void setIcon(String icon) { this.icon = icon == null ? null : icon.trim(); } public Integer getOrdered() { return ordered; } public void setOrdered(Integer ordered) { this.ordered = ordered; } public Date getDateAdded() { return dateAdded; } public void setDateAdded(Date dateAdded) { this.dateAdded = dateAdded; } public Date getLastModified() { return lastModified; } public void setLastModified(Date lastModified) { this.lastModified = lastModified; } public String getLastUserId() { return lastUserId; } public void setLastUserId(String lastUserId) { this.lastUserId = lastUserId == null ? null : lastUserId.trim(); } public String getIsOpen() { return isOpen; } public void setIsOpen(String isOpen) { this.isOpen = isOpen == null ? null : isOpen.trim(); } public String getStatus() { return status; } public void setStatus(String status) { this.status = status == null ? null : status.trim(); } public String getCategoryType() { return categoryType; } public void setCategoryType(String categoryType) { this.categoryType = categoryType == null ? null : categoryType.trim(); } }