版权声明: https://blog.csdn.net/qq_27721169/article/details/86541187
xml文件
<?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.macro.mall.dao.PmsProductCategoryDao">
<resultMap id="listWithChildrenMap" type="com.macro.mall.dto.PmsProductCategoryWithChildrenItem"
extends="com.macro.mall.mapper.PmsProductCategoryMapper.BaseResultMap">
<collection property="children" resultMap="com.macro.mall.mapper.PmsProductCategoryMapper.BaseResultMap"
columnPrefix="child_"></collection>
</resultMap>
<select id="listWithChildren" resultMap="listWithChildrenMap">
select
c1.id,
c1.name,
c2.id child_id,
c2.name child_name
from pms_product_category c1 left join pms_product_category c2 on c1.id = c2.parent_id
where c1.parent_id = 0
</select>
</mapper>
dao文件
public interface PmsProductCategoryDao {
List<PmsProductCategoryWithChildrenItem> listWithChildren();
}
dto
public class PmsProductCategoryWithChildrenItem extends PmsProductCategory {
private List<PmsProductCategory> children;
public List<PmsProductCategory> getChildren() {
return children;
}
public void setChildren(List<PmsProductCategory> children) {
this.children = children;
}
}
对应实体map
<resultMap id="BaseResultMap" type="com.macro.mall.model.PmsProductCategory">
<id column="id" jdbcType="BIGINT" property="id" />
<result column="parent_id" jdbcType="BIGINT" property="parentId" />
<result column="name" jdbcType="VARCHAR" property="name" />
<result column="level" jdbcType="INTEGER" property="level" />
<result column="product_count" jdbcType="INTEGER" property="productCount" />
<result column="product_unit" jdbcType="VARCHAR" property="productUnit" />
<result column="nav_status" jdbcType="INTEGER" property="navStatus" />
<result column="show_status" jdbcType="INTEGER" property="showStatus" />
<result column="sort" jdbcType="INTEGER" property="sort" />
<result column="icon" jdbcType="VARCHAR" property="icon" />
<result column="keywords" jdbcType="VARCHAR" property="keywords" />
</resultMap>
sql
DROP TABLE IF EXISTS `pms_product_category`;
CREATE TABLE `pms_product_category` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`parent_id` bigint(20) DEFAULT NULL COMMENT '上机分类的编号:0表示一级分类',
`name` varchar(64) DEFAULT NULL,
`level` int(1) DEFAULT NULL COMMENT '分类级别:0->1级;1->2级',
`product_count` int(11) DEFAULT NULL,
`product_unit` varchar(64) DEFAULT NULL,
`nav_status` int(1) DEFAULT NULL COMMENT '是否显示在导航栏:0->不显示;1->显示',
`show_status` int(1) DEFAULT NULL COMMENT '显示状态:0->不显示;1->显示',
`sort` int(11) DEFAULT NULL,
`icon` varchar(255) DEFAULT NULL COMMENT '图标',
`keywords` varchar(255) DEFAULT NULL,
`description` text COMMENT '描述',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=52 DEFAULT CHARSET=utf8 COMMENT='产品分类';