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

在这里插入图片描述

跨域配置

  • 启动类中加以下代码
/***
	 * 跨域配置
	 * 
	 * @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();
		}
	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值