在淘宝、京东等电商网站,其门户网站都有一个商品品类的多级联动,鼠标移动,就显示,因为前端不是我做的,所以不说明前端实现,只介绍后端实现。

搭建部署SpringBoot环境

配置文件配置:

开启了对Thymeleaf模块引擎的支持

  1. server:
  2. port: 8081
  3. #logging:
  4. # config: classpath:logback_spring.xml
  5. # level:
  6. # com.muses.taoshop: debug
  7. # path: /data/logs
  8. spring:
  9. datasource:
  10. # 主数据源
  11. shop:
  12. url: jdbc:mysql://127.0.0.1:3306/taoshop?autoReconnect=true&useUnicode=true&characterEncoding=utf8&characterSetResults=utf8&useSSL=false
  13. username: root
  14. password: root
  15. driver-class-name: com.mysql.jdbc.Driver
  16. type: com.alibaba.druid.pool.DruidDataSource
  17. # 连接池设置
  18. druid:
  19. initial-size: 5
  20. min-idle: 5
  21. max-active: 20
  22. # 配置获取连接等待超时的时间
  23. max-wait: 60000
  24. # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
  25. time-between-eviction-runs-millis: 60000
  26. # 配置一个连接在池中最小生存的时间,单位是毫秒
  27. min-evictable-idle-time-millis: 300000
  28. # Oracle请使用select 1 from dual
  29. validation-query: SELECT 'x'
  30. test-while-idle: true
  31. test-on-borrow: false
  32. test-on-return: false
  33. # 打开PSCache,并且指定每个连接上PSCache的大小
  34. pool-prepared-statements: true
  35. max-pool-prepared-statement-per-connection-size: 20
  36. # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
  37. filters: stat,wall,slf4j
  38. # 通过connectProperties属性来打开mergeSql功能;慢SQL记录
  39. connection-properties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
  40. # 合并多个DruidDataSource的监控数据
  41. use-global-data-source-stat: true
  42. # jpa:
  43. # database: mysql
  44. # hibernate:
  45. # show_sql: true
  46. # format_sql: true
  47. # ddl-auto: none
  48. # naming:
  49. # physical-strategy: org.hibernate.boot.entity.naming.PhysicalNamingStrategyStandardImpl
  50. # mvc:
  51. # view:
  52. # prefix: /WEB-INF/jsp/
  53. # suffix: .jsp
  54. #添加Thymeleaf配置
  55. thymeleaf:
  56. cache: false
  57. prefix: classpath:/templates/
  58. suffix: .html
  59. mode: HTML5
  60. encoding: UTF-8
  61. content-type: text/html
  62. #Jedis配置
  63. # jedis :
  64. # pool :
  65. # host : 127.0.0.1
  66. # port : 6379
  67. # password : redispassword
  68. # timeout : 0
  69. # config :
  70. # maxTotal : 100
  71. # maxIdle : 10
  72. # maxWaitMillis : 100000

SpringBoot启动类:

  1. package com.muses.taoshop;
  2. import org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration;
  3. import org.springframework.boot.*;
  4. import org.springframework.boot.autoconfigure.*;
  5. import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
  6. import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;
  7. import org.springframework.boot.web.servlet.ServletComponentScan;
  8. import org.springframework.cache.annotation.EnableCaching;
  9. import org.springframework.scheduling.annotation.EnableAsync;
  10. import org.springframework.scheduling.annotation.EnableScheduling;
  11. import org.springframework.stereotype.*;
  12. import org.springframework.transaction.annotation.EnableTransactionManagement;
  13. import org.springframework.web.bind.annotation.*;
  14. /**
  15. *
  16. * <pre>
  17. * SpringBoot启动配置类
  18. * </pre>
  19. * @author nicky
  20. * @version 1.00.00
  21. * <pre>
  22. * 修改记录
  23. * 修改后版本: 修改人: 修改日期: 修改内容:
  24. * </pre>
  25. */
  26. @Controller
  27. @EnableScheduling//开启对计划任务的支持
  28. @EnableTransactionManagement//开启对事务管理配置的支持
  29. @EnableCaching
  30. @EnableAsync//开启对异步方法的支持
  31. @EnableAutoConfiguration
  32. @ServletComponentScan
  33. @SpringBootApplication(exclude={DataSourceAutoConfiguration.class,
  34. MybatisAutoConfiguration.class,
  35. DataSourceTransactionManagerAutoConfiguration.class})
  36. public class PortalApplication {
  37. @RequestMapping("/")
  38. @ResponseBody
  39. String home() {
  40. return "portal web!";
  41. }
  42. @RequestMapping("/doTest")
  43. @ResponseBody
  44. String doTest(){
  45. System.out.println(Thread.currentThread().getName());
  46. String threadName = Thread.currentThread().getName();
  47. return threadName;
  48. }
  49. public static void main(String[] args) throws Exception {
  50. SpringApplication.run(PortalApplication.class, args);
  51. }
  52. }

