來(lái)源:blog.csdn.net/Gaowumao?type=blog
難度分析
雖然但是聽(tīng)到這個(gè)消息的時(shí)候,內(nèi)心還是挺震驚的,畢竟是一個(gè)完整的管理系統(tǒng),功能界面還不能太過(guò)簡(jiǎn)陋。而且從數(shù)據(jù)庫(kù)設(shè)計(jì)到整個(gè)系統(tǒng)的交付全由自己一人完成,挑戰(zhàn)效果直接拉滿(mǎn)!但是冷靜下來(lái)思考一下,其實(shí)也并不是很難,整體的項(xiàng)目流程即為:設(shè)計(jì)——>文檔——>編碼——>交付。整體的流程劃清之后,就開(kāi)始一步步從無(wú)到有的實(shí)現(xiàn),沒(méi)想到到最后一步的時(shí)候,我竟然才用一天半的時(shí)間??!后面又用了半天的時(shí)間對(duì)整體的項(xiàng)目做了一個(gè)優(yōu)化處理!
項(xiàng)目回顧
最終效果演示:
技術(shù)選型:
-
SpringBoot
Thymeleaf
mybatis-Plus
MySQL
PageHelper
Lombok
Redis(后期頁(yè)面優(yōu)化使用)
項(xiàng)目業(yè)務(wù)流程簡(jiǎn)介
登錄模塊、用戶(hù)模塊管理以及對(duì)用戶(hù)的角色分配,新聞公告模塊的管理、商品模塊(包括對(duì)商品、商品分類(lèi)、訂單)的管理、角色模塊的管理;對(duì)于前端某資源是否有權(quán)限操作該資源,使用的是thymeleaf模板語(yǔ)法進(jìn)行判斷鑒別以及文件上傳等基本功能。
項(xiàng)目搭建(使用模板引擎)
1. 首先創(chuàng)建Maven項(xiàng)目
引入相應(yīng)的依賴(lài),構(gòu)建所需文件目錄
2. 編寫(xiě)yaml配置文件
server:
port: 8080
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/supplier?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT+8
username: root
password: root
# thymeleaf 配置
thymeleaf:
# 關(guān)閉緩存
cache: false
prefix: classpath:/templates/
mybatis-plus:
mapper-locations: classpath*:/mapper/**/*.xml
3. 項(xiàng)目初期基本搭建
在搭建一個(gè)項(xiàng)目的初期,為了讓系統(tǒng)顯得更規(guī)范化,我一般會(huì)提前做好基礎(chǔ)的配置和聲明,一個(gè)項(xiàng)目從開(kāi)始設(shè)想時(shí)所涉及到技術(shù)以及這些技術(shù)對(duì)應(yīng)的一些基礎(chǔ)配置,都要提前規(guī)劃清楚(個(gè)人習(xí)慣)。比如:異常處理、攔截器、過(guò)濾器、常量類(lèi)等等。
①異常處理
@ControllerAdvice
public class exceptionHandler {
private final org.slf4j.Logger logger = LoggerFactory.getLogger(this.getClass);
@org.springframework.web.bind.Annotation.ExceptionHandler(Exception.class)
public ModelAndView exception(HttpServletrequest request, Exception e ) throws Exception {
logger.error(\"Request URL:{},Exception:{}\",request.getRequestURL,e);
if (AnnotationUtils.findAnnotation(e.getClass, ResponseStatus.class )!= ){
throw e;
}
ModelAndView mv = new ModelAndView;
mv.addObject(\"url\",request.getRequestURL);
mv.addObject(\"exception\",e);
mv.setViewName(\"error/error\");
return mv;
}
}
② 攔截器
攔截器主要是對(duì)一些資源做的處理,類(lèi)似于某些資源需要用戶(hù)登錄后才能訪問(wèn)的,某些是不需要的,比如:登錄功能就不需要有所攔截,而對(duì)用戶(hù)的各種管理就需要添加攔截操作,這樣才能使系統(tǒng)的安全性有所提高。
登錄攔截
public class LoginInterceptor extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
if (request.getSession.getAttribute(\"user\") == ){
response.sendRedirect(\"/api\");
return false;
}
return true;
}
}
資源放行
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {
@Override
public void addInterceptors(Interceptorregistry registry) {
registry.addInterceptor(new LoginInterceptor)
.addPathPatterns(\"/api/**\")
.excludePathPatterns(\"/api\",\"/api/doLogin\");
}
}
4. 編寫(xiě)Controller前端控制器代碼
首先創(chuàng)建一個(gè)FileController類(lèi)
① 跳轉(zhuǎn)文件上傳的頁(yè)面
//跳轉(zhuǎn)文件上傳的頁(yè)面
@RequestMapping(\"/File-upload\")
public StringuserList{
return \"file-upload\";
}
② 實(shí)現(xiàn)文件上傳的功能
@RequestMapping(\"/doAddForUser\")
public String doAdd(User user, @RequestParam(\"file\") MultipartFile files, HttpServletRequest request) throws IOException {
//String path = ;
if (files != && !files.isEmpty){
String name = UUID.randomUUID.toString.replace(\"-\",\"\");
//獲取文件的擴(kuò)展名
String ext = FilenameUtils.getExtension(files.getOriginalFilename);
//設(shè)置文件上傳的路徑
String url =request.getSession.getServletContext.getRealPath(\"/upload/\");
File file = new File(url);
if (!file.exists){
file.mkdir;
}
//測(cè)試路徑
System.out.println(request.getServletPath \"/upload\");
System.out.println(request.getContextPath \"/upload/\");
//以絕對(duì)路徑保存重命名后的文件
files.transferTo(new File(url \"/\" name \".\" ext));
user.setAvatar(request.getContextPath \"/upload/\" name \".\" ext);
}
user.setId(UUID.randomUUID.toString);
String salt = PasswordUtils.getSalt;
String password = user.getPassword;
String encode = PasswordUtils.encode(password, salt);
user.setSalt(salt) ;
user.setPassword(encode);
user.setCreateTime(new Date);
userService.save(user);
return \"redirect:/api/users\";
}
注:如何想要實(shí)現(xiàn)多文件上傳需要更改的地方如下:
③ 實(shí)現(xiàn)多文件上傳功能
在這個(gè)項(xiàng)目中并未實(shí)現(xiàn)多文件上傳功能
private void commons(Object obj, @RequestParam(\"file\") CommonsMultipartFile files, HttpServletRequest request) throws IOException {
//String path = ;
for (int i = 0; i < files.length; i ) {
if (files[i] != && !files[i].isEmpty){
String name = UUID.randomUUID.toString.replace(\"-\",\"\");
//獲取文件的擴(kuò)展名
String ext = FilenameUtils.getExtension(files[i].getOriginalFilename);
//設(shè)置文件上傳的路徑
String url =request.getSession.getServletContext.getRealPath(\"/upload/\");
File file = new File(url);
if (!file.exists){
file.mkdir;
}
//測(cè)試路徑
System.out.println(request.getServletPath \"/upload\");
System.out.println(request.getContextPath \"/upload/\");
//以絕對(duì)路徑保存重命名后的文件
files[i].transferTo(new File(url \"/\" name \".\" ext));
if (i == 0){
obj.setUrl1(request.getContextPath \"/upload/\" name \".\" ext);
}
if (i == 1){
obj.setUrl2(request.getContextPath \"/upload/\" name \".\" ext);
}
if (i == 2){
obj.setUrl3(request.getContextPath \"/upload/\" name \".\" ext);
}
if (i == 3){
obj.setUrl4(request.getContextPath \"/upload/\" name \".\" ext);
}
if (i == 4){
obj.setUrl5(request.getContextPath \"/upload/\" name \".\" ext);
}
}
}
}
5. 項(xiàng)目?jī)?yōu)化
對(duì)于前后端不分離的項(xiàng)目,多數(shù)使用的是頁(yè)面緩存優(yōu)化,當(dāng)系統(tǒng)某一瞬間遭受巨大流量時(shí),當(dāng)?shù)谝粋€(gè)用戶(hù)進(jìn)行頁(yè)面訪問(wèn)時(shí)可以將該頁(yè)面數(shù)據(jù)進(jìn)行緩存,這樣,后來(lái)的用戶(hù)訪問(wèn)到的頁(yè)面都是從緩存中獲取的,這樣就減少了 對(duì)數(shù)據(jù)庫(kù)的操作,減輕了數(shù)據(jù)庫(kù)的壓力,從而達(dá)到優(yōu)化的處理。
① 導(dǎo)入依賴(lài)
<!--redis-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!--commons-pools2 對(duì)象池依賴(lài)-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
② yaml配置
## Redis配置
redis:
# 服務(wù)器地址
host: localhost
# 端口
port: 6379
# 數(shù)據(jù)庫(kù)
database: 0
# 超時(shí)時(shí)間
connect-timeout: 10000ms
lettuce:
pool:
# 最大連接數(shù)
max-active: 8
# 最大連接阻塞等待時(shí)間 默認(rèn) -1
max-wait: 10000ms
# 最大空閑時(shí)間 默認(rèn)8
max-idle: 200
# 最小空閑連接 默認(rèn)8
min-idle: 5
④ Redis序列化處理
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory redisConnectionFactory){
RedisTemplate<String,Object> redisTemplate = new RedisTemplate<>;
//key序列化
redisTemplate.setKeySerializer(new StringRedisSerializer);
//value序列化
redisTemplate.setValueSerializer(new GenericJackson2JSONRedisSerializer);
//hash類(lèi)型key的序列化
redisTemplate.setHashKeySerializer(new StringRedisSerializer);
//hash類(lèi)型value的序列化
redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer);
redisTemplate.setConnectionFactory(redisConnectionFactory);
return redisTemplate;
}
}
③ 優(yōu)化處理
@Autowired
private NewsService newsService;
@Autowired
private RedisTemplate redisTemplate;
@Autowired
private ThymeleafViewResolver viewResolver;
@RequestMapping(value = \"/news\",produces = \"text/html;charset=utf-8\")
@ResponseBody
public String roles(Model model, @RequestParam(value = \"pageNo\",defaultValue = \"1\")Integer pageNo
, @RequestParam(value = \"pageSize\",defaultValue = \"10\")Integer pageSize
, HttpServletRequest request, HttpServletResponse response){
//Redis中獲取頁(yè)面,如果不為空,則直接返回頁(yè)面
ValueOperations valueOperations = redisTemplate.opsForValue;
String html = (String) valueOperations.get(\"news-list\");
if (!StringUtils.isEmpty(html)){
return html;
}
PageHelper.startPage(pageNo,pageSize);
List<News> list = newsService.list;
PageInfo<News> pageInfo = new PageInfo<>(list);
model.addAttribute(\"news\",list);
model.addAttribute(\"pageInfo\",pageInfo);
//如果為空,手動(dòng)渲染,存入Redis中并返回
WebContext context = new WebContext(request, response, request.getServletContext, request.getLocale, model.asMap);
html = viewResolver.getTemplateEngine.process(\"news-list\", context);
if (!StringUtils.isEmpty(html)){
//給緩存設(shè)置過(guò)期時(shí)間
valueOperations.set(\"news-list\",html,60, TimeUnit.SECONDS);
}
return html;
}
④ Redis查看
6. 注意事項(xiàng)
注意@Controller和@RestController的區(qū)別,本項(xiàng)目使用的是模板渲染頁(yè)面,而@Controller就是用來(lái)響應(yīng)頁(yè)面的;而@RestController是用來(lái)返回Json
在項(xiàng)目?jī)?yōu)化階段需要在方法上添加注解@ResponseBody,因?yàn)槲覀兪菍⒄麄€(gè)頁(yè)面進(jìn)行緩存 ,所以要將頁(yè)面轉(zhuǎn)換成JSON進(jìn)行存儲(chǔ)。
注入Thymeleaf解析器,將具體的 頁(yè)面進(jìn)行解析成Json字符串進(jìn)行存儲(chǔ)
將存入Redis中的數(shù)據(jù)加上過(guò)期時(shí)間,因?yàn)轫?yè)面中的數(shù)據(jù)要和數(shù)據(jù)庫(kù)保持一致,如果用戶(hù)看到是幾十秒之前或一分鐘之前的數(shù)據(jù)還是勉強(qiáng)可以接受的。
目前代碼已經(jīng)同步到Gitee:
https://gitee.com/gao-wumao/supplier
如果有需要的自行前去倉(cāng)庫(kù)拉取
版權(quán)聲明:本文內(nèi)容由互聯(lián)網(wǎng)用戶(hù)自發(fā)貢獻(xiàn),該文觀點(diǎn)僅代表作者本人。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如發(fā)現(xiàn)本站有涉嫌抄襲侵權(quán)/違法違規(guī)的內(nèi)容, 請(qǐng)發(fā)送郵件至 舉報(bào),一經(jīng)查實(shí),本站將立刻刪除。