java菜单树生成

方法一(多节点添加)

有时我们会用到一些菜单树返回给前端数据,生成目录结构
在这里插入图片描述

如何生成这样的菜单树生成给前端,接下来就是实现方法:

数据库:数据字段一定要有pid(父目录的id)
在这里插入图片描述
返回的实体类:要有一个子节点

public class ResVo extends Model<ResVo> {
private static final long serialVersionUID = 1L;
	/**
	 * id
	 */
	@TableId
	@ApiModelProperty(value="id")
	private String id;
	/**
	 * 类别设置
	 */
	@ApiModelProperty(value="类别设置")
	private String categoryName;
	/**
	 * 上级类别id
	 */
	@ApiModelProperty(value="上级类别id")
	private String pid;
	/**
	 * 上级分类
	 */
	@ApiModelProperty(value="上级分类")
	private String pname;
	/**
	 * 子节点
	 */
	@ApiModelProperty(value="子节点")
	private List<ResVo> children;

	//构造子节点
	public ResVo() {
		this.children = new ArrayList<>();
	}
	
	//子节点添加方法
	public void addChildern(ResVo resVo){
		this.children.add(resVo);
	}
}

service层:就可以实现多级分类

public List<ResVo> selectAll() {
		//新建一个list集合
		List<ResVo> treelist = new ArrayList<>();
		//从数据库查询出所有节点(这儿我演示全部查询,SQL如何查询可自定义)
		List<ResVo> resVoList = resMapper.selectAll();
		//判空
		if (null == resVoList || resVoList .isEmpty()) {
			return null;
		}
		//把List集合装换成Map集合
		Map<String, ResVo> map = resVoList .stream().collect(Collectors.toMap(ResVo::getId, a -> a, (k1, k2) -> k1));

		// 如果id是父级的话就放入tree中treelist
		for (ResVo resVo : resVoList ) {
			if (null == map.get(resVo.getPid())) {
				treelist.add(resVo);
			} else {
				// 子级通过父id获取到父级的类型
				ResVo parent = map.get(resVo.getPid());
				// 父级获得子级,再将子级放到对应的父级中
				parent.addChildern(resVo);
			}
		}
		return treelist;
	}

总结:这种方式是可以无限制添加节点,但是节点多时就会比较耗时

方法二(少节点添加)

第二种方法就是只需要mapper.xml进行修改就行了

数据库一样
返回的实体类:要有一个子节点

public class ResVo extends Model<ResVo> {
private static final long serialVersionUID = 1L;
	/**
	 * id
	 */
	@TableId
	@ApiModelProperty(value="id")
	private String id;
	/**
	 * 类别设置
	 */
	@ApiModelProperty(value="类别设置")
	private String categoryName;
	/**
	 * 上级类别id
	 */
	@ApiModelProperty(value="上级类别id")
	private String pid;
	/**
	 * 上级分类
	 */
	@ApiModelProperty(value="上级分类")
	private String pname;
	/**
	 * 子节点
	 */
	@ApiModelProperty(value="子节点")
	private List<ResVo> children;
}

service层:

public List<ResVo> selectAll() {
	List<ResVo> resVoList = resMapper.selectAll();
}

mapper层

public interface ClassifySettingMapper extends BaseMapper<ClassifySetting> {
	/**
	 * 设备分类查询全部
	 * @return
	 */
	List<ResVo> selectAll();
}

mapper.xml

<mapper namespace="com.bbibm.industry.basicsetup.mapper.ClassifySettingMapper">
	<!--映射结果集-->
	<resultMap id="resVoMap" type="com.XXX.ResVo">
		<id column="one_id" property="id"></id>
		<result column="one_category_name" property="categoryName"></result>
		<result column="one_pid" property="pid"></result>
		<result column="one_pname" property="pname"></result>
		<collection property="children" ofType="com.XXX.ResVo">
			<id column="tow_id" property="id"></id>
			<result column="tow_category_name" property="categoryName"></result>
			<result column="tow_pid" property="pid"></result>
			<result column="tow_pname" property="pname"></result>
		</collection>
	</resultMap>
	
	<select id="selectAll" resultMap="resVoMap">
		SELECT
			a.id one_id,
			a.category_name one_category_name,
			a.pid one_pid,
			a.pname one_pname,
			b.id tow_id,
			b.category_name tow_category_name,
			b.pid tow_pid,
			b.pname tow_pname
		FROM
			bbibm_classify_setting a
			LEFT JOIN bbibm_classify_setting b ON b.pid = a.id 
		WHERE
			a.pid =0
	</select>

总结:这种方法就只适合节点固定的,每连接(left join) 一张表就会多一个节点,查询快,但是不适合多节点的添加。

链接: 参考大神博客.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值