写个Controller类跳转到门户网站:

ps:品类多级联动思路其实就是先构建一个树,我这里的做法就是先查询处理,然后通过工具类,进行递归遍历,待会给出工具类代码,仅供参考。listCategory方法其实就是获取所有的品类信息

  1. package com.muses.taoshop.web.controller.portal;
  2. import com.alibaba.fastjson.JSON;
  3. import com.muses.taoshop.item.entity.ItemBrand;
  4. import com.muses.taoshop.item.entity.ItemCategory;
  5. import com.muses.taoshop.item.entity.ItemPortal;
  6. import com.muses.taoshop.item.service.IItemBrankService;
  7. import com.muses.taoshop.item.service.IItemCategoryService;
  8. import com.muses.taoshop.item.service.IItemService;
  9. import com.muses.taoshop.util.CategoryTreeUtils;
  10. import com.muses.taoshop.web.controller.BaseController;
  11. import org.springframework.beans.factory.annotation.Autowired;
  12. import org.springframework.stereotype.Controller;
  13. import org.springframework.util.CollectionUtils;
  14. import org.springframework.web.bind.annotation.*;
  15. import org.springframework.web.servlet.ModelAndView;
  16. import java.util.Date;
  17. import java.util.List;
  18. /**
  19. * <pre>
  20. * 门户网站控制类
  21. * </pre>
  22. *
  23. * @author nicky
  24. * @version 1.00.00
  25. * <pre>
  26. * 修改记录
  27. * 修改后版本: 修改人: 修改日期: 修改内容:
  28. * </pre>
  29. */
  30. @Controller
  31. @RequestMapping("/portal")
  32. public class IndexController extends BaseController{
  33. @Autowired
  34. IItemService iItemService;
  35. @Autowired
  36. IItemBrankService iItemBrankService;
  37. @Autowired
  38. IItemCategoryService iItemCategoryService;
  39. /**
  40. * 跳转到门户网站
  41. * @return
  42. */
  43. @GetMapping(value = "/toIndex.do")
  44. public ModelAndView toIndex(){
  45. info("跳转到门户网站");
  46. ModelAndView mv = this.getModelAndView();
  47. mv.setViewName("index");
  48. List<ItemPortal> items = iItemService.listItemPortal();
  49. CategoryTreeUtils treeUtil = new CategoryTreeUtils();
  50. List<ItemCategory> list = iItemCategoryService.listCategory();
  51. List<ItemCategory> categories = treeUtil.buildCategoryTree(list);
  52. mv.addObject("items" , items);
  53. mv.addObject("categories" , categories);
  54. return mv;
  55. }
  56. @GetMapping(value = "/doTest")
  57. @ResponseBody
  58. public String doTest(){
  59. List<ItemBrand> itemBrands = iItemBrankService.listItemBrand();
  60. String str = JSON.toJSON(itemBrands).toString();
  61. return str;
  62. }
  63. }

