添加过滤器:
@Bean
public FilterRegistrationBean filterRegistration() {
FilterRegistrationBean registration = new FilterRegistrationBean();
registration.setFilter(new CustomFilter());
List<String> urlList = new ArrayList<String>();
urlList.add("/*");
registration.setUrlPatterns(urlList);
registration.setName("UrlFilter");
registration.setOrder(1);
return registration;
}
添加资源视图映射:
@Bean
public InternalResourceViewResolver internalResourceViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/");
resolver.setSuffix(".html");
return resolver;
}
添加拦截器/默认http返回格式(添加了jackson-xml后有用):
@Configuration
public class WebRoleConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new RoleInterceptor()).addPathPatterns("/**");
WebMvcConfigurer.super.addInterceptors(registry);
}
@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
configurer.defaultContentType(MediaType.APPLICATION_JSON_UTF8);
configurer.favorPathExtension(false);
WebMvcConfigurer.super.configureContentNegotiation(configurer);
}
}
jpa null字段忽略更新统一配置:
@EnableJpaRepositories(value="xxx.xxx.xxx.xxx.repo", repositoryBaseClass = SimpleJpaRepositoryImpl.class)
public class MyApplication(){}
public class SimpleJpaRepositoryImpl<T, ID> extends SimpleJpaRepository<T, ID> {
private final JpaEntityInformation<T, ?> entityInformation;
private final EntityManager em;
@Autowired
public SimpleJpaRepositoryImpl(JpaEntityInformation<T, ?> entityInformation, EntityManager entityManager) {
super(entityInformation, entityManager);
this.entityInformation = entityInformation;
this.em = entityManager;
}
/**
* 通用save方法 :新增/选择性更新
*/
@Override
@Transactional
public <S extends T> S save(S entity) {
// 获取ID
ID entityId = (ID) this.entityInformation.getId(entity);
T managedEntity;
T mergedEntity;
if (entityId == null) {
em.persist(entity);
mergedEntity = entity;
} else {
Optional<T> findById = this.findById(entityId);
if (!findById.isPresent()) {
em.persist(entity);
mergedEntity = entity;
}else {
managedEntity = findById.get();
if (managedEntity == null) {
em.persist(entity);
mergedEntity = entity;
} else {
BeanUtils.copyProperties(entity, managedEntity, getNullProperties(entity));
em.merge(managedEntity);
mergedEntity = managedEntity;
}
}
}
return entity;
}
/**
* 获取对象的空属性
*/
private static String[] getNullProperties(Object src) {
// 1.获取Bean
BeanWrapper srcBean = new BeanWrapperImpl(src);
// 2.获取Bean的属性描述
PropertyDescriptor[] pds = srcBean.getPropertyDescriptors();
// 3.获取Bean的空属性
Set<String> properties = new HashSet<>();
for (PropertyDescriptor propertyDescriptor : pds) {
String propertyName = propertyDescriptor.getName();
Object propertyValue = srcBean.getPropertyValue(propertyName);
if (StringUtils.isEmpty(propertyValue)) {
srcBean.setPropertyValue(propertyName, null);
properties.add(propertyName);
}
}
return properties.toArray(new String[0]);
}
}
jpa开启创建时间createdDate:
@EnableJpaAuditing
public class MyApplication(){}
@EntityListeners(AuditingEntityListener.class)
public class Entity(){}
spring 开启跨域访问:
private CorsConfiguration buildConfig() {
CorsConfiguration corsConfiguration = new CorsConfiguration();
corsConfiguration.addAllowedOrigin("*");
corsConfiguration.addAllowedHeader("*");
corsConfiguration.addAllowedMethod("*");
corsConfiguration.addExposedHeader("x-auth-token");
corsConfiguration.addExposedHeader("x-total-count");
return corsConfiguration;
}
/**
* 跨域过滤器
*
* @return
*/
@Bean
public CorsFilter corsFilter() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", buildConfig()); // 4
return new CorsFilter(source);
}
mysql 8 连接方式:
spring.jpa.database-platform=org.hibernate.dialect.MySQL8Dialect
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://{your-host}:{your-port}/locker?useUnicode=true&characterEncoding=utf-8&useSSL=false&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=Hongkong
springboot 2.x 默认使用hikari连接池 推荐配置:
spring.datasource.type=com.zaxxer.hikari.HikariDataSource
spring.datasource.hikari.minimum-idle=10
spring.datasource.hikari.auto-commit=true
spring.datasource.hikari.idle-timeout=300000
spring.datasource.hikari.pool-name=DatebookHikariCP
spring.datasource.hikari.max-lifetime=1800000
spring.datasource.hikari.connection-timeout=30000
spring.datasource.hikari.connection-test-query=SELECT 1
网友评论