hooyantsing's Blog

第124次课程_框架项目案例_T3L3

字数统计: 787阅读时长: 4 min
2020/03/13

源辰76班

第124次课程

2020.03.13

内容

框架项目案例_T3L3[廖彦]

项目路径:

1.SpringBoot自定义配置参数和文件上传大小限制

application.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#server.port=80
#自定义的配置参数
myUploadPath: E:/ba1/
#服务器端口:80
server:
  port: 80
spring:
#连接数据库配置  
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/c76-s3-phooy-blog
    username: root
    password: a
    driver-class-name: com.mysql.jdbc.Driver
  #静态资源映射
  resources:
    staticLocations:
      - classpath:/resources/
      - classpath:/static/
      - file:d:${myUploadPath}
  #文件上传大小限制
  servlet:
    multipart:
      maxFileSize: 10000000
#mybatis
mybatis:
  #实体类别名 com.yc.C76S3PlySpringBoot.bean -> bean
  type-aliases-package: com.yc.blog.bean
  #驼峰命名法 类成员别名
  configuration:
    map-underscore-to-camel-case: true
#测试类配置  
logging:
  level:
    com:
      yc:
        blog:
          dao: debug
          web: warn

2.通过表达式读取yml文件里的参数和静态资源映射

IndexAction.java

myUploadPath代表的是上传文件存储路径,

@Value:配置静态资源位置

紫色部分SpringBoot文件上传对象

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package com.yc.blog.web;
import java.io.File;
import java.io.IOException;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yc.blog.bean.Article;
import com.yc.blog.bean.ArticleExample;
import com.yc.blog.bean.User;
import com.yc.blog.dao.ArticleMapper;
import com.yc.blog.dao.CategoryMapper;
import com.yc.blog.vo.Result;
@Controller
public class IndexAction {
     
     @Resource
     private ArticleMapper am;
     
     @Resource
     private CategoryMapper cm;
     
     @ModelAttribute
     public void init(Model m) {
           //查询分类列表
           m.addAttribute("clist",cm.selectByExample(null));
     }
     
     /*
      * 首页
      */
     @GetMapping({"/","index","index.html"})
     public String index(@RequestParam(defaultValue = "1")  Integer page,Model m) {
           Page<Article> pg = PageHelper.startPage(page, 5);
           //注意:PageHelper.startPage(page, 5)必须在查询代码的前一行
           am.selectByExampleWithBLOBs(null);
           m.addAttribute("alist",pg);
           return "index";
     }
     
     /*
      * 文章详情
      */
     @GetMapping({"article"})
     public String article(Integer id,Model m) {
           Article a = am.selectByPrimaryKey(id);
           m.addAttribute(a);
           return "article";
     }
     
     /*
      * 分类查询
      */
     @GetMapping({"category"})
     public String category(Integer  id,@RequestParam(defaultValue = "1") Integer page,Model m) {
           Page<Article> pg = PageHelper.startPage(page, 5);
           ArticleExample ae = new ArticleExample();
           ae.createCriteria().andCategoryidEqualTo(id);
           am.selectByExampleWithBLOBs(ae);
           m.addAttribute("alist",pg);
           m.addAttribute("id",id);
           return "category";
     }
     
     @GetMapping({"toreg"})
     public String toreg() {
           return "reg";
     }
     
     /*
      * AJAX 方法使用@ResponseBody返回字符串
      */
     @Value("${myUploadPath}")
     private String myUploadPath;
     @ResponseBody
     @PostMapping({"reg"})
     public Result reg(User user,@RequestParam("file")  MultipartFile file)  throws IllegalStateException, IOException {
           System.out.println(user);
           file.transferTo(new File(myUploadPath +  file.getOriginalFilename()));
           return  new Result(0,"用户注册成功");
     }
     
     
}

Result.java

向前端返回数据时,推荐将信息打包成规范化的一个类,类内包含码、信息和数据等内容。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package com.yc.blog.vo;
public class Result {
     private int code;
     private String msg;
     private Object data;
     public Result(int code) {
           super();
           this.code = code;
     }
     public Result(int code, String msg) {
           super();
           this.code = code;
           this.msg = msg;
     }
     public Result(int code, String msg, Object data) {
           super();
           this.code = code;
           this.msg = msg;
           this.data = data;
     }
     public int getCode() {
           return code;
     }
     public void setCode(int code) {
           this.code = code;
     }
     public String getMsg() {
           return msg;
     }
     public void setMsg(String msg) {
           this.msg = msg;
     }
     public Object getData() {
           return data;
     }
     public void setData(Object data) {
           this.data = data;
     }
     
}
CATALOG