文章列表

暂停73.5万年!

作者:微信小助手

<section class="mp_profile_iframe_wrp" data-mpa-powered-by="yiban.io"> <mp-common-profile class="custom_select_card mp_profile_iframe mp_common_widget" data-pluginname="mpprofile" data-id="MzA3Nzg3NjYxOQ==" data-headimg="http://mmbiz.qpic.cn/mmbiz_png/pxopGibm8O52ITYCpDic8meL4dwMv1ChmREtJGDWHsIXvM8ib0BVM6yfYCOf04SgSYoI6b8rUfFdliblxCLKnkiaGTw/0?wx_fmt=png" data-nickname="知彼而知己" data-alias="heu168" data-signature="电脑技术、软件分享" data-from="0"></mp-common-profile> </section> <p style="margin: 10px 16px;padding-top: 8px;padding-bottom: 8px;outline: 0px;text-wrap: wrap;-webkit-text-stroke-width: 0.05px;font-family: Optima-Regular, PingFangTC-light;font-size: 16px;background-color: rgb(255, 255, 255);line-height: 26px;color: rgb(43, 43, 43);letter-spacing: 2px;word-spacing: 2px;visibility: visible;-webkit-text-stroke-color: initial !important;">Windows10/11的更新频率非常频繁,也经常会修复旧bug的同时带来一些新的bug。所以有些人不喜欢系统的自动更新,总想把它禁用掉。<span style="-webkit-text-stroke: 0.05px;">禁用系统更新的</span><span style="-webkit-text-stroke: 0.05px;">软件有很多</span><span style="-webkit-text-stroke: 0.05px;">,不过这些方法大多是强行禁止,偶尔会引入一些奇怪的问题。</span></p> <section style="margin-top: 24px;text-wrap: wrap;outline: 0px;font-family: system-ui, -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;letter-spacing: 0.544px;background-color: rgb(255, 255, 255);visibility: visible;"> <section data-mid="" mpa-from-tpl="t" style="padding-left: 15px;outline: 0px;display: flex;flex-direction: column;visibility: visible;"> <section data-mid="" mpa-from-tpl="t" style="outline: 0px;align-self: flex-start;visibility: visible;"> <section data-mid="" mpa-from-tpl="t" style="outline: 0px;display: flex;align-items: flex-end;justify-content: flex-start;border-bottom: 9px solid rgb(83, 200, 118);visibility: visible;"> <section data-mid="" mpa-from-tpl="t" style="padding-right: 4px;padding-left: 3px;outline: 0px;text-align: left;flex-shrink: 0;visibility: visible;"> <p data-mid="" style="outline: 0px;font-size: 20px;color: rgb(54, 54, 54);line-height: 21px;word-break: break-word;visibility: visible;"><span style="outline: 0px;font-size: 18px;">01<br style="outline: 0px;"></span></p> </section> <section data-mid="" mpa-from-tpl="t" style="padding: 5px 1px 4px 2px;outline: 0px;text-align: left;background: linear-gradient(rgb(255, 255, 255) 0%, rgb(193, 255, 212) 100%);border-width: 1px;border-style: solid;border-color: rgb(44, 44, 44);transform: translateY(5px);visibility: visible;"> <p data-mid="" style="outline: 0px;font-size: 16px;font-weight: bold;color: rgb(48, 48, 48);line-height: 22px;word-break: break-word;visibility: visible;">「每月更新有多个分支」</p> </section> </section> </section> </section> </section> <p style="margin: 10px 16px;padding-top: 8px;padding-bottom: 8px;text-wrap: wrap;outline: 0px;-webkit-text-stroke-width: 0.05px;font-family: Optima-Regular, PingFangTC-light;font-size: 16px;background-color: rgb(255, 255, 255);line-height: 26px;color: rgb(43, 43, 43);letter-spacing: 2px;word-spacing: 2px;visibility: visible;-webkit-text-stroke-color: initial !important;"><span style="outline: 0px;visibility: visible;text-decoration: none;">分支1</span><span style="outline: 0px;-webkit-text-stroke: 0.05px;visibility: visible;">:<span style="outline: 0px;-webkit-text-stroke-width: 0.05px;visibility: visible;">名为release_svc_prod1</span>,是在每月的第二个星期二更新(俗称:<strong>补丁星期二</strong>),这个版本为RTM版,即微软会正式发布ISO镜像。已经安装了Win11正式版的,可以通过Windows Update进行自动推送。</span></p> <p style="margin: 10px 16px;padding-top: 8px;padding-bottom: 8px;text-wrap: wrap;outline: 0px;-webkit-text-stroke-width: 0.05px;font-family: Optima-Regular, PingFangTC-light;font-size: 16px;background-color: rgb(255, 255, 255);line-height: 26px;color: rgb(43, 43, 43);letter-spacing: 2px;word-spacing: 2px;visibility: visible;-webkit-text-stroke-color: initial !important;"><span style="outline: 0px;visibility: visible;text-decoration: none;">分支2</span><span style="outline: 0px;-webkit-text-stroke: 0.05px;visibility: visible;"><span style="outline: 0px;-webkit-text-stroke-width: 0.05px;visibility: visible;">:<span style="outline: 0px;-webkit-text-stroke-width: 0.05px;visibility: visible;">名为release_svc_prod2,</span>是在每月的</span>中下旬<span style="outline: 0px;-webkit-text-stroke-width: 0.05px;visibility: visible;">更新</span>,这个版本为测试版(现在微软都称之为预览版),可以在Beta通道进行<span style="outline: 0px;-webkit-text-stroke-width: 0.05px;visibility: visible;">自动推送。</span></span></p> <p style="margin: 10px 16px;padding-top: 8px;padding-bottom: 8px;text-wrap: wrap;outline: 0px;-webkit-text-stroke-width: 0.05px;font-family: Optima-Regular, PingFangTC-light;font-size: 16px;background-color: rgb(255, 255, 255);line-height: 26px;color: rgb(43, 43, 43);letter-spacing: 2px;word-spacing: 2px;visibility: visible;-webkit-text-stroke-color: initial !important;"><span style="outline: 0px;visibility: visible;text-decoration: none;">分支3</span><span style="outline: 0px;-webkit-text-stroke-width: 0.05px;visibility: visible;">:<span style="outline: 0px;-webkit-text-stroke-width: 0.05px;visibility: visible;">名为release_svc_prod3,</span>是在每月底<span style="outline: 0px;-webkit-text-stroke-width: 0.05px;visibility: visible;">更新</span>,这个版本仍为测试版。<span style="outline: 0px;-webkit-text-stroke-width: 0.05px;visibility: visible;">需要注意的是,此分支不代表是一个版本,可能会有多个,最后一个与正式版完全一样。</span>例如微软改个名直接发布为正式版,有时版本比较稳定,甚至直接将分支2改个名。<span style="outline: 0px;-webkit-text-stroke-width: 0.05px;visibility: visible;"><span style="outline: 0px;-webkit-text-stroke-width: 0.05px;visibility: visible;">此版本在正式版通道不会自动推送,但可以手动更新。</span></span></span></p> <p style="margin: 10px 16px;padding-top: 8px;padding-bottom: 8px;text-wrap: wrap;outline: 0px;-webkit-text-stroke-width: 0.05px;font-family: Optima-Regular, PingFangTC-light;font-size: 16px;background-color: rgb(255, 255, 255);line-height: 26px;color: rgb(43, 43, 43);letter-spacing: 2px;word-spacing: 2px;visibility: visible;-webkit-text-stroke-color: initial !important;"><span style="outline: 0px;-webkit-text-stroke: 0.05px;">可能每个月上面3次更新还不够,分支3很可能会有多个版本号。有时,即便是分支1正式版发布后,也可能翻车,微软还会有紧急修复的更新分支release_svc_im,自动推送。</span></p> <section style="margin-top: 24px;text-wrap: wrap;outline: 0px;font-family: system-ui, -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;letter-spacing: 0.544px;background-color: rgb(255, 255, 255);visibility: visible;"> <section data-mid="" mpa-from-tpl="t" style="padding-left: 15px;outline: 0px;display: flex;flex-direction: column;visibility: visible;"> <section data-mid="" mpa-from-tpl="t" style="outline: 0px;align-self: flex-start;visibility: visible;"> <section data-mid="" mpa-from-tpl="t" style="outline: 0px;display: flex;align-items: flex-end;justify-content: flex-start;border-bottom: 9px solid rgb(83, 200, 118);visibility: visible;"> <section data-mid="" mpa-from-tpl="t" style="padding-right: 4px;padding-left: 3px;outline: 0px;text-align: left;flex-shrink: 0;visibility: visible;"> <p data-mid="" style="outline: 0px;font-size: 20px;color: rgb(54, 54, 54);line-height: 21px;word-break: break-word;visibility: visible;"><span style="outline: 0px;font-size: 18px;">02<br style="outline: 0px;"></span></p> </section> <section data-mid="" mpa-from-tpl="t" style="padding: 5px 1px 4px 2px;outline: 0px;text-align: left;background: linear-gradient(rgb(255, 255, 255) 0%, rgb(193, 255, 212) 100%);border-width: 1px;border-style: solid;border-color: rgb(44, 44, 44);transform: translateY(5px);visibility: visible;"> <p data-mid="" style="outline: 0px;font-size: 16px;font-weight: bold;color: rgb(48, 48, 48);line-height: 22px;word-break: break-word;visibility: visible;">「暂停Windows Update」</p> </section> </section> </section> </section> </section> <p style="margin: 10px 16px;padding-top: 8px;padding-bottom: 8px;outline: 0px;text-wrap: wrap;-webkit-text-stroke-width: 0.05px;font-family: Optima-Regular, PingFangTC-light;font-size: 16px;background-color: rgb(255, 255, 255);line-height: 26px;color: rgb(43, 43, 43);letter-spacing: 2px;word-spacing: 2px;visibility: visible;-webkit-text-stroke-color: initial !important;">我们在“设置-Windows更新”页面中可以看到,微软为大家提供了暂停更新的方法。总共有五个选项,最大可以暂停5周。</p> <p style="text-align: center;padding: 0px 0.5em;"><img class="rich_pages wxw-img" data-galleryid="" data-ratio="0.7490740740740741" data-s="300,640" data-type="png" data-w="1080" style="border-radius: 3px;box-shadow: rgb(210, 210, 210) 0em 0em 0.5em 0px;font-size: 17px;height: auto !important;" src="/upload/387ed7b66ea649a9f712598eafac14bb.png"></p> <p style="margin: 10px 16px;padding-top: 8px;padding-bottom: 8px;outline: 0px;text-wrap: wrap;-webkit-text-stroke-width: 0.05px;font-family: Optima-Regular, PingFangTC-light;font-size: 16px;background-color: rgb(255, 255, 255);line-height: 26px;color: rgb(43, 43, 43);letter-spacing: 2px;word-spacing: 2px;visibility: visible;-webkit-text-stroke-color: initial !important;">我们可以通过修改注册表的方法,将此处“暂停5周”修改成更大的值,这样就可以无限延长暂停更新的时间了。</p> <p style="margin: 10px 16px;padding-top: 8px;padding-bottom: 8px;outline: 0px;text-wrap: wrap;-webkit-text-stroke-width: 0.05px;font-family: Optima-Regular, PingFangTC-light;font-size: 16px;background-color: rgb(255, 255, 255);line-height: 26px;color: rgb(43, 43, 43);letter-spacing: 2px;word-spacing: 2px;visibility: visible;-webkit-text-stroke-color: initial !important;">把如下内容复制到记事本,保存为”xxx.reg“文件,双击即可成功导入注册表。(若要恢复默认,删除以下几项即可)</p> <section class="code-snippet__fix code-snippet__js"> <ul class="code-snippet__line-index code-snippet__js"> </ul> <pre class="code-snippet__js" data-lang="cs"><code><span class="code-snippet_outer">Windows Registry Editor Version <span class="code-snippet__number">5.00</span></span></code><code><span class="code-snippet_outer"><br></span></code><code><span class="code-snippet_outer">[<span class="code-snippet__meta">HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\WindowsUpdate\UX\Settings</span>]</span></code><code><span class="code-snippet_outer"><span class="code-snippet__string">"FlightSettingsMaxPauseDays"</span>=dword:<span class="code-snippet__number">0000149f</span></span></code><code><span class="code-snippet_outer"><span class="code-snippet__string">"PauseFeatureUpdatesStartTime"</span>=<span class="code-snippet__string">"2023-08-06T08:00:00Z"</span></span></code><code><span class="code-snippet_outer"><span class="code-snippet__string">"PauseFeatureUpdatesEndTime"</span>=<span class="code-snippet__string">"2038-01-19T03:14:07Z"</span></span></code><code><span class="code-snippet_outer"><span class="code-snippet__string">"PauseQualityUpdatesStartTime"</span>=<span class="code-snippet__string">"2023-08-06T08:00:00Z"</span></span></code><code><span class="code-snippet_outer"><span class="code-snippet__string">"PauseQualityUpdatesEndTime"</span>=<span class="code-snippet__string">"2038-01-19T03:14:07Z"</span></span></code><code><span class="code-snippet_outer"><span class="code-snippet__string">"PauseUpdatesExpiryTime"</span>=<span class="code-snippet__string">"2038-01-19T03:14:07Z"</span></span></code></pre> </section> <p style="text-align: left;padding: 0px 0.5em;">修改成功后,无需重启电脑则立即生效,可以看到Windows更新中,暂停时间已经变得很长了。一下暂停了15年。</p> <p style="text-align: center;padding: 0px 0.5em;"><img class="rich_pages wxw-img" data-galleryid="" data-ratio="0.9685185185185186" data-s="300,640" data-type="png" data-w="1080" style="border-radius: 3px;box-shadow: rgb(210, 210, 210) 0em 0em 0.5em 0px;font-size: 17px;height: auto !important;" src="/upload/cafaa5c7ffebdc93eb7485ce97ab3004.png"></p> <p style="text-align: center;padding: 0px 0.5em;"><img class="rich_pages wxw-img" data-galleryid="" data-ratio="0.7768518518518519" data-s="300,640" data-type="png" data-w="1080" style="border-radius: 3px;box-shadow: rgb(210, 210, 210) 0em 0em 0.5em 0px;font-size: 17px;height: auto !important;" src="/upload/1e7c32769b859f33acb552f472eb5d54.png"></p> <p>上述注册表可以直接下载使用:</p> <p><span style="color: rgb(0, 122, 170);text-decoration: underline;">https://wwi.lanzoup.com/b00rcldhg</span> 密码:1hcj</p> <section style="margin-top: 24px;outline: 0px;font-family: system-ui, -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;letter-spacing: 0.544px;text-wrap: wrap;background-color: rgb(255, 255, 255);visibility: visible;"> <section data-mid="" mpa-from-tpl="t" style="padding-left: 15px;outline: 0px;display: flex;flex-direction: column;visibility: visible;"> <section data-mid="" mpa-from-tpl="t" style="outline: 0px;align-self: flex-start;visibility: visible;"> <section data-mid="" mpa-from-tpl="t" style="outline: 0px;display: flex;align-items: flex-end;justify-content: flex-start;border-bottom: 9px solid rgb(83, 200, 118);visibility: visible;"> <section data-mid="" mpa-from-tpl="t" style="padding-right: 4px;padding-left: 3px;outline: 0px;text-align: left;flex-shrink: 0;visibility: visible;"> <p data-mid="" style="outline: 0px;font-size: 20px;color: rgb(54, 54, 54);line-height: 21px;word-break: break-word;visibility: visible;"><span style="outline: 0px;font-size: 18px;">02<br style="outline: 0px;"></span></p> </section> <section data-mid="" mpa-from-tpl="t" style="padding: 5px 1px 4px 2px;outline: 0px;text-align: left;background: linear-gradient(rgb(255, 255, 255) 0%, rgb(193, 255, 212) 100%);border-width: 1px;border-style: solid;border-color: rgb(44, 44, 44);transform: translateY(5px);visibility: visible;"> <p data-mid="" style="outline: 0px;font-size: 16px;font-weight: bold;color: rgb(48, 48, 48);line-height: 22px;word-break: break-word;visibility: visible;">「一些说明」</p> </section> </section> </section> </section> </section> <p style="margin: 10px 16px;padding-top: 8px;padding-bottom: 8px;outline: 0px;text-wrap: wrap;-webkit-text-stroke-width: 0.05px;font-family: Optima-Regular, PingFangTC-light;font-size: 16px;background-color: rgb(255, 255, 255);line-height: 26px;color: rgb(43, 43, 43);letter-spacing: 2px;word-spacing: 2px;visibility: visible;-webkit-text-stroke-color: initial !important;"><span style="outline: 0px;-webkit-text-stroke: 0.05px;">上述暂停更新的时间理论上限为268435455天(0x7fffffff),约73.5万年,不过我相信那个时候,大家都不在了,所以没必要设置这么久~</span></p> <p style="margin: 10px 16px;padding-top: 8px;padding-bottom: 8px;outline: 0px;text-wrap: wrap;-webkit-text-stroke-width: 0.05px;font-family: Optima-Regular, PingFangTC-light;font-size: 16px;background-color: rgb(255, 255, 255);line-height: 26px;color: rgb(43, 43, 43);letter-spacing: 2px;word-spacing: 2px;visibility: visible;-webkit-text-stroke-color: initial !important;"><span style="outline: 0px;-webkit-text-stroke: 0.05px;">并且此值设置过大的话,会导致打开Windows更新页面的时候出现卡顿,所以设置一个差不多的时间即可。同一个系统能用10年不重装的人估计不多~所以咱们就设置到<strong><span style="color: rgb(43, 43, 43);font-family: Optima-Regular, PingFangTC-light;font-size: 16px;letter-spacing: 2px;word-spacing: 2px;-webkit-text-stroke-width: 0.05px;text-wrap: wrap;background-color: rgb(255, 255, 255);">2038年01月19日03:14:07</span></strong>吧,正好是著名的2038年漏洞时间,下<span style="color: rgb(43, 43, 43);font-family: Optima-Regular, PingFangTC-light;font-size: 16px;letter-spacing: 2px;word-spacing: 2px;-webkit-text-stroke-width: 0.05px;text-wrap: wrap;background-color: rgb(255, 255, 255);">一秒立即穿越回1901年。</span><img class="rich_pages wxw-img" data-ratio="1" data-w="128" style="display: inline-block;width: 20px;vertical-align: middle;background-size: cover;height: auto !important;" src="/upload/1294ed15d618af41de3829845bf27141.null"></span><span style="-webkit-text-stroke: 0.05px;"></span></p> <section class="mp_profile_iframe_wrp"> <mp-common-profile class="custom_select_card mp_profile_iframe mp_common_widget" data-pluginname="mpprofile" data-id="MzA3Nzg3NjYxOQ==" data-headimg="http://mmbiz.qpic.cn/mmbiz_png/pxopGibm8O52ITYCpDic8meL4dwMv1ChmREtJGDWHsIXvM8ib0BVM6yfYCOf04SgSYoI6b8rUfFdliblxCLKnkiaGTw/0?wx_fmt=png" data-nickname="知彼而知己" data-alias="heu168" data-signature="电脑技术、软件分享" data-from="0"></mp-common-profile> </section> <p style="text-align: center;"><img class="rich_pages wxw-img" data-galleryid="" data-ratio="0.253125" data-type="gif" data-w="640" style="height: auto !important;" src="/upload/4a655b5bbca5bcb9995b6098b405636c.png"></p> <p style="display: none;"> <mp-style-type data-value="3"></mp-style-type></p>

京东终面:ElasticSearch深度分页如何优化?

作者:微信小助手

