java父子类三级查询_java关于三级分类功能实现

本文介绍了使用SpringBoot+MybatisPlus+人人代码生成器实现后台的三级分类查询功能,前端则采用VUE+ElementUI进行展示。详细讲解了从实体类、DAO、Service到Controller的代码实现,以及前端组件的使用。最后展示了查询结果的一级、二级和三级分类效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、技术前提

1.1、后端采用的技术是SpringBoot+MybatisPlus+人人代码生成器实现的

1.2、前端采用的技术是VUE +ElementUI+人人开源实现的

2、后端实现

2.1、pom依赖

org.springframework.boot

spring-boot-starter-parent

2.1.8.RELEASE

com.baomidou

mybatis-plus-boot-starter

3.2.0

org.springframework.boot

spring-boot-starter-web

org.projectlombok

lombok

1.18.8

2.2 application.yml配置文件

spring:

application:

name: mall-product

datasource:

url: jdbc:mysql://localhost:3306/mall_pms?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC&

driver-class-name: com.mysql.cj.jdbc.Driver

username: root

password: root

mybatis-plus:

mapper-locations: classpath:/mapper/**/*.xml

2.3、实体类

@Data

public class CategoryDTO implements Serializable {

/**

* 分类id

*/

@TableId

private Long catId;

/**

* 分类名称

*/

private String name;

/**

* 父分类id

*/

private Long parentCid;

/**

* 层级

*/

private Integer catLevel;

/**

* 是否显示[0-不显示,1显示]

*/

private Integer showStatus;

/**

* 排序

*/

private Integer sort;

/**

* 图标地址

*/

private String icon;

/**

* 计量单位

*/

private String productUnit;

/**

* 商品数量

*/

private Integer productCount;

private List children;

}

2.4 dao层

@Mapper

public interface CategoryDao extends BaseMapper {

}

2.5、service

public interface CategoryService extends IService {

/**

* 查询三级分类

* @return

*/

List listWithTree();

}

2.6、service实现类具体功能实现

@Override

public List listWithTree() {

//查出所有分类

List categoryList = baseMapper.selectList(null);

List categoryDTOList = new ArrayList<>();

//查询所有父类目录

for(CategoryEntity categoryEntity : categoryList) {

if(categoryEntity.getParentCid() == 0) {

CategoryDTO categoryDTO = categoryEntity2CategoryDTO(categoryEntity);

categoryDTOList.add(categoryDTO);

}

categoryDTOList.sort(Comparator.comparing(CategoryDTO::getSort));

}

//查询子目录

findSubCategory(categoryDTOList, categoryList);

return categoryDTOList;

}

/**

* 查询子目录

* @param categoryDTOList

* @param categoryEntityList

*/

private void findSubCategory(List categoryDTOList, List categoryEntityList) {

//遍历所有父类分类

for(CategoryDTO categoryDTO : categoryDTOList) {

List subCategoryVoList = new ArrayList<>();

//遍历父类下的cat_id与子类parent_cid相匹配的分类

for(CategoryEntity category : categoryEntityList) {

if(categoryDTO.getCatId().equals(category.getParentCid())) {

CategoryDTO subCategoryVo = categoryEntity2CategoryDTO(category);

subCategoryVoList.add(subCategoryVo);

}

//升序排序

subCategoryVoList.sort(Comparator.comparing(CategoryDTO::getSort));

//设置subCategories

categoryDTO.setChildren(subCategoryVoList);

}

//递归调用

findSubCategory(subCategoryVoList, categoryEntityList);

}

}

/**

* 分类对象转换

* @param categoryEntity

* @return

*/

private CategoryDTO categoryEntity2CategoryDTO(CategoryEntity categoryEntity) {

CategoryDTO categoryDTO = new CategoryDTO();

BeanUtils.copyProperties(categoryEntity, categoryDTO);

return categoryDTO;

}

2.7controller层

/**

* 查询出所有分类以及子分类,以树型结构组装起来

* @return

*/

@RequestMapping("/list/tree")

public R list() {

List entityList = categoryService.listWithTree();

return R.ok().put("data", entityList);

}

3、前端实现

详细的我就不一一给出来了,要是看不懂可以参照人人开源或者ElementUi文档实现其他功能!!!

category.vue

show-checkbox

node-key="catId"

:data="menus"

:props="defaultProps"

@node-click="handleNodeClick"

:expand-on-click-node="false"

:default-expanded-keys="expandedKey">

>

{{ node.label }}

v-if="node.level <= 2"

type="text"

size="mini"

@click="() => append(data)">

Append

v-if="node.childNodes.length == 0"

type="text"

size="mini"

@click="() => remove(node, data)">

Delete

export default {

name: "category",

data() {

return {

menus: [],

expandedKey: [],

defaultProps: {

children: 'children',

label: 'name'

}

};

},

methods: {

handleNodeClick(data) {

console.log(data);

},

getMenus() {

this.$http({

url: this.$http.adornUrl('/product/category/list/tree'),

method: 'get'

}).then(({data}) => {

this.menus = data.data;

})

},

append(data) {

console.log("append", data)

},

remove(node, data) {

let ids = [data.catId]

this.$confirm(`是否删除【${data.name}】菜单?`, '提示', {

confirmButtonText: '确定',

cancelButtonText: '取消',

type: 'warning'

}).then(() => {

console.log("remove", node, data)

this.$http({

url: this.$http.adornUrl('/product/category/delete'),

method: 'post',

data: this.$http.adornData(ids, false)

}).then(({data}) => {

this.$message({

message: '菜单删除成功',

type: 'success'

});

//删除成功,重新加载菜单

this.getMenus()

//展开删除节点的父节点

this.expandedKey = [node.parent.data.catId]

})

}).catch(() => {

});

}

},

created() {

this.getMenus()

}

}

4、启动项目

@SpringBootApplication

//包扫描

@MapperScan("com.cluck.mall.product.dao")

public class ProductApplication {

public static void main(String[] args) {

SpringApplication.run(ProductApplication.class, args);

}

}

效果图:

一级分类

d9877062d63c

一级分类.png

二级分类

d9877062d63c

二级分类.png

三级分类

d9877062d63c

三级分类.png

主要功能实现代码都是在categoryServiceImpl实现类里面,要是前端不太会的话,其实只要把后端功能跑起来就行了,你们肯定是有更好的思路和更简洁的代码也是欢迎推荐推荐的~~~~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值