商品分类实现(一级分类、二级分类和子分类)

1.封装二级分类(CategoryVO)和三级分类(SubCategoryVO)
CategoryVO

public class CategoryVO {
    private Integer id;
    private String name;
    private String type;
    private Integer fatherId;

    private List<SubCategoryVO> SubCatList;
    省略getset方法.......
}

SubCategoryVO

public class SubCategoryVO {
    private Integer subId;
    private String SubName;
    private String SubType;
    private Integer SubFatherId;
    省略getset方法......
}

2.查询二级分类和子分类需要自定义Sql,所以得重新创建CategoryMapperCustom接口和CategoryMapperCustom.xml
CategoryMapperCustom接口

public interface CategoryMapperCustom {
    /**
     * 查询所有的二级分类
     */
    public List<CategoryVO> getSubCatList(Integer rootCatId);

}

CategoryMapperCustom.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.lzx.mapper.CategoryMapperCustom" >
  <resultMap id="myCategoryVO"  type="com.lzx.pojo.vo.CategoryVO">
    <id column="id" property="id" />
    <result column="name" property="name"/>
    <result column="type" property="type" />
    <result column="fatherId" property="fatherId" />
    <!--
      collection:用于定义关联的list
      property:对应三级分类list属性名
      ofType:集合的类型,三级分类的VO
    -->
    <collection property="SubCatList" ofType="com.lzx.pojo.vo.SubCategoryVO">
      <id column="subId" property="subId" />
      <result column="subName" property="subName"/>
      <result column="subType" property="subType" />
      <result column="subFatherId" property="subFatherId" />
    </collection>
  </resultMap>
  <select id="getSubCatList" resultMap="myCategoryVO" parameterType="int">
    SELECT
     f.`id` AS id,
     f.`name` AS `name`,
     f.`type` AS TYPE,
     f.`father_id` AS fatherId,
     c.`id` AS subId,
     c.`name` AS `subName`,
     c.`type` AS subType,
     c.`father_id` AS subFatherId
    FROM
      category f
    LEFT JOIN
      category c
    ON
    f.id = c.father_id
    WHERE
      f.father_id = #{rootCatId}
  </select>
</mapper>

3.查询一级分类和子分类目录
CategoryService

public interface CategoryService {
    /**
     * 查询一级分类
     * @param
     * @return
     */
    public List<Category> queryAllRootLevel();

    /**
     * 查询所有的二级分类和子分类
     * @param rootCatId
     * @return
     */
    public List<CategoryVO> getSubCatlist(Integer rootCatId);
}

CategoryServiceImpl

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private CategoryMapperCustom categoryMapperCustom;

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<Category> queryAllRootLevel() {
        Example example = new Example(Category.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("type",1);
        List<Category> result = categoryMapper.selectByExample(example);
        return result;
    }
    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<CategoryVO> getSubCatlist(Integer rootCatId) {
        return categoryMapperCustom.getSubCatList(rootCatId);

    }

}