业务接口类:

  1. package com.muses.taoshop.item.service;
  2. import com.muses.taoshop.item.entity.ItemCategory;
  3. import com.muses.taoshop.item.entity.ItemList;
  4. import java.util.List;
  5. /**
  6. * <pre>
  7. * 商品品类信息接口
  8. * </pre>
  9. *
  10. * @author nicky
  11. * @version 1.00.00
  12. * <pre>
  13. * 修改记录
  14. * 修改后版本: 修改人: 修改日期: 2018.06.17 10:59 修改内容:
  15. * </pre>
  16. */
  17. public interface IItemCategoryService {
  18. /**
  19. * 查询所有商品品类信息
  20. * @return
  21. */
  22. List<ItemCategory> listCategory();

业务服务实现类:

  1. package com.muses.taoshop.item.service;
  2. import com.muses.taoshop.item.entity.ItemCategory;
  3. import com.muses.taoshop.item.entity.ItemList;
  4. import com.muses.taoshop.item.mapper.ItemCategoryMapper;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.stereotype.Service;
  7. import java.util.List;
  8. /**
  9. * <pre>
  10. * 商品品类信息服务实现类
  11. * </pre>
  12. *
  13. * @author nicky
  14. * @version 1.00.00
  15. * <pre>
  16. * 修改记录
  17. * 修改后版本: 修改人: 修改日期: 2018.06.17 11:01 修改内容:
  18. * </pre>
  19. */
  20. @Service
  21. public class ItemCategoryServiceImpl implements IItemCategoryService{
  22. @Autowired
  23. ItemCategoryMapper itemCategoryMapper;
  24. /**
  25. * 查询所有的商品品类信息
  26. * @return
  27. */
  28. @Override
  29. public List<ItemCategory> listCategory() {
  30. return itemCategoryMapper.listCategory();
  31. }
  32. }

Mybatis相关代码:

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
  3. <mapper namespace="com.muses.taoshop.item.mapper.ItemCategoryMapper" >
  4. <resultMap id="BaseResultMap" type="com.muses.taoshop.item.entity.ItemCategory" >
  5. <id column="id" property="id" jdbcType="BIGINT" />
  6. <result column="category_name" property="categoryName" jdbcType="VARCHAR" />
  7. <result column="sjid" property="sjid" jdbcType="BIGINT" />
  8. <result column="last_modify_time" property="lastModifyTime" jdbcType="TIMESTAMP" />
  9. <result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
  10. </resultMap>
  11. <sql id="BaseColumnList" >
  12. id,
  13. category_name as categoryName,
  14. sjid,
  15. last_modify_time as lastModifyTime,
  16. create_time as createTime
  17. </sql>
  18. <!-- 获取所有的商品品类信息-->
  19. <select id="listCategory" resultType="ItemCategory">
  20. SELECT
  21. <include refid="BaseColumnList" />
  22. FROM item_category t
  23. </select>
  24. </mapper>

Mapper接口类:

  1. package com.muses.taoshop.item.mapper;
  2. import com.muses.taoshop.item.entity.ItemCategory;
  3. import com.muses.taoshop.item.entity.ItemList;
  4. import org.apache.ibatis.annotations.Mapper;
  5. import org.apache.ibatis.annotations.Param;
  6. import java.util.List;
  7. @Mapper
  8. public interface ItemCategoryMapper {
  9. List<ItemCategory> listCategory();
  10. }

实体类:

这里用了lombok的jar来实现,所有不用set和get方法

  1. package com.muses.taoshop.item.entity;
  2. import com.alibaba.fastjson.annotation.JSONField;
  3. import com.fasterxml.jackson.annotation.JsonFormat;
  4. import com.fasterxml.jackson.databind.annotation.JsonSerialize;
  5. import lombok.Data;
  6. import org.springframework.format.annotation.DateTimeFormat;
  7. import javax.validation.constraints.NotNull;
  8. import java.util.Date;
  9. import java.util.List;
  10. /**
  11. * <pre>
  12. * 商品品类
  13. * </pre>
  14. * @author nicky
  15. * @version 1.00.00
  16. * <pre>
  17. * 修改记录
  18. * 修改后版本: 修改人: 修改日期: 2018.06.09 21:49 修改内容:
  19. * </pre>
  20. */
  21. @Data
  22. public class ItemCategory {
  23. /**
  24. * 商品品类id
  25. */
  26. private Long id;
  27. /**
  28. * 商品品类名称
  29. */
  30. private String categoryName;
  31. /**
  32. * 上级id
  33. */
  34. private Long sjid;
  35. /**
  36. * 上次修改时间
  37. */
  38. @JSONField(format ="yyyy-MM-dd HH:mm:ss")
  39. private Date lastModifyTime;
  40. /**
  41. * 创建时间
  42. */
  43. @JSONField(format ="yyyy-MM-dd HH:mm:ss")
  44. private Date createTime;
  45. /**
  46. * 子菜单
  47. */
  48. private List<ItemCategory> subCategorys;
  49. }

构建品类树的工具类:

  1. package com.muses.taoshop.util;
  2. import com.muses.taoshop.item.entity.ItemCategory;
  3. import javax.mail.FetchProfile;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6. /**
  7. * <pre>
  8. * 构造一棵品类树
  9. * </pre>
  10. *
  11. * @author nicky
  12. * @version 1.00.00
  13. * <pre>
  14. * 修改记录
  15. * 修改后版本: 修改人: 修改日期: 2018.06.24 17:12 修改内容:
  16. * </pre>
  17. */
  18. public class CategoryTreeUtils {
  19. public List<ItemCategory> commonCategorys;
  20. public List<ItemCategory> list = new ArrayList<ItemCategory>();
  21. public List<ItemCategory> buildCategoryTree(List<ItemCategory> categories ) {
  22. this.commonCategorys = categories;
  23. for (ItemCategory c : categories){
  24. ItemCategory category = new ItemCategory();
  25. if(c.getSjid() == 0){
  26. category.setSjid(c.getSjid());
  27. category.setId(c.getId());
  28. category.setCategoryName(c.getCategoryName());
  29. category.setSubCategorys(treeChild(c.getId()));
  30. list.add(category);
  31. }
  32. }
  33. return list;
  34. }
  35. public List<ItemCategory> treeChild(long id){
  36. List<ItemCategory> list = new ArrayList<ItemCategory>();
  37. for(ItemCategory c : commonCategorys){
  38. ItemCategory category = new ItemCategory();
  39. if(c.getSjid() == id){
  40. category.setSjid(c.getSjid());
  41. category.setId(c.getId());
  42. category.setCategoryName(c.getCategoryName());
  43. category.setSubCategorys(treeChild(c.getId()));//递归循环
  44. list.add(category);
  45. }
  46. }
  47. return list;
  48. }
  49. }

前端代码:

  1. <div class="headerNav" xmlns:th="http://www.thymeleaf.org">
  2. <div class="layout">
  3. <dl class="all-brands">
  4. <dt class="all-brands-head"> <a href="#">全部商品分类</a> </dt>
  5. <dd class="all-brands-list">
  6. <div class="wrap" th:each="c : ${categories}">
  7. <div class="all-sort-list">
  8. <div class="item bo">
  9. <h3>
  10. <a href="" th:text="${c.categoryName}"></a></h3>
  11. <div class="item-list clearfix">
  12. <div class="close">x</div>
  13. <div class="subitem" th:each="s: ${c.subCategorys}">
  14. <dl class="fore1">
  15. <dt th:text="${s.categoryName}"><a th:href="@{'/portal/category/toCategoryList/'+${s.id}}"></a></dt>
  16. <dd>
  17. <em th:each="ss : ${s.subCategorys} "><a th:href="@{'/portal/category/toCategoryList/'+${ss.id}}" th:text="${ss.categoryName}"></a></em>
  18. </dd>
  19. </dl>
  20. </div>
  21. </div>
  22. </div>
  23. </div>
  24. </div>
  25. </dd>
  26. </dl>
  27. </div>
  28. </div>
  29. </div>

实现的效果如图:可以说是3级联动

这是在开发中的开源项目的一个小功能,源码已经开源,github链接

电商门户网站商品品类多级联动SpringBoot+Thymeleaf实现的更多相关文章

  1. SpringBoot电商项目实战 — 商品的SPU/SKU实现

    最近事情有点多,所以系列文章已停止好多天了.今天我们继续Springboot电商项目实战系列文章.到目前为止,整个项目的架构和基础服务已经全部实现,分布式锁也已经讲过了.那么,现在应该到数据库设计及代 ...

  2. 电商ERP系统——商品SKU与库存设计

    面试题经常问道,如何设计库存,哪些库存呢?分类属性的库存:不同颜色 不同尺码的属性的库存,这时候需要针对具体的SKU商品创建表. 总体思路 1.商品关联商品类别,商品类别关联多个商品属性,其中指定某几 ...

  3. Django电商项目---完成商品主页显示day2

    利用DjangoAdmin初始化数据库 创建项目 python manage.py startapp df_goods 添加配置 manas/urls.py manas/settings.py 新创建 ...

  4. Android开发之TextView中间设置横线,适用于电商项目,商品原价之类的功能。

    textview.getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG ); //中间横线 textview.getPaint().setFlags(Pai ...

  5. Java生鲜电商平台-商品基础业务架构设计-商品设计

    Java生鲜电商平台-商品基础业务架构设计-商品设计 在生鲜电商的商品中心,在电子商务公司一般是后台管理商品的地方.在前端而言,是商家为了展示商品信息给用户的地方,它是承担了商品的数据,订单,营销活动 ...

  6. 42、生鲜电商平台-商品的spu和sku数据结构设计与架构

    说明:Java开源生鲜电商平台中商品的spu和sku数据结构设计与架构,包括数据库图标与架构分析. 1. 先说明几个概念. 电商网站采用在商品模块,常采用spu+sku的数据结构算法,这种算法可以将商 ...

  7. Java生鲜电商平台-生鲜电商中商品类目、属性、品牌、单位架构设计与实战

    Java生鲜电商平台-生鲜电商中商品类目.属性.品牌.单位架构设计与实战 说明:Java生鲜电商平台-生鲜电商中商品类目.属性.品牌.单位架构设计与实战经验分享 凡是涉及到购物,必然是建立在商品的基础 ...

  8. 14 微服务电商【黑马乐优商城】:day04-项目搭建(一)

    本项目的笔记和资料的Download,请点击这一句话自行获取. day01-springboot(理论篇) :day01-springboot(实践篇) day02-springcloud(理论篇一) ...

  9. 电商类Web原型制作分享-IKEA

    IKEA是一个家居整合大型零售商,属于电商类官网.电商以展示商品.售后服务.购物流程为主.根据网站的图文方式排版,主导航栏使用的标签组,区域导航栏使用的是垂直选项卡,实现下拉弹出面板交互的功能. 本原 ...

