前后端分离----跨域配置+日期转化+动态sql

    技术2024-11-11  12

    跨域配置

    启动类中加以下代码 /*** * 跨域配置 * * @return */ @Bean public WebMvcConfigurer corsConfigurer() { return new WebMvcConfigurer() { /* * 添加跨域访问映射路径 */ @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**")// 允许跨域访问当前项目的所有路径 .allowCredentials(true)// 是否允许使用凭证(session) .allowedMethods("*");// 设置跨域访问的请求为任意请求 } }; }

    跨域配置

    启动类中加以下代码 @Autowired private RequestMappingHandlerAdapter handlerAdapter; /** * 此方法解决前台提交的日期参数绑定不正确问题,将自己实现的StringToDateConverter交给spring,让其知道如何进行处理 */ @PostConstruct //@PostContruct是spring框架的注解,在方法上加该注解会在项目启动的时候执行该方法,也可以理解为在spring容器初始化的时候执行该方法。 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) { // TODO Auto-generated method stub 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

    映射接口中 // @Select("select * from department") // public List<Department> getAllDep(); @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(); } }
    Processed: 0.060, SQL: 9