SpringBoot03
一.
文件上传测试
<form action="/upload" enctype="multipart/form-data" method="post">
<input type="file" name="file">
<button type="submit" name="ok">ok</button>
</form>
@RequestMapping("/upload")
public String fileUpLoad(MultipartFile file) throws Exception{
System.out.println(file.getOriginalFilename());
file.transferTo(new File("D:/"+file.getOriginalFilename()));
return "index";
}
修改上传文件大小限制
spring:
servlet:
multipart:
max-file-size: 2MB
二.
SpringBoot整合JSP技术
1.创建项目
2.修改pom文件,添加
<!--添加jsp引擎,SpringBoot内置的Tomcat没有此依赖-->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<!--添加jstl坐标依赖-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
3.创建webapp目录
4.标记为web目录
5.创建jsp
6.修改配置文件,配置视图解析器
spring:
mvc:
view:
prefix: /WEB-INF/jsp
suffix: .jsp
如果在IDEA中项目结构为聚合工程。那么在运行jsp是需要指定路径。如果项目结构为独立项目则不需要。
三.
SpringBoot整合Freemarker
1.创建项目
2.修改pom文件,添加Freemarker启动器
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
3.创建User实体,创建Controller
@Controller
public class UsersController {
@GetMapping("show")
public String showUsers(Model model){
List<User> users = new ArrayList<>();
users.add(new User("zhangsan",1));
users.add(new User("lisi",2));
users.add(new User("wangwu",3));
model.addAttribute("users",users);
return "userList";
}
}
4.修改配置文件,配置视图解析器
5.创建视图
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<#list users as user>
<div>
<h2>${user.name}</h2>
<h2>${user.age}</h2>
</div>
</#list>
</body>
</html>
三.
1.Thymeleaf常用th标签
2.
th:each
th:each遍历map
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<table>
<tr th:each="m : ${map}">
<td th:text="${m.key}"></td>
<td th:text="${m.value.name}"></td>
<td th:text="${m.value.age}"></td>
</tr>
</table>
</body>
</html>
2.Thymeleaf内置对象
4.Thymeleaf常见配置
#以下都是默认值
spring.thymeleaf.prefix=classpath:/templates
spring.thymeleaf.suffix=.html
#配置视图模板类型,如果视图模板使用的是html5需要配置,这里默认是4
spring.thymeleaf.mode=HTML
spring.thymeleaf.encoding=utf-8
#响应类型
spring.thymeleaf.servlet.content-type=text/html
#配置页面缓存,默认为true,我们一般把它改成false
spring.thymeleaf.cache=true
四.
SpringBoot整合JDBC,通过@PropertySource读取配置信息
1.配置pom
<!--JDBC启动器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!--mysql数据库驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.27</version>
</dependency>
2.配置数据源
添加Druid数据源依赖
<!--Druid数据源依赖-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.12</version>
</dependency>
3.创建properties文件
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false
jdbc.username=root
jdbc.password=123
4.创建配置类
@Configuration
@PropertySource("classpath:/jdbc.properties")
public class JdbcConfiguration {
@Value("${jdbc.driverClassName}")
private String driverClassName;
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
@Bean
public DataSource getDataSource(){
DruidDataSource druidDataSource = new DruidDataSource();
druidDataSource.setDriverClassName(driverClassName);
druidDataSource.setUrl(url);
druidDataSource.setUsername(username);
druidDataSource.setPassword(password);
return druidDataSource;
}
}
5.创建Controller,测试
@Controller
public class TestController {
@Autowired
private DataSource dataSource;
@GetMapping("test")
public String test(){
return "test";
}
}
五.
SpringBoot整合JDBC,通过@ConfigurationProperties读取配置信息
1.配置application.properties
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false
jdbc.username=root
jdbc.password=123
2.创建JdbcProperties
@ConfigurationProperties(prefix = "jdbc")
@Getter
@Setter
public class JdbcProperties {
private String driverClassName;
private String url;
private String username;
private String password;
}
3.创建JdbcConfiguration
@Configuration
@EnableConfigurationProperties(JdbcProperties.class)
public class JdbcConfiguration {
@Bean
public DataSource getDataSource(JdbcProperties jdbcProperties){
DruidDataSource druidDataSource = new DruidDataSource();
druidDataSource.setDriverClassName(jdbcProperties.getDriverClassName());
druidDataSource.setUrl(jdbcProperties.getUrl());
druidDataSource.setUsername(jdbcProperties.getUsername());
druidDataSource.setPassword(jdbcProperties.getPassword());
return druidDataSource;
}
}
4.在Controller中debug测试
六.
@ConfigurationProperties优雅写法,以上一个例子为例
只需一个@ConfigurationProperties注解,无需创建JdbcProperties也可赋值。
@Configuration
public class JdbcConfiguration {
@Bean
@ConfigurationProperties(prefix = "jdbc")
public DataSource getDataSource(){
DruidDataSource druidDataSource = new DruidDataSource();
return druidDataSource;
}
}
直接将之前Properties类省了,还省了其他注解和一些set方法,简洁明了。
七.
在application.propertes中配置,默认数据源HikariDataSource
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.datasource.username=root
spring.datasource.password=123
#如果要改变数据源
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
比上一个更简单。