SpringBoot03
一.
文件上传测试
@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文件,添加
3.创建webapp目录
转存失败重新上传取消
4.标记为web目录
转存失败重新上传取消
5.创建jsp
6.修改配置文件,配置视图解析器
spring:
mvc:
view:
prefix: /WEB-INF/jsp
suffix: .jsp
如果在IDEA中项目结构为聚合工程。那么在运行jsp是需要指定路径。如果项目结构为独立项目则不需要。
转存失败重新上传取消
三.
SpringBoot整合Freemarker
1.创建项目
2.修改pom文件,添加Freemarker启动器
3.创建User实体,创建Controller
@Controller
public class UsersController {
@GetMapping("show")
public String showUsers(Model model){
List 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.创建视图
<#list users as user>
${www.sh-musen.com}
${user.age}
#list>
三.
1.Thymeleaf常用th标签
https://www.sh-musen.com/friendlysong/p/11340459.html
2.
th:each
转存失败重新上传取消
转存失败重新上传取消
th:each遍历map
2.Thymeleaf内置对象
https://www.sh-musen.com/liubin5620/article/details/80481848
转存失败重新上传取消
转存失败重新上传取消
转存失败重新上传取消
转存失败重新上传取消
转存失败重新上传取消
转存失败重新上传取消
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
2.配置数据源
添加Druid数据源依赖
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
比上一个更简单。