動(dòng)力節(jié)點(diǎn)springboot3第八九章筆記_第1頁
動(dòng)力節(jié)點(diǎn)springboot3第八九章筆記_第2頁
動(dòng)力節(jié)點(diǎn)springboot3第八九章筆記_第3頁
動(dòng)力節(jié)點(diǎn)springboot3第八九章筆記_第4頁
動(dòng)力節(jié)點(diǎn)springboot3第八九章筆記_第5頁
已閱讀5頁,還剩11頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

動(dòng)力節(jié)點(diǎn)-SpringBoot3第八章8

文章管理模塊創(chuàng)建新的SpringBoot項(xiàng)目,綜合運(yùn)用視頻中的知識(shí)點(diǎn),做一個(gè)文章管理的后臺(tái)應(yīng)用。新的SpringBoot項(xiàng)目Lession20-BlogAdmin。Maven構(gòu)建工具,包名稱com.bjpowernode.blogJDK19,依賴:SpringWebLombokThymeleafMyBatisFrameworkMySQLDriver依賴還需要BeanValidation需求:文章管理工作,發(fā)布新文章,編輯文章,查看文章內(nèi)容等8.1

配置文件step1:組織配置文件[MISSINGIMAGE:,]app-base.yml|article:_#最低文章閱讀數(shù)量_low-read:10_#首頁顯示最多的文章數(shù)量_top-read:20db.yml|spring:datasource:type:com.zaxxer.hikari.HikariDataSourcedriver-class-name:com.mysql.cj.jdbc.Driverurl:jdbc:mysql://localhost:3306/blog?serverTimezone=Asia/Shanghaiusername:rootpassword:123456hikari:auto-commit:truemaximum-pool-size:10minimum-idle:10_#獲取連接時(shí),檢測語句_connection-test-query:select1connection-timeout:20000_#其他屬性_data-source-properties:cachePrepStmts:truedataSource.cachePrepStmtst:truedataSource.prepStmtCacheSize:250dataSource.prepStmtCacheSqlLimit:2048dataSource.useServerPrepStmts:true|||8.2

視圖文件step2:logo文件favicon.ico放在static/根目錄下step3:創(chuàng)建模板頁面[MISSINGIMAGE:,]articleList.html|閱讀最多的前10篇文章選擇序號(hào)標(biāo)題副標(biāo)題已讀數(shù)量發(fā)布時(shí)間最后修改時(shí)間編輯

編輯

發(fā)布新文章刪除文章文章概覽

<formid="delfrm"th:action="@{/article/removeArticle}"method="post"><inputtype="hidden"id="idsDom"name="ids"value=""></form>addArticle.html|發(fā)布文章標(biāo)題副標(biāo)題文章內(nèi)容

<inputtype="submit"value="發(fā)布新文章"style="margin-left:200px"></form>editArticle.html|修改文章</table><inputtype="hidden"name="id"th:value="${article.id}"><inputtype="submit"value="確定修改"style="margin-left:200px"></form>標(biāo)題副標(biāo)題文章內(nèi)容

bind.html|error.html|8.3