前端(部分代码)

				renderCategorys() {
					var serverUrl = app.serverUrl;

					// 获得商品分类 - 大分类
					axios.get(
							serverUrl + '/index/cats', {})
						.then(res => {
							
							if (res.data.status == 200) {
								var categoryList = res.data.data
								this.categoryList = categoryList;

								var rootCatHtml = "";
								for (var i = 0; i < categoryList.length; i++) {

									var cat = categoryList[i];
									rootCatHtml += '' +
										'<li class="appliance js_toggle relative">' +
										'<div class="category-info">' +
										'<h3 class="category-name b-category-name">' +
										'<i><img src="' + cat.logo + '"></i>' +
										'<a class="ml-22" title="' + cat.name + '">' + cat.name + '</a>' +
										'</h3>' +
										'<em>&gt;</em></div>' +
										'<div class="menu-item menu-in top">' +
										'<div class="area-in">' +
										'<div class="area-bg">' +
										'<div class="menu-srot">' +
										'<div class="sort-side" rootCatId="' + cat.id + '"></div>' +
										'</div>' +
										'</div>' +
										'</div>' +
										'</div>' +
										'<b class="arrow"></b>' +
										'</li>';
								}
								var $leftNav = $('#js_climit_li');
								$leftNav.html(rootCatHtml);

								$("li").hover(function () {
									// debugger;
									$(".category-content .category-list li.first .menu-in").css("display",
										"none");
									$(".category-content .category-list li.first").removeClass("hover");

									var meLi = $(this);

									var subWapper = $(this).children("div.menu-in").children("div.area-in")
										.children("div.area-bg").children("div.menu-srot").children(
											"div.sort-side");
									// console.log(subWapper.html());
									var subCatHtml = subWapper.html();
									var rootCatId = subWapper.attr("rootCatId");
									// console.log(rootCatId);
									// 如果该节点下没有内容,则发起请求查询子分类并且渲染到页面,如果有的话就不查询了(懒加载模式)
									if (subCatHtml == null || subCatHtml == '' || subCatHtml == undefined) {
										if (rootCatId != undefined && rootCatId != null && rootCatId != '') {
											// 根据root分类id查询该分类下的所有子分类
											axios.get(
													serverUrl + '/index/subCat/' + rootCatId, {})
												.then(res => {
													if (res.data.status == 200) {
														var catList = res.data.data
														// this.catList = catList;
														// debugger;
														var subRenderHtml = '';
														for (var i = 0; i < catList.length; i++) {
															var cat = catList[i];
															subRenderHtml += '' +
																'<dl class="dl-sort">' +
																'<dt><span title="' + cat.name + '">' +
																cat.name + '</span></dt>';

															// 拼接第三级分类
															var subCatList = cat.subCatList;
															for (var j = 0; j < subCatList.length; j++) {
																var subCat = subCatList[j];
																subRenderHtml += '<dd><a title="' + subCat
																	.subName + '" href="catItems.html?searchType=catItems&catId='+ subCat.subId +'" target="_blank"><span>' +
																	subCat.subName + '</span></a></dd>'
															}

															subRenderHtml += '</dl>';
														}
														subWapper.html(subRenderHtml);
														meLi.addClass("hover");
														meLi.children("div.menu-in").css("display",
															"block");
													}
												});
										}
										// var renderHtml = '' 
										// 	+ '<dl class="dl-sort">'
										// 		+ '<dt><span title="大包装">大包装</span></dt>'
										// 		+ '<dd><a title="蒸蛋糕" href="#"><span>蒸蛋糕</span></a></dd>'
										// 		+ '<dd><a title="脱水蛋糕" href="#"><span>脱水蛋糕</span></a></dd>'
										// 		+ '<dd><a title="瑞士卷" href="#"><span>瑞士卷</span></a></dd>'
										// 		+ '<dd><a title="软面包" href="#"><span>软面包</span></a></dd>'
										// 		+ '<dd><a title="马卡龙" href="#"><span>马卡龙</span></a></dd>'
										// 		+ '<dd><a title="千层饼" href="#"><span>千层饼</span></a></dd>'
										// 		+ '<dd><a title="甜甜圈" href="#"><span>甜甜圈</span></a></dd>'
										// 		+ '<dd><a title="蒸三明治" href="#"><span>蒸三明治</span></a></dd>'
										// 		+ '<dd><a title="铜锣烧" href="#"><span>铜锣烧</span></a></dd>'
										// 	+ '</dl>'
										// 	+ '<dl class="dl-sort">'
										// 		+ '<dt><span title="两件套">两件套</span></dt>'
										// 		+ '<dd><a title="蒸蛋糕" href="#"><span>蒸蛋糕</span></a></dd>'
										// 		+ '<dd><a title="脱水蛋糕" href="#"><span>脱水蛋糕</span></a></dd>'
										// 		+ '<dd><a title="瑞士卷" href="#"><span>瑞士卷</span></a></dd>'
										// 		+ '<dd><a title="软面包" href="#"><span>软面包</span></a></dd>'
										// 		+ '<dd><a title="马卡龙" href="#"><span>马卡龙</span></a></dd>'
										// 		+ '<dd><a title="千层饼" href="#"><span>千层饼</span></a></dd>'
										// 		+ '<dd><a title="甜甜圈" href="#"><span>甜甜圈</span></a></dd>'
										// 		+ '<dd><a title="蒸三明治" href="#"><span>蒸三明治</span></a></dd>'
										// 		+ '<dd><a title="铜锣烧" href="#"><span>铜锣烧</span></a></dd>'
										// 	+ '</dl>';
										// 	$(this)
										// 		.children("div.menu-in")
										// 		.children("div.area-in")
										// 		.children("div.area-bg")
										// 		.children("div.menu-srot")
										// 		.children("div.sort-side")
										// 		.html(renderHtml);
									} else {
										$(this).addClass("hover");
										$(this).children("div.menu-in").css("display", "block");
									}

									// $(this).addClass("hover");
									// $(this).children("div.menu-in").css("display", "block")
								}, function () {
									$(this).removeClass("hover")
									$(this).children("div.menu-in").css("display", "none")
								});
								this.renderSixNewItems();
							}
						});
				},
  • 5
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
DevEco-Studio是华为公司开发的一款集成开发环境,用于开发鸿蒙(HarmonyOS)操作系统的应用程序。如果您想要实现商品分类一级分类代码,可以参考以下步骤: 1. 在布局文件中添加一个ListView控件用于展示一级分类列表。 2. 创建一个一级分类数据模型,包括分类名称、分类ID等属性。 3. 创建一个一级分类数据源,用于存储一级分类数据模型。 4. 创建一个Adapter类用于将一级分类数据源中的数据绑定到ListView控件上。在getView()方法中进行数据绑定操作。 5. 在Activity或Fragment中实例化ListView控件和Adapter,并将Adapter设置给ListView控件。 6. 实现ListView控件的点击事件,当用户点击某个一级分类时,需要跳转到二级分类页面,并传递一级分类ID参数。 下面是一些示例代码,供您参考: 一级分类数据模型: ``` public class FirstCategoryModel { private String categoryName; private int categoryId; public String getCategoryName() { return categoryName; } public void setCategoryName(String categoryName) { this.categoryName = categoryName; } public int getCategoryId() { return categoryId; } public void setCategoryId(int categoryId) { this.categoryId = categoryId; } } ``` 一级分类数据源: ``` public class FirstCategoryDataSource { private List<FirstCategoryModel> dataList; public FirstCategoryDataSource() { dataList = new ArrayList<>(); // 添加一级分类数据 FirstCategoryModel model1 = new FirstCategoryModel(); model1.setCategoryName("电子产品"); model1.setCategoryId(1); dataList.add(model1); FirstCategoryModel model2 = new FirstCategoryModel(); model2.setCategoryName("家居用品"); model2.setCategoryId(2); dataList.add(model2); // 添加更多分类数据... } public List<FirstCategoryModel> getDataList() { return dataList; } } ``` Adapter类: ``` public class FirstCategoryAdapter extends BaseAdapter { private Context context; private List<FirstCategoryModel> dataList; public FirstCategoryAdapter(Context context, List<FirstCategoryModel> dataList) { this.context = context; this.dataList = dataList; } @Override public int getCount() { return dataList.size(); } @Override public Object getItem(int position) { return dataList.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; if (convertView == null) { convertView = LayoutInflater.from(context).inflate(R.layout.item_first_category, null); holder = new ViewHolder(); holder.tvCategoryName = convertView.findViewById(R.id.tv_category_name); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } FirstCategoryModel model = dataList.get(position); holder.tvCategoryName.setText(model.getCategoryName()); return convertView; } class ViewHolder { TextView tvCategoryName; } } ``` Activity中的代码: ``` public class FirstCategoryActivity extends BaseActivity { private ListView lvCategory; private FirstCategoryAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_first_category); lvCategory = findViewById(R.id.lv_category); // 创建一级分类数据源 FirstCategoryDataSource dataSource = new FirstCategoryDataSource(); List<FirstCategoryModel> dataList = dataSource.getDataList(); // 创建Adapter并设置给ListView adapter = new FirstCategoryAdapter(this, dataList); lvCategory.setAdapter(adapter); // ListView点击事件 lvCategory.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { FirstCategoryModel model = dataList.get(position); Intent intent = new Intent(FirstCategoryActivity.this, SecondCategoryActivity.class); intent.putExtra("firstCategoryId", model.getCategoryId()); startActivity(intent); } }); } } ``` 以上代码仅为示例,具体实现方式可能因项目而异。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值