JasonsGong.github.io/posts/45726.html

1281 lines
1.1 MiB
HTML
Raw Normal View History

2023-09-22 21:57:28 +08:00
<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0,viewport-fit=cover"><title>项目实战-谷粒商城 | The Blog</title><meta name="author" content="Jason"><meta name="copyright" content="Jason"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="一.项目简介1.项目背景电商模式 谷粒商城使用的B2C模式销售自营的商品给客户 市面上5种常见的电商模式B2B、B2C、C2B、C2C、O2O 1、B2B模式business to business是指商家与商家建立的商业关系。如阿里巴巴。 2、B2C模式business to consumer商对客模式。即通常说的商业零售供应商直接把商品卖给用户。如苏宁易购、京东、天猫、小米商城。">
<meta property="og:type" content="article">
<meta property="og:title" content="项目实战-谷粒商城">
2024-05-10 10:21:35 +08:00
<meta property="og:url" content="https://qingling.icu/posts/45726.html">
2023-09-22 21:57:28 +08:00
<meta property="og:site_name" content="The Blog">
<meta property="og:description" content="一.项目简介1.项目背景电商模式 谷粒商城使用的B2C模式销售自营的商品给客户 市面上5种常见的电商模式B2B、B2C、C2B、C2C、O2O 1、B2B模式business to business是指商家与商家建立的商业关系。如阿里巴巴。 2、B2C模式business to consumer商对客模式。即通常说的商业零售供应商直接把商品卖给用户。如苏宁易购、京东、天猫、小米商城。">
<meta property="og:locale" content="zh_CN">
2024-06-14 22:00:25 +08:00
<meta property="og:image" content="https://qingling.icu/img/8.png">
2023-09-22 21:57:28 +08:00
<meta property="article:published_time" content="2023-05-17T16:06:28.000Z">
2024-05-10 13:32:25 +08:00
<meta property="article:modified_time" content="2024-05-10T05:31:11.623Z">
2023-09-22 21:57:28 +08:00
<meta property="article:author" content="Jason">
<meta property="article:tag" content="项目实战">
<meta name="twitter:card" content="summary">
2024-06-14 22:00:25 +08:00
<meta name="twitter:image" content="https://qingling.icu/img/8.png"><link rel="shortcut icon" href="/img/%E5%9B%BE%E6%A0%87.png"><link rel="canonical" href="https://qingling.icu/posts/45726.html"><link rel="preconnect" href="//fastly.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="/cdn/icon/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="/cdn/css/snackbar.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="/cdn/css/fancybox.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = {
2023-09-22 21:57:28 +08:00
root: '/',
algolia: undefined,
localSearch: {"path":"/search.xml","preload":true,"top_n_per_article":1,"unescape":false,"languages":{"hits_empty":"找不到您查询的内容:${query}","hits_stats":"共找到 ${hits} 篇文章"}},
translate: undefined,
noticeOutdate: undefined,
highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":400},
copy: {
success: '复制成功',
error: '复制错误',
noSupport: '浏览器不支持'
},
relativeDate: {
homepage: true,
post: true
},
runtime: '天',
dateSuffix: {
just: '刚刚',
min: '分钟前',
hour: '小时前',
day: '天前',
month: '个月前'
},
copyright: undefined,
lightbox: 'mediumZoom',
2023-12-09 14:21:01 +08:00
Snackbar: {"chs_to_cht":"你已切换为繁体","cht_to_chs":"你已切换为简体","day_to_night":"你已切换为深色模式","night_to_day":"你已切换为浅色模式","bgLight":"#006650","bgDark":"#006650","position":"top-center"},
2023-09-22 21:57:28 +08:00
source: {
justifiedGallery: {
2023-09-30 18:36:25 +08:00
js: 'https://fastly.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.js',
css: 'https://fastly.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.css'
2023-09-22 21:57:28 +08:00
}
},
isPhotoFigcaption: false,
islazyload: false,
2023-12-10 21:57:00 +08:00
isAnchor: true,
2023-09-22 21:57:28 +08:00
percent: {
toc: true,
rightside: false,
},
2023-12-09 19:59:36 +08:00
autoDarkmode: true
2023-09-22 21:57:28 +08:00
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
title: '项目实战-谷粒商城',
isPost: true,
isHome: false,
isHighlightShrink: false,
isToc: true,
2024-05-10 13:32:25 +08:00
postUpdate: '2024-05-10 13:31:11'
2023-09-22 21:57:28 +08:00
}</script><noscript><style type="text/css">
#nav {
opacity: 1
}
.justified-gallery img {
opacity: 1
}
#recent-posts time,
#post-meta time {
display: inline !important
}
</style></noscript><script>(win=>{
win.saveToLocal = {
set: function setWithExpiry(key, value, ttl) {
if (ttl === 0) return
const now = new Date()
const expiryDay = ttl * 86400000
const item = {
value: value,
expiry: now.getTime() + expiryDay,
}
localStorage.setItem(key, JSON.stringify(item))
},
get: function getWithExpiry(key) {
const itemStr = localStorage.getItem(key)
if (!itemStr) {
return undefined
}
const item = JSON.parse(itemStr)
const now = new Date()
if (now.getTime() > item.expiry) {
localStorage.removeItem(key)
return undefined
}
return item.value
}
}
win.getScript = url => new Promise((resolve, reject) => {
const script = document.createElement('script')
script.src = url
script.async = true
script.onerror = reject
script.onload = script.onreadystatechange = function() {
const loadState = this.readyState
if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
script.onload = script.onreadystatechange = null
resolve()
}
document.head.appendChild(script)
})
win.getCSS = (url,id = false) => new Promise((resolve, reject) => {
const link = document.createElement('link')
link.rel = 'stylesheet'
link.href = url
if (id) link.id = id
link.onerror = reject
link.onload = link.onreadystatechange = function() {
const loadState = this.readyState
if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
link.onload = link.onreadystatechange = null
resolve()
}
document.head.appendChild(link)
})
win.activateDarkMode = function () {
document.documentElement.setAttribute('data-theme', 'dark')
if (document.querySelector('meta[name="theme-color"]') !== null) {
document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
}
}
win.activateLightMode = function () {
document.documentElement.setAttribute('data-theme', 'light')
if (document.querySelector('meta[name="theme-color"]') !== null) {
document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
}
}
const t = saveToLocal.get('theme')
2023-12-09 19:59:36 +08:00
const isDarkMode = window.matchMedia('(prefers-color-scheme: dark)').matches
const isLightMode = window.matchMedia('(prefers-color-scheme: light)').matches
const isNotSpecified = window.matchMedia('(prefers-color-scheme: no-preference)').matches
const hasNoSupport = !isDarkMode && !isLightMode && !isNotSpecified
if (t === undefined) {
if (isLightMode) activateLightMode()
else if (isDarkMode) activateDarkMode()
else if (isNotSpecified || hasNoSupport) {
const now = new Date()
const hour = now.getHours()
const isNight = hour <= 8 || hour >= 22
isNight ? activateDarkMode() : activateLightMode()
}
window.matchMedia('(prefers-color-scheme: dark)').addListener(function (e) {
if (saveToLocal.get('theme') === undefined) {
e.matches ? activateDarkMode() : activateLightMode()
}
})
} else if (t === 'light') activateLightMode()
else activateDarkMode()
2023-09-22 21:57:28 +08:00
const asideStatus = saveToLocal.get('aside-status')
if (asideStatus !== undefined) {
if (asideStatus === 'hide') {
document.documentElement.classList.add('hide-aside')
} else {
document.documentElement.classList.remove('hide-aside')
}
}
const detectApple = () => {
if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
document.documentElement.classList.add('apple')
}
}
detectApple()
2024-06-14 22:00:25 +08:00
})(window)</script><script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script><script type="text/javascript" src ="/js/welcome.js" ></script><script src="/js/sweetalert.js"></script><link rel="stylesheet" href="/css/sweetalert.css"><!-- hexo injector head_end start --><link rel="stylesheet" href="https://npm.elemecdn.com/hexo-butterfly-swiper/lib/swiper.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://npm.elemecdn.com/hexo-butterfly-swiper/lib/swiperstyle.css" media="print" onload="this.media='all'"><!-- hexo injector head_end end --><meta name="generator" content="Hexo 6.3.0"></head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/img/avatar.jpg" onerror="onerror=null;src='/img/loading.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">60</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">39</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">10</div></a></div><br/><div class="menus_items"><div class="menus_item"><a class="site-page" target="_blank" rel="noopener" href="https://www.tutorialspoint.com/compile_java8_online.php"><i class="fa-fw fas fa-code"></i><span> 代码</span></a></div><div class="menus_item"><a class="site-page" href="/notice/"><i class="fa-fw fas fa-stream"></i><span> 公告</span></a></div><div class="menus_item"><a class="site-page" href="/website/"><i class="fa-fw fas fa-list"></i><span> 网址</span></a></div><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="not-top-img" id="page-header"><nav id="nav"><span id="blog-info"><a href="/" title="The Blog"><img class="site-icon" src="/img/logo.png"/><span class="site-name">The Blog</span></a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search" href="javascript:void(0);"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" target="_blank" rel="noopener" href="https://www.tutorialspoint.com/compile_java8_online.php"><i class="fa-fw fas fa-code"></i><span> 代码</span></a></div><div class="menus_item"><a class="site-page" href="/notice/"><i class="fa-fw fas fa-stream"></i><span> 公告</span></a></div><div class="menus_item"><a class="site-page" href="/website/"><i class="fa-fw fas fa-list"></i><span> 网址</span></a></div><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div></div><div id="toggle-menu"><a class="site-page" href="javascript:void(0);"><i class="fas fa-bars fa-fw"></i></a></div></div></nav></header><main class="layout" id="content-inner"><div id="post"><div id="post-info"><h1 class="post-title">项目实战-谷粒商城</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2023-05-17T16:06:28.000Z" title="发表于 2023-05-18 00:06:28">2023-05-18</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2024-05-10T05:31:11.623Z" title="更新于 2024-05-10 13:31:11">2024-05-10</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E5%90%8E%E7%AB%AF/">后端</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word
2023-09-22 21:57:28 +08:00
<p>市面上5种常见的电商模式B2B、B2C、C2B、C2C、O2O</p>
<p><strong>1、B2B模式</strong><br>business to business是指商家与商家建立的商业关系。如阿里巴巴。</p>
<p><strong>2、B2C模式</strong><br>business to consumer商对客模式。即通常说的商业零售供应商直接把商品卖给用户。如苏宁易购、京东、天猫、小米商城。</p>
<p><strong>3、C2B模式</strong><br>customer to business即消费者对企业。先有消费者需求而后有企业生产。例如众筹类的商城。</p>
<p><strong>4、C2C模式</strong><br>customer to consumer客户之间直接把东西放上网上去卖。如淘宝、闲鱼。</p>
<p><strong>5、O2O模式</strong><br>online to offline线上线下。线上快速支付线下优质服务。如饿了么、美团、京东到家。</p>
<h2 id="2-项目架构图"><a href="#2-项目架构图" class="headerlink" title="2.项目架构图"></a>2.项目架构图</h2><p><strong>项目的架构图</strong></p>
<p><img src="/pictures/image-20230518000727005.png"></p>
<p> <strong>微服务划分图</strong></p>
<p><img src="/pictures/image-20230518015831413.png" alt="image-20230518015831413"></p>
<h2 id="3-项目技术和特色"><a href="#3-项目技术和特色" class="headerlink" title="3.项目技术和特色"></a>3.项目技术和特色</h2><ul>
<li><strong>前后端分离开发开发基于VUE的后台管理系统</strong></li>
<li><strong>SpringCloud全新解决方案</strong></li>
<li><strong>应用监控、限流、网关、熔断降级等分布式方案 全方位涉及</strong></li>
<li><strong>包含分布式事务、分布式锁等技术难点</strong></li>
<li><strong>分析高并发场景的编码方式、线程池、异步编排等使用</strong></li>
<li><strong>压力测试与性能优化</strong></li>
<li><strong>各种集群技术的区别已经使用</strong></li>
<li><strong>CI&#x2F;CD的使用</strong></li>
</ul>
<h2 id="4-项目的前置要求"><a href="#4-项目的前置要求" class="headerlink" title="4.项目的前置要求"></a>4.项目的前置要求</h2><ul>
<li><strong>熟悉SpringBoot以及常见的整合方案</strong></li>
<li><strong>了解SpringCloud</strong></li>
<li><strong>熟悉Maven git</strong></li>
<li><strong>熟悉linux、redis、docker基本操作</strong></li>
<li><strong>了解html、css、js、vue</strong></li>
<li><strong>熟练使用idea开发项目</strong></li>
</ul>
<h1 id="二-分布式基础概念"><a href="#二-分布式基础概念" class="headerlink" title="二.分布式基础概念"></a>二.分布式基础概念</h1><h2 id="1-微服务"><a href="#1-微服务" class="headerlink" title="1.微服务"></a>1.微服务</h2><p>微服务架构风格,就像是一个<strong>单独的应用程序</strong>开发为<strong>一套小服务</strong>每个服务运行在自己的进程中并使用轻量级机制通信通常是HTTP API。这些服务围绕业务能力来构建并通过完全自动化部署机制来独立部署。这些服务使用不同的编程语言编写以及不同的数据存储技术并保持最低限度的集中式管理。</p>
<p><strong>简而言之:拒绝大型单体应用,基于业务边界进行服务微化拆分,各个应用独立部署运行。</strong></p>
<h2 id="2-集群-分布式-节点"><a href="#2-集群-分布式-节点" class="headerlink" title="2.集群|分布式|节点"></a>2.集群|分布式|节点</h2><p><strong>集群是一个物理形态 ,分布式是个工作方式</strong></p>
<p><strong>分布式:</strong> 将不同的业务分布在不同的地方</p>
<p><strong>集群:</strong> 集群指的是将几台服务器集中在一起,实现同一业务</p>
<p><strong>分布式中的每一个节点,都可以做集群。而集群不一定是分布式的。</strong></p>
<p><strong>节点:</strong> 集群中的一个服务器</p>
<h2 id="3-远程调用"><a href="#3-远程调用" class="headerlink" title="3.远程调用"></a>3.远程调用</h2><p><strong>在分布式系统中,各个服务可能处于不同主机,但是服务之间不可避免的需要相互调用,我们称之为远程调用。</strong></p>
<p>SpringCloud中使用HTTP+JSON的方式完成远程调用</p>
<p><img src="/pictures/image-20230518010847966.png" alt="image-20230518010847966"></p>
<h2 id="4-负载均衡"><a href="#4-负载均衡" class="headerlink" title="4.负载均衡"></a>4.负载均衡</h2><img src="/pictures/image-20230518011030343.png" alt="image-20230518011030343" style="zoom:150%;" />
<pre><code>分布式系统中A服务需要调用B服务B服务在多台机器上都存在A调用任意一个服务均可完成功能。为了使每一个服务器都不要太忙或者太闲我们可以使用负载均衡的调用每一个服务器提升网站的健壮性。
</code></pre>
<p><strong>常见的负载均衡算法:</strong></p>
<p><strong>轮询:</strong> 为第一个请求选择健康池中的第一台后端服务器,然后按顺序往后依次选择,直到最后一个,然后循环。</p>
<p><strong>最小连接:</strong> 有限选择连接数量少,也就是压力最小的后端服务器,在会话较长的情况下可以考虑采取这种方式。</p>
<p><strong>随机法:</strong>通过系统的随机算法,根据后端服务器的列表大小值来随机选取其中的一台服务器进行访问。由概率统计理论可以得知,随着客户端调用服务端的次数增多,其实际效果越来越接近于平均分配调用量到后端的每一台服务器,也就是轮询的结果。</p>
<p><strong>源地址哈希法:</strong>源地址哈希的思想是根据获取客户端的IP地址通过哈希函数计算得到的一个数值用该数值对服务器列表的大小进行取模运算得到的结果便是客服端要访问服务器的序号。采用源地址哈希法进行负载均衡同一IP地址的客户端当后端服务器列表不变时它每次都会映射到同一台后端服务器进行访问。</p>
<p><strong>加权轮询法:</strong>不同的后端服务器可能机器的配置和当前系统的负载并不相同,因此它们的抗压能力也不相同。给配置高、负载低的机器配置更高的权重,让其处理更多的请;而配置低、负载高的机器,给其分配较低的权重,降低其系统负载,加权轮询能很好地处理这一问题,并将请求顺序且按照权重分配到后端。</p>
<p><strong>加权随机法:</strong>与加权轮询法一样,加权随机法也根据后端机器的配置,系统的负载分配不同的权重。不同的是,它是按照权重随机请求后端服务器,而非顺序。</p>
<h2 id="5-服务注册-发现-注册中心"><a href="#5-服务注册-发现-注册中心" class="headerlink" title="5.服务注册|发现|注册中心"></a>5.服务注册|发现|注册中心</h2><p><img src="/pictures/image-20230518012211042.png" alt="image-20230518012211042"></p>
<h2 id="6-配置中心"><a href="#6-配置中心" class="headerlink" title="6.配置中心"></a>6.配置中心</h2><pre><code>每一个服务最终会有大量的配置,并且每个服务都可能部署在多台机器上。我们经常需要变更配置,我们可以让每个服务在配置中心获取自己的配置。
</code></pre>
<p><img src="/pictures/image-20230518012538349.png" alt="image-20230518012538349"></p>
<h2 id="7-服务熔断和服务降级"><a href="#7-服务熔断和服务降级" class="headerlink" title="7.服务熔断和服务降级"></a>7.服务熔断和服务降级</h2><pre><code>在微服务的架构中,微服务之间通过网络进行通信,存在相互依赖,当其中的一个服务不可用时,有可能会造成雪崩效应。要防止这样的情况,必须要有容错机制来保护服务。
</code></pre>
<p><strong>服务熔断:</strong> 设置服务的超时,当被调用的服务经常失败到达某个阈值,我们可以开启短路保护机制,后来的请求不再去调用这个服务。本地直接返回默认的数据。</p>
<p><strong>服务降级:</strong> 在运维期间,当系统处于高峰期,系统资源紧张,我们可以让非核心的业务降级运行。降级:某些业务不处理,或者简单处理[抛异常、返回NULL、调用mock数据、调用Fallback处理逻辑]。</p>
<p><img src="/pictures/image-20230518013254404.png" alt="image-20230518013254404"></p>
<p><img src="/pictures/image-20230518013624395.png" alt="image-20230518013624395"></p>
<h2 id="8-API网关"><a href="#8-API网关" class="headerlink" title="8.API网关"></a>8.API网关</h2><pre><code>在微服务的架构中API Gateway作为整体架构的重要组件他抽象了微服务中都需要的公共功能同时提供了客户端的负载均衡服务自动熔断灰度发布、统一认证、限流流控、日志统计等丰富功能帮助我们解决很多API管理难题。
</code></pre>
<img src="/pictures/image-20230518014732955.png" alt="image-20230518014732955" style="zoom:150%;" />
<h1 id="三-环境搭建"><a href="#三-环境搭建" class="headerlink" title="三.环境搭建"></a>三.环境搭建</h1><h2 id="1-安装虚拟机"><a href="#1-安装虚拟机" class="headerlink" title="1.安装虚拟机"></a>1.<strong>安装虚拟机</strong></h2><p>需要使用虚拟机安装相关的软件和部署集群</p>
2024-05-10 13:32:25 +08:00
<p><strong><a href="https://qingling.icu/posts/47407.html">VMWare虚拟机安装Linux教程 | The Blog (qingling.icu)</a></strong></p>
2023-09-22 21:57:28 +08:00
<p>将Linxu的IP地址固定下来</p>
2024-05-10 13:32:25 +08:00
<p><strong><a href="https://qingling.icu/posts/21883.html">Linux设置静态IP | The Blog (qingling.icu)</a></strong></p>
2023-09-22 21:57:28 +08:00
<h2 id="2-Docker安装与配置"><a href="#2-Docker安装与配置" class="headerlink" title="2.Docker安装与配置"></a>2.Docker安装与配置</h2><p>包含docker安装、开启开机自启动、配置镜像加速服务</p>
2024-05-10 13:32:25 +08:00
<p><strong><a href="https://qingling.icu/posts/19306.html">Docker容器化技术 | The Blog (qingling.icu)</a></strong><img src="/pictures/image-20230518141429712.png" alt="image-20230518141429712"></p>
2023-09-22 21:57:28 +08:00
<p><img src="/pictures/image-20230518145456236.png" alt="image-20230518145456236"></p>
<h2 id="3-Docker安装mysql"><a href="#3-Docker安装mysql" class="headerlink" title="3.Docker安装mysql"></a>3.Docker安装mysql</h2><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">#</span><span class="language-bash">下载mysql的镜像文件 这里以下载mysql5.7为例</span></span><br><span class="line">docker pull mysql:5.7</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">查看是否下载成功</span></span><br><span class="line">docker images</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">使用MySQL的镜像启动一个容器</span></span><br><span class="line"> docker run -p 3306:3306 --name mysql \</span><br><span class="line">-v /mydata/mysql/log:/var/log/mysql \</span><br><span class="line">-v /mydata/mysql/data:/var/lib/mysql \</span><br><span class="line">-v /mydata/mysql/conf:/etc/mysql \</span><br><span class="line">-e MYSQL_ROOT_PASSWORD=root \</span><br><span class="line">-d mysql:5.7</span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">参数说明</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">-p 3306:3306将容器的 3306 端口映射到主机的 3306 端口</span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">-v /mydata/mysql/conf:/etc/mysql将配置文件夹挂载到主机</span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">-v /mydata/mysql/log:/var/log/mysql将日志文件夹挂载到主机</span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">-v /mydata/mysql/data:/var/lib/mysql/:将配置文件夹挂载到主机</span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">-e MYSQL_ROOT_PASSWORD=root<74><EFBC9A>
<h2 id="4-Docker安装redis"><a href="#4-Docker安装redis" class="headerlink" title="4.Docker安装redis"></a>4.Docker安装redis</h2><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">#</span><span class="language-bash">下载redis的镜像文件 直接下载最新的</span></span><br><span class="line">docker pull redis</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">创建redis挂载在宿主机的配置文件和目录</span></span><br><span class="line">mkdir -p /mydata/redis/conf #创建目录</span><br><span class="line">touch /mydata/redis/conf/redis.conf #创建配置文件</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">创建并启动容器 这是以配置文件的方式启动 \前面有一个空格</span></span><br><span class="line">docker run -p 6379:6379 --name redis \</span><br><span class="line">-v /mydata/redis/data:/data \</span><br><span class="line">-v /mydata/redis/conf/redis.conf:/etc/redis/redis.conf \</span><br><span class="line">-d redis redis-server /etc/redis/redis.conf</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">检查容器的运行情况</span></span><br><span class="line">docker ps</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">测试客户端的使用</span></span><br><span class="line">docker exec -it redis redis-cli</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">可以在redis客户端中使用如下命令</span></span><br><span class="line">127.0.0.1:6379&gt; set name tom #存值</span><br><span class="line">OK</span><br><span class="line">127.0.0.1:6379&gt; get name #取值</span><br><span class="line">&quot;tom&quot;</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">redis中的数据不能持久化 关机重启之后数据会丢失</span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">通过以下的配置 实现redis的持久化操作 AOF的持久化</span></span><br><span class="line">vi /mydata/redis/conf/redis.conf #修改之前创建的配置文件</span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">添加如下配置</span></span><br><span class="line">appendonly yes</span><br><span class="line"><span class="meta prompt_"></span></s
<h2 id="5-统一开发环境"><a href="#5-统一开发环境" class="headerlink" title="5.统一开发环境"></a>5.统一开发环境</h2><p><strong>其中Java使用的是java8及以上</strong></p>
<p><strong>Maven 配置阿里云的镜像 profiles</strong></p>
2024-05-10 13:32:25 +08:00
<p><strong><a href="https://qingling.icu/posts/63333.html">开发环境的搭建 | The Blog (qingling.icu)</a></strong></p>
2023-09-22 21:57:28 +08:00
<p><strong>IDEA中修改使用自己安装的Maven</strong></p>
<p><strong>IDEA插件:Lombok|MybatisX</strong></p>
<p><strong>VSCode插件:</strong></p>
<p><img src="/pictures/image-20230518160444927.png" alt="image-20230518160444927"></p>
<p><img src="/pictures/image-20230518155308167.png" alt="image-20230518155308167"></p>
<h2 id="6-配置Git"><a href="#6-配置Git" class="headerlink" title="6.配置Git"></a>6.配置Git</h2><p><strong>官网下载:<a target="_blank" rel="noopener" href="https://git-scm.com/downloads">https://git-scm.com/downloads</a></strong></p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">#</span><span class="language-bash">以下的操作在下载安装完毕之后进行</span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">1.鼠标在桌面右键 选择Git Bash Here 打开控制台</span></span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">2.配置用户名和邮箱</span></span><br><span class="line">git config --global user.name &quot;用户名&quot; #随意</span><br><span class="line">git config --global user.email &quot;邮箱&quot; #自己的邮箱</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">3.配置SSH免密连接</span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">生成密钥</span></span><br><span class="line">ssh-keygen -t rsa -C &quot;在码云上注册的邮箱地址&quot; #连续三次回车</span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">查看密钥并复制公钥的内容</span></span><br><span class="line">cat ~/.ssh/id_rsa.pub</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">4.将密钥的复制到码云的SSH公钥中</span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">4.1添加公钥 公钥名随意 公钥内容就是上面复制的内容</span></span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">5.测试</span></span><br><span class="line">ssh -T git@gitee.com</span><br></pre></td></tr></table></figure>
<p><strong>常用命令</strong></p>
<img src="/pictures/image-20230518161145808.png" alt="image-20230518161145808" style="zoom: 200%;" />
<h2 id="7-从Gitee上初始化一个项目"><a href="#7-从Gitee上初始化一个项目" class="headerlink" title="7.从Gitee上初始化一个项目"></a>7.从Gitee上初始化一个项目</h2><p>我们是先从码云上初始化一个项目 然后拉取到本地</p>
<p><strong>创建一个仓库</strong></p>
<p><img src="/pictures/image-20230518163703370.png" alt="image-20230518163703370"></p>
<p><strong>复制刚才创建仓库的地址</strong></p>
<p><img src="/pictures/image-20230518163838162.png" alt="image-20230518163838162"></p>
<p><strong>将工程导入到IDEA中</strong></p>
<p><img src="/pictures/image-20230518164005690.png" alt="image-20230518164005690"></p>
<p><strong>导入成功</strong></p>
<p><img src="/pictures/image-20230518164217773.png" alt="image-20230518164217773"></p>
<h2 id="8-创建微服务模块"><a href="#8-创建微服务模块" class="headerlink" title="8.创建微服务模块"></a>8.创建微服务模块</h2><img src="/pictures/image-20230518171355588.png" alt="image-20230518171355588" style="zoom:150%;" />
<h2 id="9-数据库设计"><a href="#9-数据库设计" class="headerlink" title="9.数据库设计"></a>9.数据库设计</h2><p><img src="/pictures/image-20230518171546236.png" alt="image-20230518171546236"></p>
<img src="/pictures/image-20230518173203605.png" alt="image-20230518173203605" style="zoom:200%;" />
<h2 id="10-后台管理系统的搭建"><a href="#10-后台管理系统的搭建" class="headerlink" title="10.后台管理系统的搭建"></a>10.后台管理系统的搭建</h2><h3 id="10-1-后端的搭建"><a href="#10-1-后端的搭建" class="headerlink" title="10.1.后端的搭建"></a>10.1.后端的搭建</h3><p><strong>后台管理系统使用的是人人开源提供的一个脚手架:</strong></p>
<p><strong>仓库的地址信息:</strong></p>
<p><strong>仓库主页:<a target="_blank" rel="noopener" href="https://gitee.com/renrenio">https://gitee.com/renrenio</a></strong></p>
<p><strong>后端renren-fast<a target="_blank" rel="noopener" href="https://gitee.com/renrenio/renren-fast.git">https://gitee.com/renrenio/renren-fast.git</a></strong></p>
<p><strong>前端renren-fast-vue<a target="_blank" rel="noopener" href="https://gitee.com/renrenio/renren-fast-vue.git">https://gitee.com/renrenio/renren-fast-vue.git</a></strong></p>
<p>将其作为一个模块集成到gulimall中</p>
<p><img src="/pictures/image-20230518221222845.png" alt="image-20230518221222845"></p>
<p>同时创建好这个脚手架需要的数据库</p>
<p><img src="/pictures/image-20230518221433314.png" alt="image-20230518221433314"></p>
<p><strong>修改配置文件中数据库账号密码ip的设置启动项目看有没有报错</strong></p>
<h3 id="10-2-前端的搭建"><a href="#10-2-前端的搭建" class="headerlink" title="10.2 前端的搭建"></a>10.2 前端的搭建</h3><p><strong>安装node.js 和 npm包管理工具 (node.js自带npm包管理工具)</strong></p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">#</span><span class="language-bash">检查是否安装成功</span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">出现版本号就是安装成功了</span></span><br><span class="line">node -v</span><br><span class="line">npm -v</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">npm配置淘宝的镜像 下载依赖的速度更快</span></span><br><span class="line">npm config set registry https://registry.npmmirror.com</span><br></pre></td></tr></table></figure>
<p><strong>将项目导入到vscode中并安装依赖</strong></p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">#</span><span class="language-bash">安装依赖的命令</span></span><br><span class="line">npm install</span><br></pre></td></tr></table></figure>
<p><img src="/pictures/image-20230518223021928.png" alt="image-20230518223021928"></p>
<p><strong>下载完成之后,运行前端的项目</strong></p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">#</span><span class="language-bash">运行命令</span></span><br><span class="line">npm run dev</span><br></pre></td></tr></table></figure>
<p><strong>运行成功过之后访问 <a target="_blank" rel="noopener" href="http://localhost:8001/">http://localhost:8001</a></strong></p>
<p><strong>成功的页面显示如下</strong></p>
<p><img src="/pictures/image-20230518223525151.png" alt="image-20230518223525151"></p>
<p>**使用账号:admin密码:admin 登录 **</p>
<p><strong>可以登录成功并显示以下的页面说明前后端的联调通过</strong></p>
<p><img src="/pictures/image-20230518223921179.png" alt="image-20230518223921179"></p>
<h2 id="11-快速开发-逆向工程的搭建"><a href="#11-快速开发-逆向工程的搭建" class="headerlink" title="11.快速开发-逆向工程的搭建"></a>11.快速开发-逆向工程的搭建</h2><h3 id="1-代码生成器快速使用案例"><a href="#1-代码生成器快速使用案例" class="headerlink" title="1.代码生成器快速使用案例"></a>1.代码生成器快速使用案例</h3><p><strong>人人开源代码生成器的地址:<a target="_blank" rel="noopener" href="https://gitee.com/renrenio/renren-generator.git">https://gitee.com/renrenio/renren-generator.git</a></strong></p>
<p><strong>集成代码生成器</strong></p>
<p><img src="/pictures/image-20230518230016421.png" alt="image-20230518230016421"></p>
<p><strong>修改代码生成器的配置文件</strong></p>
<p><strong>1.修改yml配置文件中数据库的连接信息</strong></p>
<p><strong>2.修改properties中与代码生成相关的配置信息</strong></p>
<p>配置举例 根据不同的模块 不同的数据库 配置不同</p>
<figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#代码生成器配置信息</span></span><br><span class="line"></span><br><span class="line"><span class="attr">mainPath</span>=<span class="string">com.atguigu</span></span><br><span class="line"><span class="comment">#包名</span></span><br><span class="line"><span class="attr">package</span>=<span class="string">com.atguigu.gulimall</span></span><br><span class="line"><span class="attr">moduleName</span>=<span class="string">product</span></span><br><span class="line"><span class="comment">#作者</span></span><br><span class="line"><span class="attr">author</span>=<span class="string">JasonGong</span></span><br><span class="line"><span class="comment">#Email</span></span><br><span class="line"><span class="attr">email</span>=<span class="string">JasonGong@gmail.com</span></span><br><span class="line"><span class="comment">#表前缀(类名不会包含表前缀)</span></span><br><span class="line"><span class="attr">tablePrefix</span>=<span class="string">pms_</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">#以下的配置信息一般不动</span></span><br><span class="line"><span class="comment">#类型转换配置信息</span></span><br><span class="line"><span class="attr">tinyint</span>=<span class="string">Integer</span></span><br><span class="line"><span class="attr">smallint</span>=<span class="string">Integer</span></span><br><span class="line"><span class="attr">mediumint</span>=<span class="string">Integer</span></span><br><span class="line"><span class="attr">int</span>=<span class="string">Integer</span></span><br><span class="line"><span class="attr">integer</span>=<span class="string">Integer</span></span><br><span class="line"><span cl
<p><strong>3.运行项目</strong></p>
<p><strong>通过<a target="_blank" rel="noopener" href="http://localhost:81/">http://localhost:81</a> 访问</strong></p>
<p><img src="/pictures/image-20230518234030568.png" alt="image-20230518234030568"></p>
<p><strong>4.使用生成代码的功能生成代码</strong></p>
<p><img src="/pictures/image-20230518234311272.png" alt="image-20230518234311272"></p>
<p>解压之后的文件如下</p>
<p><img src="/pictures/image-20230518234941961.png" alt="image-20230518234941961"></p>
<p>压缩包中的文件如下所示</p>
<p><img src="/pictures/image-20230518234922087.png" alt="image-20230518234922087"></p>
<p><strong>由于项目中没有使用shiro安全框架我们注释掉安全框架的注解生成模板</strong></p>
<p><img src="/pictures/image-20230519002146272.png" alt="image-20230519002146272"></p>
<p><strong>配置每个模块的配置文件</strong></p>
<figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">server:</span></span><br><span class="line"> <span class="attr">port:</span> <span class="number">8080</span></span><br><span class="line"><span class="attr">spring:</span></span><br><span class="line"> <span class="attr">datasource:</span></span><br><span class="line"> <span class="attr">url:</span> <span class="string">jdbc:mysql://192.168.111.100:3306/gulimall_pms?useUnicode=true&amp;characterEncoding=UTF-8&amp;useSSL=false&amp;serverTimezone=Asia/Shanghai</span></span><br><span class="line"> <span class="attr">username:</span> <span class="string">root</span></span><br><span class="line"> <span class="attr">password:</span> <span class="string">root</span></span><br><span class="line"> <span class="attr">driver-class-name:</span> <span class="string">com.mysql.cj.jdbc.Driver</span></span><br><span class="line"><span class="attr">mybatis-plus:</span></span><br><span class="line"> <span class="attr">mapper-locations:</span> <span class="string">classpath*:/mapper/**/*.xml</span> <span class="comment">#定义mapper.xml文件的位置</span></span><br><span class="line"> <span class="attr">global-config:</span></span><br><span class="line"> <span class="attr">db-config:</span></span><br><span class="line"> <span class="attr">id-type:</span> <span class="string">auto</span> <span class="comment">#主键的生成规则</span></span><br></pre></td></tr></table></figure>
<p><strong>测试生成的代码</strong></p>
<p><img src="/pictures/image-20230519005131468.png" alt="image-20230519005131468"></p>
<h3 id="2-代码生成器的使用步骤"><a href="#2-代码生成器的使用步骤" class="headerlink" title="2.代码生成器的使用步骤"></a>2.代码生成器的使用步骤</h3><p><strong>1.修改代码生成器的配置文件generator.properties</strong></p>
<figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#代码生成器配置信息</span></span><br><span class="line"></span><br><span class="line"><span class="attr">mainPath</span>=<span class="string">com.atguigu</span></span><br><span class="line"><span class="comment">#包名</span></span><br><span class="line"><span class="attr">package</span>=<span class="string">com.atguigu.gulimall</span></span><br><span class="line"><span class="attr">moduleName</span>=<span class="string">ware #1.修改模块名 </span></span><br><span class="line"><span class="comment">#作者</span></span><br><span class="line"><span class="attr">author</span>=<span class="string">JasonGong</span></span><br><span class="line"><span class="comment">#Email</span></span><br><span class="line"><span class="attr">email</span>=<span class="string">JasonGong@gmail.com</span></span><br><span class="line"><span class="comment">#表前缀(类名不会包含表前缀)</span></span><br><span class="line"><span class="attr">tablePrefix</span>=<span class="string">wms_ #2.数据库表的前缀</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">#以下的配置信息一般不动</span></span><br><span class="line"><span class="comment">#类型转换配置信息</span></span><br><span class="line"><span class="attr">tinyint</span>=<span class="string">Integer</span></span><br><span class="line"><span class="attr">smallint</span>=<span class="string">Integer</span></span><br><span class="line"><span class="attr">mediumint</span>=<span class="string">Integer</span></span><br><span class="line"><span class="attr">int</span>=<span class="string">Integer</span></span><br><span class="line"><span class="attr">integer</span>=
<p><strong>2修改要生成增删改查代码的数据库的连接配置application.yml</strong></p>
<p>只用配置数据库的连接信息即可</p>
<figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">server:</span></span><br><span class="line"> <span class="attr">port:</span> <span class="number">81</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># mysql</span></span><br><span class="line"><span class="attr">spring:</span></span><br><span class="line"> <span class="attr">main:</span></span><br><span class="line"> <span class="attr">allow-circular-references:</span> <span class="literal">true</span></span><br><span class="line"> <span class="attr">datasource:</span></span><br><span class="line"> <span class="attr">type:</span> <span class="string">com.alibaba.druid.pool.DruidDataSource</span></span><br><span class="line"> <span class="comment">#MySQL配置</span></span><br><span class="line"> <span class="attr">driverClassName:</span> <span class="string">com.mysql.cj.jdbc.Driver</span></span><br><span class="line"> <span class="attr">url:</span> <span class="string">jdbc:mysql://192.168.111.100:3306/gulimall_wms?useUnicode=true&amp;characterEncoding=UTF-8&amp;useSSL=false&amp;serverTimezone=Asia/Shanghai</span></span><br><span class="line"> <span class="attr">username:</span> <span class="string">root</span></span><br><span class="line"> <span class="attr">password:</span> <span class="string">root</span></span><br><span class="line"> <span class="comment">#oracle配置</span></span><br><span class="line"> <span class="comment"># driverClassName: oracle.jdbc.OracleDriver</span></span><br><span class="line"> <span class="comment"># url: jdbc:oracle:thin:@47.100.206.162:1521:xe</span></span><br><span class="line"> <span class="comment"># username: renren</span></span><br><span class="line"> <span class="comment"># password: 123456</span></span><br><span class="line"> <span class="comment">#SQLServer配置</span></span><br><span class="line"> <span class="comment"># driverClassName: com.microsoft.sqlserver.jdbc.SQLServerDriver</span></span><br><span class="line"> <span class="co
<p><strong>3.启动服务生成代码,将生成的代码解压并粘贴到对应的模块中去</strong></p>
<p>将main这个目录替换原来模块中的main目录即可sql文件暂时不用管</p>
<p><strong>4.创建模块的配置文件</strong></p>
<figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">server:</span></span><br><span class="line"> <span class="attr">port:</span> <span class="number">8080</span></span><br><span class="line"><span class="attr">spring:</span></span><br><span class="line"> <span class="attr">datasource:</span></span><br><span class="line"> <span class="attr">url:</span> <span class="string">jdbc:mysql://192.168.111.100:3306/gulimall_pms?useUnicode=true&amp;characterEncoding=UTF-8&amp;useSSL=false&amp;serverTimezone=Asia/Shanghai</span></span><br><span class="line"> <span class="attr">username:</span> <span class="string">root</span></span><br><span class="line"> <span class="attr">password:</span> <span class="string">root</span></span><br><span class="line"> <span class="attr">driver-class-name:</span> <span class="string">com.mysql.cj.jdbc.Driver</span></span><br><span class="line"><span class="attr">mybatis-plus:</span></span><br><span class="line"> <span class="attr">mapper-locations:</span> <span class="string">classpath*:/mapper/**/*.xml</span> <span class="comment">#定义mapper.xml文件的位置</span></span><br><span class="line"> <span class="attr">global-config:</span></span><br><span class="line"> <span class="attr">db-config:</span></span><br><span class="line"> <span class="attr">id-type:</span> <span class="string">auto</span> <span class="comment">#主键的生成规则</span></span><br></pre></td></tr></table></figure>
<p><strong>5.启动测试生成的代码</strong></p>
<pre><code>在启动的时候,我们要处理好代码依赖的包,这些包都存在与生成代码的模块中,我们在生成代码的模块中复制过来即可,然后手动的引入相应的包,处理好报错信息。
</code></pre>
<h3 id="3-后台搭建完成之后的项目树"><a href="#3-后台搭建完成之后的项目树" class="headerlink" title="3.后台搭建完成之后的项目树"></a>3.后台搭建完成之后的项目树</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</spa
<h1 id="四-项目前置知识"><a href="#四-项目前置知识" class="headerlink" title="四.项目前置知识"></a>四.项目前置知识</h1><h2 id="1-SpringCloud-Alibaba"><a href="#1-SpringCloud-Alibaba" class="headerlink" title="1.SpringCloud Alibaba"></a>1.SpringCloud Alibaba</h2><h3 id="1-1简介"><a href="#1-1简介" class="headerlink" title="1.1简介"></a>1.1简介</h3><pre><code>SpringCloud Alibaba致力于提供微服务开发的一站式解决方案。此项目包含开发分布式应用微服务必须的组件方便开发者通过SpringCloud编程模型轻松使用这些组件来开发分布式的应用服务。
依托SpringCloud Alibaba你只需要添加一些注解和少量的配置就可以将SpringCloud应用接入阿里微服务解决方案通过阿里中间件来迅速搭建分布式应用系统。
</code></pre>
<p>github的地址:<a target="_blank" rel="noopener" href="https://github.com/alibaba/spring-cloud-alibaba">https://github.com/alibaba/spring-cloud-alibaba</a></p>
<h3 id="1-2为什么使用SpringCloud-Alibaba"><a href="#1-2为什么使用SpringCloud-Alibaba" class="headerlink" title="1.2为什么使用SpringCloud Alibaba"></a>1.2为什么使用SpringCloud Alibaba</h3><p><strong>SpringCloud的不足:</strong></p>
<ul>
<li>部分组件停止维护,给开发带来不便</li>
<li>环境搭建复杂,没有完善的可视化界面,需要大量的二次开发和定制</li>
<li>配置复杂,难以上手,部分配置差别难以区分和合理应用</li>
</ul>
<p><strong>SpringCloud Alibaba的优势:</strong></p>
<p>阿里使用过的组件经历了考验,性能强悍,设计合理,开源成套的搭配和可视化的界面给开发带来极大的便利,搭建简单,学习成本低。</p>
<h3 id="1-3-SpringCloud-Alibaba最终的技术搭配方案"><a href="#1-3-SpringCloud-Alibaba最终的技术搭配方案" class="headerlink" title="1.3 SpringCloud Alibaba最终的技术搭配方案"></a>1.3 SpringCloud Alibaba最终的技术搭配方案</h3><p><strong>SpringCloud Alibaba - Nacos :注册中心(服务发现&#x2F;注册)</strong></p>
<p><strong>SpringCloud Alibaba - Nacos :配置中心(动态的配置管理)</strong></p>
<p><strong>SpringCloud - Ribbon: 负载均衡</strong></p>
<p><strong>SpringCloud - Feign: 声明式HTTP客户端远程调用服务&#x2F;服务调用)</strong></p>
<p><strong>SpringCloud Alibaba - Sentinel服务容错限流、降级、熔断</strong></p>
<p><strong>SpringCloud - Gateway: API网关webflux编程模式</strong></p>
<p><strong>SpringCloud - Sleuth调用链监控</strong></p>
<p><strong>SpringCloud Alibaba - Seata原Fescar即分布式事务解决方案</strong></p>
<img src="/pictures/image-20230519161707857.png" alt="image-20230519161707857" style="zoom:150%;" />
<h3 id="1-4-项目中使用SpringCloud-Alibaba"><a href="#1-4-项目中使用SpringCloud-Alibaba" class="headerlink" title="1.4 项目中使用SpringCloud Alibaba"></a>1.4 项目中使用SpringCloud Alibaba</h3><p>在common模块中添加如下的依赖</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dependencyManagement</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">dependencies</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>com.alibaba.cloud<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-cloud-alibaba-dependencies<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">version</span>&gt;</span>2.2.0.RELEASE<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">type</span>&gt;</span>pom<span class="tag">&lt;/<span class="name">type</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">scope</span>&gt;</span>import<span class="tag">&lt;/<span class="name">scope</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;/<span class="name">dependencies</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependencyManagement</span>&gt;</span></span><br></pre></td></tr></table></figure>
<h3 id="1-5-SpringCloud-Alibaba的组件"><a href="#1-5-SpringCloud-Alibaba的组件" class="headerlink" title="1.5 SpringCloud Alibaba的组件"></a>1.5 SpringCloud Alibaba的组件</h3><h4 id="1-Nacos注册中心"><a href="#1-Nacos注册中心" class="headerlink" title="1.Nacos注册中心"></a>1.Nacos注册中心</h4><img src="/pictures/image-20230519162859782.png" alt="image-20230519162859782" style="zoom:150%;" />
<p><strong>简介</strong></p>
<p>Nacos 是阿里巴巴推出来的一个新开源项目,这是一个更易于构建云原生应用的动态服务发现、配置管理和服务管理平台。</p>
<p>Nacos 致力于帮助您发现、配置和管理微服务。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据及流量管理。</p>
<p>Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。 Nacos 是构建以“服务”为中心的现代应用架构 (例如微服务范式、云原生范式) 的服务基础设施</p>
<p><strong>Nacos 支持如下核心特性</strong></p>
<p>1服务发现 支持 DNS 与 RPC 服务发现,也提供原生 SDK 、OpenAPI 等多种服务注册方式和 DNS、HTTP 与 API 等多种服务发现方式。<br>2服务健康监测 Nacos 提供对服务的实时的健康检查,阻止向不健康的主机或服务实例发送请求。<br>3动态配置服务 Nacos 提供配置统一管理功能,能够帮助我们将配置以中心化、外部化和动态化的方式管理所有环境的应用配置和服务配置。<br>4动态 DNS 服务: Nacos 支持动态 DNS 服务权重路由,能够让我们很容易地实现中间层负载均衡、更灵活的路由策略、流量控制以及数据中心内网的简单 DNS 解析服务。<br>5服务及其元数据管理 Nacos 支持从微服务平台建设的视角管理数据中心的所有服务及元数据,包括管理服务的描述、生命周期、服务的静态依赖分析、服务的健康状态、服务的流量管理、路由及安全策略、服务的 SLA 以及最首要的 metrics 统计数据。</p>
<p><strong>使用nacos</strong></p>
<p>使用之前需要下载nacos并启动nacos的服务</p>
<p><img src="/pictures/image-20230519164012950.png" alt="image-20230519164012950"></p>
<p>在common工程中添加依赖</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>com.alibaba.cloud<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-cloud-starter-alibaba-nacos-discovery<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>配置中心中配置Nacos的地址</p>
<figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">spring:</span></span><br><span class="line"> <span class="attr">application:</span></span><br><span class="line"> <span class="attr">name:</span> <span class="string">gulimall-coupon</span> <span class="comment">#指定服务的名字,指定了名字才能注册到注册中心中</span></span><br><span class="line"> <span class="attr">cloud:</span></span><br><span class="line"> <span class="attr">nacos:</span></span><br><span class="line"> <span class="attr">server-addr:</span> <span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span><span class="string">:8848</span> <span class="comment">#指定注册中心的地址信息</span></span><br></pre></td></tr></table></figure>
<p>在每个启动类上使用注解开启服务的注册与发现功能</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//开启服务的注册与发现功能</span></span><br><span class="line"><span class="meta">@EnableDiscoveryClient</span></span><br></pre></td></tr></table></figure>
<p>查看注册服务列表</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">通过访问: http://localhost:8848/nacos</span><br></pre></td></tr></table></figure>
<p><img src="/pictures/image-20230519165606784.png" alt="image-20230519165606784"></p>
<h4 id="2-Feign声明式远程调用"><a href="#2-Feign声明式远程调用" class="headerlink" title="2.Feign声明式远程调用"></a>2.Feign声明式远程调用</h4><p><strong>简介</strong></p>
<pre><code>Feign是一个声明式的HTTP客户端它的目的是让远程调用变得更简单。Feign提供了HTTP请求的模板通过**编写简单的接口和插入注解**就可以定义好HTTP请求的参数、格式、地址等信息。
Feign整合了Ribbon负载均衡和Hystrix服务熔断可以让我们不再需要显式的使用这两个组件。
</code></pre>
<p><strong>使用</strong></p>
<p>引入依赖,每个模块都需要引入这个依赖</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"> <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.cloud<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-cloud-starter-openfeign<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>编写代码测试远程调用,这里我们以用户模块调用优惠卷模块来获取用户的拥有的优惠卷为例来测试远程调用。</p>
<p><em><strong>会员模块提供获取用户名下优惠卷的方法</strong></em></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 获取用户明名下的优惠卷</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@RequestMapping(&quot;member/list&quot;)</span></span><br><span class="line"><span class="keyword">public</span> R <span class="title function_">membercoupons</span><span class="params">()</span>&#123;</span><br><span class="line"> <span class="type">CouponEntity</span> <span class="variable">couponEntity</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">CouponEntity</span>();</span><br><span class="line"> couponEntity.setCouponName(<span class="string">&quot;满一百减五十&quot;</span>);</span><br><span class="line"> <span class="keyword">return</span> R.ok().put(<span class="string">&quot;coupons&quot;</span>,Arrays.asList(couponEntity));</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><em><strong>用户模块远程调用优惠卷模块</strong></em></p>
<p>远程调用别的服务端步骤</p>
<ol>
<li><p>引入open-feign的依赖</p>
</li>
<li><p>调用端编写一个接口告诉SpringCloud这个接口需要调用远程服务</p>
<p>声明接口的每一个方法都是调用哪个远程服务的那个请求</p>
</li>
<li><p>开启远程调用的功能,在调用端的启动类上添加**@EnableFeignClients**注解</p>
</li>
</ol>
<p><em><strong>在用户模块创建一个Feign包(维护的时候见名知义),声明调用的接口,在启动类上添加注解</strong></em></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//注解</span></span><br><span class="line"><span class="comment">//basePackages声明的远程调用的接口所在的包</span></span><br><span class="line"><span class="meta">@EnableFeignClients(basePackages = &quot;com.atguigu.gulimall.member.feign&quot;)</span></span><br></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.atguigu.gulimall.member.feign;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.atguigu.common.utils.R;</span><br><span class="line"><span class="keyword">import</span> org.springframework.cloud.openfeign.FeignClient;</span><br><span class="line"><span class="keyword">import</span> org.springframework.web.bind.annotation.RequestMapping;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> GongChangjiang</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@version</span> 1.0</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Date</span> 2023/5/19</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Description</span> 远程调用优惠卷服务</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@FeignClient(&quot;gulimall-coupon&quot;)</span><span class="comment">//告诉springCloud这是一个远程客户端并指定要调用服务的服务名</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">interface</span> <span class="title class_">CouponFeignService</span> &#123;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 这里需要写全路径</span></span><br><span class="line"><span class="comment"> * 远程调用优惠卷模块中获取优惠卷信息的方法</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="meta">@RequestMapping(&quot;/coupon/coupon/member/list&quot;)</span></span><br><span class="line"> <span class="keyword">public</span> R <span class="title function_">membercoupons</span><span class="params">()</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><em><strong>测试远程调用</strong></em></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Autowired</span></span><br><span class="line"><span class="keyword">private</span> CouponFeignService couponFeignService;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 测试远程调用优惠卷模块的服务</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@RequestMapping(&quot;/coupons&quot;)</span></span><br><span class="line"><span class="keyword">public</span> R <span class="title function_">test</span><span class="params">()</span>&#123;</span><br><span class="line"> <span class="type">MemberEntity</span> <span class="variable">memberEntity</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">MemberEntity</span>();</span><br><span class="line"> memberEntity.setNickname(<span class="string">&quot;张三&quot;</span>);</span><br><span class="line"> <span class="type">R</span> <span class="variable">membercoupons</span> <span class="operator">=</span> couponFeignService.membercoupons();</span><br><span class="line"> <span class="keyword">return</span> R.ok().put(<span class="string">&quot;member&quot;</span>, memberEntity).put(<span class="string">&quot;coupons&quot;</span>, membercoupons.get(<span class="string">&quot;coupons&quot;</span>));</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>测试的时候出现的小问题</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc</span></span><br><span class="line"><span class="comment">出现这个问题是没有loadbalanc,但是nacos中ribbon会造成loadbalanc包失效,在common的pom中的依赖改成如下的依赖--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>com.alibaba.cloud<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-cloud-starter-alibaba-nacos-discovery<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">exclusions</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">exclusion</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.cloud<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-cloud-starter-netflix-ribbon<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;/<span class="name">exclusion</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;/<span class="name">exclusions</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.cloud<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-cloud-loadbalancer<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">version</span>&gt;</span>2.2.2.RELEASE<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>处理完上面的报错之后,浏览器访问 <a target="_blank" rel="noopener" href="http://localhost:8000/member/member/coupons">http://localhost:8000/member/member/coupons</a> ,显示如下的内容,说明远程调用成功</p>
<p><img src="/pictures/image-20230519210917423.png" alt="image-20230519210917423"></p>
<h4 id="3-Nacos配置中心"><a href="#3-Nacos配置中心" class="headerlink" title="3.Nacos配置中心"></a>3.Nacos配置中心</h4><p><strong>简介</strong></p>
<p>简介见上面的nacos注册中心这里的Nacos配置中心和上面的nacos注册中心是同一个东西</p>
<p><strong>使用</strong></p>
<p>1.在common中引入nacos配置中心的依赖和上面的注册中心的依赖不一样</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>com.alibaba.cloud<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-cloud-starter-alibaba-nacos-config<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>2.给需要配置中心管理的模块下创建配置文件<strong>bootstrap.properties</strong>这个文件会优先于application.properties读取</p>
<figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#服务名</span></span><br><span class="line"><span class="attr">spring.application.name</span>=<span class="string">gulimall-coupon</span></span><br><span class="line"><span class="comment">#Nacos配置中心的地址的地址</span></span><br><span class="line"><span class="comment">#注意看这里多了一个config和模块中nacos的地址还是有区别的</span></span><br><span class="line"><span class="attr">spring.cloud.nacos.config.server-addr</span>=<span class="string">127.0.0.1:8848</span></span><br></pre></td></tr></table></figure>
<p>3.测试</p>
<p>3.1.在application.properties配置文件中添加如下的配置</p>
<figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">coupon.user.name</span>=<span class="string">zhansan</span></span><br><span class="line"><span class="attr">coupon.user.age</span>=<span class="string">18</span></span><br></pre></td></tr></table></figure>
<p>3.2 编写接口并访问测试</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 获取配置文件中的配置信息</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@Value(&quot;$&#123;user.name&#125;&quot;)</span></span><br><span class="line"><span class="keyword">private</span> String name;</span><br><span class="line"><span class="meta">@Value(&quot;$&#123;user.age&#125;&quot;)</span></span><br><span class="line"><span class="keyword">private</span> Integer age;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 测试优先获取哪个配置文件的值</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@RequestMapping(&quot;/test&quot;)</span></span><br><span class="line"><span class="keyword">public</span> R <span class="title function_">test</span><span class="params">()</span>&#123;</span><br><span class="line"> <span class="keyword">return</span> R.ok().put(<span class="string">&quot;name&quot;</span>,name).put(<span class="string">&quot;age&quot;</span>,age);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>测试的结果如下:</p>
<img src="/pictures/image-20230519213945320.png" alt="image-20230519213945320" style="zoom: 200%;" />
<p>3.3 需求:应用不重启,实时的修改配置文件中的值</p>
<p>3.3.1 在Nacos配置中心中创建一个名为 <strong>应用名.properties</strong>(例如:gulimall-coupon.properties 的配置文件</p>
<p>进入nacos管理的可视化页面点击<strong>配置列表</strong>,点击 <strong>+</strong> ,新建一个配置 同时将配置文件中name值改为lihua</p>
<p><img src="/pictures/image-20230519215420623.png" alt="image-20230519215420623"></p>
<p>发布之后,就可以在配置列表中看到这个配置了</p>
<p><img src="/pictures/image-20230519215232782.png" alt="image-20230519215232782"></p>
<p>重启项目测试,配置中心的配置是否生效</p>
<p>没有生效的话添加这个依赖springBoot2.4以上的版本需要</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.cloud<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-cloud-starter-bootstrap<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">version</span>&gt;</span>4.0.1<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>生效的话这时刷新页面可以看到name已经由zhangsan变成lihua了</p>
<img src="/pictures/image-20230519220357933.png" alt="image-20230519220357933" style="zoom:150%;" />
<p>3.3.2 想让配置文件实时生效的话,这时我们还要添加以下的注解,然后重启应用,让注解生效</p>
<p>注意: <strong>@RefreshScope</strong> 这里的注解添加在<strong>Controller</strong>上,不是添加在启动类上</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//给需要配置中心统一配置的模块的Controller上上添加@RefreshScope的注解</span></span><br><span class="line"><span class="meta">@RefreshScope</span></span><br></pre></td></tr></table></figure>
<p><strong>最终测试</strong></p>
<p>这时我们在配置中心中修改配置并发布,这些配置就可以实时的生效了</p>
<p>我们在配置中心修改name和age的值并发布</p>
<p><img src="/pictures/image-20230519220922701.png" alt="image-20230519220922701"></p>
<p>这时刷新请求接口的页面,我们可以看到页面中的数据发生了实时的变化</p>
<img src="/pictures/image-20230519223403139.png" alt="image-20230519223403139" style="zoom:150%;" />
<p><strong>总结</strong></p>
<p>实现nacos配置中心统一配置的流程</p>
<ol>
<li><p>引入依赖</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>com.alibaba.cloud<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-cloud-starter-alibaba-nacos-config<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure></li>
<li><p>创建一个bootstrap.properties的配置文件添加如下的内容:</p>
<figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#服务名</span></span><br><span class="line"><span class="attr">spring.application.name</span>=<span class="string">gulimall-coupon</span></span><br><span class="line"><span class="comment">#Nacos配置中心的地址</span></span><br><span class="line"><span class="comment">#注意看这里多了一个config和模块中nacos的地址还是有区别的</span></span><br><span class="line"><span class="attr">spring.cloud.nacos.config.server-addr</span>=<span class="string">127.0.0.1:8848</span></span><br></pre></td></tr></table></figure></li>
<li><p>需要给配置中心中新建一个数据集(Data Id)为 <strong>应用名.properties</strong>(例如:gulimall-coupon.properties的配置。默认规则: 应用名.properties</p>
</li>
<li><p>给 应用名.properties 添加任何配置 这些配置都可以在配置中心配置生效</p>
</li>
<li><p>在Controller上添加**@RefreshScope**注解就可以实时的动态刷新配置,配置中心更新了配置并发布了之后,配置实时的生效。</p>
</li>
</ol>
<p><strong>注意</strong></p>
<p><em>如果配置中心和当前应用的配置文件都配置了相同的项,优先使用配置中心的配置</em></p>
<p><strong>细节</strong></p>
<p><strong>命名空间:</strong> 配置隔离</p>
<p>默认public保留空间</p>
<p>1.可以分别设置开发、测试、生产环境的命名空间,不同的环境下使用不同的命名空间中的配置</p>
<p>通过命令空间实现环境隔离</p>
<p><strong>bootstrap.properties</strong>设置使用哪个命名空间,可以在配置文件中添加如下的配置</p>
<p><img src="/pictures/image-20230519225637848.png" alt="image-20230519225637848"></p>
<figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 切换名称空间</span></span><br><span class="line"><span class="comment"># 可以选择对应的命名空间 # 写上对应环境的命名空间ID</span></span><br><span class="line"><span class="comment"># 使用唯一的命名空间ID</span></span><br><span class="line"><span class="attr">spring.cloud.nacos.config.namespace</span>=<span class="string">8a84e478-92d0-4581-a316-b0274fc76eb8</span></span><br></pre></td></tr></table></figure>
<p>2.可以为每一个微服务可以创建自己的命名空间,让每个服务的配置放在每个服务的命名空间下</p>
<p><strong>配置集:</strong>所有的配置的集合</p>
<p><strong>配置集ID:</strong> 类似文件名</p>
<pre><code>Data Id: 就是配置集ID
</code></pre>
<p><strong>配置分组:</strong></p>
<p>默认所有的配置集都属于 : DEFAULT_GROUP</p>
<p>例如在<strong>生产环境的命名空间</strong>下,淘宝的<strong>配置分组</strong>有以下几个: 双十一688平时</p>
<figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#指定配置分组</span></span><br><span class="line"><span class="attr">spring.cloud.nacos.config.group</span>=<span class="string">1111</span></span><br></pre></td></tr></table></figure>
<p><strong>Tips同一个命名空间下可能会有不同的配置分组</strong></p>
<p><strong>每个微服务创建自己的命令空间,使用配置分组区分环境 dev、test、prod</strong></p>
<p><em><strong>从配置中心中读取多个配置集(配置文件):</strong></em></p>
<p>可以在bootstrap.properties添加以下的配置</p>
<figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#第一个配置文件</span></span><br><span class="line"><span class="comment">#配置文件名</span></span><br><span class="line"><span class="attr">spring.cloud.nacos.config.ext-config[0].data-id</span>=<span class="string">datasource.yml </span></span><br><span class="line"><span class="comment">#配置文件的对应的配置分组</span></span><br><span class="line"><span class="attr">spring.cloud.nacos.config.ext-config[0].group</span>=<span class="string">dev</span></span><br><span class="line"><span class="comment">#是否动态的刷新</span></span><br><span class="line"><span class="attr">spring.cloud.nacos.config.ext-config[0].refresh</span>=<span class="string">true</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">#第二个配置文件</span></span><br><span class="line"><span class="attr">spring.cloud.nacos.config.ext-config[1].data-id</span>=<span class="string">mybatis.yml</span></span><br><span class="line"><span class="attr">spring.cloud.nacos.config.ext-config[1].group</span>=<span class="string">dev</span></span><br><span class="line"><span class="attr">spring.cloud.nacos.config.ext-config[1].refresh</span>=<span class="string">true</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">#第三个配置文件</span></span><br><span class="line"><span class="attr">spring.cloud.nacos.config.ext-config[2].data-id</span>=<span class="string">others.yml</span></span><br><span class="line"><span class="attr">spring.cloud.nacos.config.ext-config[2].group</span>=<span class="string">dev</span></span><br><span class="line"><span class="attr">spring.cloud.nacos.config.ext-config[2].refresh</span>=<span class="string">true</span></span><br></pre></td></tr></table></figure>
<p><strong>总结:</strong></p>
<p>1.微服务的任何配置信息,任何配置文件都可以放在配置中心中</p>
<p>2.加载配置中心的哪些配置文件只需要在bootstrap.properties说明加载哪些配置文件即可</p>
<p>3.如果配置中心和当前应用的配置文件都配置了相同的项,优先使用配置中心的配置</p>
<h4 id="4-Gateway网关"><a href="#4-Gateway网关" class="headerlink" title="4.Gateway网关"></a>4.Gateway网关</h4><img src="/pictures/image-20230519233741029.png" alt="image-20230519233741029" style="zoom:150%;" />
<p><strong>简介</strong></p>
<pre><code>网关最为流量的入口,常用的功能包括**路由转发**、**权限校验**、**限流控制**等。SpringCloud gateway是springcloud官方提供的第二代网关框架取代了Zull网关。
</code></pre>
<p><strong>网关的功能</strong></p>
<ul>
<li>针对所有请求进行登陆<strong>统一鉴权(登录态)</strong><strong>限流</strong><strong>缓存</strong><strong>日志(用户打点)</strong></li>
<li>可以根据不同的请求路径pattern来进行请求的鉴权、转发、和拒绝。</li>
<li><strong>协议转化</strong>。针对后段多种不同的协议在网关层统一处理后以HTTP对外提供服务。</li>
<li>提供<strong>统一的错误码</strong></li>
<li>请求转发,并且可以基于网关实现<strong>内网与外网的隔离</strong></li>
</ul>
<p><strong>使用</strong></p>
<ol>
<li><p>创建一个springboot initializr的工程 勾选上Gateway的依赖</p>
</li>
<li><p>在网关模块的pom.xml文件中添加上对common工程的依赖</p>
</li>
<li><p>在网关的启动类上添加上**@EnableDiscoveryClient**的注解,把网关注册到注册中心</p>
</li>
<li><p>配置nacos的地址和服务名信息</p>
<figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#配置网关的地址</span></span><br><span class="line"><span class="attr">spring.cloud.nacos.config.server-addr</span>=<span class="string">127.0.0.1:8848</span></span><br><span class="line"><span class="comment">#服务名</span></span><br><span class="line"><span class="attr">spring.application.name</span>=<span class="string">gulimall-gateway</span></span><br></pre></td></tr></table></figure></li>
<li><p>创建<strong>bootstrap.properties</strong>配置文件将网关模块的配置通过交给nacos配置中心管理</p>
<p><img src="/pictures/image-20230520205015056.png" alt="image-20230520205015056"></p>
<figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">spring.application.name</span>=<span class="string">gulimall-gateway</span></span><br><span class="line"><span class="comment">#配置配置中心的地址</span></span><br><span class="line"><span class="attr">spring.cloud.nacos.config.server-addr</span>=<span class="string">127.0.0.1:8848</span></span><br><span class="line"><span class="comment">#名称空间</span></span><br><span class="line"><span class="attr">spring.cloud.nacos.config.namespace</span>=<span class="string">4a8e80ba-3d3c-40ef-897f-d400797c4676</span></span><br></pre></td></tr></table></figure></li>
<li><p>启动测试</p>
<p>启动之后出现的问题及解决方案:</p>
<p><strong>a</strong>.单元测试@Test注解爆红,是我们更换了springboot的版本的原因删除爆红的地方重新导包即可</p>
<p><strong>b</strong>.数据库相关的报错,是因为网关中没有使用数据库,就没有配置数据库的配置,我们通过@SpringBootApplication(exclude &#x3D; {DataSourceAutoConfiguration.class})解决。</p>
</li>
<li><p>转发测试</p>
<p><strong>测试案例:</strong>我们在浏览器的地址栏输入localhost:88?url&#x3D;baidu就给我们转发到百度,输入localhost:88?url&#x3D;qq就给我们转发到腾讯qq</p>
<p><strong>实现:</strong>创建一个yml的配置文件在配置文件中添加如下的配置</p>
<figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">spring:</span></span><br><span class="line"> <span class="attr">cloud:</span></span><br><span class="line"> <span class="attr">gateway:</span></span><br><span class="line"> <span class="attr">routes:</span></span><br><span class="line"> <span class="comment"># 路由的唯一id</span></span><br><span class="line"> <span class="bullet">-</span> <span class="attr">id:</span> <span class="string">baidu_route</span></span><br><span class="line"> <span class="attr">uri:</span> <span class="string">https://www.baidu.com</span></span><br><span class="line"> <span class="comment"># 断言什么时候去上面那个url</span></span><br><span class="line"> <span class="attr">predicates:</span></span><br><span class="line"> <span class="comment"># 下面的这句话的意思是如果请求中存在请求参数url=baidu我们就去 https://www.baidu.com这个url</span></span><br><span class="line"> <span class="bullet">-</span> <span class="string">Query=url,baidu</span></span><br><span class="line"> <span class="bullet">-</span> <span class="attr">id:</span> <span class="string">qq_route</span></span><br><span class="line"> <span class="attr">uri:</span> <span class="string">https://www.qq.com</span></span><br><span class="line"> <span class="attr">predicates:</span></span><br><span class="line"> <span class="comment"># 下面的这句话的意思是如果请求中存在请求参数url=qq我们就去 https://www.qq.com这个url</span></span><br><span class="line"> <span class="bullet">-</span> <span class="string">Query=url,qq</span></span><br></pre></td></tr></table></figure>
<img src="/pictures/image-20230520212001874.png" alt="image-20230520212001874" style="zoom:150%;" /></li>
</ol>
2024-05-10 13:32:25 +08:00
<h2 id="2-前端开发的基础知识"><a href="#2-前端开发的基础知识" class="headerlink" title="2.前端开发的基础知识"></a>2.前端开发的基础知识</h2><p><strong><a href="https://qingling.icu/posts/432.html">前端基础知识 | The Blog (qingling.icu)</a></strong></p>
2023-09-22 21:57:28 +08:00
<p><strong>前后端技术类比</strong></p>
<p><img src="/pictures/image-20230520212351174.png" alt="image-20230520212351174"></p>
<h1 id="五-基础篇程序设计"><a href="#五-基础篇程序设计" class="headerlink" title="五.基础篇程序设计"></a>五.基础篇程序设计</h1><p><strong>提示</strong>:所有的API路径一定要和老师的一样不然在后面会吃亏!!!</p>
<h2 id="1-商品服务"><a href="#1-商品服务" class="headerlink" title="1.商品服务"></a>1.商品服务</h2><h3 id="1-1-三级分类"><a href="#1-1-三级分类" class="headerlink" title="1.1 三级分类"></a>1.1 三级分类</h3><h4 id="1-1-1-查询-递归树形结构数据获取"><a href="#1-1-1-查询-递归树形结构数据获取" class="headerlink" title="1.1.1 查询-递归树形结构数据获取"></a>1.1.1 查询-递归树形结构数据获取</h4><p><strong>controller</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 查询所有的分类以及子分类,以树形结构组装起来</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="meta">@RequestMapping(&quot;/list/tree&quot;)</span></span><br><span class="line"> <span class="keyword">public</span> R <span class="title function_">list</span><span class="params">()</span>&#123;</span><br><span class="line"> List&lt;CategoryEntity&gt; entities = categoryService.listWithTree();</span><br><span class="line"> <span class="keyword">return</span> R.ok().put(<span class="string">&quot;data&quot;</span>, entities);</span><br><span class="line"> &#125;</span><br></pre></td></tr></table></figure>
<p><strong>service</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.atguigu.gulimall.product.service.impl;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> org.springframework.beans.factory.annotation.Autowired;</span><br><span class="line"><span class="keyword">import</span> org.springframework.stereotype.Service;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.List;</span><br><span class="line"><span class="keyword">import</span> java.util.Map;</span><br><span class="line"><span class="keyword">import</span> java.util.stream.Collectors;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;</span><br><span class="line"><span class="keyword">import</span> com.baomidou.mybatisplus.core.metadata.IPage;</span><br><span class="line"><span class="keyword">import</span> com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;</span><br><span class="line"><span class="keyword">import</span> com.atguigu.common.utils.PageUtils;</span><br><span class="line"><span class="keyword">import</span> com.atguigu.common.utils.Query;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.atguigu.gulimall.product.dao.CategoryDao;</span><br
<h4 id="1-1-2-配置路由和网关和路径重写"><a href="#1-1-2-配置路由和网关和路径重写" class="headerlink" title="1.1.2 配置路由和网关和路径重写"></a>1.1.2 配置路由和网关和路径重写</h4><p>前端访问的地址</p>
<p><img src="/pictures/image-20230529170214769.png" alt="image-20230529170214769"></p>
<p>前端代码中的配置</p>
<p><img src="/pictures/image-20230529170149102.png" alt="image-20230529170149102"></p>
<p>修改访问的基准路径统一访问网关再由网关做请求的转发转发到指定的服务中去renren-fast-vue\static\config\index.js文件</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 开发环境</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line">;(<span class="keyword">function</span> (<span class="params"></span>) &#123;</span><br><span class="line"> <span class="variable language_">window</span>.<span class="property">SITE_CONFIG</span> = &#123;&#125;;</span><br><span class="line"></span><br><span class="line"> <span class="comment">// api接口请求地址</span></span><br><span class="line"> <span class="variable language_">window</span>.<span class="property">SITE_CONFIG</span>[<span class="string">&#x27;baseUrl&#x27;</span>] = <span class="string">&#x27;http://localhost:88&#x27;</span>;</span><br><span class="line"></span><br><span class="line"> <span class="comment">// cdn地址 = 域名 + 版本号</span></span><br><span class="line"> <span class="variable language_">window</span>.<span class="property">SITE_CONFIG</span>[<span class="string">&#x27;domain&#x27;</span>] = <span class="string">&#x27;./&#x27;</span>; <span class="comment">// 域名</span></span><br><span class="line"> <span class="variable language_">window</span>.<span class="property">SITE_CONFIG</span>[<span class="string">&#x27;version&#x27;</span>] = <span class="string">&#x27;&#x27;</span>; <span class="comment">// 版本号(年月日时分)</span></span><br><span class="line"> <span class="variable language_">window</span>.<span class="property">SITE_CONFIG</span>[<span class="string">&#x27;cdnUrl&#x27;</span>] = <span class="variable language_">window</span>.<span class="property">SITE_CONFIG</span>.<span class="property">domain</span> + <span class="variable language_">window</span>.<span class="property">SITE_CONFIG</span>.<span class="property">version</span>;</span><br><span class="line">&#125;)();</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p>将renren-fast注册到注册中心中去</p>
<p>修改pom.xml中的依赖</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!-- 修改springBoot的版本 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">parent</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-boot-starter-parent<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">version</span>&gt;</span>2.2.6.RELEASE<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">relativePath</span>/&gt;</span> </span><br><span class="line"><span class="tag">&lt;/<span class="name">parent</span>&gt;</span></span><br><span class="line"><span class="comment">&lt;!-- 依赖common文件,获取nacos相关的注解 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>com.atguigu<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>gulimall-common<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">version</span>&gt;</span>1.0-SNAPSHOT<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>在启动类上添加@EnableDiscoveryClient注解在配置文件中添加上nacos地址的配置</p>
<figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">cloud:</span></span><br><span class="line"> <span class="attr">nacos:</span></span><br><span class="line"> <span class="attr">discovery:</span></span><br><span class="line"> <span class="attr">server-addr:</span> <span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span><span class="string">:8848</span></span><br></pre></td></tr></table></figure>
<p>修改网关中的断言,让前台的请求转发到正确的路径上</p>
<figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">spring:</span></span><br><span class="line"> <span class="attr">cloud:</span></span><br><span class="line"> <span class="attr">gateway:</span></span><br><span class="line"> <span class="attr">routes:</span></span><br><span class="line"> <span class="comment"># 路由的唯一id</span></span><br><span class="line"> <span class="bullet">-</span> <span class="attr">id:</span> <span class="string">admin_route</span></span><br><span class="line"> <span class="attr">uri:</span> <span class="string">lb://renren-fast</span></span><br><span class="line"> <span class="attr">predicates:</span></span><br><span class="line"> <span class="bullet">-</span> <span class="string">Path=/api/**</span></span><br><span class="line"> <span class="comment"># 路径重写</span></span><br><span class="line"> <span class="attr">filters:</span></span><br><span class="line"> <span class="bullet">-</span> <span class="string">RewritePath=/api/(?&lt;segment&gt;.*),/renren-fast/$\&#123;segment&#125;</span></span><br><span class="line"><span class="comment"># 前端项目中的基准路径 http://localhost:88/api</span></span><br><span class="line"><span class="comment"># http://localhost:88/api/captcha.jpg 路径重写为 http://localhost:8080/renren-fast/captcha.jpg</span></span><br></pre></td></tr></table></figure>
<p>解决登录的时候的跨域问题</p>
<p>浏览器中的跨域的报错信息</p>
<p><img src="/pictures/image-20230530111856491.png" alt="image-20230530111856491"></p>
<p>跨域问题的介绍</p>
<p><img src="/pictures/image-20230530112115261.png" alt="image-20230530112115261"></p>
<p> 跨域流程</p>
<p><a target="_blank" rel="noopener" href="https://developer.mozilla.org/zh-CN/docs/Web/HTTP/CORS">文档介绍</a></p>
<p><img src="/pictures/image-20230530112630678.png" alt="image-20230530112630678"></p>
<p>跨域的解决方案</p>
<ol>
<li>使用nginx部署为同一域</li>
<li>配置当前请求允许跨域 (在网关中通过过滤器给请求添加响应头)</li>
</ol>
<p>跨域的配置</p>
<p>添加跨域的配置类</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.eatguigu.gulimall.gateway.config;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> org.springframework.context.annotation.Bean;</span><br><span class="line"><span class="keyword">import</span> org.springframework.context.annotation.Configuration;</span><br><span class="line"><span class="keyword">import</span> org.springframework.web.cors.CorsConfiguration;</span><br><span class="line"><span class="keyword">import</span> org.springframework.web.cors.reactive.CorsWebFilter;</span><br><span class="line"><span class="keyword">import</span> org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> GongChangjiang</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@version</span> 1.0</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Date</span> 2023/5/30</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Description</span> 跨域配置类</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@Configuration</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">CorsConfig</span> &#123;</span><br><span class="line"></span><br><span class="line"> <span class="meta">@Bean</span></span><br><span class="line"> <span class="keyword">public</span> CorsWebFilter <span class="title function_">corsWebFilter</span><span class="params">()</span>&#123;</span><br><span class="line"> <span class="type">UrlBasedCorsConfigurationSource</span> <span class="variable">source</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">UrlBasedCorsConfigurationSource</span>();</span><br><span class="line"> <span class="type">CorsConfiguration</span> <span class="variable">corsConfiguration</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">CorsConfiguration</span>();</span><br><span class="line"> <span class="comment">//1.配置跨域</span></span><br><span class="line"> <span class="comment">//允许所有的请求头跨域</span></span><br><span class="line"> corsConfiguration.addAllowedHeader(<span class="string">&quot;*&quot;</span>);</span><br><span class="line"> <span class="comment">//允许所有的请求方式跨域</span></span><br><span class="line"> corsConfiguration.addAllowedMethod(<span class="string">&quot;*&quot;</span>);</span><br><span class="l
<p>注掉renren-fast工程中的跨域配置避免产生两次跨域的问题</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Copyright (c) 2016-2019 人人开源 All rights reserved.</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * https://www.renren.io</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * 版权所有,侵权必究!</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">package</span> io.renren.config;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> org.springframework.context.annotation.Configuration;</span><br><span class="line"><span class="keyword">import</span> org.springframework.web.servlet.config.annotation.CorsRegistry;</span><br><span class="line"><span class="keyword">import</span> org.springframework.web.servlet.config.annotation.WebMvcConfigurer;</span><br><span class="line"></span><br><span class="line"><span class="meta">@Configuration</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">CorsConfig</span> <span class="keyword">implements</span> <span class="title class_">WebMvcConfigurer</span> &#123;</span><br><span class="line"></span><br><span class="line"><span class="comment">// @Override</span></span><br><span class="line"><span class="comment">// public void addCorsMappings(CorsRegistry registry) &#123;</span></span><br><span class="line"><span class="comment">// registry.addMapping(&quot;/**&quot;)</span></span><br><span class="line"><span class="comment">// .allowedOrigins(&quot;*&quot;)</span></span><br><span class="line"><span class="comment">// .allowCredentials(true)</span></span><br><span class="line"><span class="comment">// .allowedMethods(&quot;GET&quot;, &quot;POST&quot;, &quot;PUT&quot;, &quot;DELETE&quot;, &quot;OPTIONS&quot;)</span></span><br><span class="line"><span class="comment">// .maxAge(3600);</span></span><br><span class="line"><span class="comment">// &#125;</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>再次测试,成功的进入后台管理系统</p>
<h4 id="1-2-3-查询-页面中树形显示"><a href="#1-2-3-查询-页面中树形显示" class="headerlink" title="1.2.3 查询-页面中树形显示"></a>1.2.3 查询-页面中树形显示</h4><p><strong>新增一个商品系统的目录,并在该目录下创建一个分类维护的二级分类</strong></p>
<p>1.添加一个一级分类</p>
<p><img src="/pictures/image-20230529161308671.png" alt="image-20230529161308671"></p>
<p><img src="/pictures/image-20230529161317694.png" alt="image-20230529161317694"></p>
<p>2.在商品系统的一级分类下添加一个分类维护的二级分类</p>
<p><img src="/pictures/image-20230529161648257.png" alt="image-20230529161648257"></p>
<p>3.路径的问题</p>
<p><img src="/pictures/image-20230529162235482.png" alt="image-20230529162235482"></p>
<p>4.路径与文件的对应关系</p>
<p><img src="/pictures/image-20230529162946584.png" alt="image-20230529162946584"></p>
<p>5.发送请求的示例</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br></pre></td><td class="code"><pre><span class="line">&lt;template&gt;</span><br><span class="line"> &lt;div&gt;</span><br><span class="line"> &lt;!-- 树形控件 --&gt;</span><br><span class="line"> &lt;el-tree</span><br><span class="line"> :data=&quot;data&quot;</span><br><span class="line"> :props=&quot;defaultProps&quot;</span><br><span class="line"> @node-click=&quot;handleNodeClick&quot;</span><br><span class="line"> &gt;&lt;/el-tree&gt;</span><br><span class="line"> &lt;/div&gt;</span><br><span class="line">&lt;/template&gt;</span><br><span class="line"></span><br><span class="line">&lt;script&gt;</span><br><span class="line">export default &#123;</span><br><span class="line"> //定义变量</span><br><span class="line"> data() &#123;</span><br><span class="line"> return &#123;</span><br><span class="line"> data: [],</span><br><span class="line"> defaultProps: &#123;</span><br><span class="line"> children: &quot;children&quot;,</span><br><span class="line"> label: &quot;label&quot;,</span><br><span class="line"> &#125;,</span><br><span class="line"> &#125;;</span><br><span class="line"> &#125;,</span><br><span class="line"> //钩子函数</span><br><span class="line"> created() &#123;</span><br><span class="line"> this.getMenus();</span><br><span class="line"> &#125;,</span><br><span class="line"></span><br><span class="line"> //方法</span><br><span class="line"> methods: &#123;</span><br><span class="line"> handleNodeClick(data) &#123;</span><br><span class="line"> console.log(data);</span><br><span class="line"> &#125;,</span><br><span class="line"> //获取所有的菜单</span><br><span class="line"> getMenus() &#123;</span><br><span class="line"> //正式的发送请求</span><br><span class="line"> this.$http(&#123;</span><br><span class="line"> url: this.$http.adornUrl(&quot;/product/category/list/tree&quot;),</span><br><span class="line"> method: &quot;get&quot;,</span><br><span class="line"> &#125;).then((&#123; data &#125;) =&gt; &#123;</span><br><span class="line"> console.log(data);</span><br><span class="line"> &#125;);</span><br><span class="line"> &#125;,</span><br><span class="line"> &#125;,</span><br><span class="line">&#125;;</span><br><span class="line">&lt;/script&gt;</span><br><span class="line">&lt;style&gt;</span><br><span class="line">&lt;/style&gt;</span><br></pre></td></tr></table></figur
<p>6.树形显示商品的分类信息</p>
<p>前端修改网关商品模块的路由(注意:匹配精确的路由放在模糊的路由的上面)</p>
<figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">spring:</span></span><br><span class="line"> <span class="attr">cloud:</span></span><br><span class="line"> <span class="attr">gateway:</span></span><br><span class="line"> <span class="attr">routes:</span></span><br><span class="line"> <span class="comment">#这个路由精确一些,放在上面,避免被下面模糊的路由吞掉</span></span><br><span class="line"> <span class="bullet">-</span> <span class="attr">id:</span> <span class="string">product_route</span></span><br><span class="line"> <span class="attr">uri:</span> <span class="string">lb://gulimall-product</span></span><br><span class="line"> <span class="attr">predicates:</span></span><br><span class="line"> <span class="bullet">-</span> <span class="string">Path=/api/product/**</span></span><br><span class="line"> <span class="attr">filters:</span></span><br><span class="line"> <span class="bullet">-</span> <span class="string">RewritePath=/api/(?&lt;segment&gt;.*),/$\&#123;segment&#125;</span></span><br><span class="line"> </span><br><span class="line"> <span class="comment"># 路由的唯一id</span></span><br><span class="line"> <span class="bullet">-</span> <span class="attr">id:</span> <span class="string">admin_route</span></span><br><span class="line"> <span class="attr">uri:</span> <span class="string">lb://renren-fast</span></span><br><span class="line"> <span class="attr">predicates:</span></span><br><span class="line"> <span class="bullet">-</span> <span class="string">Path=/api/**</span></span><br><span class="line"> <span class="comment"># 路径重写</span></span><br><span class="line"> <span class="attr">filters:</span></span><br><span class="line"> <span class="bullet">-</span> <span class="string">RewritePath=/api/(?&lt;segment&gt;.*),/renren-fast/$\&#123;segment&#125;</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 前端项目中的基准路径 http://localhost:88/api</span></span><br><span class="line"><span class="comment"># http://localhost:88/api/captcha.jpg 路径重写为 http://localhost:8080/renren-fast/captcha.jpg</span></span><br></pre></td></tr></table></figure>
<p>前端发送请求,正确地获取数据</p>
<p><img src="/pictures/image-20230530172846989.png" alt="image-20230530172846989"></p>
<p>在页面上显示数据</p>
<p>Element的配置</p>
<p><img src="/pictures/image-20230530173658950.png" alt="image-20230530173658950"></p>
<p>前端代码</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br></pre></td><td class="code"><pre><span class="line">&lt;template&gt;</span><br><span class="line"> &lt;div&gt;</span><br><span class="line"> &lt;!-- 树形控件 --&gt;</span><br><span class="line"> &lt;el-tree</span><br><span class="line"> :data=&quot;menus&quot;</span><br><span class="line"> :props=&quot;defaultProps&quot;</span><br><span class="line"> @node-click=&quot;handleNodeClick&quot;</span><br><span class="line"> &gt;&lt;/el-tree&gt;</span><br><span class="line"> &lt;/div&gt;</span><br><span class="line">&lt;/template&gt;</span><br><span class="line"></span><br><span class="line">&lt;script&gt;</span><br><span class="line">export default &#123;</span><br><span class="line"> //定义变量</span><br><span class="line"> data() &#123;</span><br><span class="line"> return &#123;</span><br><span class="line"> menus: [],</span><br><span class="line"> defaultProps: &#123;//这里的配置看官方的文档</span><br><span class="line"> children: &quot;children&quot;,//父节点上的子节点</span><br><span class="line"> label: &quot;name&quot;,//每个对象要显示的属性值,比如我们当前要显示商品分类中的分类名的值这里就改成name</span><br><span class="line"> &#125;,</span><br><span class="line"> &#125;;</span><br><span class="line"> &#125;,</span><br><span class="line"> //钩子函数</span><br><span class="line"> created() &#123;</span><br><span class="line"> //调用获取商品分类数据的方法</span><br><span class="line"> this.getMenus();</span><br><span class="line"> &#125;,</span><br><span class="line"></span><br><span class="line"> //方法</span><br><span class="line"> methods: &#123;</span><br><span class="line"> handleNodeClick(data) &#123;</span><br><span class="line"> console.log(data);</span><br><span class="line"> &#125;,</span><br><span class="line"> //获取所有的菜单</span><br><span class="line"> getMenus() &#123;</span><br><span class="line"> this.$http(&#123;</span><br><span class="line"> url: this.$http.adornUrl(&quot;/product/category/list/tree&quot;),</span><br><span class="line"> method: &quot;get&quot;,</span><br><span class="line"> &#125;).then((&#123; data &#125;) =&gt; &#123;</span><br><span class="line"> this.menus = data.data;</span><br><span class="line"> &#125;);</span><br><span class="line"> &#125;,</span><br><span
<p>显示的效果</p>
<p><img src="/pictures/image-20230530173801824.png" alt="image-20230530173801824"></p>
<h4 id="1-2-4-删除-删除商品分类"><a href="#1-2-4-删除-删除商品分类" class="headerlink" title="1.2.4 删除-删除商品分类"></a>1.2.4 删除-删除商品分类</h4><p>前端页面修改</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br></pre></td><td class="code"><pre><span class="line">&lt;template&gt;</span><br><span class="line"> &lt;!-- 树形控件--&gt;</span><br><span class="line"> &lt;!-- :expand-on-click-node=&quot;false&quot;设置为点击箭头的时候才会展开,避免点击删除的按钮的时候展开 --&gt;</span><br><span class="line"> &lt;!-- show-checkbox表示开启选择框批量选择--&gt;</span><br><span class="line"> &lt;!-- node-key表示在整个树中唯一的表示 --&gt;</span><br><span class="line"> &lt;el-tree</span>
<p><strong>配置逻辑删除</strong></p>
<p>配置全局的逻辑删除规则</p>
<figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">mybatis-plus:</span></span><br><span class="line"> <span class="attr">global-config:</span></span><br><span class="line"> <span class="attr">db-config:</span></span><br><span class="line"> <span class="attr">logic-delete-value:</span> <span class="number">1</span> <span class="comment"># 逻辑已删除值(默认为 1)</span></span><br><span class="line"> <span class="attr">logic-not-delete-value:</span> <span class="number">0</span> <span class="comment"># 逻辑未删除值(默认为 0)</span></span><br></pre></td></tr></table></figure>
<p>在showStatus上添加上逻辑删除的注解</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@TableLogic(value = &quot;1&quot;,delval = &quot;0&quot;)</span></span><br></pre></td></tr></table></figure>
<p>后端代码的实现</p>
<p>controlller</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 删除</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@RequestMapping(&quot;/delete&quot;)</span></span><br><span class="line"><span class="comment">//@RequiresPermissions(&quot;product:category:delete&quot;)</span></span><br><span class="line"><span class="keyword">public</span> R <span class="title function_">delete</span><span class="params">(<span class="meta">@RequestBody</span> Long[] catIds)</span> &#123;</span><br><span class="line"> <span class="comment">//检查当前删除的菜单,是否被别的地方引用</span></span><br><span class="line"> <span class="comment">//categoryService.removeByIds(Arrays.asList(catIds));</span></span><br><span class="line"> categoryService.removeMenuByIds(Arrays.asList(catIds));</span><br><span class="line"> <span class="keyword">return</span> R.ok();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>service 这里后面需要优化</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 删除分类的方法</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> list 分类id的集合</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@Override</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">removeMenuByIds</span><span class="params">(List&lt;Long&gt; list)</span> &#123;</span><br><span class="line"> <span class="comment">//TODO 检查当前删除的菜单是否被其他的地方引用 后面优化</span></span><br><span class="line"> <span class="comment">//逻辑删除</span></span><br><span class="line"> categoryDao.deleteBatchIds(list);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="1-2-5-添加-添加商品分类"><a href="#1-2-5-添加-添加商品分类" class="headerlink" title="1.2.5 添加-添加商品分类"></a>1.2.5 添加-添加商品分类</h4><p>前端代码</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span c
<p>后端代码</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 保存</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@RequestMapping(&quot;/save&quot;)</span></span><br><span class="line"><span class="comment">//@RequiresPermissions(&quot;product:category:save&quot;)</span></span><br><span class="line"><span class="keyword">public</span> R <span class="title function_">save</span><span class="params">(<span class="meta">@RequestBody</span> CategoryEntity category)</span> &#123;</span><br><span class="line"> categoryService.save(category);</span><br><span class="line"></span><br><span class="line"> <span class="keyword">return</span> R.ok();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>实现的效果</p>
<p><img src="/pictures/image-20230605163004850.png" alt="image-20230605163004850"></p>
<h4 id="1-2-6-修改-修改商品的分类"><a href="#1-2-6-修改-修改商品的分类" class="headerlink" title="1.2.6 修改-修改商品的分类"></a>1.2.6 修改-修改商品的分类</h4><p>前端的代码(太复杂了,没有写完)</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span c
<p>老师的前端的代码</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span c
<p>后端的代码</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 批量修改商品的分类信息</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@RequestMapping(&quot;/update/sort&quot;)</span></span><br><span class="line"><span class="keyword">public</span> R <span class="title function_">updateSort</span><span class="params">(<span class="meta">@RequestBody</span> CategoryEntity[] categoryEntities)</span> &#123;</span><br><span class="line"> categoryService.updateBatchById(Arrays.asList(categoryEntities));</span><br><span class="line"> <span class="keyword">return</span> R.ok();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="1-2-品牌管理"><a href="#1-2-品牌管理" class="headerlink" title="1.2 品牌管理"></a>1.2 品牌管理</h3><h4 id="1-2-1-品牌的增删改查功能"><a href="#1-2-1-品牌的增删改查功能" class="headerlink" title="1.2.1 品牌的增删改查功能"></a>1.2.1 品牌的增删改查功能</h4><p>这里前端的代码是和先前生成的后端的代码一起生成的</p>
<p><img src="/pictures/image-20230611111440999.png" alt="image-20230611111440999"></p>
<p><strong>商品品牌的列表显示</strong></p>
<p>优化之后的前端的代码 (表头的优化 需要按钮显示的使用按钮进行显示)</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span c
<p>后端代码(修改品牌的显示状态)</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 修改</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@RequestMapping(&quot;/update&quot;)</span></span><br><span class="line"><span class="comment">//@RequiresPermissions(&quot;product:brand:update&quot;)</span></span><br><span class="line"><span class="keyword">public</span> R <span class="title function_">update</span><span class="params">(<span class="meta">@RequestBody</span> BrandEntity brand)</span>&#123;</span><br><span class="line"> brandService.updateById(brand);</span><br><span class="line"></span><br><span class="line"> <span class="keyword">return</span> R.ok();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><strong>品牌添加功能</strong></p>
2024-05-10 13:32:25 +08:00
<p><strong><a href="https://qingling.icu/posts/6319.html">阿里云对象存储OSS | The Blog (qingling.icu)</a></strong></p>
2023-09-22 21:57:28 +08:00
<p><img src="/pictures/image-20230611153418426.png" alt="image-20230611153418426"></p>
<p><strong>前端的代码</strong></p>
<p>品牌管理的列表页面</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span c
<p>品牌管理的添加和修改组件</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span c
<p><strong>后端的代码</strong></p>
<p><em>图片上传相关的后台接口</em></p>
<p>配置文件<br>apllication.yml</p>
<figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">spring:</span></span><br><span class="line"> <span class="attr">cloud:</span></span><br><span class="line"> <span class="attr">nacos:</span></span><br><span class="line"> <span class="attr">discovery:</span></span><br><span class="line"> <span class="attr">server-addr:</span> <span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span><span class="string">:8848</span></span><br><span class="line"> <span class="attr">alicloud:</span></span><br><span class="line"> <span class="attr">access-key:</span> <span class="string">XXXXXXXXXXXXXXXXXXXX</span></span><br><span class="line"> <span class="attr">secret-key:</span> <span class="string">XXXXXXXXXXXXXXXXXXXX</span></span><br><span class="line"> <span class="attr">oss:</span></span><br><span class="line"> <span class="attr">endpoint:</span> <span class="string">oss-cn-beijing.aliyuncs.com</span> </span><br><span class="line"> <span class="attr">bucket:</span> <span class="string">gulimall-0611</span> <span class="comment">#因为是公用的配置,这个配置是自己配置的</span></span><br><span class="line"></span><br><span class="line"> <span class="attr">application:</span></span><br><span class="line"> <span class="attr">name:</span> <span class="string">gulimall-third-party</span></span><br><span class="line"></span><br><span class="line"><span class="attr">server:</span></span><br><span class="line"> <span class="attr">port:</span> <span class="number">30000</span></span><br></pre></td></tr></table></figure>
<p>Controller</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.atguigu.gulimall.thirdparty.controller;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.aliyun.oss.OSS;</span><br><span class="line"><span class="keyword">import</span> com.aliyun.oss.OSSClientBuilder;</span><br><span class="line"><span class="keyword">import</span> com.aliyun.oss.common.utils.BinaryUtil;</span><br><span class="line"><span class="keyword">import</span> com.aliyun.oss.model.MatchMode;</span><br><span class="line"><span class="keyword">import</span> com.aliyun.oss.model.PolicyConditions;</span><br><span class="line"><span class="keyword">import</span> com.atguigu.common.utils.R;</span><br><span class="line"><span class="keyword">import</span> org.springframework.beans.factory.annotation.Autowired;</span><br><span class="line"><span class="keyword">import</span> org.springframework.beans.factory.annotation.Value;</span><br><span class="line"><span class="keyword">import</span> org.springframework.web.bind.annotation.RequestMapping;</span><br><span class="line"><span class="keyword">import</span> org.springframework.web.bind.annotation.RestController;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.text.SimpleDateFormat;</span><br><span class="line"><span class="keyword">import</span> java.util.Date;</span><br><span c
<p>配置网关,通过网关访问到该接口</p>
<figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="bullet">-</span> <span class="attr">id:</span> <span class="string">third_party_route</span></span><br><span class="line"> <span class="attr">uri:</span> <span class="string">lb://gulimall-third-party</span></span><br><span class="line"> <span class="attr">predicates:</span></span><br><span class="line"> <span class="bullet">-</span> <span class="string">Path=/api/thirdparty/**</span></span><br><span class="line"> <span class="attr">filters:</span></span><br><span class="line"> <span class="bullet">-</span> <span class="string">RewritePath=/api/thirdparty/(?&lt;segment&gt;.*),/$\&#123;segment&#125;</span></span><br></pre></td></tr></table></figure>
2024-05-10 13:32:25 +08:00
<h4 id="1-2-2-JSR303-数据校验"><a href="#1-2-2-JSR303-数据校验" class="headerlink" title="1.2.2 JSR303-数据校验"></a>1.2.2 JSR303-数据校验</h4><p><strong><a href="https://qingling.icu/posts/27166.html">数据校验 | The Blog (qingling.icu)</a></strong></p>
2023-09-22 21:57:28 +08:00
<p>1.依赖文件</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.hibernate<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>hibernate-validator<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">version</span>&gt;</span>6.0.7.Final<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>2.在实体类上添加上校验的规则</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.atguigu.gulimall.product.entity;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.baomidou.mybatisplus.annotation.TableId;</span><br><span class="line"><span class="keyword">import</span> com.baomidou.mybatisplus.annotation.TableName;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.io.Serializable;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> lombok.Data;</span><br><span class="line"><span class="keyword">import</span> org.hibernate.validator.constraints.URL;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> javax.validation.constraints.*;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 品牌</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> JasonGong</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@email</span> JasonGong@gmail.com</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@date</span> 2023-05-19 00:23:36</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@Data</span></span><br><span class="line"><span class="meta">@TableName(&quot;pms_brand&quot;)</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">BrandEntity</span> <span class="keyword">implements</span> <span class="title class_">Serializable</span> &#123;</span><br><span class="line"> <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="type">long</span> <span class="variable">serialVersio
<p>3.接口上开启检验,并自定义校验出错时的返回值</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 保存</span></span><br><span class="line"><span class="comment"> *&quot;<span class="doctag">@Valid</span>&quot; 开启校验</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> result 检验之后响应的结果信息</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@RequestMapping(&quot;/save&quot;)</span></span><br><span class="line"><span class="keyword">public</span> R <span class="title function_">save</span><span class="params">(<span class="meta">@Valid</span> <span class="meta">@RequestBody</span> BrandEntity brand, BindingResult result)</span>&#123;</span><br><span class="line"> <span class="keyword">if</span>(result.hasErrors())&#123;</span><br><span class="line"> Map&lt;String,String&gt; map = <span class="keyword">new</span> <span class="title class_">HashMap</span>&lt;&gt;();</span><br><span class="line"> <span class="comment">//获取检验的错误结果</span></span><br><span class="line"> <span class="keyword">for</span> (FieldError fieldError : result.getFieldErrors()) &#123;</span><br><span class="line"> <span class="comment">//获取到错误的提示</span></span><br><span class="line"> <span class="type">String</span> <span class="variable">defaultMessage</span> <span class="operator">=</span> fieldError.getDefaultMessage();</span><br><span class="line"> <span class="comment">//获取错误的属性的名字</span></span><br><span class="line"> <span class="type">String</span> <span class="variable">field</span> <span class="operator">=</span> fieldError.getField();</span><br><span class="line"> map.put(field,defaultMessage);</span><br><span class="line"> &#125;</span><br><span class="line"> <span class="keyword">return</span> R.error(<span class="number">400</span>,<span class="string">&quot;提交的数据不合法&quot;</span>).put(<span class="string">&quot;data&quot;</span>,map);</span><br><span class="line"> &#125;<span class="keyword">else</span> &#123;</span><br><span class="line"> brandService.save(brand);</span><br><span class="line"> &#125;</span><br><span class="line"> <span class="keyword">return</span> R.ok();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>返回的json格式示例</p>
<p><img src="/pictures/image-20230612165021780.png" alt="image-20230612165021780"></p>
2024-05-10 13:32:25 +08:00
<h4 id="1-2-3-统一异常处理"><a href="#1-2-3-统一异常处理" class="headerlink" title="1.2.3 统一异常处理"></a>1.2.3 统一异常处理</h4><p><strong><a href="https://qingling.icu/posts/31385.html">统一异常处理 | The Blog (qingling.icu)</a></strong></p>
2023-09-22 21:57:28 +08:00
<p>统一异常返回状态码</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.atguigu.common.exception;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> Jason Gong</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@version</span> 1.0</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Date</span> 2023/6/12</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Description</span> 返回的状态码信息</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">enum</span> <span class="title class_">BizCodeEnum</span> &#123;</span><br><span class="line"> UNKNOWN_EXCEPTION(<span class="number">10000</span>,<span class="string">&quot;系统未知异常&quot;</span>),</span><br><span class="line"> VALID_EXCEPTION(<span class="number">10001</span>,<span class="string">&quot;参数格式校验&quot;</span>);</span><br><span class="line"></span><br><span class="line"> <span class="keyword">private</span> Integer code;</span><br><span class="line"> <span class="keyword">private</span> String msg;</span><br><span class="line"></span><br><span class="line"> BizCodeEnum(Integer code, String msg) &#123;</span><br><span class="line"> <span class="built_in">this</span>.code = code;</span><br><span class="line"> <span class="built_in">this</span>.msg = msg;</span><br><span class="line"> &#125;</span><br><span class="line"></span><br><span class="line"> <span class="keyword">public</span> Integer <span class="title function_">getCode</span><span class="params">()</span> &#123;</span><br><span class="line"> <span class="keyword">return</span> code;</span><br><span class="line"> &#125;</span><br><span class="line"></span><br><span class="line"> <span class="keyword">public</span> String <span class="title function_">getMsg</span><span class="params">()</span> &#123;</span><br><span class="line"> <span class="keyword">return</span> msg;</span><br><span class="line"> &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>创建异常处理类</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.atguigu.gulimall.product.exception;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.atguigu.common.exception.BizCodeEnum;</span><br><span class="line"><span class="keyword">import</span> com.atguigu.common.utils.R;</span><br><span class="line"><span class="keyword">import</span> lombok.extern.slf4j.Slf4j;</span><br><span class="line"><span class="keyword">import</span> org.springframework.validation.BindingResult;</span><br><span class="line"><span class="keyword">import</span> org.springframework.validation.FieldError;</span><br><span class="line"><span class="keyword">import</span> org.springframework.web.bind.MethodArgumentNotValidException;</span><br><span class="line"><span class="keyword">import</span> org.springframework.web.bind.annotation.ExceptionHandler;</span><br><span class="line"><span class="keyword">import</span> org.springframework.web.bind.annotation.RestControllerAdvice;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.HashMap;</span><br><span class="line"><span class="keyword">import</span> java.util.Map;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> Jason Gong</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@version</span> 1.0</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Date</span> 2023/6/12</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Description</span> 异常处理类</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@Slf4j</span></span><br><span class="line"><span class="meta">@RestControllerAdvice(basePackages = &quot;com.atguigu.gulimall.product.controller&quot;)</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">GulimallExceptionControllerAdvice</span> &#123;</span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 统一处
<h4 id="1-2-4-关联分类功能"><a href="#1-2-4-关联分类功能" class="headerlink" title="1.2.4 关联分类功能"></a>1.2.4 关联分类功能</h4><p>功能截图</p>
<p><img src="/pictures/image-20230618232437618.png" alt="image-20230618232437618"></p>
<p>后端代码的实现</p>
<p>查询品牌和分类的关联关系</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 查询商品的关联分类数据</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> brandId 品牌的id</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@return</span> 关联的分类数据</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="meta">@GetMapping(&quot;/catelog/list&quot;)</span></span><br><span class="line"> <span class="keyword">public</span> R <span class="title function_">catelogList</span><span class="params">(<span class="meta">@RequestParam(&quot;brandId&quot;)</span> Long brandId)</span>&#123;</span><br><span class="line"> LambdaQueryWrapper&lt;CategoryBrandRelationEntity&gt; queryWrapper = <span class="keyword">new</span> <span class="title class_">LambdaQueryWrapper</span>&lt;&gt;();</span><br><span class="line"> queryWrapper.eq(CategoryBrandRelationEntity::getBrandId,brandId);</span><br><span class="line"> List&lt;CategoryBrandRelationEntity&gt; categoryBrandRelationEntityList = categoryBrandRelationService.list(queryWrapper);</span><br><span class="line"> <span class="keyword">return</span> R.ok().put(<span class="string">&quot;data&quot;</span>, categoryBrandRelationEntityList);</span><br><span class="line"> &#125;</span><br></pre></td></tr></table></figure>
<p>新增品牌和商品的关联关系</p>
<p>controller</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 保存商品的关联的分类数据信息</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="meta">@PostMapping(&quot;/save&quot;)</span></span><br><span class="line"> <span class="keyword">public</span> R <span class="title function_">saveCategoryBrandRelation</span><span class="params">(<span class="meta">@RequestBody</span> CategoryBrandRelationEntity categoryBrandRelation)</span>&#123;</span><br><span class="line"> categoryBrandRelationService.saveDetail(categoryBrandRelation);</span><br><span class="line"> <span class="keyword">return</span> R.ok();</span><br><span class="line"> &#125;</span><br></pre></td></tr></table></figure>
<p>service</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 保存商品和分类的关联数据</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> categoryBrandRelation 商品和分类的关联数据表</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="meta">@Override</span></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">saveDetail</span><span class="params">(CategoryBrandRelationEntity categoryBrandRelation)</span> &#123;</span><br><span class="line"> <span class="comment">//获取品牌的id和关联分类的id</span></span><br><span class="line"> <span class="type">Long</span> <span class="variable">brandId</span> <span class="operator">=</span> categoryBrandRelation.getBrandId();</span><br><span class="line"> <span class="type">Long</span> <span class="variable">catelogId</span> <span class="operator">=</span> categoryBrandRelation.getCatelogId();</span><br><span class="line"> <span class="comment">//根据id查询对应的品牌名和分类名</span></span><br><span class="line"> <span class="type">BrandEntity</span> <span class="variable">brand</span> <span class="operator">=</span> brandDao.selectById(brandId);</span><br><span class="line"> <span class="type">CategoryEntity</span> <span class="variable">category</span> <span class="operator">=</span> categoryDao.selectById(catelogId);</span><br><span class="line"> <span class="comment">//设置值并保存</span></span><br><span class="line"> categoryBrandRelation.setBrandName(brand.getName());</span><br><span class="line"> categoryBrandRelation.setCatelogName(category.getName());</span><br><span class="line"> <span class="built_in">this</span>.save(categoryBrandRelation);</span><br><span class="line"> &#125;</span><br></pre></td></tr></table></figure>
<p><strong>注意</strong>:这里品牌和关联分类之间的关系表中使用了冗余的字段,所以在修改品牌的时候,关系表中的字段也要修改一下</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Override</span></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">updateDetail</span><span class="params">(BrandEntity brand)</span> &#123;</span><br><span class="line"> <span class="comment">//保证冗余字段的数据的一致性</span></span><br><span class="line"> <span class="comment">//更新品牌表中的数据</span></span><br><span class="line"> <span class="built_in">this</span>.updateById(brand);</span><br><span class="line"> <span class="keyword">if</span> (!StringUtils.isEmpty(brand.getName())) &#123;</span><br><span class="line"> <span class="comment">//同步更新关联表中的数据</span></span><br><span class="line"> categoryBrandRelationService.updateBrand(brand.getBrandId(),brand.getName());</span><br><span class="line"> <span class="comment">//TODO 更新其他的关联</span></span><br><span class="line"> &#125;</span><br><span class="line"> &#125;</span><br></pre></td></tr></table></figure>
<p>关联的分类也是冗余的字段 也要同步修改</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 级联更新所有的关联的数据</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> category 商品分类的实体</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="meta">@Override</span></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">updateCascade</span><span class="params">(CategoryEntity category)</span> &#123;</span><br><span class="line"> <span class="comment">//更新分类表中的数据</span></span><br><span class="line"> <span class="built_in">this</span>.updateById(category);</span><br><span class="line"> <span class="comment">//更新分类与品牌名关联表中分类的名称</span></span><br><span class="line"> categoryBrandRelationService.updateCategory(category.getCatId(),category.getName());</span><br><span class="line"> &#125;</span><br></pre></td></tr></table></figure>
<p>功能实现之后的截图</p>
<p><img src="/pictures/image-20230619111515367.png" alt="image-20230619111515367"></p>
<h3 id="1-3-平台属性"><a href="#1-3-平台属性" class="headerlink" title="1.3 平台属性"></a>1.3 平台属性</h3><p>创建属性管理的菜单(通过sql导入所有的菜单,导入所有以后需要使用的菜单)</p>
<p>资料中的sys_menus.sql文件保存的是所有的菜单信息</p>
<p><img src="/pictures/image-20230616152846339.png" alt="image-20230616152846339"></p>
<p><strong>谷粒商城的接口的在线文档:<a target="_blank" rel="noopener" href="https://easydoc.net/s/78237135">https://easydoc.net/s/78237135</a></strong></p>
<p><img src="/pictures/image-20230616153304953.png" alt="image-20230616153304953"></p>
<p><strong>Object的划分</strong></p>
<ol>
<li><p>PO 持久对象</p>
<p>PO就是对应数据库中某个表的一条记录多个记录可以用PO的集合。PO中应该不包含对数据库的操作。</p>
</li>
<li><p>DO 领域对象</p>
<p>就是从现实世界中抽象出来的有形或者无形的业务实体。</p>
</li>
<li><p>TO 数据传输对象</p>
<p>不同的应用程序之间传输的对象</p>
</li>
<li><p>DTO 数据传输对象</p>
<p>泛指展示层与服务层之间的数据传输对象</p>
</li>
<li><p>VO 值对象</p>
<p>视图对象 接收页面传入过来的数据,封装对象;将业务处理完成的对象,封装成页面需要的数据</p>
</li>
<li><p>BO 业务对象</p>
</li>
<li><p>POJO 简单无规则的java对象</p>
</li>
<li><p>DAO 数据访问对象</p>
</li>
</ol>
<h4 id="1-3-1-属性分组功能"><a href="#1-3-1-属性分组功能" class="headerlink" title="1.3.1 属性分组功能"></a>1.3.1 属性分组功能</h4><p>实现一个分类 联动的显示相应的分组信息</p>
<p>抽取商品分类的组件</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line">&lt;template&gt;</span><br><span class="line"> &lt;el-tree</span><br><span class="line"> :data=&quot;menus&quot;</span><br><span class="line"> :props=&quot;defaultProps&quot;</span><br><span class="line"> node-key=&quot;catId&quot;</span><br><span class="line"> ref=&quot;menuTree&quot;</span><br><span class="line"> &gt;&lt;/el-tree&gt;</span><br><span class="line">&lt;/template&gt;</span><br><span class="line"> &lt;script&gt;</span><br><span class="line">export default &#123;</span><br><span class="line"> components: &#123;&#125;,</span><br><span class="line"> data() &#123;</span><br><span class="line"> return &#123;</span><br><span class="line"> menus: [],</span><br><span class="line"> expandedKey: [],</span><br><span class="line"> defaultProps: &#123;</span><br><span class="line"> children: &quot;children&quot;,</span><br><span class="line"> label: &quot;name&quot;,</span><br><span class="line"> &#125;,</span><br><span class="line"> &#125;;</span><br><span class="line"> &#125;,</span><br><span class="line"> created() &#123;</span><br><span class="line"> this.getMenus();</span><br><span class="line"> &#125;,</span><br><span class="line"> methods: &#123;</span><br><span class="line"> getMenus() &#123;</span><br><span class="line"> this.$http(&#123;</span><br><span class="line"> url: this.$http.adornUrl(&quot;/product/category/list/tree&quot;),</span><br><span class="line"> method: &quot;get&quot;,</span><br><span class="line"> &#125;).then((&#123; data &#125;) =&gt; &#123;</span><br><span class="line"> console.log(&quot;商品分类数据:&quot;, data.data);</span><br><span class="line"> this.menus = data.data;</span><br><span class="line"> &#125;);</span><br><span class="line"> &#125;,</span><br><span class="line"> &#125;,</span><br><span class="line">&#125;;</span><br><span class="line">&lt;/script&gt;</span><br><span class="line"> </span><br><span class="line"> &lt;style&gt;</span><br><span class="line">&lt;/style&gt;</span><br></pre></td></tr></table></figure>
<p>属性分组页面</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span c
<p>页面效果</p>
<p><img src="/pictures/image-20230617173759895.png" alt="image-20230617173759895"></p>
<p><strong>父子组件中数据的传递</strong></p>
<p>子组件给父组件传递的数据,事件传递,子组件给父组件发送一个事件,事件传递</p>
<p>子组件调整</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br></pre></td><td class="code"><pre><span class="line">&lt;template&gt;</span><br><span class="line"> &lt;el-tree</span><br><span class="line"> :data=&quot;menus&quot;</span><br><span class="line"> :props=&quot;defaultProps&quot;</span><br><span class="line"> node-key=&quot;catId&quot;</span><br><span class="line"> ref=&quot;menuTree&quot;</span><br><span class="line"> @node-click=&quot;nodeclick&quot;</span><br><span class="line"> &gt;&lt;/el-tree&gt;</span><br><span class="line">&lt;/template&gt;</span><br><span class="line"> &lt;script&gt;</span><br><span class="line">export default &#123;</span><br><span class="line"> components: &#123;&#125;,</span><br><span class="line"> data() &#123;</span><br><span class="line"> return &#123;</span><br><span class="line"> menus: [],</span><br><span class="line"> expandedKey: [],</span><br><span class="line"> defaultProps: &#123;</span><br><span class="line"> children: &quot;children&quot;,</span><br><span class="line"> label: &quot;name&quot;,</span><br><span class="line"> &#125;,</span><br><span class="line"> &#125;;</span><br><span class="line"> &#125;,</span><br><span class="line"> created() &#123;</span><br><span class="line"> this.getMenus();</span><br><span class="line"> &#125;,</span><br><span class="line"> methods: &#123;</span><br><span class="line"> getMenus() &#123;</span><br><span class="line"> this.$http(&#123;</span><br><span class="line"> url: this.$http.adornUrl(&quot;/product/category/list/tree&quot;),</span><br><span class="line"> method: &quot;get&quot;,</span><br><span class="line"> &#125;).then((&#123; data &#125;) =&gt; &#123;</span><br><span class="line"> console.log(&quot;商品分类数据:&quot;, data.data);</span><br><span class="line"> this.menus = data.data;</span><br><span class="line"> &#125;);</span><br><span class="line"> &#125;,</span><br><span class="line"> nodeclick(data,node,component) &#123;</span><br><span class="line"> console.log(&quot;子组件被点击,传递的数据有:data:&quot;,data,&quot;node:&quot;,node,&quot;component:&quot;,component);</span><br><span class="line"> //子组件向父组件传递数据</span><br><span class="line"> this.$emit(&quot;tree-node-click&quot;,data,node,component);</span><br><span class="line"> &#125;,</span><br><span class="line"> &#125;,</span><br><span class="line">&#125;;</span><br><span class="line">&lt;/script&gt;</span><br><span class="line"> </span><br><sp
<p>父组件接受相应的数据</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span c
<p>后端代码</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Override</span></span><br><span class="line"> <span class="keyword">public</span> PageUtils <span class="title function_">queryPage</span><span class="params">(Map&lt;String, Object&gt; params, Long catelogId)</span> &#123;</span><br><span class="line"> <span class="comment">//首先先判断catelogId是不是0 如果是0的话就查询所有的属性分组信息</span></span><br><span class="line"> <span class="keyword">if</span> (catelogId == <span class="number">0</span>) &#123;</span><br><span class="line"> IPage&lt;AttrGroupEntity&gt; page = <span class="built_in">this</span>.page(</span><br><span class="line"> <span class="keyword">new</span> <span class="title class_">Query</span>&lt;AttrGroupEntity&gt;().getPage(params),</span><br><span class="line"> <span class="keyword">new</span> <span class="title class_">QueryWrapper</span>&lt;AttrGroupEntity&gt;()</span><br><span class="line"> );</span><br><span class="line"> <span class="keyword">return</span> <span class="keyword">new</span> <span class="title class_">PageUtils</span>(page);</span><br><span class="line"> &#125; <span class="keyword">else</span> &#123;</span><br><span class="line"> <span class="comment">//查询的时候传递过来的关键词</span></span><br><span class="line"> <span class="type">String</span> <span class="variable">key</span> <span class="operator">=</span> (String) params.get(<span class="string">&quot;key&quot;</span>);</span><br><span class="line"> <span class="comment">//SQL语句:select * from pms_attr_group where catelog_id = ? and (attr_group_id = key or attr_group_name = %key%)</span></span><br><span class="line"> LambdaQueryWrapper&lt;AttrGroupEntity&gt; queryWrapper = <span class="keyword">new</span> <span class="title class_">LambdaQueryWrapper</span>&lt;&gt;();</span><br><span class="line"> queryWrapper.eq(AttrGroupEntity::getCatelogId, catelogId);</span><br><span class="line"> <span class="keyword">if</span> (!StringUtils.isEmpty(key)) &#123;</span><br><span class="line"> queryWrapper.and((obj) -&gt; &#123;</span><br><span class="line"> obj.eq(AttrGroupEntity::getAttrGroupId, key).or().like(AttrGroupEntity::getAttrGroupName, key);</span><br><span class="line"> &#125;);</span><br><span class="line"> &#125;</span><br><span class="line"> IPage&lt;AttrGroupEntity&gt; page = <span class="built_in">this</span>.page(</span><br><span class="line"> <span class="keyword">new</span> <span class="title class_">Query</span>&lt;AttrGroupEntity&gt;().getPage(params),</span><br><span class="line"> queryWrapper</span><br><span class="line"> );</span><br><span class="line"> <span class="keyword">return</span> <span class="keyword">new</span> <span class="title class_">PageUtils</span>(page);</span><br><span class="line"> &#125;</span><br><span class="line"> &#125;</span><br></pre></td></tr></table></figure>
<p><strong>属性分组的添加功能</strong></p>
<p>前端代码</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class=
<p>后端代码</p>
<p>在商品分类的children上加上@JsonInclude注解解决后面的级联选择多出一个空白的选择的问题</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@JsonInclude(JsonInclude.Include.NON_EMPTY)</span></span><br><span class="line"><span class="meta">@TableField(exist = false)</span><span class="comment">//这个注解要加,因为这个属性不是数据库中的字段</span></span><br><span class="line"><span class="keyword">private</span> List&lt;CategoryEntity&gt; children;<span class="comment">//这里的属性名不是瞎起的,与ElementUI中的显示相对应,这里改了,前端也要改</span></span><br></pre></td></tr></table></figure>
<p>修改的时候,级联选择部分数据的回显</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * 找到catelog的完整路径</span><br><span class="line"> * @param catelogId 路径的最后一个catelog的id</span><br><span class="line"> * @return 完整的路径</span><br><span class="line"> */</span><br><span class="line"> @Override</span><br><span class="line"> public Long[] findCatelogPath(Long catelogId) &#123;</span><br><span class="line"> List&lt;Long&gt; paths = new ArrayList&lt;&gt;();</span><br><span class="line"> List&lt;Long&gt; parentPath = findParentPath(catelogId, paths);</span><br><span class="line"> Collections.reverse(parentPath);</span><br><span class="line"> return (Long[]) parentPath.toArray(new Long[parentPath.size()]);</span><br><span class="line"> &#125;</span><br><span class="line"> /**</span><br><span class="line"> * 递归找出所有的路径</span><br><span class="line"> */</span><br><span class="line"> private List&lt;Long&gt; findParentPath(Long catelogId,List&lt;Long&gt; paths)&#123;</span><br><span class="line"> paths.add(catelogId);</span><br><span class="line"> //根据id查询相关的信息</span><br><span class="line"> CategoryEntity category = this.getById(catelogId);</span><br><span class="line"> if(category.getParentCid()!=0)&#123;</span><br><span class="line"> findParentPath(category.getParentCid(),paths);</span><br><span class="line"> &#125;</span><br><span class="line"> return paths;</span><br><span class="line"> &#125;</span><br></pre></td></tr></table></figure>
<p>属性关联商品的规格参数和基本属性信息</p>
<p>功能描述</p>
<p><img src="/pictures/image-20230625090118059.png" alt="image-20230625090118059"></p>
<p>查询商品下关联的属性分组信息</p>
<p>查询商品关联属性分组的后端关键代码</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 根据分组的id查询关联的所有属性</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> attrgroupId 分组的id</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@return</span> 关联的所有属性组成集合</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="meta">@Override</span></span><br><span class="line"> <span class="keyword">public</span> List&lt;AttrEntity&gt; <span class="title function_">getRelationAttr</span><span class="params">(Long attrgroupId)</span> &#123;</span><br><span class="line"> <span class="comment">//根据中间表查询出属性分组的id信息</span></span><br><span class="line"> List&lt;AttrAttrgroupRelationEntity&gt; relationEntityList = relationDao.selectList(</span><br><span class="line"> <span class="keyword">new</span> <span class="title class_">LambdaQueryWrapper</span>&lt;AttrAttrgroupRelationEntity&gt;().eq(AttrAttrgroupRelationEntity::getAttrGroupId, attrgroupId));</span><br><span class="line"> List&lt;Long&gt; attrIds = <span class="keyword">new</span> <span class="title class_">ArrayList</span>&lt;&gt;();</span><br><span class="line"> <span class="keyword">for</span> (AttrAttrgroupRelationEntity relationEntity : relationEntityList) &#123;</span><br><span class="line"> attrIds.add(relationEntity.getAttrId());</span><br><span class="line"> &#125;</span><br><span class="line"> <span class="comment">//根据属性分组的id查询属性分组的详细信息</span></span><br><span class="line"> <span class="keyword">return</span> (List&lt;AttrEntity&gt;) <span class="built_in">this</span>.listByIds(attrIds);</span><br><span class="line"> &#125;</span><br></pre></td></tr></table></figure>
<p>移除关联属性分组的功能后端的关键代码</p>
<p>service层的代码</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 批量删除商品关联的属性分组信息</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> vos 属性id和属性分组id组成的集合</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="meta">@Override</span></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">deleteRelation</span><span class="params">(AttrGroupRelationVo[] vos)</span> &#123;</span><br><span class="line"> List&lt;AttrAttrgroupRelationEntity&gt; entities = Arrays.asList(vos).stream().map((item) -&gt; &#123;</span><br><span class="line"> <span class="type">AttrAttrgroupRelationEntity</span> <span class="variable">relationEntity</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">AttrAttrgroupRelationEntity</span>();</span><br><span class="line"> BeanUtils.copyProperties(item, relationEntity);</span><br><span class="line"> <span class="keyword">return</span> relationEntity;</span><br><span class="line"> &#125;).collect(Collectors.toList());</span><br><span class="line"> relationDao.deleteBatchRelation(entities);</span><br><span class="line"> &#125;</span><br></pre></td></tr></table></figure>
<p>dao层的代码</p>
<p>使用动态的sql批量的删除属性和分组的关联关系</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--void deleteBatchRelation(List&lt;AttrAttrgroupRelationEntity&gt; entities);--&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">delete</span> <span class="attr">id</span>=<span class="string">&quot;deleteBatchRelation&quot;</span>&gt;</span></span><br><span class="line"> delete from `pms_attr_attrgroup_relation` where</span><br><span class="line"> <span class="tag">&lt;<span class="name">foreach</span> <span class="attr">collection</span>=<span class="string">&quot;entities&quot;</span> <span class="attr">item</span>=<span class="string">&quot;item&quot;</span> <span class="attr">separator</span>=<span class="string">&quot; OR &quot;</span>&gt;</span></span><br><span class="line"> (attr_id = #&#123;item.attrId&#125;) and attr_group_id = #&#123;item.attrGroupId&#125;</span><br><span class="line"> <span class="tag">&lt;/<span class="name">foreach</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;/<span class="name">delete</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>查询属性分组没有关联的属性</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 获取当前分组没有关联的所有属性</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> params 分页相关的参数</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> attrgroupId 属性分组的id</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@return</span> 分页相关的数据</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="meta">@Override</span></span><br><span class="line"> <span class="keyword">public</span> PageUtils <span class="title function_">getNoRelationAttr</span><span class="params">(Map&lt;String, Object&gt; params, Long attrgroupId)</span> &#123;</span><br><span class="line"> <span class="comment">//当前的分组只能关联自己所属的分类里面的所有属性 例如手机的分类里面只能关联手机的所有属性</span></span><br><span class="line"> <span class="type">AttrGroupEntity</span> <span class="variable">attrGroupEntity</span> <span class="operator">=</span> attrGroupDao.selectById(attrgroupId);</span><br><span class="line"> <span class="type">Long</span> <span class="variable">catelogId</span> <span class="operator">=</span> attrGroupEntity.getCatelogId();</span><br><span class="line"> <span class="comment">//当前分组只能关联别的分组没有引用的属性</span></span><br><span class="line"> List&lt;AttrGroupEntity&gt; groups = attrGroupDao.selectList(</span><br><span class="line"> <span class="keyword">new</span> <span class="title class_">LambdaQueryWrapper</span>&lt;AttrGroupEntity&gt;().eq(AttrGroupEntity::getCatelogId, catelogId));</span><br><span class="line"> List&lt;Long&gt; collect = groups.stream().map((item) -&gt; &#123;</span><br><span class="line"> <span class="keyword">return</span> item.getAttrGroupId();</span><br><span class="line"> &#125;).collect(Collectors.toList());</span><br><span class="line"> List&lt;AttrAttrgroupRelationEntity&gt; groupId = relationDao.selectList(</span><br><span class="line"> <span class="keyword">new</span> <span class="title class_">LambdaQueryWrapper</span>&lt;AttrAttrgroupRelationEntity&gt;().in(AttrAttrgroupRelationEntity::getAttrGroupId, collect));</span><br><span class="line"> List&lt;Long&gt; attrIds = groupId.stream().map((item) -&gt; &#123;</span><br><span class="line"> <span class="keyword">return</span> item.getAttrId();</span><br><span class="line"> &#125;).collect(Collectors.toList());</span><br><span class="line"> LambdaQueryWra
<p>给属性关联相关的规格参数和销售属性</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Override</span></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">saveBatch</span><span class="params">(List&lt;AttrGroupRelationVo&gt; vos)</span> &#123;</span><br><span class="line"> List&lt;AttrAttrgroupRelationEntity&gt; entities = vos.stream().map((item) -&gt; &#123;</span><br><span class="line"> <span class="type">AttrAttrgroupRelationEntity</span> <span class="variable">relationEntity</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">AttrAttrgroupRelationEntity</span>();</span><br><span class="line"> BeanUtils.copyProperties(item, relationEntity);</span><br><span class="line"> <span class="keyword">return</span> relationEntity;</span><br><span class="line"> &#125;).collect(Collectors.toList());</span><br><span class="line"> <span class="built_in">this</span>.saveBatch(entities);</span><br><span class="line"> &#125;</span><br></pre></td></tr></table></figure>
<h4 id="1-3-2-规格参数功能"><a href="#1-3-2-规格参数功能" class="headerlink" title="1.3.2 规格参数功能"></a>1.3.2 规格参数功能</h4><p>功能截图</p>
<p><img src="/pictures/image-20230619111338153.png" alt="image-20230619111338153"></p>
<p>新增功能的后端关键代码</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 保存规格参数信息</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> attr 页面中提交的规格参数相关的信息组成的实体</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="meta">@Transactional</span></span><br><span class="line"> <span class="meta">@Override</span></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">saveAttr</span><span class="params">(AttrVo attr)</span> &#123;</span><br><span class="line"> <span class="type">AttrEntity</span> <span class="variable">attrEntity</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">AttrEntity</span>();</span><br><span class="line"> BeanUtils.copyProperties(attr,attrEntity);</span><br><span class="line"> <span class="comment">//保存基本的数据</span></span><br><span class="line"> <span class="built_in">this</span>.save(attrEntity);</span><br><span class="line"> <span class="comment">//保存关联关系</span></span><br><span class="line"> <span class="type">AttrAttrgroupRelationEntity</span> <span class="variable">relationEntity</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">AttrAttrgroupRelationEntity</span>();</span><br><span class="line"> relationEntity.setAttrGroupId(attr.getAttrGroupId());</span><br><span class="line"> relationEntity.setAttrId(attrEntity.getAttrId());</span><br><span class="line"> relationDao.insert(relationEntity);</span><br><span class="line"> &#125;</span><br></pre></td></tr></table></figure>
<p>查询列表后端的关键代码</p>
<p>这里使用的stream流处理数据没有使用多表联合查询来查询数据</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Override</span></span><br><span class="line"> <span class="keyword">public</span> PageUtils <span class="title function_">queryBaseAttrPage</span><span class="params">(Map&lt;String, Object&gt; params, Long catelogId)</span> &#123;</span><br><span class="line"> LambdaQueryWrapper&lt;AttrEntity&gt; queryWrapper = <span class="keyword">new</span> <span class="title class_">LambdaQueryWrapper</span>&lt;&gt;();</span><br><span class="line"> <span class="keyword">if</span> (catelogId != <span class="number">0</span>) &#123;<span class="comment">//等于0就是查询所有的分类信息下的规格参数信息</span></span><br><span class="line"> queryWrapper.eq(AttrEntity::getCatelogId, catelogId);</span><br><span class="line"> &#125;</span><br><span class="line"> <span class="comment">//获取检索的条件</span></span><br><span class="line"> <span class="type">String</span> <span class="variable">key</span> <span class="operator">=</span> (String) params.get(<span class="string">&quot;key&quot;</span>);</span><br><span class="line"> <span class="keyword">if</span> (!StringUtils.isEmpty(key)) &#123;</span><br><span class="line"> queryWrapper.like(AttrEntity::getAttrName, key).or().eq(AttrEntity::getAttrId, key);</span><br><span class="line"> &#125;</span><br><span class="line"> IPage&lt;AttrEntity&gt; page = <span class="built_in">this</span>.page(</span><br><span class="line"> <span class="keyword">new</span> <span class="title class_">Query</span>&lt;AttrEntity&gt;().getPage(params),</span><br><span class="line"> queryWrapper</span><br><span class="line"> );</span><br><span class="line"> <span class="type">PageUtils</span> <span class="variable">pageUtils</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">PageUtils</span>(page);</span><br><span class="line"> List&lt;AttrEntity&gt; records = page.getRecords();</span><br><span class="line"> List&lt;AttrRespVo&gt; respVos = records.stream().map((attrEntity) -&gt; &#123;</span><br><span class="line"> <span class="type">AttrRespVo</span> <span class="variable">attrRespVo</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">AttrRespVo</span>();</span><br><span class="line"> BeanUtils.copyProperties(attrEntity, attrRespVo);</span><br><span class="line"> <span class="comment">//设置三级商品分类名和分组名</span></span><br><span class="line"> <span class="comment">//设置所属的商品的分类名</span></span><br><span class="line
<p>规格参数修改功能后端的关键代码</p>
<p>修改前的数据回显</p>
<p>查询属性的详情信息</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 查询属性的详细信息</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> attrId 属性的id</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@return</span> 属性的详细信息封装的集合</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="meta">@Override</span></span><br><span class="line"> <span class="keyword">public</span> AttrRespVo <span class="title function_">getAttrInfo</span><span class="params">(Long attrId)</span> &#123;</span><br><span class="line"> <span class="type">AttrRespVo</span> <span class="variable">attrRespVo</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">AttrRespVo</span>();</span><br><span class="line"> <span class="comment">//查询属性的基本信息</span></span><br><span class="line"> <span class="type">AttrEntity</span> <span class="variable">attr</span> <span class="operator">=</span> <span class="built_in">this</span>.getById(attrId);</span><br><span class="line"> BeanUtils.copyProperties(attr, attrRespVo);</span><br><span class="line"> <span class="comment">//查询分组id和分类的详细路径</span></span><br><span class="line"> <span class="comment">//分组信息的设置</span></span><br><span class="line"> <span class="type">AttrAttrgroupRelationEntity</span> <span class="variable">relationEntity</span> <span class="operator">=</span> relationDao.selectOne(</span><br><span class="line"> <span class="keyword">new</span> <span class="title class_">LambdaQueryWrapper</span>&lt;AttrAttrgroupRelationEntity&gt;().eq(AttrAttrgroupRelationEntity::getAttrId, attrRespVo.getAttrId()));</span><br><span class="line"> <span class="keyword">if</span> (relationEntity != <span class="literal">null</span>) &#123;</span><br><span class="line"> attrRespVo.setAttrGroupId(relationEntity.getAttrGroupId());</span><br><span class="line"> <span class="type">AttrGroupEntity</span> <span class="variable">attrGroupEntity</span> <span class="operator">=</span> attrGroupDao.selectById(relationEntity.getAttrGroupId());</span><br><span class="line"> <span class="keyword">if</span> (attrGroupEntity != <span class="literal">null</span>) &#123;</span><br><span class="line"> attrRespVo.setGroupName(attrGroupEntity.getAttrGroupName());</span><br><span class="line"> &#125;</span><br><span class="line"> &#125;</span><br><span class="line"> <span class="comment">
<p>修改的时候页面回显的效果</p>
<p><img src="/pictures/image-20230624192906124.png" alt="image-20230624192906124"></p>
<p>修改功能后端的关键代码</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 修改属性的方法</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> attr 页面传输过来的商品属性的实体</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="meta">@Transactional</span></span><br><span class="line"> <span class="meta">@Override</span></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">updateAttr</span><span class="params">(AttrVo attr)</span> &#123;</span><br><span class="line"> <span class="comment">//修改基本的数据</span></span><br><span class="line"> <span class="type">AttrEntity</span> <span class="variable">attrEntity</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">AttrEntity</span>();</span><br><span class="line"> BeanUtils.copyProperties(attr, attrEntity);</span><br><span class="line"> <span class="built_in">this</span>.updateById(attrEntity);</span><br><span class="line"> <span class="comment">//修改关联表的数据</span></span><br><span class="line"> <span class="type">AttrAttrgroupRelationEntity</span> <span class="variable">relationEntity</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">AttrAttrgroupRelationEntity</span>();</span><br><span class="line"> BeanUtils.copyProperties(attr, relationEntity);</span><br><span class="line"> LambdaQueryWrapper&lt;AttrAttrgroupRelationEntity&gt; queryWrapper = <span class="keyword">new</span> <span class="title class_">LambdaQueryWrapper</span>&lt;&gt;();</span><br><span class="line"> queryWrapper.eq(AttrAttrgroupRelationEntity::getAttrId, relationEntity.getAttrId());</span><br><span class="line"> <span class="type">Integer</span> <span class="variable">count</span> <span class="operator">=</span> relationDao.selectCount(queryWrapper);</span><br><span class="line"> <span class="keyword">if</span> (count &gt; <span class="number">0</span>) &#123;<span class="comment">//判断是修改还是添加的操作</span></span><br><span class="line"> relationDao.update(relationEntity, queryWrapper);</span><br><span class="line"> &#125; <span class="keyword">else</span> &#123;</span><br><span class="line"> relationDao.insert(relationEntity);</span><br><span class="line"> &#125;</span><br><span class="line"> &#125;</span><br></pre></td></tr></table></figure>
<h4 id="1-3-3-销售属性功能"><a href="#1-3-3-销售属性功能" class="headerlink" title="1.3.3 销售属性功能"></a>1.3.3 销售属性功能</h4><p>分类属性列表显示</p>
<p>controller层的代码</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * /product/attr/sale/list/&#123;catelogId&#125;</span></span><br><span class="line"><span class="comment"> * 路径 /product/attr/base/list/&#123;catelogId&#125;</span></span><br><span class="line"><span class="comment"> * 规格参数的列表显示</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> params 条件查询的参数</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> catelogId 商品分类的id</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@return</span> 规格参数的分页数据</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="meta">@GetMapping(&quot;/&#123;attrType&#125;/list/&#123;catelogId&#125;&quot;)</span></span><br><span class="line"> <span class="keyword">public</span> R <span class="title function_">baseAttrList</span><span class="params">(<span class="meta">@RequestParam</span> Map&lt;String, Object&gt; params, <span class="meta">@PathVariable</span> Long catelogId, <span class="meta">@PathVariable(&quot;attrType&quot;)</span> String type)</span> &#123;</span><br><span class="line"> log.warn(<span class="string">&quot;规格参数分页的查询条件:&#123;&#125;,商品分类的id:&#123;&#125;&quot;</span>, params, catelogId);</span><br><span class="line"> <span class="type">PageUtils</span> <span class="variable">page</span> <span class="operator">=</span> attrService.queryBaseAttrPage(params, catelogId,type);</span><br><span class="line"> <span class="keyword">return</span> R.ok().put(<span class="string">&quot;page&quot;</span>, page);</span><br><span class="line"> &#125;</span><br></pre></td></tr></table></figure>
<p>service层的代码</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Override</span></span><br><span class="line"> <span class="keyword">public</span> PageUtils <span class="title function_">queryBaseAttrPage</span><span class="params">(Map&lt;String, Object&gt; params, Long catelogId, String type)</span> &#123;</span><br><span class="line"> LambdaQueryWrapper&lt;AttrEntity&gt; queryWrapper = <span class="keyword">new</span> <span class="title class_">LambdaQueryWrapper</span>&lt;&gt;();</span><br><span class="line"> <span class="comment">//路径中属性的attrType是base就是基本属性否则就代表的是销售属性</span></span><br><span class="line"> queryWrapper.eq(AttrEntity::getAttrType, <span class="string">&quot;base&quot;</span>.equalsIgnoreCase(type) ? <span class="number">1</span> : <span class="number">0</span>);</span><br><span class="line"> <span class="keyword">if</span> (catelogId != <span class="number">0</span>) &#123;<span class="comment">//等于0就是查询所有的分类信息下的规格参数信息</span></span><br><span class="line"> queryWrapper.eq(AttrEntity::getCatelogId, catelogId);</span><br><span class="line"> &#125;</span><br><span class="line"> <span class="comment">//获取检索的条件</span></span><br><span class="line"> <span class="type">String</span> <span class="variable">key</span> <span class="operator">=</span> (String) params.get(<span class="string">&quot;key&quot;</span>);</span><br><span class="line"> <span class="keyword">if</span> (!StringUtils.isEmpty(key)) &#123;</span><br><span class="line"> queryWrapper.like(AttrEntity::getAttrName, key).or().eq(AttrEntity::getAttrId, key);</span><br><span class="line"> &#125;</span><br><span class="line"> IPage&lt;AttrEntity&gt; page = <span class="built_in">this</span>.page(</span><br><span class="line"> <span class="keyword">new</span> <span class="title class_">Query</span>&lt;AttrEntity&gt;().getPage(params),</span><br><span class="line"> queryWrapper</span><br><span class="line"> );</span><br><span class="line"> <span class="type">PageUtils</span> <span class="variable">pageUtils</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">PageUtils</span>(page);</span><br><span class="line"> List&lt;AttrEntity&gt; records = page.getRecords();</span><br><span class="line"> List&lt;AttrRespVo&gt; respVos = records.stream().map((attrEntity) -&gt; &#123;</span><br><span class="line"> <span class="type
<p>完整的功能展示</p>
<p><img src="/pictures/image-20230625090037616.png" alt="image-20230625090037616"></p>
<h3 id="1-4-商品维护"><a href="#1-4-商品维护" class="headerlink" title="1.4 商品维护"></a>1.4 商品维护</h3><h4 id="1-4-1-SPU管理"><a href="#1-4-1-SPU管理" class="headerlink" title="1.4.1 SPU管理"></a>1.4.1 SPU管理</h4><p><strong>条件查询功能Spu检索</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Spu检索功能</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> params 分页相关的数据</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@return</span> 分页对象</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="meta">@Override</span></span><br><span class="line"> <span class="keyword">public</span> PageUtils <span class="title function_">queryPageByCondition</span><span class="params">(Map&lt;String, Object&gt; params)</span> &#123;</span><br><span class="line"> LambdaQueryWrapper&lt;SpuInfoEntity&gt; queryWrapper = <span class="keyword">new</span> <span class="title class_">LambdaQueryWrapper</span>&lt;&gt;();</span><br><span class="line"> <span class="comment">//关键字检索</span></span><br><span class="line"> <span class="type">String</span> <span class="variable">key</span> <span class="operator">=</span> (String) params.get(<span class="string">&quot;key&quot;</span>);</span><br><span class="line"> <span class="keyword">if</span> (!StringUtils.isEmpty(key)) &#123;</span><br><span class="line"> <span class="comment">//这里使用了or为了和下面的区分开来,要使用and,将下面这个条件用括号括起来</span></span><br><span class="line"> <span class="comment">//SELECT COUNT(1) FROM pms_spu_info WHERE (((spu_name LIKE ? OR id = ?)) AND catalog_id = ? AND brand_id = ? AND publish_status = ?)</span></span><br><span class="line"> queryWrapper.and((wrapper) -&gt; &#123;</span><br><span class="line"> wrapper.like(SpuInfoEntity::getSpuName, key).or().eq(SpuInfoEntity::getId, key);</span><br><span class="line"> &#125;);</span><br><span class="line"> &#125;</span><br><span class="line"> <span class="comment">//分类</span></span><br><span class="line"> <span class="type">String</span> <span class="variable">catelogId</span> <span class="operator">=</span> (String) params.get(<span class="string">&quot;catelogId&quot;</span>);</span><br><span class="line"> <span class="keyword">if</span> (!StringUtils.isEmpty(catelogId)) &#123;</span><br><span class="line"> queryWrapper.eq(SpuInfoEntity::getCatalogId, catelogId);</span><br><span class="line"> &#125;</span><br><span class="line"> <span class="comment">//品牌</span></span><br><span class="line"> <span class="type">String</span
<p>实现的功能</p>
<p><img src="/pictures/image-20230627102918192.png" alt="image-20230627102918192"></p>
<p>前端显示时间格式的数据有问题</p>
<p>配置文件中的配置</p>
<figure class="highlight yml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">jackson:</span></span><br><span class="line"> <span class="attr">date-format:</span> <span class="string">yyyy-MM-dd</span> <span class="string">HH:mm:ss</span></span><br><span class="line"> <span class="attr">time-zone:</span> <span class="string">GMT+8</span></span><br></pre></td></tr></table></figure>
<p><strong>商品规格的回显</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 查询商品的规格属性</span></span><br><span class="line"><span class="comment"> * /product/attr/base/listforspu/&#123;spuId&#125;</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@GetMapping(&quot;/base/listforspu/&#123;spuId&#125;&quot;)</span></span><br><span class="line"><span class="keyword">public</span> R <span class="title function_">baseAttrList</span><span class="params">(<span class="meta">@PathVariable(&quot;spuId&quot;)</span> Long spuId)</span>&#123;</span><br><span class="line"> List&lt;ProductAttrValueEntity&gt; entities = productAttrValueService.baseAttrListForSpu(spuId);</span><br><span class="line"> <span class="keyword">return</span> R.ok().put(<span class="string">&quot;data&quot;</span>,entities);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><strong>修改商品的规格</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 修改商品的规格参数</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> spuId 商品的id</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> productAttrValueEntities 提交的修改之后的商品规格参数组成的集合</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="meta">@Transactional</span></span><br><span class="line"> <span class="meta">@Override</span></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">updateSpuAttr</span><span class="params">(Long spuId, List&lt;ProductAttrValueEntity&gt; productAttrValueEntities)</span> &#123;</span><br><span class="line"> <span class="comment">//1.删除这个spuId之前对应的所有属性</span></span><br><span class="line"> <span class="built_in">this</span>.baseMapper.delete(<span class="keyword">new</span> <span class="title class_">LambdaQueryWrapper</span>&lt;ProductAttrValueEntity&gt;().eq(ProductAttrValueEntity::getSpuId,spuId));</span><br><span class="line"> <span class="comment">//2.添加修改之后的商品的属性信息</span></span><br><span class="line"> List&lt;ProductAttrValueEntity&gt; collect = productAttrValueEntities.stream().map(item -&gt; &#123;</span><br><span class="line"> item.setSpuId(spuId);</span><br><span class="line"> <span class="keyword">return</span> item;</span><br><span class="line"> &#125;).collect(Collectors.toList());</span><br><span class="line"> <span class="built_in">this</span>.saveBatch(collect);</span><br><span class="line"> &#125;</span><br></pre></td></tr></table></figure>
<p>实现的功能</p>
<p><img src="/pictures/image-20230628165931139.png" alt="image-20230628165931139"></p>
<p>中间遇到的问题,点击规格页面显示400的问题</p>
<p><img src="/pictures/image-20230628171558364.png" alt="image-20230628171558364"></p>
<p>解决方案</p>
<p>第一步:在gulimall-admin数据库中执行以下的sql语句</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> sys_menu (menu_id, parent_id, name, url, perms, type, icon, order_num) <span class="keyword">VALUES</span> (<span class="number">76</span>, <span class="number">37</span>, <span class="string">&#x27;规格维护&#x27;</span>, <span class="string">&#x27;product/attrupdate&#x27;</span>, <span class="string">&#x27;&#x27;</span>, <span class="number">2</span>, <span class="string">&#x27;log&#x27;</span>, <span class="number">0</span>);</span><br></pre></td></tr></table></figure>
<p>第二步:将前端src&#x2F;router&#x2F;index.js替换成如下的内容</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="l
<h4 id="1-4-2-发布商品"><a href="#1-4-2-发布商品" class="headerlink" title="1.4.2 发布商品"></a>1.4.2 发布商品</h4><p><strong>根据商品的分类联动的查询商品的品牌</strong></p>
<p><img src="/pictures/image-20230626101113941.png" alt="image-20230626101113941"></p>
<p>后端关键部分的代码</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 查询商品分类关联的品牌信息</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@return</span> 某一商品分类下关联的品牌信息组成的集合</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="meta">@Override</span></span><br><span class="line"> <span class="keyword">public</span> List&lt;BrandEntity&gt; <span class="title function_">getBrandsByCatId</span><span class="params">(Long catId)</span> &#123;</span><br><span class="line"> List&lt;CategoryBrandRelationEntity&gt; categoryBrandRelationEntityList = relationDao.selectList(</span><br><span class="line"> <span class="keyword">new</span> <span class="title class_">LambdaQueryWrapper</span>&lt;CategoryBrandRelationEntity&gt;().eq(CategoryBrandRelationEntity::getCatelogId, catId));</span><br><span class="line"> List&lt;BrandEntity&gt; brandEntityList = categoryBrandRelationEntityList.stream().map(item -&gt; &#123;</span><br><span class="line"> <span class="type">Long</span> <span class="variable">brandId</span> <span class="operator">=</span> item.getBrandId();</span><br><span class="line"> <span class="keyword">return</span> brandService.getById(brandId);</span><br><span class="line"> &#125;).collect(Collectors.toList());</span><br><span class="line"> <span class="keyword">return</span> brandEntityList;</span><br><span class="line"> &#125;</span><br></pre></td></tr></table></figure>
<p><strong>根据JSON数据生成实体类</strong></p>
<p>生成工具: <a target="_blank" rel="noopener" href="https://www.bejson.com/">https://www.bejson.com/</a></p>
<p><img src="/pictures/image-20230626142854091.png" alt="image-20230626142854091"></p>
<p>前端提交的发布商品的详细信息</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class=
<p>下载生成的实体类代码</p>
<p><img src="/pictures/image-20230626143051042.png" alt="image-20230626143051042"></p>
<p>生成的实体类(将生成的实体类复制到项目中指定的位置)</p>
<p><img src="/pictures/image-20230626143228035.png" alt="image-20230626143228035"></p>
<p><strong>发布商品信息(核心业务)</strong></p>
<p>后端关键代码</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 发布商品的功能</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> vo 需要保存的商品信息封装的数据</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@Transactional</span></span><br><span class="line"><span class="meta">@Override</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span
<p>最终的实现效果</p>
<p><img src="/pictures/image-20230626184303186.png" alt="image-20230626184303186"></p>
<p><img src="/pictures/image-20230626182832804.png" alt="image-20230626182832804"></p>
<h4 id="1-4-3-商品管理"><a href="#1-4-3-商品管理" class="headerlink" title="1.4.3 商品管理"></a>1.4.3 商品管理</h4><p><strong>Sku检索</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 检索条件</span></span><br><span class="line"><span class="comment"> * &#123;</span></span><br><span class="line"><span class="comment"> * page: 1,//当前页码</span></span><br><span class="line"><span class="comment"> * limit: 10,//每页记录数</span></span><br><span class="line"><span class="comment"> * sidx: &#x27;id&#x27;,//排序字段</span></span><br><span class="line"><span class="comment"> * order: &#x27;asc/desc&#x27;,//排序方式</span></span><br><span class="line"><span class="comment"> * key: &#x27;华为&#x27;,//检索关键字</span></span><br><span class="line"><span class="comment"> * catelogId: 0,</span></span><br><span class="line"><span class="comment"> * brandId: 0,</span></span><br><span class="line"><span class="comment"> * min: 0,</span></span><br><span class="line"><span class="comment"> * max: 0</span></span><br><span class="line"><span class="comment"> * &#125;</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="meta">@Override</span></span><br><span class="line"> <span class="keyword">public</span> PageUtils <span class="title function_">queryPageByCondition</span><span class="params">(Map&lt;String, Object&gt; params)</span> &#123;</span><br><span class="line"> LambdaQueryWrapper&lt;SkuInfoEntity&gt; queryWrapper = <span class="keyword">new</span> <span class="title class_">LambdaQueryWrapper</span>&lt;&gt;();</span><br><span class="line"> <span class="comment">//Preparing: SELECT sku_id,sku_name,sku_title,catalog_id,sale_count,price,brand_id,sku_default_img,sku_subtitle,sku_desc,spu_id</span></span><br><span class="line"> <span class="comment">// FROM pms_sku_info</span></span><br><span class="line"> <span class="comment">// WHERE (( (sku_id = ? OR sku_name LIKE ?) ) AND catalog_id = ? AND brand_id = ? AND price &gt;= ? AND price &
<p>实现的效果</p>
<p><img src="/pictures/image-20230627112218894.png" alt="image-20230627112218894"></p>
<h2 id="2-库存系统"><a href="#2-库存系统" class="headerlink" title="2.库存系统"></a>2.库存系统</h2><h3 id="2-1-仓库维护"><a href="#2-1-仓库维护" class="headerlink" title="2.1 仓库维护"></a>2.1 仓库维护</h3><p><strong>仓库的分页加条件查询</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Override</span></span><br><span class="line"> <span class="keyword">public</span> PageUtils <span class="title function_">queryPage</span><span class="params">(Map&lt;String, Object&gt; params)</span> &#123;</span><br><span class="line"></span><br><span class="line"> <span class="comment">//分页查询的条件</span></span><br><span class="line"> LambdaQueryWrapper&lt;WareInfoEntity&gt; queryWrapper = <span class="keyword">new</span> <span class="title class_">LambdaQueryWrapper</span>&lt;&gt;();</span><br><span class="line"> <span class="type">String</span> <span class="variable">key</span> <span class="operator">=</span> (String) params.get(<span class="string">&quot;key&quot;</span>);</span><br><span class="line"> <span class="keyword">if</span> (!StringUtils.isEmpty(key)) &#123;</span><br><span class="line"> queryWrapper.like(WareInfoEntity::getName, key).or()</span><br><span class="line"> .eq(WareInfoEntity::getId, key)</span><br><span class="line"> .or().like(WareInfoEntity::getAddress, key)</span><br><span class="line"> .or().like(WareInfoEntity::getAreacode, key);</span><br><span class="line"> &#125;</span><br><span class="line"> IPage&lt;WareInfoEntity&gt; page = <span class="built_in">this</span>.page(</span><br><span class="line"> <span class="keyword">new</span> <span class="title class_">Query</span>&lt;WareInfoEntity&gt;().getPage(params),</span><br><span class="line"> queryWrapper</span><br><span class="line"> );</span><br><span class="line"> <span class="keyword">return</span> <span class="keyword">new</span> <span class="title class_">PageUtils</span>(page);</span><br><span class="line"> &#125;</span><br></pre></td></tr></table></figure>
<p>实现的效果</p>
<p><img src="/pictures/image-20230627153518692.png" alt="image-20230627153518692"></p>
<h3 id="2-2-库存工作单"><a href="#2-2-库存工作单" class="headerlink" title="2.2 库存工作单"></a>2.2 库存工作单</h3><h3 id="2-3-商品库存"><a href="#2-3-商品库存" class="headerlink" title="2.3 商品库存"></a>2.3 商品库存</h3><p><strong>商品库存的条件查询</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * &#123;</span></span><br><span class="line"><span class="comment"> * page: 1,//当前页码</span></span><br><span class="line"><span class="comment"> * limit: 10,//每页记录数</span></span><br><span class="line"><span class="comment"> * sidx: &#x27;id&#x27;,//排序字段</span></span><br><span class="line"><span class="comment"> * order: &#x27;asc/desc&#x27;,//排序方式</span></span><br><span class="line"><span class="comment"> * wareId: 123,//仓库id</span></span><br><span class="line"><span class="comment"> * skuId: 123//商品id</span></span><br><span class="line"><span class="comment"> * &#125;</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="meta">@Override</span></span><br><span class="line"> <span class="keyword">public</span> PageUtils <span class="title function_">queryPage</span><span class="params">(Map&lt;String, Object&gt; params)</span> &#123;</span><br><span class="line"> LambdaQueryWrapper&lt;WareSkuEntity&gt; queryWrapper = <span class="keyword">new</span> <span class="title class_">LambdaQueryWrapper</span>&lt;&gt;();</span><br><span class="line"> <span class="type">String</span> <span class="variable">wareId</span> <span class="operator">=</span> (String) params.get(<span class="string">&quot;wareId&quot;</span>);</span><br><span class="line"> <span class="keyword">if</span> (!StringUtils.isEmpty(wareId)) &#123;</span><br><span class="line"> queryWrapper.eq(WareSkuEntity::getWareId, wareId);</span><br><span class="line"> &#125;</span><br><span class="line"> <span class="type">String</span> <span class="variable">skuId</span> <span class="operator">=</span> (String) params.get(<span class="string">&quot;skuId&quot;</span>);</span><br><span class="line"> <span class="keyword">if</span> (!StringUtils.isEmpty(skuId)) &#123;</span><br><span class="line"> queryWrapper.eq(WareSkuEntity::getSkuId, skuId);</span><br><span class="line"> &#125;</span><br><span class="line"> IPage&lt;WareSkuEntity&gt; page = <span class="built_in">this</span>.page(</span><br><span class="line"> <span class="keyword">new</span> <span class="title class_">Query</span>&lt;WareSkuEntity&gt;().getPage(params),</span><br><span class="line"> queryWrapper</span><br><span class="line"> );</span><br><span class="line"> <span class="keyword">return</span> <span class="keyword">new</span> <span class="title class_">PageUtils</span>(page);</span><br><span class="line"> &#125;</span><br></pre></td></tr></table></figure>
<p>实现的效果</p>
<p><img src="/pictures/image-20230627160605717.png" alt="image-20230627160605717"></p>
<h3 id="2-4-采购单维护"><a href="#2-4-采购单维护" class="headerlink" title="2.4 采购单维护"></a>2.4 采购单维护</h3><h4 id="2-4-1-采购需求"><a href="#2-4-1-采购需求" class="headerlink" title="2.4.1 采购需求"></a>2.4.1 采购需求</h4><p><strong>采购需求的分页加条件查询</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * status: 0,//状态</span></span><br><span class="line"><span class="comment"> * wareId: 1,//仓库id</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="meta">@Override</span></span><br><span class="line"> <span class="keyword">public</span> PageUtils <span class="title function_">queryPage</span><span class="params">(Map&lt;String, Object&gt; params)</span> &#123;</span><br><span class="line"> LambdaQueryWrapper&lt;PurchaseDetailEntity&gt; queryWrapper = <span class="keyword">new</span> <span class="title class_">LambdaQueryWrapper</span>&lt;&gt;();</span><br><span class="line"> <span class="type">String</span> <span class="variable">status</span> <span class="operator">=</span> (String) params.get(<span class="string">&quot;status&quot;</span>);</span><br><span class="line"> <span class="keyword">if</span> (!StringUtils.isEmpty(status)) &#123;</span><br><span class="line"> queryWrapper.eq(PurchaseDetailEntity::getStatus, status);</span><br><span class="line"> &#125;</span><br><span class="line"> <span class="type">String</span> <span class="variable">wareId</span> <span class="operator">=</span> (String) params.get(<span class="string">&quot;wareId&quot;</span>);</span><br><span class="line"> <span class="keyword">if</span> (!StringUtils.isEmpty(wareId)) &#123;</span><br><span class="line"> queryWrapper.eq(PurchaseDetailEntity::getWareId, wareId);</span><br><span class="line"> &#125;</span><br><span class="line"> <span class="type">String</span> <span class="variable">key</span> <span class="operator">=</span> (String) params.get(<span class="string">&quot;key&quot;</span>);</span><br><span class="line"> <span class="keyword">if</span> (!StringUtils.isEmpty(key)) &#123;</span><br><span class="line"> queryWrapper.and((wrapper-&gt;&#123;</span><br><span class="line"> wrapper.eq(PurchaseDetailEntity::getPurchaseId, key).or().like(PurchaseDetailEntity::getSkuId,key);</span><br><span class="line"> &#125;));</span><br><span class="line"> &#125;</span><br><span class="line"> IPage&lt;PurchaseDetailEntity&gt; page = <span class="built_in">this</span>.page(</span><br><span class="line"> <span class="keyword">new</span> <span class="title class_">Query</span>&lt;PurchaseDetailEntity&gt;().getPage(params),</span><br><span class="line"> queryWrapper</span><br><span class="line"> );</span><br><span class="line"> <span class="keyword">return</span> <span class="keyword">new</span> <span class="title class_">PageUtils</span>(page);</span><br><span class="line"> &#125;</span><br></pre></td></tr></table></figure>
<p>实现的效果</p>
<p><img src="/pictures/image-20230627161955351.png" alt="image-20230627161955351"></p>
<p><strong>合并采购单</strong></p>
<p>业务需求介绍</p>
<p><img src="/pictures/image-20230627162707377.png" alt="image-20230627162707377"></p>
<p>获取没有领取的采购单,将采购需求合并到这个采购单</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 获取没有领取的采购单</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="meta">@Override</span></span><br><span class="line"> <span class="keyword">public</span> PageUtils <span class="title function_">queryPageUnreceivePurchase</span><span class="params">(Map&lt;String, Object&gt; params)</span> &#123;</span><br><span class="line"> IPage&lt;PurchaseEntity&gt; page = <span class="built_in">this</span>.page(</span><br><span class="line"> <span class="keyword">new</span> <span class="title class_">Query</span>&lt;PurchaseEntity&gt;().getPage(params),</span><br><span class="line"> <span class="keyword">new</span> <span class="title class_">LambdaQueryWrapper</span>&lt;PurchaseEntity&gt;().eq(PurchaseEntity::getStatus,<span class="number">0</span>).or().eq(PurchaseEntity::getStatus,<span class="number">1</span>)</span><br><span class="line"> );</span><br><span class="line"></span><br><span class="line"> <span class="keyword">return</span> <span class="keyword">new</span> <span class="title class_">PageUtils</span>(page);</span><br><span class="line"> &#125;</span><br></pre></td></tr></table></figure>
<p><strong>合并采购需求</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 合并采购需求</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> mergeVo 前端传递的需要合并的采购需求</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="meta">@Transactional</span></span><br><span class="line"> <span class="meta">@Override</span></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">mergePurchase</span><span class="params">(MergeVo mergeVo)</span> &#123;</span><br><span class="line"> <span class="comment">//判断采购单是不是存在</span></span><br><span class="line"> <span class="type">Long</span> <span class="variable">purchaseId</span> <span class="operator">=</span> mergeVo.getPurchaseId();</span><br><span class="line"> <span class="keyword">if</span> (purchaseId == <span class="literal">null</span>) &#123;<span class="comment">//说明没有选择需要合并的采购单,需要先创建一个采购单</span></span><br><span class="line"> <span class="type">PurchaseEntity</span> <span class="variable">purchaseEntity</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">PurchaseEntity</span>();</span><br><span class="line"> purchaseEntity.setStatus(WareConstant.PurchaseStatesEnum.CREATED.getCode());</span><br><span class="line"> purchaseEntity.setCreateTime(<span class="keyword">new</span> <span class="title class_">Date</span>());</span><br><span class="line"> purchaseEntity.setUpdateTime(<span class="keyword">new</span> <span class="title class_">Date</span>());</span><br><span class="line"> <span class="built_in">this</span>.save(purchaseEntity);</span><br><span class="line"> purchaseId = purchaseEntity.getId();</span><br><span class="line"> &#125;</span><br><span class="line"> List&lt;Long&gt; items = mergeVo.getItems();</span><br><span class="line"> <span class="type">Long</span> <span class="variable">finalPurchaseId</span> <span class="operator">=</span> purchaseId;</span><br><span class="line"> List&lt;PurchaseDetailEntity&gt; detailEntityList = items.stream().map(item -&gt; &#123;</span><br><span class="line"> <span class="type">PurchaseDetailEntity</span> <span class="variable">detailEntity</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">PurchaseDetailEntity</span>();</span><br><span class="line"> detailEntity.setId(item);</span><br><span class="line"> detailEntity.setPurchaseId(finalPurchaseId
<h4 id="2-4-2-采购单"><a href="#2-4-2-采购单" class="headerlink" title="2.4.2 采购单"></a>2.4.2 采购单</h4><p><strong>领取采购单</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> ids 采购单id</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="meta">@Override</span></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">received</span><span class="params">(List&lt;Long&gt; ids)</span> &#123;</span><br><span class="line"> <span class="comment">//确认当前的采购单是新建或者分配状态</span></span><br><span class="line"> List&lt;PurchaseEntity&gt; purchaseEntities = ids.stream().map(id -&gt; &#123;</span><br><span class="line"> <span class="type">PurchaseEntity</span> <span class="variable">byId</span> <span class="operator">=</span> <span class="built_in">this</span>.getById(id);</span><br><span class="line"> <span class="keyword">return</span> byId;</span><br><span class="line"> &#125;).filter(item -&gt; &#123;</span><br><span class="line"> <span class="keyword">if</span> (item.getStatus() == WareConstant.PurchaseStatesEnum.CREATED.getCode() ||</span><br><span class="line"> item.getStatus() == WareConstant.PurchaseDeatilStatesEnum.ASSIGNED.getCode()) &#123;</span><br><span class="line"> <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line"> &#125;</span><br><span class="line"> <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line"> &#125;).map(item-&gt;&#123;</span><br><span class="line"> <span class="comment">//改变采购单状态</span></span><br><span class="line"> item.setStatus(WareConstant.PurchaseStatesEnum.RECEIVE.getCode());</span><br><span class="line"> item.setUpdateTime(<span class="keyword">new</span> <span class="title class_">Date</span>());</span><br><span class="line"> <span class="keyword">return</span> item;</span><br><span class="line"> &#125;).collect(Collectors.toList());</span><br><span class="line"> <span class="built_in">this</span>.updateBatchById(purchaseEntities);</span><br><span class="line"> <span class="comment">//改变采购项状态</span></span><br><span class="line"> purchaseEntities.forEach(item-&gt;&#123;</span><br><span class="line"> List&lt;PurchaseDetailEntity&gt; entities = detailService.listDetailByPurchaseId(item.getId());</span><br><span class="line"> List&lt;PurchaseDetailEntity&gt; detailEntityList = entities.stream().map(entity -&gt; &#123;</span><br><span class="line"> <span class="type">PurchaseDetailEntity</span> <span class="variable">purchaseDetailEntity</span> <span class="operator">=</span> <span c
<p><strong>完成采购的功能</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Transactional</span></span><br><span class="line"> <span class="meta">@Override</span></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">done</span><span class="params">(PurchaseDoneVo doneVo)</span> &#123;</span><br><span class="line"> <span class="type">Long</span> <span class="variable">id</span> <span class="operator">=</span> doneVo.getId();</span><br><span class="line"> <span class="comment">//改变采购项的状态</span></span><br><span class="line"> <span class="type">Boolean</span> <span class="variable">flag</span> <span class="operator">=</span> <span class="literal">true</span>;</span><br><span class="line"> List&lt;PurchaseItemDoneVo&gt; items = doneVo.getItems();</span><br><span class="line"> List&lt;PurchaseDetailEntity&gt; purchaseItemDoneVos = <span class="keyword">new</span> <span class="title class_">ArrayList</span>&lt;&gt;();</span><br><span class="line"> <span class="keyword">for</span> (PurchaseItemDoneVo item : items) &#123;</span><br><span class="line"> <span class="type">PurchaseDetailEntity</span> <span class="variable">detailEntity</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">PurchaseDetailEntity</span>();</span><br><span class="line"> <span class="keyword">if</span> (item.getStatus() == WareConstant.PurchaseDeatilStatesEnum.HASERROR.getCode()) &#123;</span><br><span class="line"> flag = <span class="literal">false</span>;</span><br><span class="line"> detailEntity.setStatus(item.getStatus());</span><br><span class="line"> &#125; <span class="keyword">else</span> &#123;</span><br><span class="line"> detailEntity.setStatus(WareConstant.PurchaseDeatilStatesEnum.FINISH.getCode());</span><br><span class="line"> <span class="comment">//将成功采购的进行入库</span></span><br><span class="line"> <span class="type">PurchaseDetailEntity</span> <span class="variable">purchaseDetailEntity</span> <span class="operator">=</span> detailService.getById(item.getItemId());</span><br><span class="line"> wareSkuService.addStock(purchaseDetailEntity.getSkuId(),purchaseDetailEntity.getWareId(),purchaseDetailEntity.getSkuNum());</span><br><span class="line"> &#125;</span><br><span class="line"> detailEntity.setId(item.getItemId());</span><br><span class="line"> purchaseItemDoneVos.add(detailEntity);</span><br><span class="line"> &#125;</span><br><span class="line"> detailService.updateBatchById(purchaseItemDoneVos);</span><br><span class="line"> <span class="comment">//改变采购单的状态</span></span><br><span class="line"> <span class="type">PurchaseEntity</span> <span class="variable">purchaseEntity</span> <span class="operator">=</span> <span class="keyword">new</spa
<p><strong>修改库存的功能</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Transactional</span></span><br><span class="line"> <span class="meta">@Override</span></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">addStock</span><span class="params">(Long skuId, Long wareId, Integer skuNum)</span> &#123;</span><br><span class="line"> <span class="comment">//判断是添加的操作还是修改的操作</span></span><br><span class="line"> List&lt;WareSkuEntity&gt; wareSkuEntities = wareSkuDao.selectList(<span class="keyword">new</span> <span class="title class_">LambdaQueryWrapper</span>&lt;WareSkuEntity&gt;().eq(WareSkuEntity::getSkuId, skuId).eq(WareSkuEntity::getWareId, wareId));</span><br><span class="line"> <span class="keyword">if</span> (wareSkuEntities == <span class="literal">null</span> || wareSkuEntities.size() == <span class="number">0</span>) &#123;</span><br><span class="line"> <span class="type">WareSkuEntity</span> <span class="variable">wareSkuEntity</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">WareSkuEntity</span>();</span><br><span class="line"> wareSkuEntity.setSkuId(skuId);</span><br><span class="line"> wareSkuEntity.setWareId(wareId);</span><br><span class="line"> wareSkuEntity.setStock(skuNum);</span><br><span class="line"> wareSkuEntity.setStockLocked(<span class="number">0</span>);</span><br><span class="line"> <span class="comment">//远程查询sku的名字</span></span><br><span class="line"> <span class="keyword">try</span> &#123;</span><br><span class="line"> <span class="type">R</span> <span class="variable">r</span> <span class="operator">=</span> productFeignService.info(skuId);</span><br><span class="line"> <span class="keyword">if</span>(r.getCode() == <span class="number">0</span>)&#123;</span><br><span class="line"> Map&lt;String,Object&gt; skuInfo = (Map&lt;String, Object&gt;) r.get(<span class="string">&quot;skuInfo&quot;</span>);</span><br><span class="line"> wareSkuEntity.setSkuName((String) skuInfo.get(<span class="string">&quot;skuName&quot;</span>));</span><br><span class="line"> &#125;</span><br><span class="line"> &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line"> log.warn(<span class="string">&quot;远程获取商品的Sku信息失败!&quot;</span>);</span><br><span class="line"> &#125;</span><br><span class="line"> wareSkuDao.insert(wareSkuEntity);</span><br><span class="line"> &#125; <span class="keyword">else</span> &#123;</span><br><span class="line"> wareSkuDao.addStock(skuId, wareId, skuNum);</span><br><span class="line"> &#125;</span><br><span class="line"> &#125;</span><br></pre></td></tr></table></figure>
<p>sql语句</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"> <span class="comment">&lt;!--void addStock(@Param(&quot;skuId&quot;) Long skuId, @Param(&quot;wareId&quot;) Long wareId, @Param(&quot;skuNum&quot;) Integer skuNum);--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">insert</span> <span class="attr">id</span>=<span class="string">&quot;addStock&quot;</span>&gt;</span></span><br><span class="line"> update `wms_ware_sku`</span><br><span class="line"> set stock = stock + #&#123;skuNum&#125;</span><br><span class="line"> where sku_id = #&#123;skuId&#125;</span><br><span class="line"> and ware_id = #&#123;wareId&#125;</span><br><span class="line"><span class="tag">&lt;/<span class="name">insert</span>&gt;</span></span><br></pre></td></tr></table></figure>
<h2 id="3-基础篇总结"><a href="#3-基础篇总结" class="headerlink" title="3.基础篇总结"></a><strong>3.基础篇总结</strong></h2><img src="/pictures/image-20230628170218776.png" alt="image-20230628170218776" style="zoom:150%;" />
<p>个人总结:</p>
<ul>
<li>细节很重要,编写代码的时候要注意细节问题(比如网关的配置,模糊路径和精确路径先后配置的问题)</li>
<li>要有良好的编码习惯,注意代码解耦和代码复用</li>
<li>业务逻辑很重要,编码之前要搞懂了业务逻辑之后再开始写代码</li>
<li>学习技术的时候要融会贯通,学习编程的思想</li>
<li>学技术一定要学会看开发文档技术说明书可以先从开发文档提供的demo开始</li>
</ul>
<h1 id="六-高级篇程序设计"><a href="#六-高级篇程序设计" class="headerlink" title="六.高级篇程序设计"></a>六.高级篇程序设计</h1><h2 id="1-ElasticSearch"><a href="#1-ElasticSearch" class="headerlink" title="1.ElasticSearch"></a>1.ElasticSearch</h2><p><strong>详细的教程</strong></p>
2024-05-10 13:32:25 +08:00
<p><strong><a href="https://qingling.icu/posts/22654.html">ElasticSearch | The Blog (qingling.icu)</a></strong></p>
2023-09-22 21:57:28 +08:00
<h2 id="2-商城业务"><a href="#2-商城业务" class="headerlink" title="2.商城业务"></a>2.商城业务</h2><h3 id="2-1-商品上架"><a href="#2-1-商品上架" class="headerlink" title="2.1 商品上架"></a>2.1 商品上架</h3><p><strong>1.商品Mapping</strong></p>
<p>创建一个product索引和指定映射关系</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br></pre></td><td class="code"><pre><span class="line">PUT product</span><br><span class="line"><span class="punctuation">&#123;</span></span><br><span class="line"> <span class="attr">&quot;mappings&quot;</span><span class="punctuation">:</span> <span class="punctuation">&#123;</span></span><br><span class="line"> <span class="attr">&quot;properties&quot;</span><span class="punctuation">:</span> <span class="punctuation">&#123;</span></span><br><span class="line"> <span class="attr">&quot;skuId&quot;</span><span class="punctuation">:</span> <span class="punctuation">&#123;</span></span><br><span class="line"> <span class="attr">&quot;type&quot;</span><span class="punctuation">:</span> <span class="string">&quot;long&quot;</span></span><br><span class="line"> <span class="punctuation">&#125;</span><span class="punctuation">,</span></span><br><span class="line"> <span class="attr">&quot;spuId&quot;</span><span class="punctuation">:</span> <span class="punctuation">&#123;</span></span><br><span class="line"> <span class="attr">&quot;type&quot;</span><span class="punctuation">:</span> <span class="string">&quot;keyword&quot;</span></span><br><span class="line"> <span class="punctuation">&#125;</span><span class="punctuation">,</span></span><br><span class="line"> <span class="attr">&quot;skuTitle&quot;</span><span class="punctuation">:</span> <span class="punctuation">&#123;</span></span><br><span class="line"> <span class="attr">&quot;type&quot;</span><span class="punctuation">:</span> <span class="string">&quot;text&quot;</span><span class="punctuation">,</span></span><br><span class="line"> <span class="attr">&quot;analyzer&quot;</span><span class="punctuation">:</span> <span class="
<p><strong>2.上架功能后端代码</strong></p>
<p>所需的实体类</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.atguigu.common.to.es;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> lombok.Data;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.math.BigDecimal;</span><br><span class="line"><span class="keyword">import</span> java.util.List;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> Jason Gong</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@version</span> 1.0</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Date</span> 2023/7/5</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Description</span> 对应ES中保存的文档的实体类</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@Data</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">SkuEsModel</span> &#123;</span><br><span class="line"> <span class="keyword">private</span> Long skuId;</span><br><span class="line"></span><br><span class="line"> <span class="keyword">private</span> Long spuId;</span><br><span class="line"></span><br><span class="line"> <span class="keyword">private</span> String skuTitle;</span><br><span class="line"></span><br><span class="line"> <span class="keyword">private</span> BigDecimal skuPrice;</span><br><span class="line"></span><br><span class="line"> <span class="keyword">private</span> String skuImg;</span><br><span class="line"></span><br><span class="line"> <span class="keyword">private</span> Long saleCount;</span><br><span class="line"></span><br><span class="line"> <span class="keyword">private</span> Boolean hasStock;</span><br><span class="line"></span><br><span class="line"> <span class="keyword">private</span> Long hotScore;</span><br><span class="line"></span><br><span class="line"> <span class="keyword">private</span> Long brandId;</span><br><span class="li
<p>商品上架的后端关键代码</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 商品上架的功能</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="meta">@Override</span></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">up</span><span class="params">(Long spuId)</span> &#123;</span><br><span class="line"> <span class="comment">//查询当前spuId对应的所有sku信息</span></span><br><span class="line"> List&lt;SkuInfoEntity&gt; skus = skuInfoService.getSkusBySpuId(spuId);</span><br><span class="line"> List&lt;Long&gt; skuIds = skus.stream().map(SkuInfoEntity::getSkuId).collect(Collectors.toList());</span><br><span class="line"> <span class="comment">//规格属性信息的封装(可以被检索的规格属性信息)</span></span><br><span class="line"> List&lt;ProductAttrValueEntity&gt; baseAttrs = productAttrValueService.baseAttrListForSpu(spuId);</span><br><span class="line"> List&lt;Long&gt; attrIds = baseAttrs.stream().map(attr -&gt; &#123;</span><br><span class="line"> <span class="keyword">return</span> attr.getAttrId();</span><br><span class="line"> &#125;).collect(Collectors.toList());</span><br><span class="line"> <span class="comment">//获取可以检索的属性id的集合</span></span><br><span class="line"> List&lt;Long&gt; searchAttrIds = attrService.selectSearchAttrs(attr
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 过滤出属性id中可以被检索的属性的id</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> attrIds 原属性id的集合</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@return</span> 可以被检索的属性id的集合</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="meta">@Override</span></span><br><span class="line"> <span class="keyword">public</span> List&lt;Long&gt; <span class="title function_">selectSearchAttrs</span><span class="params">(List&lt;Long&gt; attrIds)</span> &#123;</span><br><span class="line"> <span class="comment">//select attr_id from `pms_attr` where attr_id in (?) and search_type=1</span></span><br><span class="line"> <span class="keyword">return</span> attrDao.selectSearchAttrs(attrIds);</span><br><span class="line"> &#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">&quot;selectSearchAttrs&quot;</span> <span class="attr">resultType</span>=<span class="string">&quot;java.lang.Long&quot;</span>&gt;</span></span><br><span class="line"> select `attr_id`from `pms_attr` where `attr_id` in</span><br><span class="line"> <span class="tag">&lt;<span class="name">foreach</span> <span class="attr">collection</span>=<span class="string">&quot;attrIds&quot;</span> <span class="attr">item</span>=<span class="string">&quot;attr&quot;</span> <span class="attr">separator</span>=<span class="string">&quot;,&quot;</span> <span class="attr">open</span>=<span class="string">&quot;(&quot;</span> <span class="attr">close</span>=<span class="string">&quot;)&quot;</span>&gt;</span></span><br><span class="line"> #&#123;attr&#125;</span><br><span class="line"> <span class="tag">&lt;/<span class="name">foreach</span>&gt;</span></span><br><span class="line"> and `search_type`=1</span><br><span class="line"><span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 查询库存的方法</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="meta">@Override</span></span><br><span class="line"> <span class="keyword">public</span> List&lt;SkuHasStockVo&gt; <span class="title function_">getSkusHasStock</span><span class="params">(List&lt;Long&gt; skuIds)</span> &#123;</span><br><span class="line"> List&lt;SkuHasStockVo&gt; skuHasStockVos = skuIds.stream().map(skuId -&gt; &#123;</span><br><span class="line"> <span class="type">SkuHasStockVo</span> <span class="variable">skuHasStockVo</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">SkuHasStockVo</span>();</span><br><span class="line"> <span class="type">Long</span> <span class="variable">count</span> <span class="operator">=</span> wareSkuDao.getSkuStock(skuId);</span><br><span class="line"> skuHasStockVo.setSkuId(skuId);</span><br><span class="line"> <span class="keyword">if</span>(count != <span class="literal">null</span>)&#123;</span><br><span class="line"> skuHasStockVo.setHasStock(count &gt; <span class="number">0</span>);</span><br><span class="line"> &#125;<span class="keyword">else</span> &#123;</span><br><span class="line"> skuHasStockVo.setHasStock(<span class="literal">false</span>);</span><br><span class="line"> &#125;</span><br><span class="line"> <span class="keyword">return</span> skuHasStockVo;</span><br><span class="line"> &#125;).collect(Collectors.toList());</span><br><span class="line"> <span class="keyword">return</span> skuHasStockVos;</span><br><span class="line"> &#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 存储商品数据</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="meta">@Override</span></span><br><span class="line"> <span class="keyword">public</span> Boolean <span class="title function_">productStatusUp</span><span class="params">(List&lt;SkuEsModel&gt; skuEsModels)</span> <span class="keyword">throws</span> IOException &#123;</span><br><span class="line"> <span class="comment">//将数据保存到ES中</span></span><br><span class="line"> <span class="comment">//在ES中建立好索引</span></span><br><span class="line"> <span class="type">BulkRequest</span> <span class="variable">bulkRequest</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">BulkRequest</span>();</span><br><span class="line"> <span class="keyword">for</span> (SkuEsModel skuEsModel : skuEsModels) &#123;</span><br><span class="line"> <span class="type">IndexRequest</span> <span class="variable">indexRequest</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">IndexRequest</span>(EsConstant.PRODUCT_INDEX);</span><br><span class="line"> indexRequest.id(skuEsModel.getSkuId().toString());</span><br><span class="line"> indexRequest.source(JSON.toJSONString(skuEsModel), XContentType.JSON);</span><br><span class="line"> bulkRequest.add(indexRequest);</span><br><span class="line"> &#125;</span><br><span class="line"> <span class="type">BulkResponse</span> <span class="variable">bulk</span> <span class="operator">=</span> restHighLevelClient.bulk(bulkRequest, GulimallElasticSearchConfig.COMMON_OPTIONS);</span><br><span class="line"> <span class="type">boolean</span> <span class="variable">hasFailure</span> <span class="operator">=</span> bulk.hasFailures();</span><br><span class="line"> List&lt;String&gt; ids = Arrays.stream(bulk.getItems()).map(item -&gt; &#123;</span><br><span class="line"> <span class="keyword">return</span> item.getId();</span><br><span class="line"> &#125;).collect(Collectors.toList());</span><br><span class="line"> log.info(<span class="string">&quot;上架商品的id:&#123;&#125;&quot;</span>,ids);</span><br><span class="line"> <span class="keyword">return</span> !hasFailure;</span><br><span class="line"> &#125;</span><br></pre></td></tr></table></figure>
<h3 id="2-2-首页"><a href="#2-2-首页" class="headerlink" title="2.2 首页"></a>2.2 首页</h3><p>项目的页面视图部署在对应的微服务项目的静态资源中</p>
<p><img src="/pictures/image-20230706112521327.png" alt="image-20230706112521327"></p>
<h4 id="2-2-1整合thymeleaf"><a href="#2-2-1整合thymeleaf" class="headerlink" title="2.2.1整合thymeleaf"></a>2.2.1整合thymeleaf</h4><p>添加thymeleaf的依赖</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!-- thymeleaf--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-boot-starter-thymeleaf<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>将资料中的静态资源放在微服务工程的对应位置,并在配置文件中加入对应的配置</p>
<p><img src="/pictures/image-20230706114304695.png" alt="image-20230706114304695"></p>
<p>首页页面</p>
<p><img src="/pictures/image-20230706145050684.png" alt="image-20230706145050684"></p>
<p>在index.html上加上thymeleaf的名称空间</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">html</span> <span class="attr">xmlns:th</span>=<span class="string">&quot;http://www.thymeleaf.org&quot;</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>修改页面的时候需要频繁的重启项目,使用热更新来解决该问题</p>
<p>导入devtools的依赖</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--devtools--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-boot-devtools<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">optional</span>&gt;</span>true<span class="tag">&lt;/<span class="name">optional</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>重启项目之后会显示如下的效果</p>
<p><img src="/pictures/image-20230706154112026.png" alt="image-20230706154112026"></p>
<p>这时我们在每次修改页面之后使用Ctrl+Shift+F9键可以热更新</p>
<p><img src="/pictures/image-20230706161401528.png" alt="image-20230706161401528"></p>
<h4 id="2-2-2-首页面三级分类显示"><a href="#2-2-2-首页面三级分类显示" class="headerlink" title="2.2.2 首页面三级分类显示"></a>2.2.2 首页面三级分类显示</h4><p>首页面的跳转以及一级分类的显示</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 首页面的跳转</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@GetMapping(&#123;&quot;/&quot;,&quot;index.html&quot;&#125;)</span></span><br><span class="line"><span class="keyword">public</span> String <span class="title function_">indexPage</span><span class="params">(Model model)</span>&#123;</span><br><span class="line"> <span class="comment">//查询所有的一级分类</span></span><br><span class="line"> List&lt;CategoryEntity&gt; categorys = categoryService.getLevelOneCategorys();</span><br><span class="line"> model.addAttribute(<span class="string">&quot;categorys&quot;</span>,categorys);</span><br><span class="line"> <span class="keyword">return</span> <span class="string">&quot;index&quot;</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>用于用户端首页面三级分类封装的数据</p>
2024-05-10 13:32:25 +08:00
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.atguigu.gulimall.product.vo;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> lombok.AllArgsConstructor;</span><br><span class="line"><span class="keyword">import</span> lombok.Data;</span><br><span class="line"><span class="keyword">import</span> lombok.NoArgsConstructor;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.List;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> Jason Gong</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@version</span> 1.0</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@website</span> https://qingling.icu</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Date</span> 2023/7/7</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Description</span> 用户端首页面三级分类封装的数据</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@Data</span></span><br><span class="line"><span class="meta">@AllArgsConstructor</span></span><br><span class="line"><span class="meta">@NoArgsConstructor</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Catelog2Vo</span> &#123;</span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 一级父分类的id</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">private</span> String catalog1Id;</span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 三级子分类</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">private</span> List&lt;Category3Vo&gt; catalog3List;</
2023-09-22 21:57:28 +08:00
<p>前端相关的修改</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"> <span class="tag">&lt;<span class="name">li</span> <span class="attr">th:each</span>=<span class="string">&quot; category:$&#123;categorys&#125;&quot;</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">a</span> <span class="attr">href</span>=<span class="string">&quot;#&quot;</span> <span class="attr">class</span>=<span class="string">&quot;header_main_left_a&quot;</span> <span class="attr">ctg-data</span>=<span class="string">&quot;3&quot;</span> <span class="attr">th:attr</span>=<span class="string">&quot;ctg-data=$&#123;category.catId&#125;&quot;</span>&gt;</span><span class="tag">&lt;<span class="name">b</span> <span class="attr">th:text</span>=<span class="string">&quot;$&#123;category.name&#125;&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">b</span>&gt;</span><span class="tag">&lt;/<span class="name">a</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p><img src="/pictures/image-20230707151505310.png" alt="image-20230707151505310"></p>
<p>controller层代码</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 首页面的商品分分类信息</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="meta">@ResponseBody</span></span><br><span class="line"> <span class="meta">@GetMapping(&quot;/index/catalog.json&quot;)</span></span><br><span class="line"> <span class="keyword">public</span> Map&lt;String, List&lt;Catelog2Vo&gt;&gt; <span class="title function_">getCatalogJson</span><span class="params">()</span>&#123;</span><br><span class="line"> <span class="keyword">return</span> categoryService.getCatalogJson();</span><br><span class="line"> &#125;</span><br></pre></td></tr></table></figure>
<p>service层代码</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Override</span></span><br><span class="line"> <span class="keyword">public</span> Map&lt;String, List&lt;Catelog2Vo&gt;&gt; <span class="title function_">getCatalogJson</span><span class="params">()</span> &#123;</span><br><span class="line"> <span class="comment">//查出所有的一级分类</span></span><br><span class="line"> List&lt;CategoryEntity&gt; levelOneCategorys = getLevelOneCategorys();</span><br><span class="line"> <span class="comment">//封装数据</span></span><br><span class="line"> Map&lt;String, List&lt;Catelog2Vo&gt;&gt; catelog2VoMap = levelOneCategorys.stream().collect(Collectors.toMap(k -&gt; k.getCatId().toString(), v -&gt; &#123;</span><br><span class="line"> <span class="comment">//查询以及分类下的二级分类</span></span><br><span class="line"> List&lt;CategoryEntity&gt; categoryEntities = categoryDao.selectList(<span class="keyword">new</span> <span class="title class_">LambdaQueryWrapper</span>&lt;CategoryEntity&gt;().eq(CategoryEntity::getParentCid, v.getCatId()));</span><br><span class="line"> List&lt;Catelog2Vo&gt; catelog2Vos = <span class="literal">null</span>;</span><br><span class="line"> <span class="keyword">if</span> (categoryEntities != <span class="literal">null</span>) &#123;</span><br><span class="line"> catelog2Vos = categoryEntities.stream().map(item -&gt; &#123;</span><br><span class="line"> <span class="type">Catelog2Vo</span> <span class="variable">catelog2Vo</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Catelog2Vo</span>(v.getCatId().toString(), <span class="literal">null</span>, item.getCatId().toString(), item.getName());</span><br><span class="line"> <span class="comment">//当前二级分类的三级分类</span></span><br><span class="line"> List&lt;CategoryEntity&gt; levelThreeCatelog = categoryDao.selectList(<span class="keyword">new</span> <span class="title class_">LambdaQueryWrapper</span>&lt;CategoryEntity&gt;().eq(CategoryEntity::getParentCid, item.getCatId()));</span><br><span class="line"> <span class="keyword">if</span>(levelThreeCatelog != <span class="literal">null</span>)&#123;</span><br><span class="line"> <span class="comment">//封装成指定的数据</span></span><br><span class="line"> List&lt;Catelog2Vo.Category3Vo&gt; category3VoList = levelThreeCatelog.stream().map(threeCatelog -&gt; &#123;</span><br><span class="line"> Catelog2Vo.<span class="type">Category3Vo</span> <span class="variable">category3Vo</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Catelog2Vo</span>.Category3Vo();</span><br><span class="line"> category3Vo.setCatalog2Id(item.getCatId().toStr
<p>页面实现的效果</p>
<p><img src="/pictures/image-20230707151543593.png" alt="image-20230707151543593"></p>
<h3 id="2-3-搭建域名访问环境"><a href="#2-3-搭建域名访问环境" class="headerlink" title="2.3 搭建域名访问环境"></a>2.3 搭建域名访问环境</h3><p><strong>1.安装SwitchHosts软件</strong></p>
<p>可以快捷的编辑操作hosts文件</p>
<p>打开的时候以管理员的身份打开</p>
<p><img src="/pictures/image-20230707153628394.png" alt="image-20230707153628394"></p>
<p><img src="/pictures/image-20230707154052952.png" alt="image-20230707154052952"></p>
<p>在添加的方案中添加如下的内容点击右下角的保存按钮保存保存失败的更改hosts文件的权限,将只读勾选掉)</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">192.168.195.100 gulimall.com</span><br></pre></td></tr></table></figure>
<p>保存成功之后可以直接访问域名,解析到虚拟机的ip</p>
<p><strong>Tips:有科学上网的测试的时候要先关闭科学上网的软件</strong></p>
<p> 安装了ES的访问gulimall.com:9200可以看到相应的内容</p>
<p><img src="/pictures/image-20230707160328001.png" alt="image-20230707160328001"></p>
<p><strong>2.正式搭建项目的域名访问环境</strong></p>
<p><img src="/pictures/image-20230708092745456.png" alt="image-20230708092745456"></p>
<p>nginx的配置文件</p>
<p><img src="/pictures/image-20230708093600923.png" alt="image-20230708093600923"></p>
<p>配置nginx</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">#</span><span class="language-bash">切换到nginx挂载在本机的配置文件所在的目录</span></span><br><span class="line">cd /mydata/nginx/conf</span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">切换到nginx的分配置文件下复制一份 定义gulimall的配置,主配置文件中有include /etc/nginx/conf.d/*.conf;这么一段配置,主配置文件</span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">会包含conf.d目录下的所有配置</span></span><br><span class="line">cd /mydata/nginx/conf/conf.d</span><br><span class="line">cp default.conf gulimall.conf 复制一份</span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">在gulimall.conf配置文件中配置我们的配置</span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">配置如下配置</span></span><br></pre></td></tr></table></figure>
<p>niginx的配置文件如下</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">server &#123;</span><br><span class="line"> listen 80;</span><br><span class="line"> server_name gulimall.com;</span><br><span class="line"></span><br><span class="line"> location / &#123;</span><br><span class="line"> #服务的IP 192.168.0.112</span><br><span class="line"> #服务的端口: 10000</span><br><span class="line"> proxy_pass http://192.168.0.112:10000;</span><br><span class="line"> &#125;</span><br><span class="line"> error_page 500 502 503 504 /50x.html;</span><br><span class="line"> location = /50x.html &#123;</span><br><span class="line"> root /usr/share/nginx/html;</span><br><span class="line"> &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>这时我们在访问gulimall.com的时候就可以访问一下的页面了</p>
<p><img src="/pictures/image-20230708095940126.png" alt="image-20230708095940126"></p>
<p><strong>3.让nginx代理到网关</strong></p>
<p>在nginx.conf配置文件中加上如下的配置</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">#配置上游服务器</span><br><span class="line"> #gulimall是上游服务器的组名</span><br><span class="line"> upstream gulimall&#123;</span><br><span class="line"> #网关的地址</span><br><span class="line"> server 192.168.0.112:88;</span><br><span class="line"> &#125;</span><br></pre></td></tr></table></figure>
<p>修改gulimall.conf中的配置</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">server &#123;</span><br><span class="line"> listen 80;</span><br><span class="line"> server_name gulimall.com;</span><br><span class="line"></span><br><span class="line"> location / &#123;</span><br><span class="line"> proxy_pass http://gulimall;</span><br><span class="line"> &#125;</span><br><span class="line"></span><br><span class="line"> error_page 500 502 503 504 /50x.html;</span><br><span class="line"> location = /50x.html &#123;</span><br><span class="line"> root /usr/share/nginx/html;</span><br><span class="line"> &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>在网关的配置文件中加上一下的配置</p>
<figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># nginx代理相关的断言</span></span><br><span class="line"><span class="bullet">-</span> <span class="attr">id:</span> <span class="string">gulimall_host_route</span></span><br><span class="line"> <span class="attr">uri:</span> <span class="string">lb://gulimall-product</span></span><br><span class="line"> <span class="attr">predicates:</span></span><br><span class="line"> <span class="bullet">-</span> <span class="string">Host=**.gulimall.com,gulimall.com</span></span><br></pre></td></tr></table></figure>
<p>这时我们重启网关的时候发现无法访问到相应的页面 是由于nginx访问网关的时候丢了host</p>
<p>这时我们需要加上如下的配置</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">proxy_set_header Host $host;</span><br></pre></td></tr></table></figure>
<p><img src="/pictures/image-20230708104321024.png" alt="image-20230708104321024"></p>
<img src="/pictures/image-20230708104848501.png" alt="image-20230708104848501" style="zoom:150%;" />
2024-05-10 13:32:25 +08:00
<h3 id="2-4-性能压测"><a href="#2-4-性能压测" class="headerlink" title="2.4 性能压测"></a>2.4 性能压测</h3><p><strong><a href="https://qingling.icu/posts/1416.html">压力测试与性能监控 | The Blog (qingling.icu)</a></strong></p>
2023-09-22 21:57:28 +08:00
<p>JMeter压力测试</p>
<p><img src="/pictures/image-20230708152102009.png" alt="image-20230708152102009"></p>
<p>性能监控</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">jconsole</span><br><span class="line">jvisualvm</span><br></pre></td></tr></table></figure>
<p><img src="/pictures/image-20230709155555491.png" alt="image-20230709155555491"></p>
<h4 id="2-4-1优化中间件对性能的影响"><a href="#2-4-1优化中间件对性能的影响" class="headerlink" title="2.4.1优化中间件对性能的影响"></a>2.4.1优化中间件对性能的影响</h4><p>网关的优化、数据库索引的优化、thymeleaf的优化、静态资源的优化、nginx动静分离</p>
<p><img src="/pictures/image-20230710111805705.png" alt="image-20230710111805705"></p>
<h3 id="2-5-缓存使用"><a href="#2-5-缓存使用" class="headerlink" title="2.5 缓存使用"></a>2.5 缓存使用</h3><h4 id="2-5-1-本地缓存与分布式缓存"><a href="#2-5-1-本地缓存与分布式缓存" class="headerlink" title="2.5.1 本地缓存与分布式缓存"></a>2.5.1 本地缓存与分布式缓存</h4><p>为了系统性能的提升,我们一般都会将部分数据放入缓存中,加速访问。而 db 承担数据落盘工作。<br><strong>哪些数据适合放入缓存?</strong><br>1.即时性、数据一致性要求不高的<br>2.访问量大且更新频率不高的数据(读多,写少)</p>
<p><strong>本地缓存</strong></p>
<p><img src="/pictures/image-20230710143906286.png" alt="image-20230710143906286"></p>
<p><strong>分布式缓存</strong></p>
<p><img src="/pictures/image-20230710143341531.png" alt="image-20230710143341531"></p>
<h4 id="2-5-2-整合redis"><a href="#2-5-2-整合redis" class="headerlink" title="2.5.2 整合redis"></a>2.5.2 整合redis</h4><p><strong>1.安装Redis</strong></p>
<p><img src="/pictures/image-20230710144052877.png" alt="image-20230710144052877"></p>
<p><strong>2.项目中使用Redis</strong></p>
2024-05-10 13:32:25 +08:00
<p><strong><a href="https://qingling.icu/posts/13813.html">SpringBoot整合Redis | The Blog (qingling.icu)</a></strong></p>
2023-09-22 21:57:28 +08:00
<p>2.1 引入依赖</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-boot-starter-data-redis<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>2.2 添加配置</p>
<figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">spring:</span></span><br><span class="line"> <span class="attr">redis:</span></span><br><span class="line"> <span class="attr">host:</span> <span class="number">192.168</span><span class="number">.195</span><span class="number">.100</span></span><br><span class="line"> <span class="attr">port:</span> <span class="number">6379</span></span><br></pre></td></tr></table></figure>
<p>2.3 测试使用</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Autowired</span></span><br><span class="line"><span class="keyword">private</span> StringRedisTemplate stringRedisTemplate;</span><br><span class="line"></span><br><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="keyword">void</span> <span class="title function_">testRedis</span><span class="params">()</span>&#123;</span><br><span class="line"> ValueOperations&lt;String, String&gt; ops = stringRedisTemplate.opsForValue();</span><br><span class="line"> ops.set(<span class="string">&quot;hello&quot;</span>,<span class="string">&quot;word_&quot;</span>+ UUID.randomUUID().toString());</span><br><span class="line"> <span class="type">String</span> <span class="variable">hello</span> <span class="operator">=</span> ops.get(<span class="string">&quot;hello&quot;</span>);</span><br><span class="line"> System.out.println(hello);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><img src="/pictures/image-20230710145648233.png" alt="image-20230710145648233"></p>
<p><strong>lettuce堆外内存溢出bug</strong></p>
<p>产生原因:</p>
<p>1)、springboot2.0以后<strong>默认</strong>使用 <code>lettuce</code>作为操作redis的客户端它使用netty进行网络通信</p>
<p>2)、lettuce的bug导致netty堆外内存溢出。netty如果没有指定堆外内存默认使用Xms的值可以使用-Dio.netty.maxDirectMemory进行设置</p>
<p>解决方案由于是lettuce的bug造成不要直接使用-Dio.netty.maxDirectMemory去调大虚拟机堆外内存治标不治本。</p>
<ul>
<li>1)、升级lettuce客户端</li>
<li>2)、切换使用jedis</li>
</ul>
<p>修改依赖文件使用jedis作为客户端工具</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-boot-starter-data-redis<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">exclusions</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">exclusion</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>io.lettuce<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>lettuce-core<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;/<span class="name">exclusion</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;/<span class="name">exclusions</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>redis.clients<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>jedis<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>
<h4 id="2-5-3-高并发下缓存失效问题"><a href="#2-5-3-高并发下缓存失效问题" class="headerlink" title="2.5.3 高并发下缓存失效问题"></a>2.5.3 高并发下缓存失效问题</h4><p><strong>缓存穿透</strong></p>
<img src="/pictures/image-20230710153635804.png" alt="image-20230710153635804" style="zoom:150%;" />
<p><strong>缓存雪崩</strong></p>
<img src="/pictures/image-20230710153651752.png" alt="image-20230710153651752" style="zoom:150%;" />
<p><strong>缓存击穿</strong></p>
<img src="/pictures/image-20230710153709464.png" alt="image-20230710153709464" style="zoom:150%;" />
<p><strong>分布式下如何加锁?</strong></p>
<img src="/pictures/image-20230710163735955.png" alt="image-20230710163735955" style="zoom:150%;" />
<p><strong>锁-时序问题</strong></p>
<img src="/pictures/image-20230710165738655.png" alt="image-20230710165738655" style="zoom:150%;" />
<p><strong>分布式锁演进-基本原理</strong></p>
<p><img src="/pictures/image-20230714163437984.png" alt="image-20230714163437984"></p>
<p><strong>分布式锁的最终形态</strong></p>
<p><img src="/pictures/image-20230714171221296.png" alt="image-20230714171221296"></p>
<h4 id="2-5-4-分布式锁-Redisson"><a href="#2-5-4-分布式锁-Redisson" class="headerlink" title="2.5.4 分布式锁-Redisson"></a>2.5.4 分布式锁-Redisson</h4><p>github地址: <a target="_blank" rel="noopener" href="https://github.com/redisson/redisson">https://github.com/redisson/redisson</a></p>
<p><strong>1.引入依赖</strong></p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--redisson--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.redisson<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>redisson<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">version</span>&gt;</span>3.12.0<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p><strong>2.配置redisson</strong></p>
2024-05-10 13:32:25 +08:00
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.atguigu.gulimall.product.config;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> org.redisson.Redisson;</span><br><span class="line"><span class="keyword">import</span> org.redisson.api.RedissonClient;</span><br><span class="line"><span class="keyword">import</span> org.redisson.config.Config;</span><br><span class="line"><span class="keyword">import</span> org.springframework.context.annotation.Bean;</span><br><span class="line"><span class="keyword">import</span> org.springframework.context.annotation.Configuration;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.io.IOException;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> Jason Gong</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@version</span> 1.0</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@website</span> https://qingling.icu</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Date</span> 2023/7/14</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Description</span> Redisson的配置</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@Configuration</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">MyRedissonConfig</span> &#123;</span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 所有对redisson的操作通过RedissonClient</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="meta">@Bean(destroyMethod = &quot;shutdown&quot;)</span></span><br><span class="line"> <span class="keyword">public</span> RedissonClient <span class="title function_">redisson</span><span class="params">()</span> <span class="keyword">throws</span> IOException &#123;</span><br><span class="line"> <span class="comment">//创建配置</span></span><br><span class="line"> <span class="type">Config</span> <span class="variable">config</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Config</span>();</span><br><span class="line"> config.useSingleServer().setAddress(<span class="string">&quot;redis://192.168.195.100:6379&quot;</span>);</span><br><span class="line"> <span class="comment">//根据配置config创建RedissonClient实例</span></span><br><span class="line"> <span class="keyword">return</span> Redisson.create(config);</span><br><span class="line"> &#125;</span><br><spa
2023-09-22 21:57:28 +08:00
<p><strong>3.测试使用</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Autowired</span></span><br><span class="line"><span class="keyword">private</span> RedissonClient redissonClient;</span><br><span class="line"></span><br><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="keyword">void</span> <span class="title function_">testRedisson</span><span class="params">()</span>&#123;</span><br><span class="line"> System.out.println(redissonClient);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><img src="/pictures/image-20230714175205635.png" alt="image-20230714175205635"></p>
<p><strong>4.简单的使用分布式锁</strong></p>
<p>上一个线程加锁之后没有解锁的话,后面的线程会一直等待前面的线程释放锁,自己再加锁。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@ResponseBody</span></span><br><span class="line"><span class="meta">@GetMapping(&quot;/hello&quot;)</span></span><br><span class="line"><span class="keyword">public</span> String <span class="title function_">hello</span><span class="params">()</span> &#123;</span><br><span class="line"> <span class="comment">//打印时间需要</span></span><br><span class="line"> <span class="type">SimpleDateFormat</span> <span class="variable">sdf</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">SimpleDateFormat</span>();<span class="comment">// 格式化时间</span></span><br><span class="line"> sdf.applyPattern(<span class="string">&quot;yyyy-MM-dd HH:mm:ss&quot;</span>);<span class="comment">// a为am/pm的标记</span></span><br><span class="line"> <span class="type">Date</span> <span class="variable">date</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Date</span>();<span class="comment">// 获取当前时间</span></span><br><span class="line"></span><br><span class="line"> <span class="comment">//获取同一把锁,只要锁的名字一样,就是同一把锁</span></span><br><span class="line"> <span class="type">RLock</span> <span class="variable">lock</span> <span class="operator">=</span> redissonClient.getLock(<span class="string">&quot;my-lock&quot;</span>);</span><br><span class="line"> <span class="comment">//加锁</span></span><br><span class="line"> lock.lock();<span class="comment">//阻塞式等待,锁没有释放的情况下会一直等待下去</span></span><br><span class="line"> <span class="comment">//1.锁的自动续期如果业务耗费的时间超长运行时会自动给锁续上新的30s默认时续期30s不必担心业务时间长锁自动过期被删掉</span></span><br><span class="line"> <span class="comment">//2.加锁的业务只要运行完成就不会给当前锁续期即使不手动解锁锁默认在30s以后自动s</span></span><br><span class="line"> System.out.println(<span class="string">&quot;现在时间:&quot;</span> + sdf.format(date));</span><br><span class="line"> <span class="keyword">try</span> &#123;</span><br><span class="line"> <span class="comment">//执行业务</span></span><br><span class="line"> System.out.println(<span class="string">&quot;加锁成功,执行业务,线程Id:&quot;</span>+Thread.currentThread().getId());</span><br><span class="line"> Thread.sleep(<span class="number">10000</span>);<span class="comment">//等待10秒</span></span><br><span class="line"> &#125; <span class="keyword">catch</span> (InterruptedException e) &#123;</span><br><span class="line"> <span class="keyword">throw</span> <span class="keyword">new</span> <span class="title class_">RuntimeException</span>(e);</span><br><span class="line"> &#125; <span class="keyword">finally</span> &#123;</span><br><span class="line"> <span class="comment">//解锁</span></span><br><span class="li
<p><img src="/pictures/image-20230714181620263.png" alt="image-20230714181620263"></p>
<p>my-lock对应的值会跟随线程的变化而变化</p>
<p><img src="/pictures/image-20230714182057982.png" alt="image-20230714182057982"></p>
<p><strong>读写锁</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 写锁没有释放的情况下,读锁必须等待写锁的释放,才能进行读的操作</span></span><br><span class="line"><span class="comment"> * 读写锁的测试</span></span><br><span class="line"><span class="comment"> * 写锁</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="meta">@ResponseBody</span></span><br><span class="line"> <span class="meta">@GetMapping(&quot;/write&quot;)</span></span><br><span class="line"> <span class="keyword">public</span> String <span class="title function_">writeValue</span><span class="params">()</span>&#123;</span><br><span class="line"> <span class="type">RReadWriteLock</span> <span class="variable">lock</span> <span class="operator">=</span> redissonClient.getReadWriteLock(<span class="string">&quot;rw-lock&quot;</span>);</span><br><span class="line"> <span class="type">String</span> <span class="variable">s</span> <span class="operator">=</span> <span class="string">&quot;&quot;</span>;</span><br><span class="line"> <span class="type">RLock</span> <span class="variable">rLock</span> <span class="operator">=</span> lock.writeLock();<span class="comment">//写锁</span></span><br><span class="line"> <span class="keyword">try</span> &#123;</span><br><span class="line"> rLock.lock();</span><br><span class="line"> s = UUID.randomUUID().toString();</span><br><span class="line"> Thread.sleep(<span class="number">30000</span>);</span><br><span class="line"> <span class="comment">//写的操作</span></span><br><span class="line"> redisTemplate.opsForValue().set(<span class="string">&quot;writeValue&quot;</span>,s);</span><br><span class="line"> &#125; <span class="keyword">catch</span> (InterruptedException e) &#123;</span><br><span class="line"> e.printStackTrace();</span><br><span class="line"> &#125;<span class="keyword">finally</span> &#123;</span><br><span class="line"> rLock.unlock();</span><br><span class="line"> &#125;</span><br><span class="line"> <span class="keyword">return</span> s;</span><br><span class="line"> &#125;</span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 读写锁的测试</span></span><br><span class="line"><span class="comment"> * 读锁</
<h4 id="2-5-5-分布式缓存一致性问题"><a href="#2-5-5-分布式缓存一致性问题" class="headerlink" title="2.5.5 分布式缓存一致性问题"></a>2.5.5 分布式缓存一致性问题</h4><p><strong>双写模式</strong></p>
<p><img src="/pictures/image-20230716100845389.png" alt="image-20230716100845389"></p>
<p><strong>失效模式</strong></p>
<p><img src="/pictures/image-20230716100909465.png" alt="image-20230716100909465"></p>
<p><strong>缓存数据一致性-解决方案</strong></p>
<p><img src="/pictures/image-20230716101939869.png" alt="image-20230716101939869"></p>
<p><strong>缓存数据一致性-解决-Canal</strong></p>
<p><img src="/pictures/image-20230716102510738.png" alt="image-20230716102510738"></p>
<h4 id="2-5-6-缓存-SpringCache"><a href="#2-5-6-缓存-SpringCache" class="headerlink" title="2.5.6 缓存-SpringCache"></a>2.5.6 缓存-SpringCache</h4><p><strong>1、简介</strong></p>
<ul>
<li><p>Spring 从 3.1 开始定义了 org.springframework.cache.Cache和 org.springframework.cache.CacheManager 接口来统一不同的缓存技术;并支持使用 JCacheJSR-107注解简化我们开发</p>
</li>
<li><p>Cache 接口为缓存的组件规范定义,包含缓存的各种操作集合;<br>Cache 接 口 下 Spring 提 供 了 各 种 xxxCache 的 实 现 如 RedisCache EhCacheCache ,ConcurrentMapCache 等;</p>
</li>
<li><p>每次调用需要缓存功能的方法时Spring 会检查检查指定参数的指定的目标方法是否已经被调用过;如果有就直接从缓存中获取方法调用后的结果,如果没有就调用方法并缓存结果后返回给用户。下次调用直接从缓存中获取。</p>
</li>
<li><p>使用 Spring 缓存抽象时我们需要关注以下两点;</p>
<p>1、确定方法需要被缓存以及他们的缓存策略</p>
<p>2、从缓存中读取之前缓存存储的数据</p>
</li>
</ul>
<p><strong>2.以前的笔记教程</strong></p>
2024-05-10 13:32:25 +08:00
<p><strong><a href="https://qingling.icu/posts/13813.html">SpringBoot整合Redis | The Blog (qingling.icu)</a></strong></p>
2023-09-22 21:57:28 +08:00
<p><strong>3.项目中整合SpringCache简化开发</strong></p>
<p><strong>3.1.引入依赖</strong></p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--我们使redis作为缓存的使用场景在前面我们还要引入redis的依赖--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-boot-starter-cache<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p><strong>3.2.编写配置文件</strong></p>
<figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#使用缓存的类型</span></span><br><span class="line"><span class="attr">spring.cache.type</span>=<span class="string">redis</span></span><br></pre></td></tr></table></figure>
<p><strong>3.3.测试使用缓存</strong></p>
<p>常用的缓存注解</p>
<p>@Cacheable 触发将数据保存到缓存的操作</p>
<p>@CachePut 不影响方法执行更新缓存</p>
<p>@CacheEvict 触发将数据从缓存中删除的操作</p>
<p>@Caching 组合多个缓存操作</p>
<p>@CacheConfig 在类级别上共享缓存的相关配置</p>
<p>1.在启动类上添加缓存的注解</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@EnableCaching</span> <span class="comment">//开启缓存功能</span></span><br></pre></td></tr></table></figure>
<p>2.测试使用</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//每一个需要缓存的数据需要我们指定放到那个名字的缓存[缓存的分区(按照业务类型分)]</span></span><br><span class="line"><span class="meta">@Cacheable(&#123;&quot;category&quot;&#125;)</span> <span class="comment">//代表当前方法的结果需要缓存,如果缓存中有,方法就不需要调用了,如果缓存中没有,就会调用方法,将方法的结果放入到缓存</span></span><br><span class="line"><span class="meta">@Override</span></span><br><span class="line"><span class="keyword">public</span> List&lt;CategoryEntity&gt; <span class="title function_">getLevelOneCategorys</span><span class="params">()</span> &#123;</span><br><span class="line"> <span class="keyword">return</span> categoryDao.selectList(<span class="keyword">new</span> <span class="title class_">LambdaQueryWrapper</span>&lt;CategoryEntity&gt;().eq(CategoryEntity::getParentCid, <span class="number">0</span>));</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><img src="/pictures/image-20230716112105848.png" alt="image-20230716112105848"></p>
<p><strong>自定义数据的存储</strong></p>
<p>设置自定义的key值</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Cacheable(value = &#123;&quot;category&quot;&#125;,key = &quot;&#x27;levelOneCategorys&#x27;&quot;)</span> <span class="comment">//自定义key值</span></span><br></pre></td></tr></table></figure>
<p>tips: #root.method.name是SpEL表达式</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Cacheable(value = &#123;&quot;category&quot;&#125;,key = &quot;#root.method.name&quot;)</span> <span class="comment">//使用方法名作为key值</span></span><br></pre></td></tr></table></figure>
<p>设置缓存数据的存活时间</p>
<figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#设置一个小时的存活时间</span></span><br><span class="line"><span class="attr">spring.cache.redis.time-to-live</span>=<span class="string">3600000</span></span><br></pre></td></tr></table></figure>
<p><img src="/pictures/image-20230716152821097.png" alt="image-20230716152821097"></p>
<p><strong>将数据保存为json的格式</strong></p>
<p>添加配置类</p>
2024-05-10 13:32:25 +08:00
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.atguigu.gulimall.product.config;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> org.springframework.boot.autoconfigure.cache.CacheProperties;</span><br><span class="line"><span class="keyword">import</span> org.springframework.boot.context.properties.EnableConfigurationProperties;</span><br><span class="line"><span class="keyword">import</span> org.springframework.context.annotation.Bean;</span><br><span class="line"><span class="keyword">import</span> org.springframework.context.annotation.Configuration;</span><br><span class="line"><span class="keyword">import</span> org.springframework.data.redis.cache.RedisCacheConfiguration;</span><br><span class="line"><span class="keyword">import</span> org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;</span><br><span class="line"><span class="keyword">import</span> org.springframework.data.redis.serializer.RedisSerializationContext;</span><br><span class="line"><span class="keyword">import</span> org.springframework.data.redis.serializer.StringRedisSerializer;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> Jason Gong</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@version</span> 1.0</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@website</span> https://qingling.icu</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Date</span> 2023/7/16</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Description</span> 缓存的序列化的配置</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@EnableConfigurationProperties(CacheProperties.class)</span></span><br><span class="line"><span class="meta">@Configuration</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">MyCacheConfig</span> &#123;</span><br><span class="line"> <span class="meta">@Bean</span></span><br><span class="line"> RedisCacheConfiguration <span class="title function_">redisCacheConfiguration</span><span class="params">(CacheProperties cacheProperties)</span> &#123;</span><br><span class="line"> <span class="type">RedisCacheConfiguration</span> <span class="variable">config</span> <span class="operator">=</span> RedisCacheConfigu
2023-09-22 21:57:28 +08:00
<p>效果</p>
<p><img src="/pictures/image-20230716160027391.png" alt="image-20230716160027391"></p>
<p><strong>其它的常用配置</strong></p>
<figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#缓存的前缀,如果指定了前缀就使用指定的前缀,没有指定前缀就使用缓存的名字作为前缀</span></span><br><span class="line"><span class="attr">spring.cache.redis.key-prefix</span>=<span class="string">CACHE_</span></span><br><span class="line"><span class="comment">#是否使用缓存的前缀</span></span><br><span class="line"><span class="attr">spring.cache.redis.use-key-prefix</span>=<span class="string">true</span></span><br><span class="line"><span class="comment">#是否缓存空值(缓存穿透的问题的解决)</span></span><br><span class="line"><span class="attr">spring.cache.redis.cache-null-values</span>=<span class="string">true</span></span><br></pre></td></tr></table></figure>
<h3 id="2-6-检索服务"><a href="#2-6-检索服务" class="headerlink" title="2.6 检索服务"></a>2.6 检索服务</h3><h4 id="2-6-1-整合页面"><a href="#2-6-1-整合页面" class="headerlink" title="2.6.1 整合页面"></a>2.6.1 整合页面</h4><p><img src="/pictures/image-20230716173751202.png" alt="image-20230716173751202"></p>
<h4 id="2-6-2-配置域名访问"><a href="#2-6-2-配置域名访问" class="headerlink" title="2.6.2 配置域名访问"></a>2.6.2 配置域名访问</h4><p><img src="/pictures/image-20230716173453604.png" alt="image-20230716173453604"></p>
<p>Nginx的配置</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">server &#123;</span><br><span class="line"> listen 80;</span><br><span class="line"> server_name *.gulimall.com;</span><br><span class="line"></span><br><span class="line"> location / &#123;</span><br><span class="line"> proxy_set_header Host $host;</span><br><span class="line"> proxy_pass http://gulimall;</span><br><span class="line"> &#125;</span><br><span class="line"></span><br><span class="line"> error_page 500 502 503 504 /50x.html;</span><br><span class="line"> location = /50x.html &#123;</span><br><span class="line"> root /usr/share/nginx/html;</span><br><span class="line"> &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>网关中配置断言</p>
<figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#搜索</span></span><br><span class="line"><span class="bullet">-</span> <span class="attr">id:</span> <span class="string">gulimall_search_route</span></span><br><span class="line"> <span class="attr">uri:</span> <span class="string">lb://gulimall-search</span></span><br><span class="line"> <span class="attr">predicates:</span></span><br><span class="line"> <span class="bullet">-</span> <span class="string">Host=search.gulimall.com</span></span><br></pre></td></tr></table></figure>
<p><img src="/pictures/image-20230716173628024.png" alt="image-20230716173628024"></p>
<h4 id="2-6-3-首页和搜索页相互跳转"><a href="#2-6-3-首页和搜索页相互跳转" class="headerlink" title="2.6.3 首页和搜索页相互跳转"></a>2.6.3 首页和搜索页相互跳转</h4><p><strong>首页无法跳转到搜索页的原因gumall改为gulimall即可</strong></p>
<p><img src="/pictures/image-20230716181052580.png" alt="image-20230716181052580"></p>
<p><strong>搜索框跳转到搜索页,修改页面中的代码</strong></p>
<p><img src="/pictures/image-20230717094656565.png" alt="image-20230717094656565"></p>
<h4 id="2-6-4-商品的检索"><a href="#2-6-4-商品的检索" class="headerlink" title="2.6.4 商品的检索"></a>2.6.4 商品的检索</h4><p><img src="/pictures/image-20230717095740594.png" alt="image-20230717095740594"></p>
<p>检索条件的VO</p>
2024-05-10 13:32:25 +08:00
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.atguigu.gulimall.search.vo;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> lombok.Data;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.List;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> Jason Gong</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@version</span> 1.0</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@website</span> https://qingling.icu</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Date</span> 2023/7/17</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Description</span> 检索条件的vo</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@Data</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">SearchParam</span> &#123;</span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 页面传递过来的全文匹配关键字</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">private</span> String keyword;</span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 品牌id,可以多选</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">private</span> List&lt;Long&gt; brandId;</span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 三级分类id</span></span><b
2023-09-22 21:57:28 +08:00
<p>检索的结果</p>
2024-05-10 13:32:25 +08:00
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.atguigu.gulimall.search.vo;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.atguigu.common.to.es.SkuEsModel;</span><br><span class="line"><span class="keyword">import</span> lombok.Data;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.ArrayList;</span><br><span class="line"><span class="keyword">import</span> java.util.List;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> Jason Gong</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@version</span> 1.0</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@website</span> https://qingl
2023-09-22 21:57:28 +08:00
<p><strong>未完待续(学习到177)……</strong></p>
2024-06-14 22:00:25 +08:00
</article><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98/">项目实战</a></div><div class="post_share"><div class="social-share" data-image="/img/8.png" data-sites="wechat,weibo,qq"></div><link rel="stylesheet" href="/cdn/css/share.min.css" media="print" onload="this.media='all'"><script src="/cdn/js/social-share.min.js" defer></script></div></div><div class="post-reward"><div class="reward-button"><i class="fas fa-qrcode"></i> 打赏</div><div class="reward-main"><ul class="reward-all"><li class="reward-item"><a href="/img/wechat.jpg" target="_blank"><img class="post-qr-code-img" src="/img/wechat.jpg" alt="微信"/></a><div class="post-qr-code-desc">微信</div></li><li class="reward-item"><a href="/img/alipay.jpg" target="_blank"><img class="post-qr-code-img" src="/img/alipay.jpg" alt="支付宝"/></a><div class="post-qr-code-desc">支付宝</div></li></ul></div></div><br/><div id="post-comment"><div class="comment-head"><div class="comment-headline"><i class="far fa-comment-alt fa-fw"></i><span> 评论</span></div></div><div class="comment-wrap"><div><div id="gitalk-container"></div></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/img/avatar.jpg" onerror="this.onerror=null;this.src='/img/loading.gif'" alt="avatar"/></div><div class="author-info__name">Jason</div><div class="author-info__description">Debug the World</div></div><div class="card-info-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">60</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">39</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">10</div></a></div><a id="card-info-btn"><i class="fab fa-microsoft"></i><span>Ctrl + D 收藏</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://github.com/JasonsGong" target="_blank" title="Github"><i class="fab fa-github"></i></a><a class="social-icon" href="tencent://AddContact/?fromId=45&amp;fromSubId=1&amp;subcmd=all&amp;uin=2602183349&amp;website=www.oicqzone.com" target="_blank" title="QQ"><i class="fab fa-qq"></i></a><a class="social-icon" href="mailto:2602183349@qq.com" target="_blank" title="Email"><i class="fas fa-envelope-open-text"></i></a><a class="social-icon" href="https://github.com/JasonsGong?tab=repositories" target="_blank" title="代码仓库"><i class="fas fa-database"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">本网站是静态网站,更新页面资源请使用Ctrl+F5;若网站内文章对你有帮助,请使用Ctrl+D收藏该网站</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content is-expand"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%B8%80-%E9%A1%B9%E7%9B%AE%E7%AE%80%E4%BB%8B"><span class="toc-text">一.项目简介</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#1-%E9%A1%B9%E7%9B%AE%E8%83%8C%E6%99%AF"><span class="toc-text">1.项目背景</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-%E9%A1%B9%E7%9B%AE%E6%9E%B6%E6%9E%84%E5%9B%BE"><span class="toc-text">2.项目架构图</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-%E9%A1%B9%E7%9B%AE%E6%8A%80%E6%9C%AF%E5%92%8C%E7%89%B9%E8%89%B2"><span class="toc-text">3.项目技术和特色</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-%E9%A1%B9%E7%9B%AE%E7%9A%84%E5%89%8D%E7%BD%AE%E8%A6%81%E6%B1%82"><span class="toc-text">4.项目的前置要求</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="
2024-01-13 16:32:52 +08:00
function initGitalk () {
var gitalk = new Gitalk(Object.assign({
clientID: '00fb27b1e484536359c2',
clientSecret: 'be41a12281c68b6e228d1a27e8d08aeb91541145',
repo: 'BlogComment',
owner: 'JasonsGong',
admin: ['JasonsGong'],
id: 'e92a578055ada87a3876e96bcfbb5763',
updateCountCallback: commentCount
},null))
gitalk.render('gitalk-container')
}
if (typeof Gitalk === 'function') initGitalk()
else {
2024-01-13 17:50:17 +08:00
getCSS('/cdn/css/gitalk.min.css')
getScript('/cdn/js/gitalk.min.js').then(initGitalk)
2024-01-13 16:32:52 +08:00
}
}
function commentCount(n){
let isCommentCount = document.querySelector('#post-meta .gitalk-comment-count')
if (isCommentCount) {
isCommentCount.textContent= n
}
}
if ('Gitalk' === 'Gitalk' || !true) {
if (true) btf.loadComment(document.getElementById('gitalk-container'), loadGitalk)
else loadGitalk()
} else {
function loadOtherComment () {
loadGitalk()
}
}</script></div><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span> 数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><br/><div class="no-result" id="local-search-results"></div><div id="local-search-stats-wrap"></div></div></div><div id="search-mask"></div><script src="/js/search/local-search.js"></script></div></div><!-- hexo injector body_end start --><script data-pjax>
2023-09-22 21:57:28 +08:00
function butterfly_swiper_injector_config(){
2024-01-13 22:42:28 +08:00
var parent_div_git = document.getElementById('recent-posts');
2024-06-14 22:00:25 +08:00
var item_html = '<div class="recent-post-item" style="height: auto;width: 100%"><div class="blog-slider swiper-container-fade swiper-container-horizontal" id="swiper_container"><div class="blog-slider__wrp swiper-wrapper" style="transition-duration: 0ms;"><div class="blog-slider__item swiper-slide" style="width: 750px; opacity: 1; transform: translate3d(0px, 0px, 0px); transition-duration: 0ms;"><a class="blog-slider__img" href="posts/19306.html" alt=""><img width="48" height="48" src="/img/1.png" alt="" onerror="this.src=https://unpkg.zhimg.com/akilar-candyassets/image/loading.gif; this.onerror = null;"/></a><div class="blog-slider__content"><span class="blog-slider__code">2023-04-21</span><a class="blog-slider__title" href="posts/19306.html" alt="">Docker容器化技术</a><div class="blog-slider__text">Docker</div><a class="blog-slider__button" href="posts/19306.html" alt="">详情 </a></div></div><div class="blog-slider__item swiper-slide" style="width: 750px; opacity: 1; transform: translate3d(0px, 0px, 0px); transition-duration: 0ms;"><a class="blog-slider__img" href="posts/47003.html" alt=""><img width="48" height="48" src="/img/5.png" alt="" onerror="this.src=https://unpkg.zhimg.com/akilar-candyassets/image/loading.gif; this.onerror = null;"/></a><div class="blog-slider__content"><span class="blog-slider__code">2023-03-10</span><a class="blog-slider__title" href="posts/47003.html" alt="">常用正则表达式大全</a><div class="blog-slider__text">正则表达式</div><a class="blog-slider__button" href="posts/47003.html" alt="">详情 </a></div></div><div class="blog-slider__item swiper-slide" style="width: 750px; opacity: 1; transform: translate3d(0px, 0px, 0px); transition-duration: 0ms;"><a class="blog-slider__img" href="posts/20683.html" alt=""><img width="48" height="48" src="/img/8.png" alt="" onerror="this.src=https://unpkg.zhimg.com/akilar-candyassets/image/loading.gif; this.onerror = null;"/></a><div class="blog-slider__content"><span class="blog-slider__code">2023-06-05</span><a class="blog-slider__title" href="posts/20683.html" alt="">Linux中开发环境的搭建</a><div class="blog-slider__text">环境搭建</div><a class="blog-slider__button" href="posts/20683.html" alt="">详情 </a></div></div><div class="blog-slider__item swiper-slide" style="width: 750px; opacity: 1; transform: translate3d(0px, 0px, 0px); transition-duration: 0ms;"><a class="blog-slider__img" href="posts/63333.html" alt=""><img width="48" height="48" src="/img/10.png" alt="" onerror="this.src=https://unpkg.zhimg.com/akilar-candyassets/image/loading.gif; this.onerror = null;"/></a><div class="blog-slider__content"><span class="blog-slider__code">2023-06-03</span><a class="blog-slider__title" href="posts/63333.html" alt="">开发环境的搭建</a><div class="blog-slider__text">环境搭建</div><a class="blog-slider__button" href="posts/63333.html" alt="">详情 </a></div></div></div><div class="blog-slider__pagination swiper-pagination-clickable swiper-pagination-bullets"></div></div></div>';
2024-01-13 22:42:28 +08:00
if (parent_div_git !== null && typeof parent_div_git !== 'undefined') {
parent_div_git.insertAdjacentHTML("afterbegin",item_html)
}
2023-09-22 21:57:28 +08:00
}
var elist = 'undefined'.split(',');
var cpage = location.pathname;
2023-10-28 10:47:20 +08:00
var epage = 'all';
2023-09-22 21:57:28 +08:00
var flag = 0;
for (var i=0;i<elist.length;i++){
if (cpage.includes(elist[i])){
flag++;
}
}
if ((epage ==='all')&&(flag == 0)){
butterfly_swiper_injector_config();
}
else if (epage === cpage){
butterfly_swiper_injector_config();
}
2024-01-13 22:10:58 +08:00
</script><script defer src="https://npm.elemecdn.com/hexo-butterfly-swiper/lib/swiper.min.js"></script><script defer data-pjax src="https://npm.elemecdn.com/hexo-butterfly-swiper/lib/swiper_init.js"></script><!-- hexo injector body_end end --></body></html>