- MyBatisPlus分页配置
MyBatis-plus
mybatis-plus 分页配置:
@Configuration public class MyBatisPlusConfiguration { @Bean public MybatisPlusInterceptor mybatisPlusInterceptor() { MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.H2)); return interceptor; } }
- 日期工具
//获取指定年月最后一刻 public static Date getLastMoment(String date) { String[] split = date.split("-"); Calendar c = Calendar.getInstance(); c.set(Calendar.YEAR, Integer.parseInt(split[0])); c.set(Calendar.MONTH, Integer.parseInt(split[1])-1); c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH)); //获取月最后一天 c.set(Calendar.HOUR_OF_DAY, 23); //将小时至23 c.set(Calendar.MINUTE, 59); //将分钟至59 c.set(Calendar.SECOND,59); //将秒至59 c.set(Calendar.MILLISECOND, 999); //将毫秒至999 return c.getTime(); } //获取指定年月最先一刻 public static Date getFirstMonment(String date) { String[] split = date.split("-"); Calendar c = Calendar.getInstance(); c.set(Calendar.YEAR, Integer.parseInt(split[0])); c.set(Calendar.MONTH, Integer.parseInt(split[1])-1); c.set(Calendar.DAY_OF_MONTH, c.getActualMinimum(Calendar.DAY_OF_MONTH)); c.set(Calendar.HOUR_OF_DAY, 0); c.set(Calendar.MINUTE, 0); c.set(Calendar.SECOND,0); c.set(Calendar.MILLISECOND, 0); return c.getTime(); }
- @Value单个变量从配置文件注入
#文件访问地址前缀 resources-url: <http://internal.doreen.com.cn:50898>
package com.erp.adm.configuration; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.PropertySource; import org.springframework.stereotype.Component; @Component @PropertySource("classpath:bootstrap.yml") public class ResourcesUrlConfig { //注入到成员变量, 如果要注入到静态变量,使用下列方式 @Value("${resources-url}") public String url; public static String RESOURCES_URL; //注入到参数中并set到静态方法 @Value("${resources-url}") public void setResourcesUrl(String resourcesUrl) { RESOURCES_URL = resourcesUrl; } }
- 使用依赖从配置文件内容多值注入变量
加上该依赖,配置文件能自动提示
<!--配置自定义ConfigurationProperties--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency>
- 配置文件中如下内容(yml)
myproxy: domainName: <http://gcm6aq.natappfree.cc>
- 会注入到如下内容中
@RestController @ConfigurationProperties(prefix = "myproxy") public class FileUploadController { @Autowired private UserService userService; @Autowired private GoodsService goodsService; private String domainName/* = "<http://tjvjnw.natappfree.cc>"*/; public String getDomainName() { return domainName; } public void setDomainName(String domainName) { this.domainName = domainName; } }
注意该字段需要get/set方法
- 跨域问题配置
@Configuration public class ResourceConfig implements WebMvcConfigurer { @Override public void addCorsMappings(CorsRegistry corsRegistry){ corsRegistry.addMapping("/**") .allowCredentials(true) .allowedOriginPatterns("*") //.allowedOrigins("*") .allowedMethods("*") .allowedHeaders("*") .maxAge(3600); } }