@NotNull, @NotBlank, @NotEmpty的区别
- @NotNull 适用于基本数据类型(Integer,Long,Double等等),当 @NotNull 注解被使用在 String
类型的数据上,则表示该数据不能为 Null(但是可以为 Empty) - @NotBlank 适用于 String 类型的数据上,加了@NotBlank 注解的参数不能为 Null 且 trim() 之后
size > 0 - @NotEmpty 适用于 String、Collection集合、Map、数组等等,加了@NotEmpty 注解的参数不能为 Null
或者 长度为 0
时间格式化工具
使用DateTimeFormatter类进行时间格式化
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
formatter.format(product.getCreateTime());
Springboot 全局配置文件的使用
导入预加载配置文件依赖
<!-- Springboot processor-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</dependency>
编写一个配置类,这个类会在项目启动前进行预加载配置文件,并将配置文件中的配置字段加载到项目中
// 在配置文件中进行预处理加载,读取到该对象中
// image.server表示匹配以image.server开头的字段
@ConfigurationProperties("image.server")
@Data
@Component
public class ImageServerConfig {
// 如此处匹配的是 image.server.url
private String url;
private String username;
private String password;
}
在配置文件application.properties中编写
# 图片服务器配置
image.server.url=XXX
image.server.username=YYY
image.server.password=ZZZ
Mybatis-plus 分页
Mybatis-plus分页需要使用到一个插件PaginationInnerInterceptor
第一步,编写配置类,将PaginationInnerInterceptor分页插件导入到IOC容器中
@Configuration
@MapperScan("com.example.mystore.persistence")
// 配置分页插件
public class MyBatisPlusConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor(){
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
// 添加PaginationInnerInterceptor,并设置连接池为Mysql
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
}
这样就可以直接使用分页了,只用Page类完成分页,Page类中关键的字段
records:当前页中的数据对象
current:当前第几页
size:每一页有多少条记录
total:总共的数据记录数
Page<Product> result = new Page<>();
// 设置当前第几页
result.setCurrent(pageNum);
// 设置每页大小
result.setSize(pageSize);
// 调用selectPage方法,第一个参数是Page类,第二个参数是查询的条件
result = productMapper.selectPage(result, queryWrapper);