前期环境
做一个信息提交和图片上传的表单。
主要技术:
- mysql 数据库
2.springboot 框架
3.前台 thymeleaf模板
4.数据库连接 spring jpa
载入controller层
@RequestMapping("/dictoryChange")public String dicitoryChange(Model model){List<CatalogDto> list = iDictoryService.getDictoryList();DictoryDto d = new DictoryDto();d.setName("aa");model.addAttribute("dictory", d);model.addAttribute("list", list);return "dictoryChange";}
跳转一个页面,并传递一个DictoryDto 的对象作为名为dictory的对象传递过去,和一个用来做列表的list对象传递到前端thymeleaf模板中。
thymeleaf模板
<!DOCTYPE html>
<html>
<head><meta charset="utf-8" /><title>目录登录</title>
</head>
<body>
<form method="post" th:action="@{/dictoryUpdate}" th:method="post" th:object="${dictory}" enctype="multipart/form-data"><p class="login_info">目录登录</p>父级目录:<select><option th:value="${em}"></option></select><br>目录名称:<input class="login_textbox" type="text" name="name" th:field="*{name}">目录图片:<input type="file" name="file00">备 注:<input class="login_textbox" type="text" name="mark" th:field="*{mark}"><button class="login_btn" type="submit">目录登录</button>
</form>
</body>
</html>
模板对应的使用th:object来介绍对应的对象,这个对象也会对应的提交上去,th:field=“*{name}” 使用th:field来绑定对应的对象的属性,使用*{}*来获取对应的属性值。文件是作为单独的属性去设置的<input type=“file” name=“file00”> 会将提交的文件对象放在file00提交上去。
接收的controller
th:action=“@{/dictoryUpdate}” 指向对应的控制层的url
@RequestMapping(value = "/dictoryUpdate",method = RequestMethod.POST)public String dictoryUpdate(@RequestParam("file00") MultipartFile file, @ModelAttribute Dictory dictory,Model model) throws IOException {if(!file.isEmpty()){//获得文件原始名称String originalFilename = file.getOriginalFilename();file.transferTo(new File("F:\\myweb\\img\\"+originalFilename));dictory.setImg(originalFilename);}model.addAttribute("msg","更新成功1");iDictoryService.updateDictory(dictory);return "success";}
这里居然能接收这么多的参数我也是想不到的
- @RequestParam(“file00”) MultipartFile file 接收文件
- @ModelAttribute Dictory dictory 接收对应的对象值
最后调用jpa下的sava保存对象值就可以了。还有需要在application.yml中配置对应的属性值:
server:port: 8080
spring:thymeleaf:cache: falsemode: HTML5suffix: .htmlencoding: UTF-8jpa:properties:hibernate:hbm2ddl:auto: updatedialect: org.hibernate.dialect.MySQL5InnoDBDialectformat_sql: trueenable_lazy_load_no_trans: trueshow-sql: truedatasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://自己的数据库ip地址:3306/testusername: rootpassword: w25UKsFgl(C+hikari:# 连接池最大连接数,默认是 10maximum-pool-size: 60# 链接超时时间,默认 30000(30 秒)connection-timeout: 60000# 空闲连接存活最大时间,默认 600000(10 分钟)idle-timeout: 60000# 连接将被测试活动的最大时间量validation-timeout: 3000# 此属性控制池中连接的最长生命周期,值 0 表示无限生命周期,默认 1800000(30 分钟)max-lifetime: 60000# 连接到数据库时等待的最长时间(秒)login-timeout: 5# 池中维护的最小空闲连接数minimum-idle: 10