跨域配置
@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurer() {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowCredentials(true)// 是否允许使用凭证(session)
.allowedMethods("*");// 设置跨域访问的请求为任意请求
}
};
}
跨域配置
@Autowired
private RequestMappingHandlerAdapter handlerAdapter;
@PostConstruct
public void initEditableValidation() {
ConfigurableWebBindingInitializer initializer = (ConfigurableWebBindingInitializer) handlerAdapter.getWebBindingInitializer();
if (initializer.getConversionService() != null) {
GenericConversionService genericConversionService = (GenericConversionService) initializer.getConversionService();
genericConversionService.addConverter(new StringToDateConverter());
}
}
- 转换日期的工具类:StringToDateConverter.java
package com.bwl.base.util;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.commons.lang3.StringUtils;
import org.springframework.core.convert.converter.Converter;
public class StringToDateConverter implements Converter<String,Date>{
private static final String dateFormat = "yyyy-MM-dd HH:mm:ss";
private static final String shortDateFormat = "yyyy-MM-dd";
private static final String dateFormat2 = "yyyy/MM/dd HH:mm:ss";
private static final String shortDateFormat2 = "yyyy/MM/dd";
@Override
public Date convert(String source) {
if (StringUtils.isBlank(source)) {
return null;
}
source = source.trim();
try {
SimpleDateFormat formatter;
if (source.contains("-")) {
if (source.contains(":")) {
formatter = new SimpleDateFormat(dateFormat);
} else {
formatter = new SimpleDateFormat(shortDateFormat);
}
Date dtDate = formatter.parse(source);
return dtDate;
} else if (source.contains("/")) {
if (source.contains(":")) {
formatter = new SimpleDateFormat(dateFormat2);
} else {
formatter = new SimpleDateFormat(shortDateFormat2);
}
Date dtDate = formatter.parse(source);
return dtDate;
}
} catch (Exception e) {
throw new RuntimeException(String.format("parser %s to Date fail", source));
}
throw new RuntimeException(String.format("parser %s to Date fail", source));
}
}
动态sql
@SelectProvider(type = GetDep.class, method = "findDep")
public List<Department> getAllDep(Department department);
class GetDep {
public String findDep(Department department) {
return new SQL() {{
SELECT("dpmtid,dpmtname");
FROM("department");
if(department.getDpmtid() != null&&(department.getDpmtid()).toString()!="") {
WHERE("dpmtid=#{dpmtid}");
}
if(department.getDpmtname() != null&&department.getDpmtname()!="") {
WHERE("dpmtname=#{dpmtname}");
}
}}.toString();
}
}