随机推荐

  1. 网址导航18A

    [导航] hao268 百度导航 泡泡导航 35Q网址导航 [名站] 百度 网易 腾讯 新华 中新 凤凰 [邮箱] 163邮箱 126邮箱 Yeah邮箱 QQ邮箱 阿里邮箱 189邮箱 [新闻] 联合 ...

  2. POJ 3140.Contestants Division 基础树形dp

    Contestants Division Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10704   Accepted:  ...

  3. 安装swoole

    php需要安装swoole扩展 swoole4.3.2 cd /usr/local/src/ wget https://pecl.php.net/get/swoole-4.3.2.tgz tar -z ...

  4. laravel 默认所有请求带session解决办法

    laravel  app/Http/Kernel.php protected $middlewareGroups = [ 'web' => [ \App\Http\Middleware\Encr ...

  5. pip使用国内镜像安装各种库

    1. 指定阿里云镜像, 安装requirements.txt中的所有 pip install -i http://mirrors.aliyun.com/pypi/simple/ --trusted-h ...

  6. js unicode转中文 &#26041;&#26696;&#27010;&#36848;&#32852;&#32593;LED&#29031;&#26126;&#26041;&#26696;&#21487;&#25191;&#34892;&#20840;&#37096;&#30340;DALI &#21644;

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. 企业微信自建应用移动端动态获取li并给其事件问题总结

    前段时间一个项目增加企业微信移动端应用,其中几个小功能用到ul-li列表点击并获得相应数据: 开始用var lis=$('#ul li'); for(var=i;i<lis.length;i++ ...

  8. 小白的CTF学习之路3——二进制数据基础与运算(下)

    处理了二进制的整数运算,下面我们来进行令人绝望的浮点数运算 我们先来看一下float事列程序: #include<"stdio.sh"> int main() { fl ...

  9. Hibernate框架:CRM练习--保存客户

    crm:customer ralation manager 客户关系管理系统 一.准备 1.创建web项目 2.导包 最终为: 3.引入静态页面 将文件复制放入项目的WebContent目录下面: 4 ...

  10. SSH通过密钥登陆

    A服务器上操作 ssh-keygen -t rsa/dsa 后面所带参数rsa/dsa为加密方式,默认为dsa [root@localhost ~]# ssh-keygen Generating pu ...