<section data-tool="mdnice编辑器" data-website="https://www.mdnice.com" style="line-height: 1.6;word-break: break-word;text-align: left;padding: 5px;font-size: 16px;color: rgb(53, 53, 53);word-spacing: 0.8px;letter-spacing: 0.8px;border-radius: 16px;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, &quot;PingFang SC&quot;, Cambria, Cochin, Georgia, Times, &quot;Times New Roman&quot;, serif;"> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;line-height: 1.75;margin-top: 0.8em;margin-bottom: 0.8em;">Elasticsearch 是一个实时的分布式搜索与分析引擎,在使用过程中,有一些典型的使用场景,比如分页、遍历等。</p> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;line-height: 1.75;margin-top: 0.8em;margin-bottom: 0.8em;">在使用关系型数据库中,我们被告知要注意甚至被明确禁止使用深度分页,同理,在 Elasticsearch 中,也应该尽量避免使用深度分页。</p> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;line-height: 1.75;margin-top: 0.8em;margin-bottom: 0.8em;">这篇文章主要介绍 Elasticsearch 中分页相关内容!</p> <h2 data-tool="mdnice编辑器" style="font-weight: bold;color: black;font-size: 22px;margin-top: 20px;margin-right: 10px;"><span style="display: none;"></span><span style="font-size: 18px;color: rgb(234, 84, 41);letter-spacing: 0.5444px;padding-bottom: 10px;border-bottom: 2px solid rgb(234, 84, 41);visibility: visible;">From/Size参数</span></h2> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;line-height: 1.75;margin-top: 0.8em;margin-bottom: 0.8em;">在ES中,分页查询默认返回最顶端的10条匹配hits。</p> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;line-height: 1.75;margin-top: 0.8em;margin-bottom: 0.8em;">如果需要分页,需要使用from和size参数。</p> <ul data-tool="mdnice编辑器" style="margin-top: 8px;margin-bottom: 8px;padding-left: 25px;color: rgb(248, 57, 41);" class="list-paddingleft-1"> <li> <section style="margin-top: 5px;margin-bottom: 5px;line-height: 26px;color: rgb(53, 53, 53);"> from参数定义了需要跳过的hits数,默认为0; </section></li> <li> <section style="margin-top: 5px;margin-bottom: 5px;line-height: 26px;color: rgb(53, 53, 53);"> size参数定义了需要返回的hits数目的最大值。 </section></li> </ul> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;line-height: 1.75;margin-top: 0.8em;margin-bottom: 0.8em;">一个基本的ES查询语句是这样的:</p> <pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block;background: url(&quot;https://mmbiz.qpic.cn/mmbiz_svg/A7sq8BD8oezBibiciavS1eDQetDm5vJkjZsSiaiaSh7QrHNmsESCqZKQfIKUpuPFqsibicnQ2YEdpQFQ3ah0aYQaSticP1uyvRiavGA9o/640?wx_fmt=svg&quot;) 10px 10px / 40px no-repeat rgb(255, 255, 255);height: 30px;width: 100%;margin-bottom: -7px;border-radius: 5px;"></span><code style="overflow-x: auto;padding: 16px;color: black;display: -webkit-box;font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;font-size: 12px;-webkit-overflow-scrolling: touch;padding-top: 15px;background: #fff;border-radius: 5px;">POST&nbsp;/my_index/my_type/_search<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #836C28;line-height: 26px;">"query"</span>:&nbsp;{&nbsp;<span style="color: #836C28;line-height: 26px;">"match_all"</span>:&nbsp;{}},<br>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #836C28;line-height: 26px;">"from"</span>:&nbsp;<span style="color: #1c00cf;line-height: 26px;">100</span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #836C28;line-height: 26px;">"size"</span>:&nbsp;&nbsp;<span style="color: #1c00cf;line-height: 26px;">10</span><br>}<br></code></pre> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;line-height: 1.75;margin-top: 0.8em;margin-bottom: 0.8em;">上面的查询表示从搜索结果中取第100条开始的10条数据。</p> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;line-height: 1.75;margin-top: 0.8em;margin-bottom: 0.8em;"><span style="font-weight: 700;color: rgb(248, 57, 41);">那么,这个查询语句在ES集群内部是怎么执行的呢?</span></p> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;line-height: 1.75;margin-top: 0.8em;margin-bottom: 0.8em;">在ES中,搜索一般包括两个阶段,query 和 fetch 阶段,可以简单的理解,query 阶段确定要取哪些doc,fetch 阶段取出具体的 doc。</p> <blockquote data-tool="mdnice编辑器" style="border-top: none;border-right: none;border-bottom: none;font-size: 0.9em;overflow: auto;color: rgb(106, 115, 125);border-left-width: 2px;padding: 8px 10px 8px 15px;background: rgb(255, 249, 249);border-left-color: rgb(239, 112, 96);margin-top: 0px;margin-bottom: 20px;letter-spacing: 0.5444px;"> <p style="padding-top: 8px;padding-bottom: 8px;line-height: 26px;color: rgb(53, 53, 53);font-size: 16px;margin-right: 10px;margin-left: 10px;">Query阶段</p> </blockquote> <figure data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;display: flex;flex-direction: column;justify-content: center;align-items: center;border-radius: 16px;overflow: hidden;"> <img class="rich_pages wxw-img" data-ratio="0.6985294117647058" data-w="544" style="border-radius: 6px;display: block;margin: 20px auto;max-width: 95%;object-fit: contain;height: auto !important;" src="/upload/aa31286b51b72896c3391a50404a7101.png"> </figure> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;line-height: 1.75;margin-top: 0.8em;margin-bottom: 0.8em;">如上图所示,描述了一次搜索请求的 query 阶段:·</p> <ol data-tool="mdnice编辑器" style="margin-top: 8px;margin-bottom: 8px;padding-left: 25px;color: rgb(248, 57, 41);" class="list-paddingleft-1"> <li> <section style="margin-top: 5px;margin-bottom: 5px;line-height: 26px;color: rgb(53, 53, 53);"> Client 发送一次搜索请求,node1 接收到请求,然后,node1 创建一个大小为 <code style="font-size: 14px;padding: 2px 4px;border-radius: 4px;margin-right: 2px;margin-left: 2px;background-color: rgba(27, 31, 35, 0.05);font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace;word-break: break-all;color: rgb(255, 93, 108);">from + size</code>的优先级队列用来存结果,我们管 node1 叫 coordinating node。 </section></li> <li> <section style="margin-top: 5px;margin-bottom: 5px;line-height: 26px;color: rgb(53, 53, 53);"> coordinating node将请求广播到涉及到的 shards,每个 shard 在内部执行搜索请求,然后,将结果存到内部的大小同样为 <code style="font-size: 14px;padding: 2px 4px;border-radius: 4px;margin-right: 2px;margin-left: 2px;background-color: rgba(27, 31, 35, 0.05);font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace;word-break: break-all;color: rgb(255, 93, 108);">from + size</code> 的优先级队列里,可以把优先级队列理解为一个包含 <code style="font-size: 14px;padding: 2px 4px;border-radius: 4px;margin-right: 2px;margin-left: 2px;background-color: rgba(27, 31, 35, 0.05);font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace;word-break: break-all;color: rgb(255, 93, 108);">top N</code>结果的列表。 </section></li> <li> <section style="margin-top: 5px;margin-bottom: 5px;line-height: 26px;color: rgb(53, 53, 53);"> 每个 shard 把暂存在自身优先级队列里的数据返回给 coordinating node,coordinating node 拿到各个 shards 返回的结果后对结果进行一次合并,产生一个全局的优先级队列,存到自身的优先级队列里。 </section></li> </ol> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;line-height: 1.75;margin-top: 0.8em;margin-bottom: 0.8em;">在上面的例子中,coordinating node 拿到<code style="font-size: 14px;padding: 2px 4px;border-radius: 4px;margin-right: 2px;margin-left: 2px;background-color: rgba(27, 31, 35, 0.05);font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace;word-break: break-all;color: rgb(255, 93, 108);">(from + size) * 6</code>条数据,然后合并并排序后选择前面的<code style="font-size: 14px;padding: 2px 4px;border-radius: 4px;margin-right: 2px;margin-left: 2px;background-color: rgba(27, 31, 35, 0.05);font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace;word-break: break-all;color: rgb(255, 93, 108);">from + size</code>条数据存到优先级队列,以便 fetch 阶段使用。</p> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;line-height: 1.75;margin-top: 0.8em;margin-bottom: 0.8em;">另外,各个分片返回给 coordinating node 的数据用于选出前<code style="font-size: 14px;padding: 2px 4px;border-radius: 4px;margin-right: 2px;margin-left: 2px;background-color: rgba(27, 31, 35, 0.05);font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace;word-break: break-all;color: rgb(255, 93, 108);">from + size</code>条数据,所以,只需要返回唯一标记 doc 的<code style="font-size: 14px;padding: 2px 4px;border-radius: 4px;margin-right: 2px;margin-left: 2px;background-color: rgba(27, 31, 35, 0.05);font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace;word-break: break-all;color: rgb(255, 93, 108);">_id</code>以及用于排序的<code style="font-size: 14px;padding: 2px 4px;border-radius: 4px;margin-right: 2px;margin-left: 2px;background-color: rgba(27, 31, 35, 0.05);font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace;word-break: break-all;color: rgb(255, 93, 108);">_score</code>即可,这样也可以保证返回的数据量足够小。</p> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;line-height: 1.75;margin-top: 0.8em;margin-bottom: 0.8em;">coordinating node 计算好自己的优先级队列后,query 阶段结束,进入 fetch 阶段。</p> <blockquote data-tool="mdnice编辑器" style="border-top: none;border-right: none;border-bottom: none;font-size: 0.9em;overflow: auto;color: rgb(106, 115, 125);border-left-width: 2px;padding: 8px 10px 8px 15px;background: rgb(255, 249, 249);border-left-color: rgb(239, 112, 96);margin-top: 0px;margin-bottom: 20px;letter-spacing: 0.5444px;"> <p style="padding-top: 8px;padding-bottom: 8px;line-height: 26px;color: rgb(53, 53, 53);font-size: 16px;margin-right: 10px;margin-left: 10px;">Fetch阶段</p> </blockquote> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;line-height: 1.75;margin-top: 0.8em;margin-bottom: 0.8em;">query 阶段知道了要取哪些数据,但是并没有取具体的数据,这就是 fetch 阶段要做的。</p> <figure data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;display: flex;flex-direction: column;justify-content: center;align-items: center;border-radius: 16px;overflow: hidden;"> <img class="rich_pages wxw-img" data-ratio="0.7016574585635359" data-w="543" style="border-radius: 6px;display: block;margin: 20px auto;max-width: 95%;object-fit: contain;height: auto !important;" src="/upload/58734840461fd1073a84abeddca28000.png"> </figure> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;line-height: 1.75;margin-top: 0.8em;margin-bottom: 0.8em;">上图展示了 fetch 过程:</p> <ol data-tool="mdnice编辑器" style="margin-top: 8px;margin-bottom: 8px;padding-left: 25px;color: rgb(248, 57, 41);" class="list-paddingleft-1"> <li> <section style="margin-top: 5px;margin-bottom: 5px;line-height: 26px;color: rgb(53, 53, 53);"> coordinating node 发送 GET 请求到相关shards。 </section></li> <li> <section style="margin-top: 5px;margin-bottom: 5px;line-height: 26px;color: rgb(53, 53, 53);"> shard 根据 doc 的 <code style="font-size: 14px;padding: 2px 4px;border-radius: 4px;margin-right: 2px;margin-left: 2px;background-color: rgba(27, 31, 35, 0.05);font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace;word-break: break-all;color: rgb(255, 93, 108);">_id</code>取到数据详情,然后返回给 coordinating node。 </section></li> <li> <section style="margin-top: 5px;margin-bottom: 5px;line-height: 26px;color: rgb(53, 53, 53);"> coordinating node 返回数据给 Client。 </section></li> </ol> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;line-height: 1.75;margin-top: 0.8em;margin-bottom: 0.8em;">coordinating node 的优先级队列里有<code style="font-size: 14px;padding: 2px 4px;border-radius: 4px;margin-right: 2px;margin-left: 2px;background-color: rgba(27, 31, 35, 0.05);font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace;word-break: break-all;color: rgb(255, 93, 108);">from + size</code> 个<code style="font-size: 14px;padding: 2px 4px;border-radius: 4px;margin-right: 2px;margin-left: 2px;background-color: rgba(27, 31, 35, 0.05);font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace;word-break: break-all;color: rgb(255, 93, 108);">_doc _id</code>,但是,在 fetch 阶段,并不需要取回所有数据,在上面的例子中,前100条数据是不需要取的,只需要取优先级队列里的第101到110条数据即可。</p> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;line-height: 1.75;margin-top: 0.8em;margin-bottom: 0.8em;">需要取的数据可能在不同分片,也可能在同一分片,coordinating node 使用 <span style="font-weight: 700;color: rgb(248, 57, 41);">multi-get</span> 来避免多次去同一分片取数据,从而提高性能。</p> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;line-height: 1.75;margin-top: 0.8em;margin-bottom: 0.8em;"><span style="font-weight: 700;color: rgb(248, 57, 41);">这种方式请求深度分页是有问题的:</span></p> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;line-height: 1.75;margin-top: 0.8em;margin-bottom: 0.8em;">我们可以假设在一个有 5 个主分片的索引中搜索。当我们请求结果的第一页(结果从 1 到 10 ),每一个分片产生前 10 的结果,并且返回给 <span style="font-weight: 700;color: rgb(248, 57, 41);">协调节点</span> ,协调节点对 50 个结果排序得到全部结果的前 10 个。</p> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;line-height: 1.75;margin-top: 0.8em;margin-bottom: 0.8em;">现在假设我们请求第 1000 页—结果从 10001 到 10010 。所有都以相同的方式工作除了每个分片不得不产生前10010个结果以外。然后协调节点对全部 50050 个结果排序最后丢弃掉这些结果中的 50040 个结果。</p> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;line-height: 1.75;margin-top: 0.8em;margin-bottom: 0.8em;"><span style="font-weight: 700;color: rgb(248, 57, 41);">对结果排序的成本随分页的深度成指数上升。</span></p> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;line-height: 1.75;margin-top: 0.8em;margin-bottom: 0.8em;"><span style="font-weight: 700;color: rgb(248, 57, 41);">注意1:</span></p> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;line-height: 1.75;margin-top: 0.8em;margin-bottom: 0.8em;">size的大小不能超过<code style="font-size: 14px;padding: 2px 4px;border-radius: 4px;margin-right: 2px;margin-left: 2px;background-color: rgba(27, 31, 35, 0.05);font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace;word-break: break-all;color: rgb(255, 93, 108);">index.max_result_window</code>这个参数的设置,默认为10000。</p> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;line-height: 1.75;margin-top: 0.8em;margin-bottom: 0.8em;">如果搜索size大于10000,需要设置<code style="font-size: 14px;padding: 2px 4px;border-radius: 4px;margin-right: 2px;margin-left: 2px;background-color: rgba(27, 31, 35, 0.05);font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace;word-break: break-all;color: rgb(255, 93, 108);">i

MyBatis 动态 SQL 最全教程,这样写 SQL 太爽了!

作者:微信小助手

<section data-tool="mdnice编辑器" data-website="https://www.mdnice.com" data-mpa-powered-by="yiban.io" style="padding-right: 10px;padding-left: 10px;outline: 0px;text-wrap: wrap;background-color: rgb(255, 255, 255);font-size: 16px;color: black;line-height: 1.6;letter-spacing: 0px;word-break: break-word;text-align: left;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, &quot;PingFang SC&quot;, Cambria, Cochin, Georgia, Times, &quot;Times New Roman&quot;, serif;visibility: visible;scroll-behavior: auto !important;"> <h2 data-tool="mdnice编辑器" style="margin: 1em auto;padding-left: 10px;outline: 0px;font-weight: bold;font-size: 22px;color: rgb(0, 150, 136);border-left: 3px solid rgb(0, 150, 136);visibility: visible;scroll-behavior: auto !important;">一、MyBatis动态 sql 是什么</h2> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;outline: 0px;line-height: 26px;text-align: justify;visibility: visible;scroll-behavior: auto !important;">动态 SQL 是 MyBatis 的强大特性之一。在 JDBC 或其它类似的框架中,开发人员通常需要手动拼接 SQL 语句。根据不同的条件拼接 SQL 语句是一件极其痛苦的工作。例如,拼接时要确保添加了必要的空格,还要注意去掉列表最后一个列名的逗号。而动态 SQL 恰好解决了这一问题,可以根据场景动态的构建查询。</p> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;outline: 0px;line-height: 26px;text-align: justify;visibility: visible;scroll-behavior: auto !important;">动态SQL(code that is executed dynamically),它一般是根据用户输入或外部条件动态组合的SQL语句块。动态SQL能灵活的发挥SQL强大的功能、方便的解决一些其它方法难以解决的问题。相信使用过动态SQL的人都能体会到它带来的便利,然而动态SQL有时候在执行性能 (效率)上面不如静态SQL,而且使用不恰当,往往会在安全方面存在隐患 (SQL 注入式攻击)。</p> <h3 data-tool="mdnice编辑器" style="margin: 0.6em auto;padding-left: 10px;outline: 0px;font-weight: bold;font-size: 20px;border-left: 2px solid rgb(0, 150, 136);visibility: visible;scroll-behavior: auto !important;">1.Mybatis 动态 sql 是做什么的?</h3> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;outline: 0px;line-height: 26px;text-align: justify;visibility: visible;scroll-behavior: auto !important;">Mybatis 动态 sql 可以让我们在 Xml 映射文件内,以标签的形式编写动态 sql,完成逻辑判断和动态拼接 sql 的功能。</p> <h3 data-tool="mdnice编辑器" style="margin: 0.6em auto;padding-left: 10px;outline: 0px;font-weight: bold;font-size: 20px;border-left: 2px solid rgb(0, 150, 136);visibility: visible;scroll-behavior: auto !important;">2.Mybatis 的 9 种 动 态 sql 标 签有哪些?</h3> <figure data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;outline: 0px;display: flex;flex-direction: column;justify-content: center;align-items: center;visibility: visible;scroll-behavior: auto !important;"> <img class="rich_pages wxw-img" src="/upload/619dbe7242626203e9d9ec515bd52477.png" data-cropx1="0" data-cropx2="499" data-cropy1="0" data-cropy2="262" data-ratio="0.5250501002004008" src="https://mmbiz.qpic.cn/sz_mmbiz_jpg/eZzl4LXykQw893o2Ryk4f0A01x5RAJlg7yhzduS0exFoNpLk6WL8AdedgunAfMEauuL6SYWsWRQHme0He7eCWA/640?wx_fmt=jpeg" data-type="jpeg" data-w="499" style="margin-right: auto;margin-left: auto;outline: 0px;display: block;scroll-behavior: auto !important;visibility: visible !important;width: 499px;height: 262px;"> </figure> <h3 data-tool="mdnice编辑器" style="margin: 0.6em auto;padding-left: 10px;outline: 0px;font-weight: bold;font-size: 20px;border-left: 2px solid rgb(0, 150, 136);visibility: visible;scroll-behavior: auto !important;">3.动态 sql 的执行原理?</h3> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;outline: 0px;line-height: 26px;text-align: justify;scroll-behavior: auto !important;">原理为:使用 OGNL 从 sql 参数对象中计算表达式的值,根据表达式的值动态拼接 sql,以此来完成动态 sql 的功能。</p> <h2 data-tool="mdnice编辑器" style="margin: 1em auto;padding-left: 10px;outline: 0px;font-weight: bold;font-size: 22px;color: rgb(0, 150, 136);border-left: 3px solid rgb(0, 150, 136);scroll-behavior: auto !important;">二、MyBatis标签</h2> <h3 data-tool="mdnice编辑器" style="margin: 0.6em auto;padding-left: 10px;outline: 0px;font-weight: bold;font-size: 20px;border-left: 2px solid rgb(0, 150, 136);scroll-behavior: auto !important;">1.if标签:条件判断</h3> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;outline: 0px;line-height: 26px;text-align: justify;scroll-behavior: auto !important;">MyBatis if 类似于 Java 中的 if 语句,是 MyBatis 中最常用的判断语句。使用 if 标签可以节省许多拼接 SQL 的工作,把精力集中在 XML 的维护上。</p> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;outline: 0px;line-height: 26px;text-align: justify;scroll-behavior: auto !important;"><strong style="outline: 0px;scroll-behavior: auto !important;">1)不使用动态sql</strong></p> <pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;outline: 0px;border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;scroll-behavior: auto !important;"><span style="margin-bottom: -7px;outline: 0px;display: block;background: url(&quot;https://mmbiz.qpic.cn/mmbiz_svg/GPyw0pGicibl5sEj4cuKR5asffAcRoIbjtLSlth4jWWibIvd7jA9srffR3cLqiaibT3ibSyCxwqXqSyictzYzriciavr5N2ibW6TsnPyza/640?wx_fmt=svg&quot;) 10px 10px / 40px no-repeat rgb(40, 44, 52);height: 30px;width: 657px;border-radius: 5px;scroll-behavior: auto !important;"></span><code style="padding: 15px 16px 16px;outline: 0px;overflow-x: auto;color: rgb(171, 178, 191);display: -webkit-box;font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace;font-size: 12px;background: rgb(40, 44, 52);border-radius: 5px;scroll-behavior: auto !important;"><span style="outline: 0px;line-height: 26px;scroll-behavior: auto !important;">&lt;<span style="outline: 0px;color: rgb(224, 108, 117);line-height: 26px;scroll-behavior: auto !important;">select</span>&nbsp;<span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;scroll-behavior: auto !important;">id</span>=<span style="outline: 0px;color: rgb(152, 195, 121);line-height: 26px;scroll-behavior: auto !important;">"selectUserByUsernameAndSex"</span><br style="outline: 0px;scroll-behavior: auto !important;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;scroll-behavior: auto !important;">resultType</span>=<span style="outline: 0px;color: rgb(152, 195, 121);line-height: 26px;scroll-behavior: auto !important;">"user"</span>&nbsp;<span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;scroll-behavior: auto !important;">parameterType</span>=<span style="outline: 0px;color: rgb(152, 195, 121);line-height: 26px;scroll-behavior: auto !important;">"com.ys.po.User"</span>&gt;</span><br style="outline: 0px;scroll-behavior: auto !important;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="outline: 0px;color: rgb(92, 99, 112);font-style: italic;line-height: 26px;scroll-behavior: auto !important;">&lt;!--&nbsp;这里和普通的sql&nbsp;查询语句差不多,对于只有一个参数,后面的&nbsp;#{id}表示占位符,里面&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;不一定要写id,<br style="outline: 0px;scroll-behavior: auto !important;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;写啥都可以,但是不要空着,如果有多个参数则必须写pojo类里面的属性&nbsp;--&gt;</span><br style="outline: 0px;scroll-behavior: auto !important;">&nbsp;&nbsp;&nbsp;&nbsp;select&nbsp;*&nbsp;from&nbsp;user&nbsp;where&nbsp;username=#{username}&nbsp;and&nbsp;sex=#{sex}<br style="outline: 0px;scroll-behavior: auto !important;"><span style="outline: 0px;line-height: 26px;scroll-behavior: auto !important;">&lt;/<span style="outline: 0px;color: rgb(224, 108, 117);line-height: 26px;scroll-behavior: auto !important;">select</span>&gt;</span><br style="outline: 0px;scroll-behavior: auto !important;"></code></pre> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;outline: 0px;line-height: 26px;text-align: justify;scroll-behavior: auto !important;">if 语句使用方法简单,常常与 test 属性联合使用。语法如下:</p> <pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;outline: 0px;border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;scroll-behavior: auto !important;"><span style="margin-bottom: -7px;outline: 0px;display: block;background: url(&quot;https://mmbiz.qpic.cn/mmbiz_svg/GPyw0pGicibl5sEj4cuKR5asffAcRoIbjtLSlth4jWWibIvd7jA9srffR3cLqiaibT3ibSyCxwqXqSyictzYzriciavr5N2ibW6TsnPyza/640?wx_fmt=svg&quot;) 10px 10px / 40px no-repeat rgb(40, 44, 52);height: 30px;width: 657px;border-radius: 5px;scroll-behavior: auto !important;"></span><code style="padding: 15px 16px 16px;outline: 0px;overflow-x: auto;color: rgb(171, 178, 191);display: -webkit-box;font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace;font-size: 12px;background: rgb(40, 44, 52);border-radius: 5px;scroll-behavior: auto !important;">&lt;<span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;scroll-behavior: auto !important;">if</span>&nbsp;<span style="outline: 0px;color: rgb(230, 192, 123);line-height: 26px;scroll-behavior: auto !important;">test</span>=<span style="outline: 0px;color: rgb(152, 195, 121);line-height: 26px;scroll-behavior: auto !important;">"判断条件"</span>&gt;&nbsp;&nbsp;&nbsp;&nbsp;SQL语句&lt;/<span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;scroll-behavior: auto !important;">if</span>&gt;<br style="outline: 0px;scroll-behavior: auto !important;"></code></pre> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;outline: 0px;line-height: 26px;text-align: justify;scroll-behavior: auto !important;"><strong style="outline: 0px;scroll-behavior: auto !important;">2)使用动态sql</strong></p> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;outline: 0px;line-height: 26px;text-align: justify;scroll-behavior: auto !important;">上面的查询语句,我们可以发现,如果&nbsp;<code style="margin-right: 2px;margin-left: 2px;padding: 2px 4px;outline: 0px;font-size: 14px;border-radius: 4px;background-color: rgba(27, 31, 35, 0.05);font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace;word-break: break-all;color: rgb(0, 150, 136);scroll-behavior: auto !important;">#{username}</code>&nbsp;为空,那么查询结果也是空,如何解决这个问题呢?使用 if 来判断,可多个 if 语句同时使用。</p> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;outline: 0px;line-height: 26px;text-align: justify;scroll-behavior: auto !important;">以下语句表示为可以按照网站名称(name)或者网址(url)进行模糊查询。如果您不输入名称或网址,则返回所有的网站记录。但是,如果你传递了任意一个参数,它就会返回与给定参数相匹配的记录。</p> <pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;outline: 0px;border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;scroll-behavior: auto !important;"><span style="margin-bottom: -7px;outline: 0px;display: block;background: url(&quot;https://mmbiz.qpic.cn/mmbiz_svg/GPyw0pGicibl5sEj4cuKR5asffAcRoIbjtLSlth4jWWibIvd7jA9srffR3cLqiaibT3ibSyCxwqXqSyictzYzriciavr5N2ibW6TsnPyza/640?wx_fmt=svg&quot;) 10px 10px / 40px no-repeat rgb(40, 44, 52);height: 30px;width: 657px;border-radius: 5px;scroll-behavior: auto !important;"></span><code style="padding: 15px 16px 16px;outline: 0px;overflow-x: auto;color: rgb(171, 178, 191);display: -webkit-box;font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace;font-size: 12px;background: rgb(40, 44, 52);border-radius: 5px;scroll-behavior: auto !important;"><span style="outline: 0px;line-height: 26px;scroll-behavior: auto !important;">&lt;<span style="outline: 0px;color: rgb(224, 108, 117);line-height: 26px;scroll-behavior: auto !important;">select</span>&nbsp;<span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;scroll-behavior: auto !important;">id</span>=<span style="outline: 0px;color: rgb(152, 195, 121);line-height: 26px;scroll-behavior: auto !important;">"selectAllWebsite"</span>&nbsp;<span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;scroll-behavior: auto !important;">resultMap</span>=<span style="outline: 0px;color: rgb(152, 195, 121);line-height: 26px;scroll-behavior: auto !important;">"myResult"</span>&gt;</span>&nbsp;&nbsp;<br style="outline: 0px;scroll-behavior: auto !important;">&nbsp;&nbsp;&nbsp;&nbsp;select&nbsp;id,name,url&nbsp;from&nbsp;website&nbsp;<br style="outline: 0px;scroll-behavior: auto !important;">&nbsp;&nbsp;&nbsp;&nbsp;where&nbsp;1=1&nbsp;&nbsp;&nbsp;&nbsp;<br style="outline: 0px;scroll-behavior: auto !important;">&nbsp;&nbsp;&nbsp;<span style="outline: 0px;line-height: 26px;scroll-behavior: auto !important;">&lt;<span style="outline: 0px;color: rgb(224, 108, 117);line-height: 26px;scroll-behavior: auto !important;">if</span>&nbsp;<span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;scroll-behavior: auto !important;">test</span>=<span style="outline: 0px;color: rgb(152, 195, 121);line-height: 26px;scroll-behavior: auto !important;">"name&nbsp;!=&nbsp;null"</span>&gt;</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br style="outline: 0px;scroll-behavior: auto !important;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AND&nbsp;name&nbsp;like&nbsp;#{name}&nbsp;&nbsp;&nbsp;<br style="outline: 0px;scroll-behavior: auto !important;">&nbsp;&nbsp;&nbsp;<span style="outline: 0px;line-height: 26px;scroll-behavior: auto !important;">&lt;/<span style="outline: 0px;color: rgb(224, 108, 117);line-height: 26px;scroll-behavior: auto !important;">if</span>&gt;</span>&nbsp;&nbsp;&nbsp;&nbsp;<br style="outline: 0px;scroll-behavior: auto !important;">&nbsp;&nbsp;&nbsp;<span style="outline: 0px;line-height: 26px;scroll-behavior: auto !important;">&lt;<span style="outline: 0px;color: rgb(224, 108, 117);line-height: 26px;scroll-behavior: auto !important;">if</span>&nbsp;<span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;scroll-behavior: auto !important;">test</span>=<span style="outline: 0px;color: rgb(152, 195, 121);line-height: 26px;scroll-behavior: auto !important;">"url!=&nbsp;null"</span>&gt;</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br style="outline: 0px;scroll-behavior: auto !important;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AND&nbsp;url&nbsp;like&nbsp;#{url}&nbsp;&nbsp;&nbsp;&nbsp;<br style="outline: 0px;scroll-behavior: auto !important;">&nbsp;&nbsp;&nbsp;<span style="outline: 0px;line-height: 26px;scroll-behavior: auto !important;">&lt;/<span style="outline: 0px;color: rgb(224, 108, 117);line-height: 26px;scroll-behavior: auto !important;">if</span>&gt;</span><br style="outline: 0px;scroll-behavior: auto !important;"><span style="outline: 0px;line-height: 26px;scroll-behavior: auto !important;">&lt;/<span style="outline: 0px;color: rgb(224, 108, 117);line-height: 26px;scroll-behavior: auto !important;">select</span>&gt;</span><br style="outline: 0px;scroll-behavior: auto !important;"></code></pre> <h3 data-tool="mdnice编辑器" style="margin: 0.6em auto;padding-left: 10px;outline: 0px;font-weight: bold;font-size: 20px;border-left: 2px solid rgb(0, 150, 136);scroll-behavior: auto !important;">2.where+if标签</h3> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;outline: 0px;line-height: 26px;text-align: justify;scroll-behavior: auto !important;">where、if同时使用可以进行查询、模糊查询</p> <blockquote data-tool="mdnice编辑器" style="margin-top: 20px;margin-bottom: 20px;padding: 10px 10px 10px 1em;outline: 0px;border-left-width: 2px;border-left-color: rgb(136, 136, 136);color: rgb(119, 119, 119);font-size: 0.9em;border-top: none;border-bottom: none;overflow: auto;background: rgba(0, 0, 0, 0.05);border-right: 2px solid rgb(136, 136, 136);scroll-behavior: auto !important;"> <p style="padding-top: 8px;padding-bottom: 8px;outline: 0px;font-size: 16px;text-align: justify;color: black;line-height: 26px;scroll-behavior: auto !important;">注意,<code style="margin-right: 2px;margin-left: 2px;padding: 2px 4px;outline: 0px;font-size: 14px;border-radius: 4px;background-color: rgba(27, 31, 35, 0.05);font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace;word-break: break-all;color: rgb(0, 150, 136);scroll-behavior: auto !important;">&lt;if&gt;</code>失败后,&nbsp;<code style="margin-right: 2px;margin-left: 2px;padding: 2px 4px;outline: 0px;font-size: 14px;border-radius: 4px;background-color: rgba(27, 31, 35, 0.05);font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace;word-break: break-all;color: rgb(0, 150, 136);scroll-behavior: auto !important;">&lt;where&gt;</code>&nbsp;关键字只会去掉库表字段赋值前面的and,不会去掉语句后面的and关键字,即注意,<code style="margin-right: 2px;margin-left: 2px;padding: 2px 4px;outline: 0px;font-size: 14px;border-radius: 4px;background-color: rgba(27, 31, 35, 0.05);font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace;word-break: break-all;color: rgb(0, 150, 136);scroll-behavior: auto !important;">&lt;where&gt;</code>&nbsp;只会去掉<code style="margin-right: 2px;margin-left: 2px;padding: 2px 4px;outline: 0px;font-size: 14px;border-radius: 4px;background-color: rgba(27, 31, 35, 0.05);font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace;word-break: break-all;color: rgb(0, 150, 136);scroll-behavior: auto !important;">&lt;if&gt;</code>&nbsp;语句中的最开始的and关键字。所以下面的形式是不可取的</p> </blockquote> <pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;outline: 0px;border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;scroll-behavior: auto !important;"><span style="margin-bottom: -7px;outline: 0px;display: block;background: url(&quot;https://mmbiz.qpic.cn/mmbiz_svg/GPyw0pGicibl5sEj4cuKR5asffAcRoIbjtLSlth4jWWibIvd7jA9srffR3cLqiaibT3ibSyCxwqXqSyictzYzriciavr5N2ibW6TsnPyza/640?wx_fmt=svg&quot;) 10px 10px / 40px no-repeat rgb(40, 44, 52);height: 30px;width: 657px;border-radius: 5px;scroll-behavior: auto !important;"></span><code style="padding: 15px 16px 16px;outline: 0px;overflow-x: auto;color: rgb(171, 178, 191);display: -webkit-box;font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace;font-size: 12px;background: rgb(40, 44, 52);border-radius: 5px;scroll-behavior: auto !important;"><span style="outline: 0px;line-height: 26px;scroll-behavior: auto !important;">&lt;<span style="outline: 0px;color: rgb(224, 108, 117);line-height: 26px;scroll-behavior: auto !important;">select</span>&nbsp;<span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;scroll-behavior: auto !important;">id</span>=<span style="outline: 0px;color: rgb(152, 195, 121);line-height: 26px;scroll-behavior: auto !important;">"findQuery"</span>&nbsp;<span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;scroll-behavior: auto !important;">resultType</span>=<span style="outline: 0px;color: rgb(152, 195, 121);line-height: 26px;scroll-behavior: auto !important;">"Student"</span>&gt;</span><br style="outline: 0px;scroll-behavior: auto !important;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="outline: 0px;line-height: 26px;scroll-behavior: auto !important;">&lt;<span style="outline: 0px;color: rgb(224, 108, 117);line-height: 26px;scroll-behavior: auto !important;">include</span>&nbsp;<span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;scroll-behavior: auto !important;">refid</span>=<span style="outline: 0px;color: rgb(152, 195, 121);line-height: 26px;scroll-behavior: auto !important;">"selectvp"</span>/&gt;</span><br style="outline: 0px;scroll-behavior: auto !important;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="outline: 0px;line-height: 26px;scroll-behavior: auto !important;">&lt;<span style="outline: 0px;color: rgb(224, 108, 117);line-height: 26px;scroll-behavior: auto !important;">where</span>&gt;</span><br style="outline: 0px;scroll-behavior: auto !important;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="outline: 0px;line-height: 26px;scroll-behavior: auto !important;">&lt;<span style="outline: 0px;color: rgb(224, 108, 117);line-height: 26px;scroll-behavior: auto !important;">if</span>&nbsp;<span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;scroll-behavior: auto !important;">test</span>=<span style="outline: 0px;color: rgb(152, 195, 121);line-height: 26px;scroll-behavior: auto !important;">"sacc&nbsp;!=&nbsp;null"</span>&gt;</span><br style="outline: 0px;scroll-behavior: auto !important;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sacc&nbsp;like&nbsp;concat('%'&nbsp;#{sacc}&nbsp;'%')<br style="outline: 0px;scroll-behavior: auto !important;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="outline: 0px;line-height: 26px;scroll-behavior: auto !important;">&lt;/<span style="outline: 0px;color: rgb(224, 108, 117);line-height: 26px;scroll-behavior: auto !important;">if</span>&gt;</span><br style="outline: 0px;scroll-behavior: auto !important;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="outline: 0px;line-height: 26px;scroll-behavior: auto !important;">&lt;<span style="outline: 0px;color: rgb(224, 108, 117);line-height: 26px;scroll-behavior: auto !important;">if</span>&nbsp;<span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;scroll-behavior: auto !important;">test</span>=<span style="outline: 0px;color: rgb(152, 195, 121);line-height: 26px;scroll-behavior: auto !important;">"sname&nbsp;!=&nbsp;null"</span>&gt;</span><br style="outline: 0px;scroll-behavior: auto !important;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AND&nbsp;sname&nbsp;like&nbsp;concat('%'&nbsp;#{sname}&nbsp;'%')<br style="outline: 0px;scroll-behavior: auto !important;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="outline: 0px;line-height: 26px;scroll-behavior: auto !important;">&lt;/<span style="outline: 0px;color: rgb(224, 108, 117);line-height: 26px;scroll-behavior: auto !important;">if</span>&gt;</span><br style="outline: 0px;scroll-behavior: auto !important;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="outline: 0px;line-height: 26px;scroll-behavior: auto !important;">&lt;<span style="outline: 0px;color: rgb(224, 108, 117);line-height: 26px;scroll-behavior: auto !important;">if</span>&nbsp;<span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;scroll-behavior: auto !important;">test</span>=<span style="outline: 0px;color: rgb(152, 195, 121);line-height: 26px;scroll-behavior: auto !important;">"sex&nbsp;!=&nbsp;null"</span>&gt;</span><br style="outline: 0px;scroll-behavior: auto !important;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AND&nbsp;sex=#{sex}<br style="outline: 0px;scroll-behavior: auto !important;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="outline: 0px;line-height: 26px;scroll-behavior: auto !important;">&lt;/<span style="outline: 0px;color: rgb(224, 108, 117);line-height: 26px;scroll-behavior: auto !important;">if</span>&gt;</span><br style="outline: 0px;scroll-behavior: auto !important;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="outline: 0px;line-height: 26px;scroll-behavior: auto !important;">&lt;<span style="outline: 0px;color: rgb(224, 108, 117);line-height: 26px;scroll-behavior: auto !important;">if</span>&nbsp;<span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;scroll-behavior: auto !important;">test</span>=<span style="outline: 0px;color: rgb(152, 195, 121);line-height: 26px;scroll-behavior: auto !important;">"phone&nbsp;!=&nbsp;null"</span>&gt;</span><br style="outline: 0px;scroll-behavior: auto !important;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AND&nbsp;phone=#{phone}<br style="outline: 0px;scroll-behavior: auto !important;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="outline: 0px;line-height: 26px;scroll-behavior: auto !important;">&lt;/<span style="outline: 0px;color: rgb(224, 108, 117);line-height: 26px;scroll-behavior: auto !important;">if</span>&gt;</span><br style="outline: 0px;scroll-behavior: auto !important;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="outline: 0px;line-height: 26px;scroll-behavior: auto !important;">&lt;/<span style="outline: 0px;color: rgb(224, 108, 117);line-height: 26px;scroll-behavior: auto !important;">where</span>&gt;</span><br style="outline: 0px;scroll-behavior: auto !important;"><span style="outline: 0px;line-height: 26px;scroll-behavior: auto !important;">&lt;/<span style="outline: 0px;color: rgb(224, 108, 117);line-height: 26px;scroll-behavior: auto !important;">select</span>&gt;</span><br style="outline: 0px;scroll-behavior: auto !important;"></code></pre> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;outline: 0px;line-height: 26px;text-align: justify;scroll-behavior: auto !important;">这个“where”标签会知道如果它包含的标签中有返回值的话,它就插入一个‘where’。此外,如果标签返回的内容是以AND 或OR 开头的,则它会剔除掉。</p> <h3 data-tool="mdnice编辑器" style="margin: 0.6em auto;padding-left: 10px;outline: 0px;font-weight: bold;font-size: 20px;border-left: 2px solid rgb(0, 150, 136);scroll-behavior: auto !important;">3.set标签</h3> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;outline: 0px;line-height: 26px;text-align: justify;scroll-behavior: auto !important;">set可以用来修改</p> <pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;outline: 0px;border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;scroll-behavior: auto !important;"><span style="margin-bottom: -7px;outline: 0px;display: block;background: url(&quot;https://mmbiz.qpic.cn/mmbiz_svg/GPyw0pGicibl5sEj4cuKR5asffAcRoIbjtLSlth4jWWibIvd7jA9srffR3cLqiaibT3ibSyCxwqXqSyictzYzriciavr5N2ibW6TsnPyza/640?wx_fmt=svg&quot;) 10px 10px / 40px no-repeat rgb(40, 44, 52);height: 30px;width: 657px;border-radius: 5px;scroll-behavior: auto !important;"></span><code style="padding: 15px 16px 16px;outline: 0px;overflow-x: auto;color: rgb(171, 178, 191);display: -webkit-box;font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace;font-size: 12px;background: rgb(40, 44, 52);border-radius: 5px;scroll-behavior: auto !important;"><span style="outline: 0px;line-height: 26px;scroll-behavior: auto !important;">&lt;<span style="outline: 0px;color: rgb(224, 108, 117);line-height: 26px;scroll-behavior: auto !important;">update</span>&nbsp;<span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;scroll-behavior: auto !important;">id</span>=<span style="outline: 0px;color: rgb(152, 195, 121);line-height: 26px;scroll-behavior: auto !important;">"upd"</span>&gt;</span><br style="outline: 0px;scroll-behavior: auto !important;">&nbsp;&nbsp;&nbsp;&nbsp;update&nbsp;student<br style="outline: 0px;scroll-behavior: auto !important;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="outline: 0px;line-height: 26px;scroll-behavior: auto !important;">&lt;<span style="outline: 0px;color: rgb(224, 108, 117);line-height: 26px;scroll-behavior: auto !important;">set</span>&gt;</span><br style="outline: 0px;scroll-behavior: auto !important;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="outline: 0px;line-height: 26px;scroll-behavior: auto !important;">&lt;<span style="outline: 0px;color: rgb(224, 108, 117);line-height: 26px;scroll-behavior: auto !important;">if</span>&nbsp;<span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;scroll-behavior: auto !important;">test</span>=<span style="outline: 0px;color: rgb(152, 195, 121);line-height: 26px;scroll-behavior: auto !important;">"sname&nbsp;!=&nbsp;null"</span>&gt;</span>sname=#{sname},<span style="outline: 0px;line-height: 26px;scroll-behavior: auto !important;">&lt;/<span style="outline: 0px;color: rgb(224, 108, 117);line-height: 26px;scroll-behavior: auto !important;">if</span>&gt;</span><br style="outline: 0px;scroll-behavior: auto !important;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="outline: 0px;line-height: 26px;scroll-behavior: auto !important;">&lt;<span style="outline: 0px;color: rgb(224, 108, 117);line-height: 26px;scroll-behavior: auto !important;">if</span>&nbsp;<span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;scroll-behavior: auto !important;">test</span>=<span style="outline: 0px;color: rgb(152, 195, 121);line-height: 26px;scroll-behavior: auto !important;">"spwd&nbsp;!=&nbsp;null"</span>&gt;</span>spwd=#{spwd},<span style="outline: 0px;line-height: 26px;scroll-behavior: auto !important;">&lt;/<span style="outline: 0px;color: rgb(224, 108, 117);line-height: 26px;scroll-behavior: auto !important;">if</span>&gt;</span><br style="outline: 0px;scroll-behavior: auto !important;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="outline: 0px;line-height: 26px;scroll-behavior: auto !important;">&lt;<span style="outline: 0px;color: rgb(224, 108, 117);line-height: 26px;scroll-behavior: auto !important;">if</span>&nbsp;<span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;scroll-behavior: auto !important;">test</span>=<span style="outline: 0px;color: rgb(152, 195, 121);line-height: 26px;scroll-behavior: auto !important;">"sex&nbsp;!=&nbsp;null"</span>&gt;</span>sex=#{sex},<span style="outline: 0px;line-height: 26px;scroll-behavior: auto !important;">&lt;/<span style="outline: 0px;color: rgb(224, 108, 117);line-height: 26px;scroll-behavior: auto !important;">if</span>&gt;</span><br style="outline: 0px;scroll-behavior: auto !important;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="outline: 0px;line-height: 26px;scroll-behavior: auto !important;">&lt;<span style="outline: 0px;color: rgb(224, 108, 117);line-height: 26px;scroll-behavior: auto !important;">if</span>&nbsp;<span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;scroll-behavior: auto !important;">test</span>=<span style="outline: 0px;color: rgb(152, 195, 121);line-height: 26px;scroll-behavior: auto !important;">"phone&nbsp;!=&nbsp;null"</span>&gt;</span>phone=#{phone}<span style="outline: 0px;line-height: 26px;scroll-behavior: auto !important;">&lt;/<span style="outline: 0px;color: rgb(224, 108, 117);line-height: 26px;scroll-behavior: auto !important;">if</span>&gt;</span><br style="outline: 0px;scroll-behavior: auto !important;">&nbsp;&nbsp;&nbsp;&nbsp;sid=#{sid}<br style="outline: 0px;scroll-behavior: auto !important;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="outline: 0px;line-height: 26px;scroll-behavior: auto !important;">&lt;/<span style="outline: 0px;color: rgb(224, 108, 117);line-height: 26px;scroll-behavior: auto !important;">set</span>&gt;</span><br style="outline: 0px;scroll-behavior: auto !important;">&nbsp;&nbsp;&nbsp;&nbsp;where&nbsp;sid=#{sid}<br style="outline: 0px;scroll-behavior: auto !important;"><span style="outline: 0px;line-height: 26px;scroll-behavior: auto !important;">&lt;/<span style="outline: 0px;color: rgb(224, 108, 117);line-height: 26px;scroll-behavior: auto !important;">update</span>&gt;</span><br style="outline: 0px;scroll-behavior: auto !important;"></code></pre> <h3 data-tool="mdnice编辑器" style="margin: 0.6em auto;padding-left: 10px;outline: 0px;font-weight: bold;font-size: 20px;border-left: 2px solid rgb(0, 150, 136);scroll-behavior: auto !important;">4.choose(when,otherwise) 语句</h3> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;outline: 0px;line-height: 26px;text-align: justify;scroll-behavior: auto !important;">有时候,我们不想用到所有的查询条件,只想选择其中的一个,查询条件有一个满足即可,使用 choose 标签可以解决此类问题,类似于 Java 的 switch 语句</p> <pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;outline: 0px;border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;scroll-behavior: auto !important;"><span style="margin-bottom: -7px;outline: 0px;display: block;background: url(&quot;https://mmbiz.qpic.cn/mmbiz_svg/GPyw0pGicibl5sEj4cuKR5asffAcRoIbjtLSlth4jWWibIvd7jA9srffR3cLqiaibT3ibSyCxwqXqSyictzYzriciavr5N2ibW6TsnPyza/640?wx_fmt=svg&quot;) 10px 10px / 40px no-repeat rgb(40, 44, 52);height: 30px;width: 657px;border-radius: 5px;scroll-behavior: auto !important;"></span><code style="padding: 15px 16px 16px;outline: 0px;overflow-x: auto;color: rgb(171, 178, 191);display: -webkit-box;font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace;font-size: 12px;background: rgb(40, 44, 52);border-radius: 5px;scroll-behavior: auto !important;"><span style="outline: 0px;line-height: 26px;scroll-behavior: auto !important;">&lt;<span style="outline: 0px;color: rgb(224, 108, 117);line-height: 26px;scroll-behavior: auto !important;">select</span>&nbsp;<span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;scroll-behavior: auto !important;">id</span>=<span style="outline: 0px;color: rgb(152, 195, 121);line-height: 26px;scroll-behavior: auto !important;">"selectUserByChoose"</span>&nbsp;<span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;scroll-behavior: auto !important;">resultType</span>=<span style="outline: 0px;color: rgb(152, 195, 121);line-height: 26px;scroll-behavior: auto !important;">"com.ys.po.User"</span>&nbsp;<span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;scroll-behavior: auto !important;">parameterType</span>=<span style="outline: 0px;color: rgb(152, 195, 121);line-height: 26px;scroll-behavior: auto !important;">"com.ys.po.User"</span>&gt;</span><br style="outline: 0px;scroll-behavior: auto !important;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;select&nbsp;*&nbsp;from&nbsp;user<br style="outline: 0px;scroll-behavior: auto !important;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="outline: 0px;line-height: 26px;scroll-behavior: auto !important;">&lt;<span style="outline: 0px;color: rgb(224, 108, 117);line-height: 26px;scroll-behavior: auto !important;">where</span>&gt;</span><br style="outline: 0px;scroll-behavior: auto !important;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="outline: 0px;line-height: 26px;scroll-behavior: auto !important;">&lt;<span style="outline: 0px;color: rgb(224, 108, 117);line-height: 26px;scroll-behavior: auto !important;">choose</span>&gt;</span><br style="outline: 0px;scroll-behavior: auto !important;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="outline: 0px;line-height: 26px;scroll-behavior: auto !important;">&lt;<span style="outline: 0px;color: rgb(224, 108, 117);line-height: 26px;scroll-behavior: auto !important;">when</span>&nbsp;<span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;scroll-behavior: auto !important;">test</span>=<span style="outline: 0px;color: rgb(152, 195, 121);line-height: 26px;scroll-behavior: auto !important;">"id&nbsp;!=''&nbsp;and&nbsp;id&nbsp;!=&nbsp;null"</span>&gt;</span><br style="outline: 0px;scroll-behavior: auto !important;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;id=#{id}<br style="outline: 0px;scroll-behavior: auto !important;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="outline: 0px;line-height: 26px;scroll-behavior: auto !important;">&lt;/<span style="outline: 0px;color: rgb(224, 108, 117);line-height: 26px;scroll-behavior: auto !important;">when</span>&gt;</span><br style="outline: 0px;scroll-behavior: auto !important;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="outline: 0px;line-height: 26px;scroll-behavior: auto !important;">&lt;<span style="outline: 0px;color: rgb(224, 108, 117);line-height: 26px;scroll-behavior: auto !important;">when</span>&nbsp;<span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;scroll-behavior: auto !important;">test</span>=<span style="outline: 0px;color: rgb(152, 195, 121);line-height: 26px;scroll-behavior: auto !important;">"username&nbsp;!=''&nbsp;and&nbsp;username&nbsp;!=&nbsp;null"</span>&gt;</span><br style="outline: 0px;scroll-behavior: auto !important;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;and&nbsp;username=#{username}<br style="outline: 0px;scroll-behavior: auto !important;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="outline: 0px;line-height: 26px;scroll-behavior: auto !important;">&lt;/<span style="outline: 0px;color: rgb(224, 108, 117);line-height: 26px;scroll-behavior: auto !important;">when</span>&gt;</span><br style="outline: 0px;scroll-behavior: auto !important;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="outline: 0px;line-height: 26px;scroll-behavior: auto !important;">&lt;<span style="outline: 0px;color: rgb(224, 108, 117);line-height: 26px;scroll-behavior: auto !important;">otherwise</span>&gt;</span><br style="outline: 0px;scroll-behavior: auto !important;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;and&nbsp;sex=#{sex}<br style="outline: 0px;scroll-behavior: auto !important;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="outline: 0px;line-height: 26px;scroll-behavior: auto !important;">&lt;/<span style="outline: 0px;color: rgb(224, 108, 117);line-height: 26px;scroll-behavior: auto !important;">otherwise</span>&gt;</span><br style="outline: 0px;scroll-behavior: auto !important;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="outline: 0px;line-height: 26px;scroll-behavior: auto !important;">&lt;/<span style="outline: 0px;color: rgb(224, 108, 117);line-height: 26px;scroll-behavior: auto !important;">choose</span>&gt;</span><br style="outline: 0px;scroll-behavior: auto !important;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="outline: 0px;line-height: 26px;scroll-behavior: auto !important;">&lt;/<span style="outline: 0px;color: rgb(224, 108, 117);line-height: 26px;scroll-behavior: auto !important;">where</span>&gt;</span><br style="outline: 0px;scroll-behavior: auto !important;">&nbsp;&nbsp;<span style="outline: 0px;line-height: 26px;scroll-behavior: auto !important;">&lt;/<span style="outline: 0px;color: rgb(224, 108, 117);line-height: 26px;scroll-behavior: auto !important;">select</span>&gt;</span><br style="outline: 0px;scroll-behavior: auto !important;"></code></pre> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;outline: 0px;line-height: 26px;text-align: justify;scroll-behavior: auto !important;">也就是说,这里我们有三个条件,id、username、sex,只能选择一个作为查询条件</p> <ul data-tool="mdnice编辑器" class="list-paddingleft-1" style="margin-top: 8px;margin-bottom: 8px;padding-left: 25px;outline: 0px;scroll-behavior: auto !important;"> <li style="outline: 0px;scroll-behavior: auto !important;"> <section style="margin-top: 5px;margin-bottom: 5px;outline: 0px;line-height: 26px;color: rgb(1, 1, 1);scroll-behavior: auto !important;"> <p style="padding-top: 8px;padding-bottom: 8px;outline: 0px;line-height: 26px;color: black;text-align: justify;scroll-behavior: auto !important;">如果 id 不为空,那么查询语句为:<code style="margin-right: 2px;margin-left: 2px;padding: 2px 4px;outline: 0px;font-size: 14px;border-radius: 4px;background-color: rgba(27, 31, 35, 0.05);font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace;word-break: break-all;color: rgb(0, 150, 136);scroll-behavior: auto !important;">select * from user where id=?</code></p> </section></li> <li style="outline: 0px;scroll-behavior: auto !important;"> <section style="margin-top: 5px;margin-bottom: 5px;outline: 0px;line-height: 26px;color: rgb(1, 1, 1);scroll-behavior: auto !important;"> <p style="padding-top: 8px;padding-bottom: 8px;outline: 0px;line-height: 26px;color: black;text-align: justify;scroll-behavior: auto !important;">如果 id 为空,那么看username 是否为空,如果不为空,那么语句为<code style="margin-right: 2px;margin-left: 2px;padding: 2px 4px;outline: 0px;font-size: 14px;border-radius: 4px;background-color: rgba(27, 31, 35, 0.05);font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace;word-break: break-all;color: rgb(0, 150, 136);scroll-behavior: auto !important;">&nbsp;select * from user where username=?;</code></p> </section></li> <li style="outline: 0px;scroll-behavior: auto !important;"> <section style="margin-top: 5px;margin-bottom: 5px;outline: 0px;line-height: 26px;color: rgb(1, 1, 1);scroll-behavior: auto !important;"> <p style="padding-top: 8px;padding-bottom: 8px;outline: 0px;line-height: 26px;color: black;text-align: justify;scroll-behavior: auto !important;">如果 username 为空,那么查询语句为&nbsp;<code style="margin-right: 2px;margin-left: 2px;padding: 2px 4px;outline: 0px;font-size: 14px;border-radius: 4px;background-color: rgba(27, 31, 35, 0.05);font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace;word-break: break-all;color: rgb(0, 150, 136);scroll-behavior: auto !important;">select * from user where sex=?</code></p> </section></li> </ul> <h3 data-tool="mdnice编辑器" style="margin: 0.6em auto;padding-left: 10px;outline: 0px;font-weight: bold;font-size: 20px;border-left: 2px solid rgb(0, 150, 136);scroll-behavior: auto !important;">5.trim</h3> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;outline: 0px;line-height: 26px;text-align: justify;scroll-behavior: auto !important;">trim标记是一个格式化的标记,可以完成set或者是where标记的功能</p> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;outline: 0px;line-height: 26px;text-align: justify;scroll-behavior: auto !important;"><strong style="outline: 0px;scroll-behavior: auto !important;">①、用 trim 改写上面第二点的 if+where 语句</strong></p> <pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;outline: 0px;border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;scroll-behavior: auto !important;"><span style="margin-bottom: -7px;outline: 0px;display: block;background: url(&quot;https://mmbiz.qpic.cn/mmbiz_svg/GPyw0pGicibl5sEj4cuKR5asffAcRoIbjtLSlth4jWWibIvd7jA9srffR3cLqiaibT3ibSyCxwqXqSyictzYzriciavr5N2ibW6TsnPyza/640?wx_fmt=svg&quot;) 10px 10px / 40px no-repeat rgb(40, 44, 52);height: 30px;width: 657px;border-radius: 5px;scroll-behavior: auto !important;"></span><code style="padding: 15px 16px 16px;outline: 0px;overflow-x: auto;color: rgb(171, 178, 191);display: -webkit-box;font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace;font-size: 12px;background: rgb(40, 44, 52);border-radius: 5px;scroll-behavior: auto !important;"><span style="outline: 0px;line-height: 26px;scroll-behavior: auto !important;">&lt;<span style="outline: 0px;color: rgb(224, 108, 117);line-height: 26px;scroll-behavior: auto !important;">select</span>&nbsp;<span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;scroll-behavior: auto !important;">id</span>=<span style="outline: 0px;color: rgb(152, 195, 121);line-height: 26px;scroll-behavior: auto !important;">"selectUserByUsernameAndSex"</span>&nbsp;<span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;scroll-behavior: auto !important;">resultType</span>=<span style="outline: 0px;color: rgb(152, 195, 121);line-height: 26px;scroll-behavior: auto !important;">"user"</span>&nbsp;<span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;scroll-behavior: auto !important;">parameterType</span>=<span style="outline: 0px;color: rgb(152, 195, 121);line-height: 26px;scroll-behavior: auto !important;">"com.ys.po.User"</span>&gt;</span><br style="outline: 0px;scroll-behavior: auto !important;">&nbsp;&nbsp;&nbsp;&nbsp;select&nbsp;*&nbsp;from&nbsp;user<br style="outline: 0px;scroll-behavior: auto !important;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="outline: 0px;color: rgb(92, 99, 112);font-style: italic;line-height: 26px;scroll-behavior: auto !important;">&lt;!--&nbsp;&lt;where&gt;<br style="outline: 0px;scroll-behavior: auto !important;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;if&nbsp;test="username&nbsp;!=&nbsp;null"&gt;<br style="outline: 0px;scroll-behavior: auto !important;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;username=#{username}<br style="outline: 0px;scroll-behavior: auto !important;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/if&gt;<br style="outline: 0px;scroll-behavior: auto !important;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br style="outline: 0px;scroll-behavior: auto !important;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;if&nbsp;test="username&nbsp;!=&nbsp;null"&gt;<br style="outline: 0px;scroll-behavior: auto !important;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;and&nbsp;sex=#{sex}<br style="outline: 0px;scroll-behavior: auto !important;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/if&gt;<br style="outline: 0px;scroll-behavior: auto !important;">&nbsp;&nbsp;&nbsp;&nbsp;&lt;/where&gt;&nbsp;&nbsp;--&gt;</span><br style="outline: 0px;scroll-behavior: auto !important;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="outline: 0px;line-height: 26px;scroll-behavior: auto !important;">&lt;<span style="outline: 0px;color: rgb(224, 108, 117);line-height: 26px;scroll-behavior: auto !important;">trim</span>&nbsp;<span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;scroll-behavior: auto !important;">prefix</span>=<span style="outline: 0px;color: rgb(152, 195, 121);line-height: 26px;scroll-behavior: auto !important;">"where"</span>&nbsp;<span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;scroll-behavior: auto !important;">prefixOverrides</span>=<span style="outline: 0px;color: rgb(152, 195, 121);line-height: 26px;scroll-behavior: auto !important;">"and&nbsp;|&nbsp;or"</span>&gt;</span><br style="outline: 0px;scroll-behavior: auto !important;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="outline: 0px;line-height: 26px;scroll-behavior: auto !important;">&lt;<span style="outline: 0px;color: rgb(224, 108, 117);line-height: 26px;scroll-behavior: auto !important;">if</span>&nbsp;<span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;scroll-behavior: auto !important;">test</span>=<span style="outline: 0px;color: rgb(152, 195, 121);line-height: 26px;scroll-behavior: auto !important;">"username&nbsp;!=&nbsp;null"</span>&gt;</span><br style="outline: 0px;scroll-behavior: auto !important;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;and&nbsp;username=#{username}<br style="outline: 0px;scroll-behavior: auto !important;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="outline: 0px;line-height: 26px;scroll-behavior: auto !important;">&lt;/<span style="outline: 0px;color: rgb(224, 108, 117);line-height: 26px;scroll-behavior: auto !important;">if</span>&gt;</span><br style="outline: 0px;scroll-behavior: auto !important;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="outline: 0px;line-height: 26px;scroll-behavior: auto !important;">&lt;<span style="outline: 0px;color: rgb(224, 108, 117);line-height: 26px;scroll-behavior: auto !important;">if</span>&nbsp;<span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;scroll-behavior: auto !important;">test</span>=<span style="outline: 0px;color: rgb(152, 195, 121);line-height: 26px;scroll-behavior: auto !important;">"sex&nbsp;!=&nbsp;null"</span>&gt;</span><br style="outline: 0px;scroll-behavior: auto !important;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;and&nbsp;sex=#{sex}<br style="outline: 0px;scroll-behavior: auto !important;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="outline: 0px;line-height: 26px;scroll-behavior: auto !important;">&lt;/<span style="outline: 0px;color: rgb(224, 108, 117);line-height: 26px;scroll-behavior: auto !important;">if</span>&gt;</span><br style="outline: 0px;scroll-behavior: auto !important;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="outline: 0px;line-height: 26px;scroll-behavior: auto !important;">&lt;/<span style="outline: 0px;color: rgb(224, 108, 117);line-height: 26px;scroll-behavior: auto !important;">trim</span>&gt;</span><br style="outline: 0px;scroll-behavior: auto !important;"><span style="outline: 0px;line-height: 26px;scroll-behavior: auto !important;">&lt;/<span style="outline: 0px;color: rgb(224, 108, 117);line-height: 26px;scroll-behavior: auto !important;">select</span>&gt;</span><br style="outline: 0px;scroll-behavior: auto !important;"></code></pre> <ul data-tool="mdnice编辑器" class="list-paddingleft-1" style="margin-top: 8px;margin-bottom: 8px;padding-left: 25px;outline: 0px;scroll-behavior: auto !important;"> <li style="outline: 0px;scroll-behavior: auto !important;"> <section style="margin-top: 5px;margin-bottom: 5px;outline: 0px;line-height: 26px;color: rgb(1, 1, 1);scroll-behavior: auto !important;"> <p style="padding-top: 8px;padding-bottom: 8px;outline: 0px;line-height: 26px;color: black;text-align: justify;scroll-behavior: auto !important;">prefix:前缀</p> </section></li> <li style="outline: 0px;scroll-behavior: auto !important;"> <section style="margin-top: 5px;margin-bottom: 5px;outline: 0px;line-height: 26px;color: rgb(1, 1, 1);scroll-behavior: auto !important;"> <p style="padding-top: 8px;padding-bottom: 8px;outline: 0px;line-height: 26px;color: black;text-align: justify;scroll-behavior: auto !important;">prefixoverride:去掉第一个and或者是or</p> </section></li> </ul> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;outline: 0px;line-height: 26px;text-align: justify;scroll-behavior: auto !important;"><strong style="outline: 0px;scroll-behavior: auto !important;">②、用 trim 改写上面第三点的 if+set 语句</strong></p> <pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;outline: 0px;border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;scroll-behavior: auto !important;"><span style="margin-bottom: -7px;outline: 0px;display: block;background: url(&quot;https://mmbiz.qpic.cn/mmbiz_svg/GPyw0pGicibl5sEj4cuKR5asffAcRoIbjtLSlth4jWWibIvd7jA9srffR3cLqiaibT3ibSyCxwqXqSyictzYzriciavr5N2ibW6TsnPyza/640?wx_fmt=svg&quot;) 10px 10px / 40px no-repeat rgb(40, 44, 52);height: 30px;width: 657px;border-radius: 5px;scroll-behavior: auto !important;"></span><code style="padding: 15px 16px 16px;outline: 0px;overflow-x: auto;color: rgb(171, 178, 191);display: -webkit-box;font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace;font-size: 12px;background: rgb(40, 44, 52);border-radius: 5px;scroll-behavior: auto !important;"><span style="outline: 0px;color: rgb(92, 99, 112);font-style: italic;line-height: 26px;scroll-behavior: auto !important;">&lt;!--&nbsp;根据&nbsp;id&nbsp;更新&nbsp;user&nbsp;表的数据&nbsp;--&gt;</span><br style="outline: 0px;scroll-behavior: auto !important;"><span style="outline: 0px;line-height: 26px;scroll-behavior: auto !important;">&lt;<span style="outline: 0px;color: rgb(224, 108, 117);line-height: 26px;scroll-behavior: auto !important;">update</span>&nbsp;<span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;scroll-behavior: auto !important;">id</span>=<span style="outline: 0px;color: rgb(152, 195, 121);line-height: 26px;scroll-behavior: auto !important;">"updateUserById"</span>&nbsp;<span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;scroll-behavior: auto !important;">parameterType</span>=<span style="outline: 0px;color: rgb(152, 195, 121);line-height: 26px;scroll-behavior: auto !important;">"com.ys.po.User"</span>&gt;</span><br style="outline: 0px;scroll-behavior: auto !important;">&nbsp;&nbsp;&nbsp;&nbsp;update&nbsp;user&nbsp;u<br style="outline: 0px;scroll-behavior: auto !important;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="outline: 0px;color: rgb(92, 99, 112);font-style: italic;line-height: 26px;scroll-behavior: auto !important;">&lt;!--&nbsp;&lt;set&gt;<br style="outline: 0px;scroll-behavior: auto !important;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;if&nbsp;test="username&nbsp;!=&nbsp;null&nbsp;and&nbsp;username&nbsp;!=&nbsp;''"&gt;<br style="outline: 0px;scroll-behavior: auto !important;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;u.username&nbsp;=&nbsp;#{username},<br style="outline: 0px;scroll-behavior: auto !important;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/if&gt;<br style="outline: 0px;scroll-behavior: auto !important;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;if&nbsp;test="sex&nbsp;!=&nbsp;null&nbsp;and&nbsp;sex&nbsp;!=&nbsp;''"&gt;<br style="outline: 0px;scroll-behavior: auto !important;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;u.sex&nbsp;=&nbsp;#{sex}<br style="outline: 0px;scroll-behavior: auto !important;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/if&gt;<br style="outline: 0px;scroll-behavior: auto !important;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/set&gt;&nbsp;--&gt;</span><br style="outline: 0px;scroll-behavior: auto !important;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="outline: 0px;line-height: 26px;scroll-behavior: auto !important;">&lt;<span style="outline: 0px;color: rgb(224, 108, 117);line-height: 26px;scroll-behavior: auto !important;">trim</span>&nbsp;<span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;scroll-behavior: auto !important;">prefix</span>=<span style="outline: 0px;color: rgb(152, 195, 121);line-height: 26px;scroll-behavior: auto !important;">"set"</span>&nbsp;<span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;scroll-behavior: auto !important;">suffixOverrides</span>=<span style="outline: 0px;color: rgb(152, 195, 121);line-height: 26px;scroll-behavior: auto !important;">","</span>&gt;</span><br style="outline: 0px;scroll-behavior: auto !important;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="outline: 0px;line-height: 26px;scroll-behavior: auto !important;">&lt;<span style="outline: 0px;color: rgb(224, 108, 117);line-height: 26px;scroll-behavi

阿里这款多级缓存框架一定要掌握,非常不错!

作者:微信小助手

<section data-tool="mdnice编辑器" data-website="https://www.mdnice.com" style="line-height: 1.6;word-break: break-word;text-align: left;padding: 5px;font-size: 16px;color: rgb(53, 53, 53);word-spacing: 0.8px;letter-spacing: 0.8px;border-radius: 16px;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, &quot;PingFang SC&quot;, Cambria, Cochin, Georgia, Times, &quot;Times New Roman&quot;, serif;" data-mpa-powered-by="yiban.io"> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;line-height: 1.75;margin-top: 0.8em;margin-bottom: 0.8em;">在实际应用中,并不是单一的使用本地缓存或者redis,更多是组合使用来满足不同的业务场景,于是如何优雅的组合本地缓存和远程缓存就成了我们要研究的问题,而这一点,阿里开源的<code style="font-size: 14px;padding: 2px 4px;border-radius: 4px;margin-right: 2px;margin-left: 2px;background-color: rgba(27, 31, 35, 0.05);font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace;word-break: break-all;color: rgb(255, 93, 108);">jetcache</code>组件帮我们实现了</p> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;line-height: 1.75;margin-top: 0.8em;margin-bottom: 0.8em;text-align: right;"><span style="color: rgb(248, 57, 41);font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, &quot;PingFang SC&quot;, Cambria, Cochin, Georgia, Times, &quot;Times New Roman&quot;, serif;font-size: 16px;font-weight: 700;letter-spacing: 0.8px;text-align: right;word-spacing: 0.8px;text-wrap: wrap;background-color: rgb(255, 255, 255);">Java技术指南:https://java-family.cn</span></p> <h2 data-tool="mdnice编辑器" style="font-weight: bold;color: black;font-size: 22px;margin-top: 20px;margin-right: 10px;"><span style="display: none;"></span><span style="font-size: 18px;color: rgb(234, 84, 41);letter-spacing: 0.5444px;padding-bottom: 10px;border-bottom: 2px solid rgb(234, 84, 41);visibility: visible;">1. jetcache简介</span></h2> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;line-height: 1.75;margin-top: 0.8em;margin-bottom: 0.8em;"><code style="font-size: 14px;padding: 2px 4px;border-radius: 4px;margin-right: 2px;margin-left: 2px;background-color: rgba(27, 31, 35, 0.05);font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace;word-break: break-all;color: rgb(255, 93, 108);">jetcache</code>是阿里开源的基于java开发的缓存框架,支持多种缓存类型:本地缓存、分布式缓存、多级缓存。能够满足不同业务场景的缓存需求。</p> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;line-height: 1.75;margin-top: 0.8em;margin-bottom: 0.8em;">jetcache具有上手简单、性能高效、拓展性强的特点。支持缓存预热 、缓存key前缀等功能。结合spring-cache使用,可以实现十分优雅的缓存类型切换</p> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;line-height: 1.75;margin-top: 0.8em;margin-bottom: 0.8em;">官网地址:https://github.com/alibaba/jetcache</p> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;line-height: 1.75;margin-top: 0.8em;margin-bottom: 0.8em;">官方文档:https://github.com/alibaba/jetcache/tree/master/docs/CN</p> <h2 data-tool="mdnice编辑器" style="font-weight: bold;color: black;font-size: 22px;margin-top: 20px;margin-right: 10px;"><span style="display: none;"></span><span style="font-size: 18px;color: rgb(234, 84, 41);letter-spacing: 0.5444px;padding-bottom: 10px;border-bottom: 2px solid rgb(234, 84, 41);visibility: visible;">2. jetcache使用</span></h2> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;line-height: 1.75;margin-top: 0.8em;margin-bottom: 0.8em;">1、引入依赖,这里我们使用sringboot项目框架,同时使用redis作为远程缓存。于是我们引入<code style="font-size: 14px;padding: 2px 4px;border-radius: 4px;margin-right: 2px;margin-left: 2px;background-color: rgba(27, 31, 35, 0.05);font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace;word-break: break-all;color: rgb(255, 93, 108);">jetcache-starter-redis</code>依赖,这里我的springboot版本为<code style="font-size: 14px;padding: 2px 4px;border-radius: 4px;margin-right: 2px;margin-left: 2px;background-color: rgba(27, 31, 35, 0.05);font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace;word-break: break-all;color: rgb(255, 93, 108);">2.6.13</code></p> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;line-height: 1.75;margin-top: 0.8em;margin-bottom: 0.8em;">如果是非springboot项目可以参考官网说明配置</p> <p><img class="rich_pages wxw-img" data-ratio="0.32133333333333336" src="/upload/10b696674346ebb8b01e06e1580a8273.png" data-type="png" data-w="750"></p> <pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block;background: none 10px 10px / 40px no-repeat rgb(255, 255, 255);height: 30px;width: 100%;margin-bottom: -7px;border-radius: 5px;"></span><code style="overflow-x: auto;padding: 16px;color: black;display: -webkit-box;font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;font-size: 12px;-webkit-overflow-scrolling: touch;padding-top: 15px;background: #fff;border-radius: 5px;"><span style="color: #aa0d91;line-height: 26px;">&lt;<span style="line-height: 26px;">dependency</span>&gt;</span><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #aa0d91;line-height: 26px;">&lt;<span style="line-height: 26px;">groupId</span>&gt;</span>com.alicp.jetcache<span style="color: #aa0d91;line-height: 26px;">&lt;/<span style="line-height: 26px;">groupId</span>&gt;</span><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #aa0d91;line-height: 26px;">&lt;<span style="line-height: 26px;">artifactId</span>&gt;</span>jetcache-starter-redis<span style="color: #aa0d91;line-height: 26px;">&lt;/<span style="line-height: 26px;">artifactId</span>&gt;</span><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #aa0d91;line-height: 26px;">&lt;<span style="line-height: 26px;">version</span>&gt;</span>2.7.0<span style="color: #aa0d91;line-height: 26px;">&lt;/<span style="line-height: 26px;">version</span>&gt;</span><br><span style="color: #aa0d91;line-height: 26px;">&lt;/<span style="line-height: 26px;">dependency</span>&gt;</span><br><br><span style="color: #007400;line-height: 26px;">&lt;!--&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;jetcache2.7.x版本需要额外添加该依赖--&gt;</span><br><span style="color: #aa0d91;line-height: 26px;">&lt;<span style="line-height: 26px;">dependency</span>&gt;</span><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #aa0d91;line-height: 26px;">&lt;<span style="line-height: 26px;">groupId</span>&gt;</span>redis.clients<span style="color: #aa0d91;line-height: 26px;">&lt;/<span style="line-height: 26px;">groupId</span>&gt;</span><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #aa0d91;line-height: 26px;">&lt;<span style="line-height: 26px;">artifactId</span>&gt;</span>jedis<span style="color: #aa0d91;line-height: 26px;">&lt;/<span style="line-height: 26px;">artifactId</span>&gt;</span><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #aa0d91;line-height: 26px;">&lt;<span style="line-height: 26px;">version</span>&gt;</span>4.3.1<span style="color: #aa0d91;line-height: 26px;">&lt;/<span style="line-height: 26px;">version</span>&gt;</span><br><span style="color: #aa0d91;line-height: 26px;">&lt;/<span style="line-height: 26px;">dependency</span>&gt;</span><br></code></pre> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;line-height: 1.75;margin-top: 0.8em;margin-bottom: 0.8em;">对应的版本说明如下:https://github.com/alibaba/jetcache/blob/master/docs/CN/Compatibility.md</p> <p><img class="rich_pages wxw-img" data-ratio="0.2826666666666667" src="/upload/86e1d48856d9814a6412c56266f843e1.png" data-type="png" data-w="750"></p> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;line-height: 1.75;margin-top: 0.8em;margin-bottom: 0.8em;">2、修改配置文件,配置redis地址和线程数</p> <pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block;background: none 10px 10px / 40px no-repeat rgb(255, 255, 255);height: 30px;width: 100%;margin-bottom: -7px;border-radius: 5px;"></span><code style="overflow-x: auto;padding: 16px;color: black;display: -webkit-box;font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;font-size: 12px;-webkit-overflow-scrolling: touch;padding-top: 15px;background: #fff;border-radius: 5px;"><span style="color: #836C28;line-height: 26px;">jetcache:</span><br>&nbsp;&nbsp;<span style="color: #007400;line-height: 26px;">##&nbsp;统计间隔,0表示不统计,开启后定期在控制台输出缓存信息</span><br>&nbsp;&nbsp;<span style="color: #836C28;line-height: 26px;">statIntervalMinutes:</span>&nbsp;<span style="color: #1c00cf;line-height: 26px;">15</span><br>&nbsp;&nbsp;<span style="color: #007400;line-height: 26px;">##&nbsp;是否把cacheName作为远程缓存key前缀</span><br>&nbsp;&nbsp;<span style="color: #836C28;line-height: 26px;">areaInCacheName:</span>&nbsp;<span style="color: #aa0d91;line-height: 26px;">false</span><br>&nbsp;&nbsp;<span style="color: #007400;line-height: 26px;">##&nbsp;本地缓存配置</span><br>&nbsp;&nbsp;<span style="color: #836C28;line-height: 26px;">local:</span><br>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #836C28;line-height: 26px;">default:</span>&nbsp;<span style="color: #007400;line-height: 26px;">##&nbsp;default表示全部生效,也可以指定某个cacheName</span><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #007400;line-height: 26px;">##&nbsp;本地缓存类型,其他可选:caffeine/linkedhashmap</span><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #836C28;line-height: 26px;">type:</span>&nbsp;<span style="color: #c41a16;line-height: 26px;">linkedhashmap</span><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #836C28;line-height: 26px;">keyConvertor:</span>&nbsp;<span style="color: #c41a16;line-height: 26px;">fastjson</span><br>&nbsp;&nbsp;<span style="color: #007400;line-height: 26px;">##&nbsp;远程缓存配置</span><br>&nbsp;&nbsp;<span style="color: #836C28;line-height: 26px;">remote:</span><br>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #836C28;line-height: 26px;">default:</span>&nbsp;<span style="color: #007400;line-height: 26px;">##&nbsp;default表示全部生效,也可以指定某个cacheName</span><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #836C28;line-height: 26px;">type:</span>&nbsp;<span style="color: #c41a16;line-height: 26px;">redis</span><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #007400;line-height: 26px;">##&nbsp;key转换器方式n</span><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #836C28;line-height: 26px;">keyConvertor:</span>&nbsp;<span style="color: #c41a16;line-height: 26px;">fastjson</span><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #836C28;line-height: 26px;">broadcastChannel:</span>&nbsp;<span style="color: #c41a16;line-height: 26px;">projectA</span><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #007400;line-height: 26px;">##&nbsp;redis序列化方式</span><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #836C28;line-height: 26px;">valueEncoder:</span>&nbsp;<span style="color: #c41a16;line-height: 26px;">java</span><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #836C28;line-height: 26px;">valueDecoder:</span>&nbsp;<span style="color: #c41a16;line-height: 26px;">java</span><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #007400;line-height: 26px;">##&nbsp;redis线程池</span><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #836C28;line-height: 26px;">poolConfig:</span><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #836C28;line-height: 26px;">minIdle:</span>&nbsp;<span style="color: #1c00cf;line-height: 26px;">5</span><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #836C28;line-height: 26px;">maxIdle:</span>&nbsp;<span style="color: #1c00cf;line-height: 26px;">20</span><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #836C28;line-height: 26px;">maxTotal:</span>&nbsp;<span style="color: #1c00cf;line-height: 26px;">50</span><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #007400;line-height: 26px;">##&nbsp;redis地址与端口</span><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #836C28;line-height: 26px;">host:</span>&nbsp;<span style="color: #1c00cf;line-height: 26px;">127.0</span><span style="color: #1c00cf;line-height: 26px;">.0</span><span style="color: #1c00cf;line-height: 26px;">.1</span><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #836C28;line-height: 26px;">port:</span>&nbsp;<span style="color: #1c00cf;line-height: 26px;">6379</span><br></code></pre> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;line-height: 1.75;margin-top: 0.8em;margin-bottom: 0.8em;">更详细的参数配置可参考官网说明:</p> <p><img class="rich_pages wxw-img" data-ratio="0.8866666666666667" src="/upload/310fb9ca0a59470d5781979e68f056c.png" data-type="png" data-w="750"></p> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;line-height: 1.75;margin-top: 0.8em;margin-bottom: 0.8em;">3、启动类添加注解<code style="font-size: 14px;padding: 2px 4px;border-radius: 4px;margin-right: 2px;margin-left: 2px;background-color: rgba(27, 31, 35, 0.05);font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace;word-break: break-all;color: rgb(255, 93, 108);">@EnableCreateCacheAnnotation</code>,开启缓存,添加<code style="font-size: 14px;padding: 2px 4px;border-radius: 4px;margin-right: 2px;margin-left: 2px;background-color: rgba(27, 31, 35, 0.05);font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace;word-break: break-all;color: rgb(255, 93, 108);">@EnableMethodCache(basePackages = "com.example.jetcachedemo")</code>注解,配置缓存方法扫描路径</p> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;line-height: 1.75;margin-top: 0.8em;margin-bottom: 0.8em;">4、使用缓存可以通过三种方式:</p> <ul data-tool="mdnice编辑器" style="margin-top: 8px;margin-bottom: 8px;padding-left: 25px;color: rgb(248, 57, 41);" class="list-paddingleft-1"> <li> <section style="margin-top: 5px;margin-bottom: 5px;line-height: 26px;color: rgb(53, 53, 53);"> 方式一(推荐)AOP模式:通过 <code style="font-size: 14px;padding: 2px 4px;border-radius: 4px;margin-right: 2px;margin-left: 2px;background-color: rgba(27, 31, 35, 0.05);font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace;word-break: break-all;color: rgb(255, 93, 108);">@Cached</code>, <code style="font-size: 14px;padding: 2px 4px;border-radius: 4px;margin-right: 2px;margin-left: 2px;background-color: rgba(27, 31, 35, 0.05);font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace;word-break: break-all;color: rgb(255, 93, 108);">@CacheUpdate</code>, <code style="font-size: 14px;padding: 2px 4px;border-radius: 4px;margin-right: 2px;margin-left: 2px;background-color: rgba(27, 31, 35, 0.05);font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace;word-break: break-all;color: rgb(255, 93, 108);">@CacheInvalidate</code>注解 </section></li> </ul> <pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block;background: none 10px 10px / 40px no-repeat rgb(255, 255, 255);height: 30px;width: 100%;margin-bottom: -7px;border-radius: 5px;"></span><code style="overflow-x: auto;padding: 16px;color: black;display: -webkit-box;font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;font-size: 12px;-webkit-overflow-scrolling: touch;padding-top: 15px;background: #fff;border-radius: 5px;"><span style="color: #643820;line-height: 26px;">@RestController</span><br><span style="color: #643820;line-height: 26px;">@RequestMapping</span>(<span style="color: #c41a16;line-height: 26px;">"user"</span>)<br><span style="color: #aa0d91;line-height: 26px;">public</span>&nbsp;<span style="line-height: 26px;"><span style="color: #aa0d91;line-height: 26px;">class</span>&nbsp;<span style="color: #5c2699;line-height: 26px;">UserController</span>&nbsp;</span>{<br><br>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #643820;line-height: 26px;">@GetMapping</span>(<span style="color: #c41a16;line-height: 26px;">"getRemote"</span>)<br>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #643820;line-height: 26px;">@Cached</span>(name=<span style="color: #c41a16;line-height: 26px;">"userCache:"</span>,&nbsp;key&nbsp;=&nbsp;<span style="color: #c41a16;line-height: 26px;">"#id"</span>,&nbsp;expire&nbsp;=&nbsp;<span style="color: #1c00cf;line-height: 26px;">3600</span>,&nbsp;timeUnit&nbsp;=&nbsp;TimeUnit.SECONDS,&nbsp;cacheType&nbsp;=&nbsp;CacheType.REMOTE)<br>&nbsp;&nbsp;&nbsp;&nbsp;<span style="line-height: 26px;"><span style="color: #aa0d91;line-height: 26px;">public</span>&nbsp;User&nbsp;<span style="color: #1c00cf;line-height: 26px;">getRemote</span><span style="color: #5c2699;line-height: 26px;">(Long&nbsp;id)</span></span>{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #007400;line-height: 26px;">//&nbsp;直接新建用户,模拟从数据库获取数据</span><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;User&nbsp;user&nbsp;=&nbsp;<span style="color: #aa0d91;line-height: 26px;">new</span>&nbsp;User();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user.setId(id);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user.setName(<span style="color: #c41a16;line-height: 26px;">"用户remote"</span>+id);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user.setAge(<span style="color: #1c00cf;line-height: 26px;">23</span>);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user.setSex(<span style="color: #1c00cf;line-height: 26px;">1</span>);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(<span style="color: #c41a16;line-height: 26px;">"第一次获取数据,未走缓存:"</span>+id);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #aa0d91;line-height: 26px;">return</span>&nbsp;user;<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br><br>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #643820;line-height: 26px;">@GetMapping</span>(<span style="color: #c41a16;line-height: 26px;">"getLocal"</span>)<br>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #643820;line-height: 26px;">@Cached</span>(name=<span style="color: #c41a16;line-height: 26px;">"userCache:"</span>,&nbsp;key&nbsp;=&nbsp;<span style="color: #c41a16;line-height: 26px;">"#id"</span>,&nbsp;expire&nbsp;=&nbsp;<span style="color: #1c00cf;line-height: 26px;">3600</span>,&nbsp;timeUnit&nbsp;=&nbsp;TimeUnit.SECONDS,&nbsp;cacheType&nbsp;=&nbsp;CacheType.LOCAL)<br>&nbsp;&nbsp;&nbsp;&nbsp;<span style="line-height: 26px;"><span style="color: #aa0d91;line-height: 26px;">public</span>&nbsp;User&nbsp;<span style="color: #1c00cf;line-height: 26px;">getLocal</span><span style="color: #5c2699;line-height: 26px;">(Long&nbsp;id)</span></span>{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #007400;line-height: 26px;">//&nbsp;直接新建用户,模拟从数据库获取数据</span><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;User&nbsp;user&nbsp;=&nbsp;<span style="color: #aa0d91;line-height: 26px;">new</span>&nbsp;User();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user.setId(id);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user.setName(<span style="color: #c41a16;line-height: 26px;">"用户local"</span>+id);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user.setAge(<span style="color: #1c00cf;line-height: 26px;">23</span>);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user.setSex(<span style="color: #1c00cf;line-height: 26px;">1</span>);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(<span style="color: #c41a16;line-height: 26px;">"第一次获取数据,未走缓存:"</span>+id);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #aa0d91;line-height: 26px;">return</span>&nbsp;user;<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br><br>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #643820;line-height: 26px;">@GetMapping</span>(<span style="color: #c41a16;line-height: 26px;">"getBoth"</span>)<br>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #643820;line-height: 26px;">@Cached</span>(name=<span style="color: #c41a16;line-height: 26px;">"userCache:"</span>,&nbsp;key&nbsp;=&nbsp;<span style="color: #c41a16;line-height: 26px;">"#id"</span>,&nbsp;expire&nbsp;=&nbsp;<span style="color: #1c00cf;line-height: 26px;">3600</span>,&nbsp;timeUnit&nbsp;=&nbsp;TimeUnit.SECONDS,&nbsp;cacheType&nbsp;=&nbsp;CacheType.BOTH)<br>&nbsp;&nbsp;&nbsp;&nbsp;<span style="line-height: 26px;"><span style="color: #aa0d91;line-height: 26px;">public</span>&nbsp;User&nbsp;<span style="color: #1c00cf;line-height: 26px;">getBoth</span><span style="color: #5c2699;line-height: 26px;">(Long&nbsp;id)</span></span>{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #007400;line-height: 26px;">//&nbsp;直接新建用户,模拟从数据库获取数据</span><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;User&nbsp;user&nbsp;=&nbsp;<span style="color: #aa0d91;line-height: 26px;">new</span>&nbsp;User();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user.setId(id);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user.setName(<span style="color: #c41a16;line-height: 26px;">"用户both"</span>+id);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user.setAge(<span style="color: #1c00cf;line-height: 26px;">23</span>);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user.setSex(<span style="color: #1c00cf;line-height: 26px;">1</span>);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(<span style="color: #c41a16;line-height: 26px;">"第一次获取数据,未走缓存:"</span>+id);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #aa0d91;line-height: 26px;">return</span>&nbsp;user;<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br><br>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #643820;line-height: 26px;">@PostMapping</span>(<span style="color: #c41a16;line-height: 26px;">"updateUser"</span>)<br>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #643820;line-height: 26px;">@CacheUpdate</span>(name&nbsp;=&nbsp;<span style="color: #c41a16;line-height: 26px;">"userCache:"</span>,&nbsp;key&nbsp;=&nbsp;<span style="color: #c41a16;line-height: 26px;">"#user.id"</span>,&nbsp;value&nbsp;=&nbsp;<span style="color: #c41a16;line-height: 26px;">"#user"</span>)<br>&nbsp;&nbsp;&nbsp;&nbsp;<span style="line-height: 26px;"><span style="color: #aa0d91;line-height: 26px;">public</span>&nbsp;Boolean&nbsp;<span style="color: #1c00cf;line-height: 26px;">updateUser</span><span style="color: #5c2699;line-height: 26px;">(@RequestBody&nbsp;User&nbsp;user)</span></span>{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #007400;line-height: 26px;">//&nbsp;TODO&nbsp;更新数据库</span><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #aa0d91;line-height: 26px;">return</span>&nbsp;<span style="color: #aa0d91;line-height: 26px;">true</span>;<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br><br>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #643820;line-height: 26px;">@PostMapping</span>(<span style="color: #c41a16;line-height: 26px;">"deleteUser"</span>)<br>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #643820;line-height: 26px;">@CacheInvalidate</span>(name&nbsp;=&nbsp;<span style="color: #c41a16;line-height: 26px;">"userCache:"</span>,&nbsp;key&nbsp;=&nbsp;<span style="color: #c41a16;line-height: 26px;">"#id"</span>)<br>&nbsp;&nbsp;&nbsp;&nbsp;<span style="line-height: 26px;"><span style="color: #aa0d91;line-height: 26px;">public</span>&nbsp;Boolean&nbsp;<span style="color: #1c00cf;line-height: 26px;">deleteUser</span><span style="color: #5c2699;line-height: 26px;">(Long&nbsp;id)</span></span>{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #007400;line-height: 26px;">//&nbsp;TODO&nbsp;从数据库删除</span><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #aa0d91;line-height: 26px;">return</span>&nbsp;<span style="color: #aa0d91;line-height: 26px;">true</span>;<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br><br>}<br></code></pre> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;line-height: 1.75;margin-top: 0.8em;margin-bottom: 0.8em;">这里要注意实体类<code style="font-size: 14px;padding: 2px 4px;border-radius: 4px;margin-right: 2px;margin-left: 2px;background-color: rgba(27, 31, 35, 0.05);font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace;word-break: break-all;color: rgb(255, 93, 108);">User</code>一定要实现序列化,即声明<code style="font-size: 14px;padding: 2px 4px;border-radius: 4px;margin-right: 2px;margin-left: 2px;background-color: rgba(27, 31, 35, 0.05);font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace;word-break: break-all;color: rgb(255, 93, 108);">Serializable</code></p> <pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block;background: none 10px 10px / 40px no-repeat rgb(255, 255, 255);height: 30px;width: 100%;margin-bottom: -7px;border-radius: 5px;"></span><code style="overflow-x: auto;padding: 16px;color: black;display: -webkit-box;font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;font-size: 12px;-webkit-overflow-scrolling: touch;padding-top: 15px;background: #fff;border-radius: 5px;"><span style="color: #643820;line-height: 26px;">@Data</span><br><span style="color: #aa0d91;line-height: 26px;">public</span>&nbsp;<span style="line-height: 26px;"><span style="color: #aa0d91;line-height: 26px;">class</span>&nbsp;<span style="color: #5c2699;line-height: 26px;">User</span>&nbsp;<span style="color: #aa0d91;line-height: 26px;">implements</span>&nbsp;<span style="color: #5c2699;line-height: 26px;">Serializable</span>&nbsp;</span>{<br><br>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #aa0d91;line-height: 26px;">private</span>&nbsp;Long&nbsp;id;<br>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #aa0d91;line-height: 26px;">private</span>&nbsp;String&nbsp;name;<br>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #aa0d91;line-height: 26px;">private</span>&nbsp;Integer&nbsp;age;<br>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #aa0d91;line-height: 26px;">private</span>&nbsp;Integer&nbsp;sex;<br>}<br></code></pre> <ul data-tool="mdnice编辑器" style="margin-top: 8px;margin-bottom: 8px;padding-left: 25px;color: rgb(248, 57, 41);" class="list-paddingleft-1"> <li> <section style="margin-top: 5px;margin-bottom: 5px;line-height: 26px;color: rgb(53, 53, 53);"> 方式二 API模式:通过 <code style="font-size: 14px;padding: 2px 4px;border-radius: 4px;margin-right: 2px;margin-left: 2px;background-color: rgba(27, 31, 35, 0.05);font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace;word-break: break-all;color: rgb(255, 93, 108);">@CreateCache</code>,注:在jetcache 2.7 版本CreateCache注解已废弃,不推荐使用 </section></li> </ul> <pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block;background: none 10px 10px / 40px no-repeat rgb(255, 255, 255);height: 30px;width: 100%;margin-bottom: -7px;border-radius: 5px;"></span><code style="overflow-x: auto;padding: 16px;color: black;display: -webkit-box;font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;font-size: 12px;-webkit-overflow-scrolling: touch;padding-top: 15px;background: #fff;border-radius: 5px;"><span style="color: #643820;line-height: 26px;">@RestController</span><br><span style="color: #643820;line-height: 26px;">@RequestMapping</span>(<span style="color: #c41a16;line-height: 26px;">"user2"</span>)<br><span style="color: #aa0d91;line-height: 26px;">public</span>&nbsp;<span style="line-height: 26px;"><span style="color: #aa0d91;line-height: 26px;">class</span>&nbsp;<span style="color: #5c2699;line-height: 26px;">User2Controller</span>&nbsp;</span>{<br><br>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #643820;line-height: 26px;">@CreateCache</span>(name=&nbsp;<span style="color: #c41a16;line-height: 26px;">"userCache:"</span>,&nbsp;expire&nbsp;=&nbsp;<span style="color: #1c00cf;line-height: 26px;">3600</span>,&nbsp;timeUnit&nbsp;=&nbsp;TimeUnit.SECONDS,&nbsp;cacheType&nbsp;=&nbsp;CacheType.BOTH)<br>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #aa0d91;line-height: 26px;">private</span>&nbsp;Cache&lt;Long,&nbsp;Object&gt;&nbsp;userCache;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #643820;line-height: 26px;">@GetMapping</span>(<span style="color: #c41a16;line-height: 26px;">"get"</span>)<br>&nbsp;&nbsp;&nbsp;&nbsp;<span style="line-height: 26px;"><span style="color: #aa0d91;line-height: 26px;">public</span>&nbsp;User&nbsp;<span style="color: #1c00cf;line-height: 26px;">get</span><span style="color: #5c2699;line-height: 26px;">(Long&nbsp;id)</span></span>{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #aa0d91;line-height: 26px;">if</span>(userCache.get(id)&nbsp;!=&nbsp;<span style="color: #aa0d91;line-height: 26px;">null</span>){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #aa0d91;line-height: 26px;">return</span>&nbsp;(User)&nbsp;userCache.get(id);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;User&nbsp;user&nbsp;=&nbsp;<span style="color: #aa0d91;line-height: 26px;">new</span>&nbsp;User();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user.setId(id);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user.setName(<span style="color: #c41a16;line-height: 26px;">"用户both"</span>+id);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user.setAge(<span style="color: #1c00cf;line-height: 26px;">23</span>);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user.setSex(<span style="color: #1c00cf;line-height: 26px;">1</span>);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;userCache.put(id,&nbsp;user);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(<span style="color: #c41a16;line-height: 26px;">"第一次获取数据,未走缓存:"</span>+id);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #aa0d91;line-height: 26px;">return</span>&nbsp;user;<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br><br>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #643820;line-height: 26px;">@PostMapping</span>(<span style="color: #c41a16;line-height: 26px;">"updateUser"</span>)<br>&nbsp;&nbsp;&nbsp;&nbsp;<span style="line-height: 26px;"><span style="color: #aa0d91;line-height: 26px;">public</span>&nbsp;Boolean&nbsp;<span style="color: #1c00cf;line-height: 26px;">updateUser</span><span style="color: #5c2699;line-height: 26px;">(@RequestBody&nbsp;User&nbsp;user)</span></span>{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #007400;line-height: 26px;">//&nbsp;TODO&nbsp;更新数据库</span><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;userCache.put(user.getId(),&nbsp;user);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #aa0d91;line-height: 26px;">return</span>&nbsp;<span style="color: #aa0d91;line-height: 26px;">true</span>;<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br><br>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #643820;line-height: 26px;">@PostMapping</span>(<span style="color: #c41a16;line-height: 26px;">"deleteUser"</span>)<br>&nbsp;&nbsp;&nbsp;&nbsp;<span style="line-height: 26px;"><span style="color: #aa0d91;line-height: 26px;">public</span>&nbsp;Boolean&nbsp;<span style="color: #1c00cf;line-height: 26px;">deleteUser</span><span style="color: #5c2699;line-height: 26px;">(Long&nbsp;id)</span></span>{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #007400;line-height: 26px;">//&nbsp;TODO&nbsp;从数据库删除</span><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;userCache.remove(id);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #aa0d91;line-height: 26px;">return</span>&nbsp;<span style="color: #aa0d91;line-height: 26px;">true</span>;<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br><br>}<br></code></pre> <ul data-tool="mdnice编辑器" style="margin-top: 8px;margin-bottom: 8px;padding-left: 25px;color: rgb(248, 57, 41);" class="list-paddingleft-1"> <li> <section style="margin-top: 5px;margin-bottom: 5px;line-height: 26px;color: rgb(53, 53, 53);"> 方式三 高级API模式:通过 <code style="font-size: 14px;padding: 2px 4px;border-radius: 4px;margin-right: 2px;margin-left: 2px;background-color: rgba(27, 31, 35, 0.05);font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace;word-break: break-all;color: rgb(255, 93, 108);">CacheManager</code>,2.7 版本才可使用 </section></li> </ul> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;line-height: 1.75;margin-top: 0.8em;margin-bottom: 0.8em;">(1)添加依赖</p> <pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block;background: none 10px 10px / 40px no-repeat rgb(255, 255, 255);height: 30px;width: 100%;margin-bottom: -7px;border-radius: 5px;"></span><code style="overflow-x: auto;padding: 16px;color: black;display: -webkit-box;font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;font-size: 12px;-webkit-overflow-scrolling: touch;padding-top: 15px;background: #fff;border-radius: 5px;"><span style="color: #aa0d91;line-height: 26px;">&lt;<span style="line-height: 26px;">dependency</span>&gt;</span><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #aa0d91;line-height: 26px;">&lt;<span style="line-height: 26px;">groupId</span>&gt;</span>com.alibaba<span style="color: #aa0d91;line-height: 26px;">&lt;/<span style="line-height: 26px;">groupId</span>&gt;</span><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #aa0d91;line-height: 26px;">&lt;<span style="line-height: 26px;">artifactId</span>&gt;</span>fastjson<span style="color: #aa0d91;line-height: 26px;">&lt;/<span style="line-height: 26px;">artifactId</span>&gt;</span><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #aa0d91;line-height: 26px;">&lt;<span style="line-height: 26px;">version</span>&gt;</span>2.0.25<span style="color: #aa0d91;line-height: 26px;">&lt;/<span style="line-height: 26px;">version</span>&gt;</span><br><span style="color: #aa0d91;line-height: 26px;">&lt;/<span style="line-height: 26px;">dependency</span>&gt;</span><br></code></pre> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;line-height: 1.75;margin-top: 0.8em;margin-bottom: 0.8em;">(2)书写配置类</p> <pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block;background: none 10px 10px / 40px no-repeat rgb(255, 255, 255);height: 30px;width: 100%;margin-bottom: -7px;border-radius: 5px;"></span><code style="overflow-x: auto;padding: 16px;color: black;display: -webkit-box;font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;font-size: 12px;-webkit-overflow-scrolling: touch;padding-top: 15px;background: #fff;border-radius: 5px;"><span style="color: #643820;line-height: 26px;">@Configuration</span><br><span style="color: #aa0d91;line-height: 26px;">public</span>&nbsp;<span style="line-height: 26px;"><span style="color: #aa0d91;line-height: 26px;">class</span>&nbsp;<span style="color: #5c2699;line-height: 26px;">JetcacheConfig</span>&nbsp;</span>{<br><br>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #643820;line-height: 26px;">@Autowired</span><br>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #aa0d91;line-height: 26px;">private</span>&nbsp;CacheManager&nbsp;cacheManager;<br>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #aa0d91;line-height: 26px;">private</span>&nbsp;Cache&lt;Long,&nbsp;Object&gt;&nbsp;userCache;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #643820;line-height: 26px;">@PostConstruct</span><br>&nbsp;&nbsp;&nbsp;&nbsp;<span style="line-height: 26px;"><span style="color: #aa0d91;line-height: 26px;">public</span>&nbsp;<span style="color: #aa0d91;line-height: 26px;">void</span>&nbsp;<span style="color: #1c00cf;line-height: 26px;">init</span><span style="color: #5c2699;line-height: 26px;">()</span></span>{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;QuickConfig&nbsp;qc&nbsp;=&nbsp;QuickConfig.newBuilder(<span style="color: #c41a16;line-height: 26px;">"userCache:"</span>)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.expire(Duration.ofSeconds(<span style="color: #1c00cf;line-height: 26px;">3600</span>))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.cacheType(CacheType.BOTH)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #007400;line-height: 26px;">//&nbsp;本地缓存更新后,将在所有的节点中删除缓存,以保持强一致性</span><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.syncLocal(<span style="color: #aa0d91;line-height: 26px;">false</span>)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.build();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;userCache&nbsp;=&nbsp;cacheManager.getOrCreateCache(qc);<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br><br>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #643820;line-height: 26px;">@Bean</span><br>&nbsp;&nbsp;&nbsp;&nbsp;<span style="line-height: 26px;"><span style="color: #aa0d91;line-height: 26px;">public</span>&nbsp;Cache&lt;Long,&nbsp;Object&gt;&nbsp;<span style="color: #1c00cf;line-height: 26px;">getUserCache</span><span style="color: #5c2699;line-height: 26px;">()</span></span>{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #aa0d91;line-height: 26px;">return</span>&nbsp;userCache;<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>}<br></code></pre> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;line-height: 1.75;margin-top: 0.8em;margin-bottom: 0.8em;">(3)调用代码</p> <pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block;background: none 10px 10px / 40px no-repeat rgb(255, 255, 255);height: 30px;width: 100%;margin-bottom: -7px;border-radius: 5px;"></span><code style="overflow-x: auto;padding: 16px;color: black;display: -webkit-box;font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;font-size: 12px;-webkit-overflow-scrolling: touch;padding-top: 15px;background: #fff;border-radius: 5px;"><span style="color: #643820;line-height: 26px;">@RestController</span><br><span style="color: #643820;line-height: 26px;">@RequestMapping</span>(<span style="color: #c41a16;line-height: 26px;">"user3"</span>)<br><span style="color: #aa0d91;line-height: 26px;">public</span>&nbsp;<span style="line-height: 26px;"><span style="color: #aa0d91;line-height: 26px;">class</span>&nbsp;<span style="color: #5c2699;line-height: 26px;">User3Controller</span>&nbsp;</span>{<br><br>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #643820;line-height: 26px;">@Autowired</span><br>&nbsp;&nbsp;&nbsp;&nbsp;JetcacheConfig&nbsp;jetcacheConfig;<br>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #643820;line-height: 26px;">@Autowired</span><br>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #aa0d91;line-height: 26px;">private</span>&nbsp;Cache&lt;Long,&nbsp;Object&gt;&nbsp;userCache;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #643820;line-height: 26px;">@GetMapping</span>(<span style="color: #c41a16;line-height: 26px;">"get"</span>)<br>&nbsp;&nbsp;&nbsp;&nbsp;<span style="line-height: 26px;"><span style="color: #aa0d91;line-height: 26px;">public</span>&nbsp;User&nbsp;<span style="color: #1c00cf;line-height: 26px;">get</span><span style="color: #5c2699;line-height: 26px;">(Long&nbsp;id)</span></span>{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #aa0d91;line-height: 26px;">if</span>(userCache.get(id)&nbsp;!=&nbsp;<span style="color: #aa0d91;line-height: 26px;">null</span>){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #aa0d91;line-height: 26px;">return</span>&nbsp;(User)&nbsp;userCache.get(id);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;User&nbsp;user&nbsp;=&nbsp;<span style="color: #aa0d91;line-height: 26px;">new</span>&nbsp;User();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user.setId(id);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user.setName(<span style="color: #c41a16;line-height: 26px;">"用户both"</span>+id);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user.setAge(<span style="color: #1c00cf;line-height: 26px;">23</span>);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user.setSex(<span style="color: #1c00cf;line-height: 26px;">1</span>);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;userCache.put(id,&nbsp;user);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(<span style="color: #c41a16;line-height: 26px;">"第一次获取数据,未走缓存:"</span>+id);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #aa0d91;line-height: 26px;">return</span>&nbsp;user;<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br><br>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #643820;line-height: 26px;">@PostMapping</span>(<span style="color: #c41a16;line-height: 26px;">"updateUser"</span>)<br>&nbsp;&nbsp;&nbsp;&nbsp;<span style="line-height: 26px;"><span style="color: #aa0d91;line-height: 26px;">public</span>&nbsp;Boolean&nbsp;<span style="color: #1c00cf;line-height: 26px;">updateUser</span><span style="color: #5c2699;line-height: 26px;">(@RequestBody&nbsp;User&nbsp;user)</span></span>{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #007400;line-height: 26px;">//&nbsp;TODO&nbsp;更新数据库</span><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;userCache.put(user.getId(),&nbsp;user);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #aa0d91;line-height: 26px;">return</span>&nbsp;<span style="color: #aa0d91;line-height: 26px;">true</span>;<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br><br>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #643820;line-height: 26px;">@PostMapping</span>(<span style="color: #c41a16;line-height: 26px;">"deleteUser"</span>)<br>&nbsp;&nbsp;&nbsp;&nbsp;<span style="line-height: 26px;"><span style="color: #aa0d91;line-height: 26px;">public</span>&nbsp;Boolean&nbsp;<span style="color: #1c00cf;line-height: 26px;">deleteUser</span><span style="color: #5c2699;line-height: 26px;">(Long&nbsp;id)</span></span>{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #007400;line-height: 26px;">//&nbsp;TODO&nbsp;从数据库删除</span><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;userCache.remove(id);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #aa0d91;line-height: 26px;">return</span>&nbsp;<span style="color: #aa0d91;line-height: 26px;">true</span>;<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br><br>}<br></code></pre> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;line-height: 1.75;margin-top: 0.8em;margin-bottom: 0.8em;">多级缓存的形式,会先从本地缓存获取数据,本地获取不到会从远程缓存获取;<span style="letter-spacing: 0.8px;word-spacing: 0.8px;">关注工众号:码猿技术专栏,回复关键词:1111 获取阿里内部Java性能调优手册!</span></p> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;line-height: 1.75;margin-top: 0.8em;margin-bottom: 0.8em;">5、启动redis,启动演示项目</p> <blockquote data-tool="mdnice编辑器" style="border-top: none;border-right: none;border-bottom: none;font-size: 0.9em;overflow: auto;color: rgb(106, 115, 125);border-left-width: 2px;padding: 8px 10px 8px 15px;background: rgb(255, 249, 249);border-left-color: rgb(239, 112, 96);margin-top: 0px;margin-bottom: 20px;letter-spacing: 0.5444px;"> <p style="padding-top: 8px;padding-bottom: 8px;line-height: 26px;color: rgb(53, 53, 53);font-size: 16px;margin-right: 10px;margin-left: 10px;">注意,如果启动出现<code style="font-size: 14px;padding: 2px 4px;border-radius: 4px;margin-right: 2px;margin-left: 2px;background-color: rgba(27, 31, 35, 0.05);font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace;word-break: break-all;color: rgb(255, 93, 108);">NoClassDefFoundError: redis/clients/util/Pool</code>或<code style="font-size: 14px;padding: 2px 4px;border-radius: 4px;margin-right: 2px;margin-left: 2px;background-color: rgba(27, 31, 35, 0.05);font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace;word-break: break-all;color: rgb(255, 93, 108);">NoClassDefFoundError: redis/clients/jedis/UnifiedJedis</code>报错,说明springboot与jetcache版本不一致,对应关系可参考上述第一步中的说明 同时如果使用的是jetcache2.7.x版本,因为该版本中有jedis包的依赖,需要额外添加如下依赖,或者将jetcache版本将至2.6.5以下</p> </blockquote> <pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block;background: none 10px 10px / 40px no-repeat rgb(255, 255, 255);height: 30px;width: 100%;margin-bottom: -7px;border-radius: 5px;"></span><code style="overflow-x: auto;padding: 16px;color: black;display: -webkit-box;font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;font-size: 12px;-webkit-overflow-scrolling: touch;padding-top: 15px;background: #fff;border-radius: 5px;"><span style="color: #aa0d91;line-height: 26px;">&lt;<span style="line-height: 26px;">dependency</span>&gt;</span><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #aa0d91;line-height: 26px;">&lt;<span style="line-height: 26px;">groupId</span>&gt;</span>redis.clients<span style="color: #aa0d91;line-height: 26px;">&lt;/<span style="line-height: 26px;">groupId</span>&gt;</span><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #aa0d91;line-height: 26px;">&lt;<span style="line-height: 26px;">artifactId</span>&gt;</span>jedis<span style="color: #aa0d91;line-height: 26px;">&lt;/<span style="line-height: 26px;">artifactId</span>&gt;</span><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #aa0d91;line-height: 26px;">&lt;<span style="line-height: 26px;">version</span>&gt;</span>4.3.1<span style="color: #aa0d91;line-height: 26px;">&lt;/<span style="line-height: 26px;">version</span>&gt;</span><br><span style="color: #aa0d91;line-height: 26px;">&lt;/<span style="line-height: 26px;">dependency</span>&gt;</span><br></code></pre> <h2 data-tool="mdnice编辑器" style="font-weight: bold;color: black;font-size: 22px;margin-top: 20px;margin-right: 10px;"><span style="display: none;"></span><span style="font-size: 18px;color: rgb(234, 84, 41);letter-spacing: 0.5444px;padding-bottom: 10px;border-bottom: 2px solid rgb(234, 84, 41);visibility: visible;">3. 测试</span></h2> <h4 data-tool="mdnice编辑器" style="margin-top: 30px;margin-bottom: 15px;font-weight: bold;color: black;font-size: 18px;"><span style="display: none;"></span>3.1 方式一测试<span style="display: none;"></span></h4> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;line-height: 1.75;margin-top: 0.8em;margin-bottom: 0.8em;">1、访问localhost:8088/user/getRemote?id=1</p> <p><img class="rich_pages wxw-img" data-ratio="0.7426666666666667" src="/upload/216715bc6150079c819d88c0c241005.png" data-type="png" data-w="750"></p> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;line-height: 1.75;margin-top: 0.8em;margin-bottom: 0.8em;">因为配置的是远程缓存,在redis中也能看到对应的key</p> <p><img class="rich_pages wxw-img" data-ratio="0.332" src="/upload/e94aec08b7121ef9338c5a388ed768e9.png" data-type="png" data-w="750"></p> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;line-height: 1.75;margin-top: 0.8em;margin-bottom: 0.8em;">2、访问localhost:8088/user/getLocal?id=1,这个方法是从本地缓存获取的,现在只有远程缓存上有数据,我们调用发现缓存数据还是拿到了,这说明当我们在配置文件中配置了本地缓存和远程缓存后,方式一中本地缓存和远程缓存会自动相互调用</p> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;line-height: 1.75;margin-top: 0.8em;margin-bottom: 0.8em;">比如本地缓存有这个key,redis中没有,通过远程缓存方式访问时,会先从redis获取,如果没有会自动获取本地缓存,但是数据还是存储在本地缓存,并不会同步到redis上,这样更加灵活的实现了多级缓存架构</p> <p><img class="rich_pages wxw-img" data-ratio="0.6706666666666666" src="/upload/a6d2d8bf938e873d147de04c4881ea72.png" data-type="png" data-w="750"></p> <h4 data-tool="mdnice编辑器" style="margin-top: 30px;margin-bottom: 15px;font-weight: bold;color: black;font-size: 18px;"><span style="display: none;"></span>3.2 方式二测试<span style="display: none;"></span></h4> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;line-height: 1.75;margin-top: 0.8em;margin-bottom: 0.8em;">1、再测试下<code style="font-size: 14px;padding: 2px 4px;border-radius: 4px;margin-right: 2px;margin-left: 2px;background-color: rgba(27, 31, 35, 0.05);font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace;word-break: break-all;color: rgb(255, 93, 108);">CreateCache</code>的形式:localhost:8088/user2/get?id=4</p> <p><img class="rich_pages wxw-img" data-ratio="0.5706666666666667" src="/upload/7fdae38cc768fc5cc5b84b95a37f72e8.png" data-type="png" data-w="750"></p> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;line-height: 1.75;margin-top: 0.8em;margin-bottom: 0.8em;">正常获取了,并且redis中也有了对应的值</p> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;line-height: 1.75;margin-top: 0.8em;margin-bottom: 0.8em;"><img class="rich_pages wxw-img" data-ratio="0.37466666666666665" src="/upload/e4d580f5f4d2052eade6b35d56c123ed.png" data-type="png" data-w="750"> 而当我们把缓存方式更改为<code style="font-size: 14px;padding: 2px 4px;border-radius: 4px;margin-right: 2px;margin-left: 2px;background-color: rgba(27, 31, 35, 0.05);font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace;word-break: break-all;color: rgb(255, 93, 108);">LOCAL</code>后,再访问localhost:8088/user2/get?id=5</p> <pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block;background: none 10px 10px / 40px no-repeat rgb(255, 255, 255);height: 30px;width: 100%;margin-bottom: -7px;border-radius: 5px;"></span><code style="overflow-x: auto;padding: 16px;color: black;display: -webkit-box;font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;font-size: 12px;-webkit-overflow-scrolling: touch;padding-top: 15px;background: #fff;border-radius: 5px;"><span style="color: #643820;line-height: 26px;">@CreateCache</span>(name=&nbsp;<span style="color: #c41a16;line-height: 26px;">"userCache:"</span>,&nbsp;expire&nbsp;=&nbsp;<span style="color: #1c00cf;line-height: 26px;">3600</span>,&nbsp;timeUnit&nbsp;=&nbsp;TimeUnit.SECONDS,&nbsp;cacheType&nbsp;=&nbsp;CacheType.LOCAL)<br></code></pre> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;line-height: 1.75;margin-top: 0.8em;margin-bottom: 0.8em;">会发现redis中就没有对应缓存了,只在本地缓存存在,说明我们指定本地缓存的形式成功了</p> <p><img class="rich_pages wxw-img" data-ratio="0.8825347758887172" src="/upload/fcdfd1a3c08cd06812bb370a3830aead.png" data-type="png" data-w="647"></p> <h2 data-tool="mdnice编辑器" style="font-weight: bold;color: black;font-size: 22px;margin-top: 20px;margin-right: 10px;"><span style="display: none;"></span><span style="font-size: 18px;color: rgb(234, 84, 41);letter-spacing: 0.5444px;padding-bottom: 10px;border-bottom: 2px solid rgb(234, 84, 41);visibility: visible;">3.3 方式三测试</span></h2> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;line-height: 1.75;margin-top: 0.8em;margin-bottom: 0.8em;">1、调用localhost:8088/user3/get?id=11</p> <p><img class="rich_pages wxw-img" data-ratio="0.6706666666666666" src="/upload/61831090bf05a24fd91079a531fb15be.png" data-type="png" data-w="750"></p> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;line-height: 1.75;margin-top: 0.8em;margin-bottom: 0.8em;">redis中缓存设置成功!</p> <p><img class="rich_pages wxw-img" data-ratio="0.32133333333333336" src="/upload/61c4cbda701de608c9a6e72d0aaca17a.png" data-type="png" data-w="750"></p> <h2 data-tool="mdnice编辑器" style="font-weight: bold;color: black;font-size: 22px;margin-top: 20px;margin-right: 10px;"><span style="display: none;"></span><span style="font-size: 18px;color: rgb(234, 84, 41);letter-spacing: 0.5444px;padding-bottom: 10px;border-bottom: 2px solid rgb(234, 84, 41);visibility: visible;">4. 常见报错</span></h2> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;line-height: 1.75;margin-top: 0.8em;margin-bottom: 0.8em;">1、 <code style="font-size: 14px;padding: 2px 4px;border-radius: 4px;margin-right: 2px;margin-left: 2px;background-color: rgba(27, 31, 35, 0.05);font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace;word-break: break-all;color: rgb(255, 93, 108);">ClassNotFoundException: com.alibaba.fastjson.JSON</code> 解决:添加依赖</p> <pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block;background: none 10px 10px / 40px no-repeat rgb(255, 255, 255);height: 30px;width: 100%;margin-bottom: -7px;border-radius: 5px;"></span><code style="overflow-x: auto;padding: 16px;color: black;display: -webkit-box;font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;font-size: 12px;-webkit-overflow-scrolling: touch;padding-top: 15px;background: #fff;border-radius: 5px;"><span style="color: #aa0d91;line-height: 26px;">&lt;<span style="line-height: 26px;">dependency</span>&gt;</span><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #aa0d91;line-height: 26px;">&lt;<span style="line-height: 26px;">groupId</span>&gt;</span>com.alibaba<span style="color: #aa0d91;line-height: 26px;">&lt;/<span style="line-height: 26px;">groupId</span>&gt;</span><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #aa0d91;line-height: 26px;">&lt;<span style="line-height: 26px;">artifactId</span>&gt;</span>fastjson<span style="color: #aa0d91;line-height: 26px;">&lt;/<span style="line-height: 26px;">artifactId</span>&gt;</span><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #aa0d91;line-height: 26px;">&lt;<span style="line-height: 26px;">version</span>&gt;</span>2.0.25<span style="color: #aa0d91;line-height: 26px;">&lt;/<span style="line-height: 26px;">version</span>&gt;</span><br><span style="color: #aa0d91;line-height: 26px;">&lt;/<span style="line-height: 26px;">dependency</span>&gt;</span><br></code></pre> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;line-height: 1.75;margin-top: 0.8em;margin-bottom: 0.8em;">2、<code style="font-size: 14px;padding: 2px 4px;border-radius: 4px;margin-right: 2px;margin-left: 2px;background-color: rgba(27, 31, 35, 0.05);font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace;word-break: break-all;color: rgb(255, 93, 108);">NoClassDefFoundError: redis/clients/jedis/UnifiedJedis</code> 解决: 添加依赖</p> <pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block;background: none 10px 10px / 40px no-repeat rgb(255, 255, 255);height: 30px;width: 100%;margin-bottom: -7px;border-radius: 5px;"></span><code style="overflow-x: auto;padding: 16px;color: black;display: -webkit-box;font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;font-size: 12px;-webkit-overflow-scrolling: touch;padding-top: 15px;background: #fff;border-radius: 5px;"><span style="color: #aa0d91;line-height: 26px;">&lt;<span style="line-height: 26px;">dependency</span>&gt;</span><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #aa0d91;line-height: 26px;">&lt;<span style="line-height: 26px;">groupId</span>&gt;</span>redis.clients<span style="color: #aa0d91;line-height: 26px;">&lt;/<span style="line-height: 26px;">groupId</span>&gt;</span><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #aa0d91;line-height: 26px;">&lt;<span style="line-height: 26px;">artifactId</span>&gt;</span>jedis<span style="color: #aa0d91;line-height: 26px;">&lt;/<span style="line-height: 26px;">artifactId</span>&gt;</span><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #aa0d91;line-height: 26px;">&lt;<span style="line-height: 26px;">version</span>&gt;</span>4.3.1<span style="color: #aa0d91;line-height: 26px;">&lt;/<span style="line-height: 26px;">version</span>&gt;</span><br><span style="color: #aa0d91;line-height: 26px;">&lt;/<span style="line-height: 26px;">dependency</span>&gt;</span><br></code></pre> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;line-height: 1.75;margin-top: 0.8em;margin-bottom: 0.8em;">或者将jetcache版本降低至2.6.5以下</p> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;line-height: 1.75;margin-top: 0.8em;margin-bottom: 0.8em;text-align: left;"><span style="font-size: 14px;color: rgb(178, 178, 178);">来源:https://juejin.cn/post/7247151913437937701</span></p> <blockquote data-tool="mdnice编辑器" style="border-top: none;border-right: none;border-bottom: none;font-size: 0.9em;overflow: auto;color: rgb(106, 115, 125);border-left-width: 2px;padding: 8px 10px 8px 15px;background: rgb(255, 249, 249);border-left-color: rgb(239, 112, 96);margin-top: 0px;margin-bottom: 20px;letter-spacing: 0.5444px;"></blockquote> </section>

gitlab删除大文件

作者:じ☆ve不哭

## 1. 拉取项目:将项目所有分支拉下来 (单分支 直接clone即可) ``` git clone xxx # 默认拉取master分支 cd xx #进入拉取的文件夹 git branch -r | grep -v '\->' | while read remote; do git branch --track "${remote#origin/}" "$remote"; done git fetch --all #如果无人提交代码,可以不拉 git pull --all #如果无人提交代码,可以不拉 ``` **查看删除前大小** ``` $ git count-objects -vH warning: garbage found: .git/objects/80/tmp_obj_RmCfwe count: 124 size: 30.14 KiB in-pack: 5507 packs: 1 size-pack: 20.42 MiB prune-packable: 0 garbage: 1 size-garbage: 172 bytes ``` ## 2. 查找大文件 ``` git rev-list --objects --all | grep "$(git verify-pack -v .git/objects/pack/*.idx | sort -k 3 -n | tail -10 | awk '{print$1}')" ``` ``` $ git rev-list --objects --all | grep "$(git verify-pack -v .git/objects/pack/*.idx | sort -k 3 -n | tail -10 | awk '{print$1}')" 21f94c21a02afa2e28dbbcb13640bf77aba59c7f videochat4user/src/main/assets/apu.mp4 cead7aeea3e606ba158e96d02b2ff43eeffda3ca videochat4user/proguardMapping.txt 2b34b6945d81fdc86f72af9e8e58c7b3645b50b2 videochat4user/proguardMapping.txt 9533dfddf0c3dece13e81831c62161bcf9d9f493 videochat4user/proguardMapping.txt 53ee920749181d51f6969b160cc0e38f2ef0d147 videochat4user/proguardMapping.txt 01553b94780b057bbd54d6dfb8025f1f21083541 videochat4user/proguardMapping.txt b72ff924ecef61b2ba3990b6d60a12f8a519bcc0 videochat4user/proguardMapping.txt 50699882ff36cca85520c372da930c747425b0f6 videochat4user/proguardMapping.txt c6b4d9d3f07a3da43444dc9f16c2fb694f96a074 videochat4user/proguardMapping.txt 9130775fe603c6834f474ab105307fc1bee94be3 videochat4user/src/main/jniLibs/armeabi-v7a/libopencv_java4.so ``` ## 3.删除大文件 可以用通配符, 这里用\*/proguardMapping.txt > :warning:一次只能删除一个文件或者文件夹 ``` git filter-branch --force --index-filter "git rm -r --cached --ignore-unmatch */proguardMapping.txt" --prune-empty --tag-name-filter cat -- --all ``` ``` WARNING: git-filter-branch has a glut of gotchas generating mangled history rewrites. Hit Ctrl-C before proceeding to abort, then use an alternative filtering tool such as 'git filter-repo' (https://github.com/newren/git-filter-repo/) instead. See the filter-branch manual page for more details; to squelch this warning, set FILTER_BRANCH_SQUELCH_WARNING=1. Proceeding with filter-branch... Rewrite 1ab5a65a4c80d6c196f486925584fd501c382fac (428/510) (206 seconds passed, remaining 39 predicted) rm 'camerafix/.gitignore' rm 'camerafix/README.md' rm 'camerafix/build.gradle' rm 'camerafix/proguard-rules.pro' ``` 删除每个commit中包含的文件, 出现rm表示该commit包含文件同时删除成功 **同时回收本地空间** ``` git for-each-ref --format='delete %(refname)' refs/original | git update-ref --stdin && git reflog expire --expire=now --all|git gc --prune=now ``` **查看删除后大小** ``` $ git count-objects -vH count: 0 size: 0 bytes in-pack: 4510 packs: 1 size-pack: 12.07 MiB prune-packable: 0 garbage: 0 size-garbage: 0 bytes ``` ### 4.强制推送到服务器 (分支需要取消保护) ``` git push origin --force "refs/heads/*" --tags ``` ### 5.已有本地代码强制同步 ``` git fetch --all && git reset --hard origin/master && git pull ``` ### 6.清除服务缓存 ``` # 进入git服务器-->这个操作需要root权限,不然连文件夹都进不去 cd /var/opt/gitlab/git-data/repositories #根据项目,进入对应的git项目文件夹 # 进入 项目.git文件,就可以看到和本地的.git目录中一样的目目录了 #查询git项目大小 git count-objects -vH # 此时还是旧的大小 git gc --prune=now # 清理无效文件 git count-objects -vH # 此时就和本地一样,从库减小了 ``` > :warning:清理完之后,每个人一定要删掉之前拉取的项目,重新从git上拉项目。不要使用之前的项目了!!!之前的项目中的.git文件会将已将删除的文件重新加进来,甚至变的更大 [参考](https://www.freesion.com/article/23591313490/)

git问题汇总

作者:じ☆ve不哭

### git clone --depth=1后 怎么获取完整提交的记录? 修改`.git`文件夹内`config`文件的`[remote "origin"]`节的内容 ``` [remote "origin"] url = https://xxx.com/abc/xxx.git -- fetch = +refs/heads/master:refs/remotes/origin/master ++ fetch = +refs/heads/*:refs/remotes/origin/* ``` 或者使用命令 ``` git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*" ``` 最后获取 ``` git fetch -pv ``` ### Git-永久删除某个重要文件或文件夹 (包括历史记录) 强制 提交错误的文件后,又将文件删除后重新提交,虽然文件不在了,**但是历史记录还保存着文件for revert** , 如果文件过大可能会导致git clone失败 ``` git rev-list --objects --all | grep "$(git verify-pack -v .git/objects/pack/*.idx | sort -k 3 -n | tail -50 | awk '{print$1}')" ``` #### 从你的资料库中清除文件 删除多余的文件或者文件夹 file2remove (都不能以 '/' 开头, 文件夹需要加 -r参数 ,可以使用 * 通配符) ``` git filter-branch --force --index-filter 'git rm --cached --ignore-unmatch file2giremove' --prune-empty --tag-name-filter cat -- --all git filter-branch --force --index-filter 'git rm -r --cached --ignore-unmatch */proguardMapping.txt' --prune-empty --tag-name-filter cat -- --all git filter-branch --force --index-filter 'git rm -r --cached --ignore-unmatch doc' --prune-empty --tag-name-filter cat -- --all ``` ![git删除多余的大文件](https://cdn.jsdelivr.net/gh/Leon406/jsdelivr/img/git删除多余的大文件.jpg) #### 删除后推送到远程服务器,每个分支都需要提交 [gitlab](https://docs.gitlab.com/ee/user/project/repository/reducing_the_repo_size_using_git.html#reducing-the-repository-size-using-git) ``` git push origin --force 'refs/heads/*' ``` ![推送git](https://cdn.jsdelivr.net/gh/Leon406/jsdelivr/img/推送git.jpg) #### **清理和回收空间** ``` git for-each-ref --format='delete %(refname)' refs/original | git update-ref --stdin && git reflog expire --expire=now --all|git gc --prune=now ``` ![资源回收](https://cdn.jsdelivr.net/gh/Leon406/jsdelivr/img/回收资源.jpg) 执行后,发现.git目录文件大小显著减少 130M -->16.8M (webKernal文件100M+) ### git clone、git pull和git fetch的用法及区别 - git clone 将其他仓库克隆到本地,**`包括被clone仓库的版本变化`** clone操作是一个从无到有的**克隆**操作,不需要`git init`初始化。 - git fetch 理解 fetch 的关键, 是理解 FETCH_HEAD,FETCH_HEAD指的是: 某个branch在服务器上的最新状态’。这个列表保存在 .git/FETCH_HEAD 文件中, 其中每一行对应于远程服务器的一个分支。 - git pull 拉取远程分支更新到本地仓库的操作.git pull是相当于从远程仓库获取最新版本,然后再与本地分支merge(合并) 即:`git pull = git fetch + git merge` `git clone下来的项目可以直接推送到远程,git pull 和 git fetch 需要先执行 git remote add 添加远程仓库后才能 push。` ### fatal: refusing to merge unrelated histories 因为远程仓库已经存在代码记录了,并且那部分代码没有和本地仓库进行关联,我们可以使用如下操作允许pull未关联的远程仓库旧代码 ``` git pull origin master --allow-unrelated-histories ``` 注意可能需要解决下冲突 ### 多个仓库配置 ``` git remote add origin 第一个仓库地址 git remote set-url --add origin 第二个仓库地址 git push -u origin master //推送命令 ``` ### 统计 ``` # 统计当前作者今天(从凌晨 1 点开始)提交次数 $ git log --author="$(git config --get user.name)" --no-merges --since=1am --stat # 按提交作者统计,按提交次数排序 $ git shortlog -sn $ git shortlog --numbered --summary # 只看某作者提交的 commit 数 $ git log --author="faker" --oneline --shortstat # 按提交作者统计,提交数量排名前 5(看全部,去掉 head 管道即可) $ git log --pretty='% aN' | sort | uniq -c | sort -k1 -n -r | head -n 5 # 按提交者邮箱统计,提交数量排名前 5 $ git log --pretty=format:% ae | gawk -- '{ ++c [$0]; } END { for (cc in c) printf "%5d % s\n",c [cc],cc; }' | sort -u -n -r | head -n 5 # 统计贡献者数量 $ git log --pretty='% aN' | sort -u | wc -l # 统计提交数量 $ git log --oneline | wc -l ``` ### [OpenSSL SSL_read: Connection was reset, errno 10054](https://www.cnblogs.com/jfen625/p/12995408.html) 这是服务器的SSL证书没有经过第三方机构的签署,所以报错。 解决办法: ``` git config --global http.sslVerify "false" ``` ### [解决 Failed to connect to github.com port 443:connection timed out](https://blog.csdn.net/Hodors/article/details/103226958) gfw拦截了,需要走代理或者配置下[hosts](https://raw.fastgit.org/Leon406/pyutil/master/github/hosts) ``` git config --global http.proxy http://127.0.0.1:1080 git config --global https.proxy http://127.0.0.1:1080 ``` 取消代理 ``` git config --global --unset http.proxy git config --global --unset https.proxy ``` ### error: invalid path ``` git config core.protectNTFS false ``` ### 强制本地代码 ``` git fetch --all && git reset --hard origin/master && git pull ``` ### SSH登录 - git bash 输入 ``` ssh-keygen -t rsa -C "自己的邮箱" ``` 三次回车,结果如下 ![screenshot-20220507-162047](https://gitee.com/LeonShih/Image/raw/master/screenshot-20220507-162047.png) 复制公钥 /c/Users/Leon/.ssh/id_rsa.pub内容 到github/gitlab ssh配置页面,即可 ## no matching host key type found - 找到git安装目录 - 进入 etc目录,找到ssh目录ssh_config文件 - 新增 ``` Host *.对应的域名.com HostkeyAlgorithms +ssh-rsa PubkeyAcceptedAlgorithms +ssh-rsa ``` ## File name too long ``` git config --system core.longpaths true ``` ### 参考 [git clone --depth=1 后获取其他分支](https://www.cnblogs.com/zhangyiqiu/p/12295572.html) [Git-永久删除某个重要文件或文件夹 (包括历史记录) 强制](https://blog.51cto.com/phpervip/2497305?source=dra) [git clone、git pull和git fetch的用法及区别](https://www.cnblogs.com/lalalagq/p/9968949.html) [Git同时推送多个远程仓库](https://www.jianshu.com/p/a74c5ab4fdfc) [Git统计操作](https://yelog.org/2017/05/16/Git%E7%BB%9F%E8%AE%A1%E6%93%8D%E4%BD%9C/) [git error:invalid path问题解决(win下)](https://www.cnblogs.com/GyForever1004/p/13702643.html) [no matching host key type found](https://www.jianshu.com/p/fdd8c6a1c2a5)

Typora + PicGo + Github + jsdelivr个人笔记系统搭建

作者:じ☆ve不哭

## 准备 [Typora官网 (Markdown 编辑查看软件)](https://typora.io/) 下载安装 [PicGo (图床工具) github](https://github.com/Molunerfinn/PicGo/releases) 下载安装 [nodejs (PicGo 插件下载需要)](https://nodejs.org/en/) 下载安装 以上windows系统均可以通过 [scoop 命令安装](https://gitee.com/LeonShih/notes/blob/master/other/Windows%20Scoop%E5%AE%89%E8%A3%85.md) ## 配置 ### nodejs安装后配置 设置npm镜像,国内加速 ``` npm config set registry https://registry.npm.taobao.org ``` ### PicGo配置 插件设置 下载 搜索 github-plus (支持gitee 和github) 版本是1.2.0 - repo github仓库名称 - branch 项目分支 默认 master即可 - token github access token - path github 仓库的目录 不填为主目录, - customUrl 主要用于github cdn加速 如https://github.com/Leon406/jsdelivr customUrl地址 https://cdn.jsdelivr.net/gh/Leon406/jsdelivr cdn.jsdelivr.net/gh替换github.com即可 - origin 选择github ,可选gitee 下载完配置如下 ![image-20200701151950539](https://cdn.jsdelivr.net/gh/Leon406/jsdelivr/img/image-20200701151950539.png) #### 新建github仓库 [github加速](https://gitee.com/LeonShih/notes/blob/master/other/github%E5%8A%A0%E9%80%9F.md) 如下图的 repo 为 Leon406/Img ![image-20200701150837688](https://cdn.jsdelivr.net/gh/Leon406/jsdelivr/img/image-20200701150837688.png) #### 获取token 设置--开发者设置 -- 个人 access token ![image-20200701152711132](https://cdn.jsdelivr.net/gh/Leon406/jsdelivr/img/image-20200701152711132.png) 配置好后,点击上传区, 拖动文件到PicGo进行上传 ### Typora 配置 <kbd>Ctrl</kbd>+<kbd>,</kbd> 进入偏好设置 ![image-20200701153356749](https://cdn.jsdelivr.net/gh/Leon406/jsdelivr/img/image-20200701153356749.png) 上传配置 - 插入图片时 上传图片 - 上传服务设置PicGo app - 再配置下picgo路径 - 最后验证图片上传选项 ![image-20200701153550052](https://cdn.jsdelivr.net/gh/Leon406/jsdelivr/img/image-20200701153550052.png) ## 使用 直接拖动图片到typora,或者截图后直接复制到typora会自动进行图片上传 自动上传脚本windows git项目根目录新建 upload.bat,写入以下内容 ``` @echo off tree /f .|findstr /V "卷" >doc_tree.md git add . git commit -m "commit from auto upload shell" git push pause ``` git项目根目录新建 pull.bat,写入以下内容 ``` git pull ``` 以后写好笔记后,双击 upload.bat即可 ## 参考 [GithubPlus+PicGo + Typora 一键式图床](http://www.mamicode.com/info-detail-2995230.html) [个人笔记项目](https://gitee.com/LeonShih/notes.git)

Maven Reposity

作者:じ☆ve不哭

### 平时用到的库 | 仓库名 | 地址 | 备注 | | -------------- | ------------------------------------------------------------ | -------------------------------------------------------- | | mavenCentral | https://repo1.maven.org/maven2/<br/>https://repo.maven.apache.org/maven2/ | 全区最大的maven库,第二个为apache的镜像库, gradle默认地址 | | jcenter | https://jcenter.bintray.com/anverus/tools/ | bintray私有库 兼容mavenCentral中心仓库,且性能更优 | | google | https://dl.google.com/dl/android/maven2/ | google私有库 | | jitpack | https://www.jitpack.io | 自动构建库github,及其他git 项目,自带cdn加速 | | mavenLocal | ~/.m2/repository | 本地仓库 | | Spring | https://repo.spring.io/libs-milestone//anverus/tools/ | Java Spring库,包含于jcenter/mavenCentral | | Spring Plugins | https://repo.spring.io/plugins-release/ | Java Spring 插件库,包含于jcenter/mavenCentral | ## 国内阿里云镜像库 | 仓库名称 | 代理源地址 | 使用地址 | | :------------ | :------------------------------------- | :------------------------------------------------ | | central | https://repo1.maven.org/maven2/ | https://maven.aliyun.com/repository/central | | jcenter | http://jcenter.bintray.com/ | https://maven.aliyun.com/repository/jcenter | | public | central/jcenter 聚合仓 | https://maven.aliyun.com/repository/public | | google | https://maven.google.com/ | https://maven.aliyun.com/repository/google | | gradle-plugin | https://plugins.gradle.org/m2/ | https://maven.aliyun.com/repository/gradle-plugin | | spring | http://repo.spring.io/libs-milestone/ | https://maven.aliyun.com/repository/spring | | spring-plugin | http://repo.spring.io/plugins-release/ | https://maven.aliyun.com/repository/spring-plugin | ### 加速配置 将central/jcenter/google 换成阿里云地址即可 #### maven配置 打开maven的配置文件(windows机器一般在maven安装目录的conf/settings.xml),在`<mirrors></mirrors>`标签添加mirror子节点: ``` <mirror> <id>aliyunmaven</id> <mirrorOf>*</mirrorOf> <name>阿里云公共仓库</name> <url>https://maven.aliyun.com/repository/public</url> </mirror> ``` 如果想使用其它代理仓库,可在`<repositories></repositories>`节点中加入对应的仓库使用地址。以使用google代理仓为例: ``` <repository> <id>spring</id> <url>https://maven.aliyun.com/repository/google</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </repository> ``` #### gradle配置 在build.gradle文件中加入以下代码: ``` allprojects { repositories { maven { url 'https://maven.aliyun.com/repository/public/' } maven { url 'https://maven.aliyun.com/repository/google/' } mavenLocal() } } ``` ### 参考 [maven repo排名](https://mvnrepository.com/repos) [阿里云效文档](https://help.aliyun.com/document_detail/102512.html?spm=a2c40.aliyun_maven_repo.0.0.36183054XM40LO#h2-u914Du7F6Eu6307u53572) [阿里云repositories 一览](https://maven.aliyun.com/mvn/view)

Sonatype Nexus 3.x Maven 私服搭建

作者:じ☆ve不哭

# Sonatype Nexus 3.x Maven 私服搭建 ## 0.准备 [Nexus]: https://www.sonatype.com/download-oss-sonatype ### 1.解压nexus文件 ​ 将下载的nexus-3.14.0-04-win64.zip解压到自定义目录即可。 ### 2.配置nexus的端口和上下文路径  打开zip解压文件下的 ../nexus-3.14.0-04-win64/nexus-3.14.0-04/etc/nexus-default.properties。   如下属性可以自定义修改。 - application-host : Nexus服务监听的主机 - application-port: Nexus服务监听的端口, - nexus-context-path : Nexus服务的上下文路径   通常可以不做任何修改,但个人习惯于修改 application-host 为127.0.0.1。 ### 3.nexus 启动 在.../nexus-3.14.0-04-win64/nexus-3.14.0-04/bin 目录下,以管理员身份运行cmd   1. nexus.exe /run 命令可以启动nexus服务(参考[官方文档](https://help.sonatype.com/repomanager3/installation/installation-methods))   2. nexus.exe /install 命令来启动(推荐使用这种方式,参考[官方文档](https://help.sonatype.com/repomanager3/installation/run-as-a-service)),。 ### 4.登录网页 启动成功后,输入配置的地址,如127.0.0.1:8081,登录用户名admin,密码 admin123 #### 参考 https://blog.csdn.net/u010015108/article/details/54945125 https://www.cnblogs.com/hujunzheng/p/9807646.html

OpenGL知识点总结

作者:じ☆ve不哭

# 1.OpenGL简介 ## 1.1简介 - 应用程序编程接口 - 对图形硬件设备特性进行访问的软件库 - 与硬件无关,可以通过软件的方式实现OpenGL接口 - 不包含任何执行窗口任务,或者处理用户输入的函数 - 不提供三维物体,几何图元来创建三维空间物体 - API是过程性 - 可以软件实现,也可以硬件实现 ## 1.2 应用 - 视频 图形 图片处理 - 2D/3D游戏引擎开发 - 科学可视化,医学软件的开发 - CAD(计算机辅助技术) - 虚拟实境(AR VR) - AI人工智能 ## 1.3 OpenGL 与OpenGL ES ​ OpenGL ES是一个在移动平台上能够支持 OpenGL 最基本功能的精简规范。 ## 1.4 OpenGL ES Android 支持版本 ![androidversion](https://cdn.jsdelivr.net/gh/Leon406/jsdelivr/img/androidversion.png) ## 1.5 OpenGL绘制流程 ![process](https://cdn.jsdelivr.net/gh/Leon406/jsdelivr/img/process.png) 1. 从OpenGL的几何图元中设置数据,用于构建形状 2. 用不同的着色器对输入的图元数据执行计算操作,判断位置,颜色以及其他渲染属性 3. 输入图元的数学描述 转换为与屏幕位置对应的像素片元,也称**光栅化** 4. 针对光栅化过程产生的每个片元,执行 片元着色器,从而决定这个片元的最终颜色和位置 5. 如果有必要 可以对片元执行一些额外操作(判断片元对应的对象是否可见,或者将片元的颜色与当前屏幕位置的颜色进行融合。) # 2.GLSL ## 2.1 着色器介绍 - 顶点着色器------- 将虚拟空间三维坐标 映射到 屏幕显示二维坐标 +Z-buffer深度信息 模型如下: ![vertex](https://cdn.jsdelivr.net/gh/Leon406/jsdelivr/img/vertex.png) - 片元着色器 --------计算每个像素的颜色和其它属性 (如光照值,贴图,阴影) 模型如下: ![fragment](https://cdn.jsdelivr.net/gh/Leon406/jsdelivr/img/fragment.png) ## 2.2 七大数据类型 1. 标量----基本数据类型 - int 32位 - float 32位 - bool 8位 2. 向量----常用于表示颜色,纹理,坐标 ![base](https://cdn.jsdelivr.net/gh/Leon406/jsdelivr/img/base.png) 3. 矩阵----常用于位移,旋转,缩放操作 - 类型 mat2 mat3 mat4 - 填充顺序从下向下,从左往右 4. 采样器----纹理采样 (用在片元着色器,在宿主(java)中初始化) - 3类采样器 sampler2D sampler3D samplerCube(立体贴图,需要手动开启功能) 5. 结构体----简化运算 ``` struct info { vec3 a; vec2 b; vec3 c } ``` 6. 数组----float f[] 7. 空类型 ----void ## 2.3 类型修饰符 - attribute (**顶点着色器特有**,一般用于各个顶点各不相同的量) - uniform (一般用于对于3D物体中所有顶点都相同的量) - varing (一般用于顶点着色器传递到片元着色器的量) - const (常量) - in/out/inout (输入输出修饰符,默认in) ## 2.4 浮点精度 | precision | 描述 | 位数(位) | | --------- | ---- | ----- | | lowp | 低精度 | 8 | | mediump | 中精度 | 10 | | highp | 高精度 | 16 | # 3.绘制及纹理映射 ## 3.1绘制方式 | 绘制方式 | 说明 | | ----------------- | ------------------------------------- | | GL_POINTS | 点类下唯一的绘制方式,用来绘制点。 | | GL_LINES | 将着色器传入的顶点按顺序两个一组来绘制成线段 | | GL_LINES_STRIP | 按照顶点顺序连接顶点。(不封口) | | GL_LINES_LOOP | 按照顶点顺序连接顶点,并将第一个顶点和最后一个顶点相连。(封口) | | GL_TRIANGLES | 按照顶点顺序每3个点组成三角形进行绘制 | | GL_TRIANGLE_STRIP | 顶点按照顺序依次组织成三角形进行绘制, 最后实际形成的是一个三角形带。若有 | | GL_TRIANGLE_FAN | 将第一个点作为中心点其他点作为边缘点,绘制一系列组成扇形的相邻三角形 | ## 3.2 纹理映射 ### 3.2.1 映射原理 1. ​ 为顶点指定纹理坐标 2. ​ 通过纹理坐标确认纹理区域 3. ​ 将选定区域根据纹理坐标映射到图元上 ### 3.3.2 注意 - 坐标范围 0-1 - 纹理图片 宽高必须是2^n ### 3.3.3纹理拉伸和截取 ``` //1============================================================= 重复拉伸 GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D                                   ,GLES20.GL_TEXTURE_WRAP_S                                         ,GLES20.GL_REPEAT); GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D                                     ,GLES20.GL_TEXTURE_WRAP_T                                         ,GLES20.GL_REPEAT);     //2================================================================= 截取拉伸 GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D,                                     GLES20.GL_TEXTURE_WRAP_S,                                         GLES20.GL_CLAMP_TO_EDGE);         GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D,                                     GLES20.GL_TEXTURE_WRAP_T,                                         GLES20.GL_CLAMP_TO_EDGE); ``` ### 3.3.4纹理采样 | | 最近点采样 | 线性采样 | | ---- | --------------- | -------- | | 基本原理 | 对应像素点 | 加权平均 | | 优点 | 简单 采样快 | 线性平滑 | | 缺点 | 将小图映射到大图上,会产生锯齿 | 容易造成线条模糊 | ``` 【1】最近点采样 GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D,                             GLES20.GL_TEXTURE_MIN_FILTER,                                      GLES20.GL_NEAREST); GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D                                ,GLES20.GL_TEXTURE_MAG_FILTER                              ,GLES20.GL_NEAREST); 【2】线性采样 GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D,                            GLES20.GL_TEXTURE_MIN_FILTER,                                               GLES20.GL_LINEAR); GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D,                           GLES20.GL_TEXTURE_MAG_FILTER,                                               GLES20.GL_LINEAR); ``` #### MIN与MAG采样 当纹理图中的一个像素对应到待映射图元上的多个片元时,采用MAG采样;反之则采用MIN采样。 通俗版:纹理比图元小,采用MAG采样纹理设置。纹理比图元大,用MIN采样纹理设置 配合:MIN与最近点,MAG与线性采样 # 4.光照 - 环境光 = 材质反射系数*环境光强度 - 散射光 = 材质反射系数 * 散射光强度*max(cos(入射角),0) ``` vec4 pointLight(vec3 normal,vec3 lightLocation, vec4 lightDiffuse){        vec3 vp=normalize(lightLocation‐(vMatrix*vec4(vPosition,1)).xyz);             vec3 newTarget=normalize((vMatrix*vec4(normal+vPosition,1)).xyz                        ‐ (vMatrix*vec4(vPosition,1)).xyz);              return lightDiffuse* max(0.0,dot(newTarget,vp));      } ``` - 镜面光 = 材质反射系数 * 镜面光强度 * max(cos(入射角)^粗糙度 ,0) ``` void pointLight( //定位光光照计算的方法                   in vec3 normal, //法向量              inout vec4 specular, //镜面反射光分量           in vec3 lightLocation, //光源位置           in vec4 lightSpecular //镜面光强度      ){   vec3 normalTarget=aPosition+normal;        vec3 newNormal=(uMMatrix*vec4(normalTarget,1)).xyz‐(uMMatrix*vec4(aPosition,1)).xyz;   newNormal=normalize(newNormal);     vec3 eye= normalize(uCamera‐(uMMatrix*vec4(aPosition,1)).xyz);    vec3 vp= normalize(lightLocation‐(uMMatrix*vec4(aPosition,1)).xyz);    vp=normalize(vp);   vec3 halfVector=normalize(vp+eye);         float shininess=50.0;                  float nDotViewHalfVector=dot(newNormal,halfVector);                float powerFactor=max(0.0,pow(nDotViewHalfVector,shininess));          specular=lightSpecular*powerFactor;     } ``` - 定位光 ``` void directionalLight(            in vec3 normal,                 inout vec4 ambient,             inout vec4 diffuse,                 inout vec4 specular,            in vec3 lightDirection,             in vec4 lightAmbient,           in vec4 lightDiffuse,           in vec4 lightSpecular         ){   ambient=lightAmbient; //环境光        //计算变之后的法向量   vec3 normalTarget=aPosition+normal;     vec3 newNormal=(uMMatrix*vec4(normalTarget,1)).xyz‐(uMMatrix*vec4(aPosition,1)).xyz;   newNormal=normalize(newNormal); //计算表面点到照相机的向量       vec3 eye= normalize(uCamera‐(uMMatrix*vec4(aPosition,1)).xyz);     //规格化定向光方向向量   vec3 vp= normalize(lightDirection);    vec3 halfVector=normalize(vp+eye); //求视线与光线的半向量        float shininess=50.0; //粗糙度 ,越小越光滑         ///散射光=材质反射系数 * 散射光强度*max(cos(入射角),0)   float nDotViewPosition=max(0.0,dot(newNormal,vp)); //求法向量vp的点积与0的最大值    diffuse=lightDiffuse*nDotViewPosition;          //镜面光=材质反射系数 * 镜面光强度 * max(cos(入射角)粗糙度 ,0)   float nDotViewHalfVector=dot(newNormal,halfVector); //点积(法线与半向量的点积)      float powerFactor=max(0.0,pow(nDotViewHalfVector,shininess));//镜面光反射强度的因子   specular=lightSpecular*powerFactor;  } ``` # 5.图片处理 | 算法 | 原理 | | :-----: | :--------------------------------------- | | 灰度 | 1.**浮点算法:Gray= R * 0.3+G * 0.59 +B * 0.114** 2.整数算法:Gray=( R * 30+G * 59 +B * 11) 3.仅取绿色:Gray=G 4.平均值:Gray=( R+G +B )/3 5.位移算法:Gray=( R * 76+G * 59 +B * 28)>>8 | | 冷暖 色调 | 冷色调:增加单一的蓝色分量 暖色调:增加单一的红色和绿色色分量 | | 模糊 处理 | 普通模糊:平均 高斯模糊:加权平均 | | 放大 | 纹理坐标范围变小 ( s/2.0,t/2.0) | | 颠倒 | 1.0-T的坐标 | | 扭曲 | 不同半径位置旋转角度不同 (-(r/R)*(r/R)+1.0) | | 浮雕 | 当前点的RGB值减去相邻的RGB值然后加上128作为新的RGB值! | | 马赛克 | 把图片的一个区域用同一个点的颜色来表示,(大规模降低分辨率,隐藏图像细节) | | 对比度增强 | x(1-a)+ya(线性插值) | | 膨胀 腐蚀 | 亮的更亮,反之 | # 6 OBJ文件和MTL文件 ## 6.1 obj文件 | # | 说明 | | --------------------------- | ------------------------------ | | v --vertex | 顶点坐标 | | vt --vertex texture | 纹理坐标 | | vn --vertex normal | 顶点法向量 | | g --group | 组 | | f --face | 面 1/2/3 分布代表 顶点索引 纹理索引 顶点法向量索引 | ## 6.2 mtl文件 | # | 说明 | | -------------------- | ------------------------------------ | | Ka /Kd/ Ks | 光照强度 a--环境光 d--散射光 s--镜面光 | | newmtl _5___Default | 材质名 | | illum | 材质光照模型 | | Ns | 材质反射系数 | | bump | 为材质指定凹凸的纹理文件 | | map_KA/map_KS/map_Kd | 为对应光匹配的指定纹理文件 a--环境光 d--散射光 s--镜面光 | | map_d | 为消隐指数指定标量纹理文件 | | refl | 指定一个球体区域,将指定的纹理反射到映射物体上 | # 7 混合 ## 7.1 源因子和目标因子 输入片元,当前存储在帧缓存中的像素颜色值和 ## 7.2 混合原理 ``` 源颜色(Rs,Gs,Bs,As) 目标颜色(Rd,Gd,Bd,Ad) 设源因子 为(Sr,Sg,Sb,Sa) 设目标因子 为(Dr,Dg,Db,Da) 混合表达式: 混合效果=(Rs * Sr + Rd * Dr , Gs * Sg+Gd * Db , Bs * Sb + Bd * Db,As * Sa + Ad * Da) ``` ## 7.3 代码 ``` //开启混合 gl.glEnable(GL10.GL_BLEND); //定义像素算法(源混合因子,目标混合因子) gl.glBlendFunc(GL10.GL_ONE, GL10.GL_ONE_MINUS_DST_ALPHA); ``` # 8 雾 1. 数学模型 ``` 线性算法:f=max(min((end‐dist)/(end‐start),1.0),1.0) f :  雾化因子(1不浓,0浓)         dist: 当前要绘制的片元到照相机的距离         start:特定距离,当前片元距离照相机距离小于start的时候 ,f=1         end: 特定距离,当前片元距离照相机距离大于end的时候 ,f=0         非线性算法:f=1.0‐smoothstep( start, end,  dist ) smoothstep函数         如果:dist<=start  返回0.0         如果:dist>=end   返回1.0         如果:start < dist<  end   执行0‐1之间的平滑埃尔米特插值         如果:start 》=  end  没效果(未定应) ``` 2. 代码 ``` float  get(){     //计算顶点到照相机的距离     float fogDistance=length( uCamera‐(uMMatrix*vec4(aPosition,1.0)).xyz);     const float end=490.0;//雾的结束位置     const float start=350.0;//雾的开始位置     //计算雾因子     float tmp=1.0‐smoothstep(start,end,fogDistance);       return tmp; } ``` # 9 压缩纹理 (1)图片占用内存公式 内存大小=宽高每一个像素的位数 (2)核心代码 ``` // 读取ZipInputStream 并且创建纹理( ETC1Util.ETC1Texture) ETC1Util.ETC1Texture t = getNextTexture(); ETC1Util.ETC1Texture tAlpha = getNextTexture(); //加载ETC1纹理 ETC1Util.loadTexture(GLES20.GL_TEXTURE_2D , 0//纹理层次 , 0//边框大小。通常为0. , GLES20.GL_RGB//格式使用ETC1纹理压缩,如果不支持。必须gl_rgb。 //要使用的类型如果ETC1纹理压缩不支持。 // 可以gl_unsigned_short_5_6_5, // 这意味着每个像素的16位或gl_unsigned_byte,这意味着每像素24位。 , GLES20.GL_UNSIGNED_SHORT_5_6_5 , t);//ETC1Util.ETC1Texture ETC1Util.loadTexture(GLES20.GL_TEXTURE_2D, 0, 0, GLES20.GL_RGB, GLES20 .GL_UNSIGNED_SHORT_5_6_5, tAlpha); ``` # 10 FBO (1)Frame Buffer和Render Buffer (2)核心代码 ``` //创建缓冲帧 GLES20.glGenFramebuffers(1, fFrame, 0); GLES20.glGenRenderbuffers(1, fRender, 0); //绑定RenderBuffer GLES20.glBindRenderbuffer(GLES20.GL_RENDERBUFFER, fRender[0]); //为深度的Render Buffer,并传入大小 GLES20.glRenderbufferStorage(GLES20.GL_RENDERBUFFER , GLES20.GL_DEPTH_COMPONENT16, mBitmap.getWidth(), mBitmap.getHeight());//渲染缓存图像的像素维度 //创建的渲染缓冲区挂载到帧缓冲区上 GLES20.glFramebufferRenderbuffer(GLES20.GL_FRAMEBUFFER, GLES20.GL_DEPTH_ATTACHMENT, GLES20.GL_RENDERBUFFER, fRender[0]); //解绑Render Buffer GLES20.glBindRenderbuffer(GLES20.GL_RENDERBUFFER, 0); //绑定FrameBuffer GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, fFrame[0]); GLES20.glFramebufferTexture2D( GLES20.GL_FRAMEBUFFER , GLES20.GL_COLOR_ATTACHMENT0 , GLES20.GL_TEXTURE_2D // 附加的纹理对象ID , fTexture[1] , 0); //为FrameBuffer挂载fRender[0]来存储深度 GLES20.glFramebufferRenderbuffer(GLES20.GL_FRAMEBUFFER,//帧缓冲类型的目标 GLES20.GL_DEPTH_ATTACHMENT,// 附着点 GLES20.GL_RENDERBUFFER,// 必须为GL_RENDERBUFFER fRender[0]); // 渲染缓冲区对象 //设置视口 GLES20.glViewport(0, 0, mBitmap.getWidth(), mBitmap.getHeight()); //绘制 帧缓存里读取一个像素块 draw(); GLES20.glReadPixels(0, 0//定义图像区域左下角点的坐标 //图像的高度和宽度 , mBitmap.getWidth(), mBitmap.getHeight() //所读象素数据元素的格式 , GLES20.GL_RGBA, //数据类型 GLES20.GL_UNSIGNED_BYTE,//每个元素的数据类型 mBuffer);//图片 //删除纹理 GLES20.glDeleteTextures(2, fTexture, 0); //删除Render Buffer GLES20.glDeleteRenderbuffers(1, fRender, 0); //删除Frame Buffer GLES20.glDeleteFramebuffers(1, fFrame, 0); ```