來源:blog.csdn.net/Gaowumao?type=blog
難度分析
雖然但是聽到這個消息的時候,內心還是挺震驚的,畢竟是一個完整的管理系統,功能界面還不能太過簡陋。而且從數據庫設計到整個系統的交付全由自己一人完成,挑戰效果直接拉滿!但是冷靜下來思考一下,其實也并不是很難,整體的項目流程即為:設計——>文檔——>編碼——>交付。整體的流程劃清之后,就開始一步步從無到有的實現,沒想到到最后一步的時候,我竟然才用一天半的時間!!后面又用了半天的時間對整體的項目做了一個優化處理!
項目回顧 最終效果演示:
技術選型:
SpringBoot
Thymeleaf
Mybatis-Plus
MySQL
PageHelper
Lombok
Redis(后期頁面優化使用)
登錄模塊、用戶模塊管理以及對用戶的角色分配,新聞公告模塊的管理、商品模塊(包括對商品、商品分類、訂單)的管理、角色模塊的管理;對于前端某資源是否有權限操作該資源,使用的是thymeleaf模板語法進行判斷鑒別以及文件上傳等基本功能。
項目搭建(使用模板引擎) 1. 首先創建Maven項目
引入相應的依賴,構建所需文件目錄
2. 編寫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%2B8
username: root
password: root
# thymeleaf 配置
thymeleaf:
# 關閉緩存
cache: false
prefix: classpath:/templates/
mybatis-plus:
3. 項目初期基本搭建
mapper-locations: classpath*:/mapper/**/*.xml
在搭建一個項目的初期,為了讓系統顯得更規范化,我一般會提前做好基礎的配置和聲明,一個項目從開始設想時所涉及到技術以及這些技術對應的一些基礎配置,都要提前規劃清楚(個人習慣)。比如:異常處理、攔截器、過濾器、常量類等等。
①異常處理
@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 )!= null){
throw e;
}
ModelAndView mv = new ModelAndView();
mv.addObject("url",request.getRequestURL());
mv.addObject("exception",e);
mv.setViewName("error/error");
return mv;
}
}
② 攔截器
攔截器主要是對一些資源做的處理,類似于某些資源需要用戶登錄后才能訪問的,某些是不需要的,比如:登錄功能就不需要有所攔截,而對用戶的各種管理就需要添加攔截操作,這樣才能使系統的安全性有所提高。
登錄攔截
public class LoginInterceptor extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
if (request.getSession().getAttribute("user") == null){
response.sendRedirect("/api");
return false;
}
return true;
}
}
資源放行
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {
@Override
4. 編寫Controller前端控制器代碼
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LoginInterceptor())
.addPathPatterns("/api/**")
.excludePathPatterns("/api","/api/doLogin");
}
}
首先創建一個FileController類
① 跳轉文件上傳的頁面
//跳轉文件上傳的頁面
@RequestMapping("/file-upload")
public String userList(){
return "file-upload";
② 實現文件上傳的功能
@RequestMapping("/doAddForUser")
public String doAdd(User user, @RequestParam("file") MultipartFile files, HttpServletRequest request) throws IOException {
//String path = null;
if (files != null && !files.isEmpty()){
String name = UUID.randomUUID().toString().replace("-","");
//獲取文件的擴展名
String ext = FilenameUtils.getExtension(files.getOriginalFilename());
//設置文件上傳的路徑
String url =request.getSession().getServletContext().getRealPath("/upload/");
File file = new File(url);
if (!file.exists()){
file.mkdir();
}
//測試路徑
System.out.println(request.getServletPath()+ "/upload");
System.out.println(request.getContextPath() + "/upload/");
//以絕對路徑保存重命名后的文件
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";
}
注:如何想要實現多文件上傳需要更改的地方如下:
③ 實現多文件上傳功能
在這個項目中并未實現多文件上傳功能
private void commons(Object obj, @RequestParam("file") CommonsMultipartFile[] files, HttpServletRequest request) throws IOException {
//String path = null;
for (int i = 0; i < files.length; i++) {
if (files[i] != null && !files[i].isEmpty()){
String name = UUID.randomUUID().toString().replace("-","");
//獲取文件的擴展名
String ext = FilenameUtils.getExtension(files[i].getOriginalFilename());
//設置文件上傳的路徑
String url =request.getSession().getServletContext().getRealPath("/upload/");
File file = new File(url);
if (!file.exists()){
file.mkdir();
}
//測試路徑
System.out.println(request.getServletPath()+ "/upload");
System.out.println(request.getContextPath() + "/upload/");
//以絕對路徑保存重命名后的文件
files[i].transferTo(new File(url+"/"+name+"."+ext));
if (i == 0){
5. 項目優化
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);
}
}
}
}
對于前后端不分離的項目,多數使用的是頁面緩存優化,當系統某一瞬間遭受巨大流量時,當第一個用戶進行頁面訪問時可以將該頁面數據進行緩存,這樣,后來的用戶訪問到的頁面都是從緩存中獲取的,這樣就減少了 對數據庫的操作,減輕了數據庫的壓力,從而達到優化的處理。
① 導入依賴
org.springframework.boot
spring-boot-starter-data-redis
org.apache.commons
commons-pool2
② yaml配置
## Redis配置
redis:
# 服務器地址
host: localhost
# 端口
port: 6379
# 數據庫
database: 0
# 超時時間
connect-timeout: 10000ms
lettuce:
pool:
# 最大連接數
max-active: 8
# 最大連接阻塞等待時間 默認 -1
max-wait: 10000ms
# 最大空閑時間 默認8
max-idle: 200
# 最小空閑連接 默認8
min-idle: 5
④ Redis序列化處理
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory){
RedisTemplate redisTemplate = new RedisTemplate<>();
//key序列化
redisTemplate.setKeySerializer(new StringRedisSerializer());
//value序列化
redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
//hash類型key的序列化
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
//hash類型value的序列化
redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
redisTemplate.setConnectionFactory(redisConnectionFactory);
return redisTemplate;
}
}
③ 優化處理
@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中獲取頁面,如果不為空,則直接返回頁面
ValueOperations valueOperations = redisTemplate.opsForValue();
String html = (String) valueOperations.get("news-list");
if (!StringUtils.isEmpty(html)){
return html;
}
PageHelper.startPage(pageNo,pageSize);
List list = newsService.list();
PageInfo pageInfo = new PageInfo<>(list);
model.addAttribute("news",list);
model.addAttribute("pageInfo",pageInfo);
//如果為空,手動渲染,存入Redis中并返回
WebContext context = new WebContext(request, response, request.getServletContext(), request.getLocale(), model.asMap());
html = viewResolver.getTemplateEngine().process("news-list", context);
if (!StringUtils.isEmpty(html)){
//給緩存設置過期時間
valueOperations.set("news-list",html,60, TimeUnit.SECONDS);
}
return html;
}
④ Redis查看
6. 注意事項
注意@Controller和@RestController的區別,本項目使用的是模板渲染頁面,而@Controller就是用來響應頁面的;而@RestController是用來返回Json
在項目優化階段需要在方法上添加注解@ResponseBody,因為我們是將整個頁面進行緩存 ,所以要將頁面轉換成JSON進行存儲。
注入Thymeleaf解析器,將具體的 頁面進行解析成Json字符串進行存儲
將存入Redis中的數據加上過期時間,因為頁面中的數據要和數據庫保持一致,如果用戶看到是幾十秒之前或一分鐘之前的數據還是勉強可以接受的。
目前代碼已經同步到Gitee:
https://gitee.com/gao-wumao/supplier
如果有需要的自行前去倉庫拉取
特別聲明:以上內容(如有圖片或視頻亦包括在內)為自媒體平臺“網易號”用戶上傳并發布,本平臺僅提供信息存儲服務。
Notice: The content above (including the pictures and videos if any) is uploaded and posted by a user of NetEase Hao, which is a social media platform and only provides information storage services.