Java代碼step4:java代碼model包:[MISSINGIMAGE:,]ArticleVO.java|\h@DatapublicclassArticleVO{privateIntegerid;privateIntegeruserId;privateStringtitle;privateStringsummary;privateStringcontent;privateIntegerreadCount;privateLocalDateTimecreateTime;privateLocalDateTimeupdateTime;}ArticleParam.java|packagecom.bjpowernode.blog.model.param;importjakarta.validation.constraints.Max;importjakarta.validation.constraints.Min;importjakarta.validation.constraints.NotBlank;importjakarta.validation.constraints.NotNull;importjakarta.validation.constraints.Size;importjava.awt.PageAttributes.MediaType;importlombok.Data;importlombok.NonNull;importorg.hibernate.validator.constraints.Range;\h@DatapublicclassArticleParam{publicstaticinterfaceAddArticle{};publicstaticinterfaceEditArticle{};\h@NotNull(message="修改時(shí)必須有id",groups=EditArticle.class)\h@Min(value=1,message="id必須從{value}開始",groups=EditArticle.class)privateIntegerid;\h@NotBlank(message="請輸入文章標(biāo)題",groups={AddArticle.class,EditArticle.class})\h@Size(min=2,max=20,message="文章標(biāo)題{min}-{max}",groups={AddArticle.class,EditArticle.class})privateStringtitle;\h@NotBlank(message="請輸入文章副標(biāo)題",groups={AddArticle.class,EditArticle.class})\h@Size(min=10,max=30,message="文章副標(biāo)題{min}-{max}",groups={AddArticle.class,EditArticle.class})privateStringsummary;\h@NotBlank(message="請輸入文章副標(biāo)題",groups={AddArticle.class,EditArticle.class})\h@Size(min=50,max=8000,message="文章至少五十個(gè)字,文章至多八千字",groups={AddArticle.class,EditArticle.class})privateStringcontent;}ArticleDTO.java|\h@DatapublicclassArticleDTO{privateIntegerid;privateIntegeruserId;privateStringtitle;privateStringsummary;privateIntegerreadCount;privateStringcontent;privateLocalDateTimecreateTime;privateLocalDateTimeupdateTime;}po包[MISSINGIMAGE:,]ArticlePO.java|\h@DatapublicclassArticlePO{privateIntegerid;privateIntegeruserId;privateStringtitle;privateStringsummary;privateIntegerreadCount;privateLocalDateTimecreateTime;privateLocalDateTimeupdateTime;}ArticleDetailPO.java|\h@DatapublicclassArticleDetailPO{privateIntegerid;privateIntegerarticleId;privateStringcontent;}mapper包[MISSINGIMAGE:,]ArticleMapper.java|packagecom.bjpowernode.blog.mapper;_publicinterfaceArticleMapper{@Select("""selectid,user_id,title,summary,read_count,create_time,update_time

fromarticlewhereread_count>=#{lowRead}orderbyread_countdesclimit#{topRead}""")@Results(id="ArticleBaseMap",value={@Result(id=true,column="id",property="id"),\h@Result(column="user_id",property="userId"),\h@Result(column="title",property="title"),\h@Result(column="summary",property="summary"),\h@Result(column="read_count",property="readCount"),\h@Result(column="create_time",property="createTime"),\h@Result(column="update_time",property="updateTime"),})ListtopSortByReadCount(IntegerlowRead,IntegertopRead);@Insert("""insertintoarticle(user_id,title,summary,read_count,create_time,update_time)\

values(#{userId},#{title},#{summary},#{readCount},#{createTime},#{updateTime})""")\h@Options(useGeneratedKeys=true,keyProperty="id",keyColumn="id")intinsertArticle(ArticlePOarticlePO);@Insert("""insertintoarticle_detail(article_id,content)values(#{articleId},#{content})""")intinsertArticleDetail(ArticleDetailPOarticleDetailPO);@Select("""selectm.idasarticleId,title,summary,contentfromarticlemleftjoinarticle_detailadonm.id=ad.article_id

wherem.id=#{id}""")@Results({@Result(id=true,column="articleId",property="id"),\h@Result(column="title",property="title"),\h@Result(column="summary",property="summary"),\h@Result(column="content",property="content",jdbcType=JdbcType.LONGVARCHAR,javaType=String.class)})ArticleDTOselectArticleAndDetail(Integerid);//更新文章title,summary@Update("""updatearticlesettitle=#{title},summary=#{summary}whereid=#{id}""")intupdateArticle(ArticlePOarticlePO);@Update("""updatearticle_detailsetcontent=#{content}wherearticle_id=#{articleId}""")intupdateArticleDetail(ArticleDetailPOarticleDetailPO);//@Delete("""""")intdeleteArticle(Listids);@Delete("""""")intdeleteArticleDetail(Listids);@Select("""selectid,article_id,contentfromarticle_detailwherearticle_id=#{id}""")ArticleDetailPOselectDetailByArticleId(Integerid);}[MISSINGIMAGE:,]ArticleService.java|publicinterfaceArticleService{ListqueryTopAritcle();booleanaddArticle(ArticleDTOarticle);booleanmodifyArticle(ArticleParamparam);intremoveArticle(Listids);ArticleDTOqueryByArticleId(Integerid);StringqueryTop20Detail(Integerid);}ArticleServiceImpl.java|@RequiredArgsConstructor\h@ServicepublicclassArticleServiceImplimplementsArticleService{privatefinalArticleMapperarticleMapper;privatefinalArticleSettingsarticleSettings;\h@OverridepublicListqueryTopAritcle(){IntegerlowRead=articleSettings.getLowRead();IntegertopRead=articleSettings.getTopRead();returnarticleMapper.topSortByReadCount(lowRead,topRead);}\h@Transactional(rollbackFor=Exception.class)\h@OverridepublicbooleanaddArticle(ArticleDTOarticle){ArticlePOarticlePO=newArticlePO();articlePO.setTitle(article.getTitle());articlePO.setSummary(article.getSummary());//從登陸信息中獲取,現(xiàn)在給個(gè)默認(rèn)articlePO.setUserId(newRandom().nextInt(1000));articlePO.setReadCount(newRandom().nextInt(50));articlePO.setCreateTime(LocalDateTime.now());articlePO.setUpdateTime(LocalDateTime.now());articleMapper.insertArticle(articlePO);ArticleDetailPOarticleDetailPO=newArticleDetailPO();articleDetailPO.setArticleId(articlePO.getId());articleDetailPO.setContent(article.getContent());articleMapper.insertArticleDetail(articleDetailPO);returntrue;}\h@Transactional(rollbackFor=Exception.class)publicbooleanmodifyArticle(ArticleParamparam){ArticlePOarticlePO=newArticlePO();articlePO.setId(param.getId());articlePO.setTitle(param.getTitle());articlePO.setSummary(param.getSummary());inteditArticle=articleMapper.updateArticle(articlePO);ArticleDetailPOdetailPO=newArticleDetailPO();detailPO.setArticleId(param.getId());detailPO.setContent(param.getContent());inteditDetail=articleMapper.updateArticleDetail(detailPO);if(editArticle>0&&editDetail>0){returntrue;}returnfalse;}\h@Transactional(rollbackFor=Exception.class)\h@OverridepublicintremoveArticle(Listids){intmaster=articleMapper.deleteArticle(ids);intdetail=articleMapper.deleteArticleDetail(ids);returnmaster;}\h@OverridepublicArticleDTOqueryByArticleId(Integerid){returnarticleMapper.selectArticleAndDetail(id);}\h@OverridepublicStringqueryTop20Detail(Integerid){ArticleDetailPOarticleDetailPO=articleMapper.selectDetailByArticleId(id);Stringcontent=articleDetailPO.getContent();if(StringUtils.hasText(content)){content=content.substring(0,content.length()>=20?20:content.length());}returncontent;}}[MISSINGIMAGE:,]ArticleController.java|@RequiredArgsConstructor\h@ControllerpublicclassArticleController{privatefinalArticleServicearticleService;@GetMapping(value={"/","/article/hot"})publicStringshowHotArticle(Modelmodel){ListarticlePOList=articleService.queryTopAritcle();//轉(zhuǎn)為VOListarticleVOList=BeanUtil.copyToList(articlePOList,ArticleVO.class);//存儲(chǔ)數(shù)據(jù)model.addAttribute("articleList",articleVOList);//視圖return"/blog/articleList";}//添加文章@PostMapping("/article/add")publicStringaddArticle(@Validated(ArticleParam.AddArticle.class)ArticleParamparam){ArticleDTOarticle=newArticleDTO();article.setTitle(param.getTitle());article.setSummary(param.getSummary());article.setContent(param.getContent());booleanadd=articleService.addArticle(article);return"redirect:/article/hot";}//查詢文章@GetMapping("/article/get")publicStringqueryById(Integerid,Modelmodel){ArticleDTOarticleDTO=articleService.queryByArticleId(id);ArticleVOarticleVO=BeanUtil.copyProperties(articleDTO,ArticleVO.class);model.addAttribute("article",articleVO);return"/blog/editArticle";}//修改文章@PostMapping("/article/edit")publicStringmodifyArticle(@Validated(ArticleParam.EditArticle.class)ArticleParamparam){booleanedit=articleService.modifyArticle(param);return"redirect:/article/hot";}//刪除文章@PostMapping("/article/removeArticle")publicStringremoveArticle(@RequestParam("ids")IdTypeidType){System.out.println("ids="+idType);if(idType.getIdList()==null){thrownewIdNullException("Id為null");}articleService.removeArticle(idType.getIdList());return"redirect:/article/hot";}//查詢文章開始的20個(gè)字@GetMapping("/article/detail/overview")\h@ResponseBodypublicStringqueryDetail(Integerid){Stringtop20Content=articleService.queryTop20Detail(id);return

top20Content;}}[MISSINGIMAGE:,]IdNullException.java|publicclassIdNullExceptionextendsBlogRootException{publicIdNullException(){super();}publicIdNullException(Stringmessage){super(message);}}BlogRootException.java|publicclassBlogRootExceptionextendsRuntimeException{publicBlogRootException(){super();}publicBlogRootException(Stringmessage){super(message);}}GlobalHandleException.java|@ControllerAdvicepublicclassGlobalHandleException{@ExceptionHandler(BindException.class)publicStringhandlerBindException(BindExceptionbindException,Modelmodel){BindingResultresult=bindException.getBindingResult();if(result.hasErrors()){model.addAttribute("errors",result.getFieldErrors());System.out.println("result.getFieldErrors()="+result.getFieldErrors().size());}return"/blog/error/bind";}@ExceptionHandler(Exception.class)publicStringhandlerDefaultException(Exceptionexception,Modelmodel){model.addAttribute("msg","請稍后重試?。?!");return"/blog/error/error";}}[MISSINGIMAGE:,]IdType.java|\h@DatapublicclassIdType{privateListidList;}IdTypeFormatter.java|publicclassIdTypeFormatterimplementsFormatter{\h@OverridepublicIdTypeparse(Stringtext,Localelocale)throwsParseException{IdTypeidType=newIdType();if(StringUtils.hasText(text)){List<Integer>ids=newArrayList<>();for(Stringid:text.split(",")){ids.add(Integer.parseInt(id));}idType.setIdList(ids);}returnidType;}\h@OverridepublicStringprint(IdTypeobject,Localelocale){returnnull;}}[MISSINGIMAGE:,]ArticleSettings.java|\h@Data\h@ConfigurationProperties(prefix="article")publicclassArticleSettings{privateIntegerlowRead;privateIntegertopRead;}WebMvcSettings.java|\h@ConfigurationpublicclassWebMvcSettingsimplementsWebMvcConfigurer{\h@OverridepublicvoidaddFormatters(FormatterRegistryregistry){registry.addFormatter(newIdTypeFormatter());}\h@OverridepublicvoidaddViewControllers(ViewControllerRegistryregistry){registry.addViewController("/view/addArticle").setViewName("/blog/addArticle");}}啟動(dòng)類:Lession19AdminApplication.java|\h@MapperScan(basePackages={"com.bjpowernode.blog.mapper"})@EnableConfigurationProperties({ArticleSettings.class})@SpringBootApplicationpublicclassLession19AdminApplication{publicstaticvoidmain(String[]args){SpringApplication.run(Lession19AdminApplication.class,args);}}動(dòng)力節(jié)點(diǎn)-SpringBoot3第九章9

了解AOT和GraalVM9.1

提供性能的技術(shù)什么是AOT:Ahead-of-TimeCompilation:預(yù)編譯(提前編譯)它在JEP-295中描述,并在Java9中作為實(shí)驗(yàn)性功能添加。AOT是提升Java程序性能的一種方法,特別是提供JVM的啟動(dòng)時(shí)間。在啟動(dòng)虛擬機(jī)之前,將Java類編譯為本機(jī)代碼。改進(jìn)小型和大型Java應(yīng)用程序的啟動(dòng)時(shí)間。JIT(justintime):JIT是現(xiàn)在JVM提高執(zhí)行速度的技術(shù),JVM執(zhí)行Java字節(jié)碼,并將經(jīng)常執(zhí)行的代碼編譯為本機(jī)代碼。這稱為實(shí)時(shí)(JIT)編譯。當(dāng)JVM發(fā)現(xiàn)某個(gè)方法或代碼塊運(yùn)行特別頻繁的時(shí)候,就會(huì)認(rèn)為這是“熱點(diǎn)代碼”(HotSpotCode)。然后JIT會(huì)把“

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論