1.生成自己的注解(为了确定在哪些位置使用)
1
2
3
4
5
6
7
8
|
/**
* 关闭patch delete的model处理,否则会报错
*/
@Target ({ElementType.METHOD})
@Retention (RetentionPolicy.RUNTIME)
@Documented
public @interface AliasProcessor {
}
|
2.实现自己的ServletModelAttributeMethodProcessor
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
/**
* 为了减少使用 @RequestPath 将get参数封装到实体类中 重写ModelAttributeMethodProcessor
* 所以需要自定义一个处理器,进行该操作 *
* @author lw
*/
public class AliasModelAttributeMethodProcessor extends ServletModelAttributeMethodProcessor {
private ApplicationContext applicationContext;
/**
* 过滤掉patch请求,防止报错
*/
@Override
public boolean supportsParameter(MethodParameter parameter) {
return parameter.getMethodAnnotation(AliasProcessor. class )!= null ;
}
public AliasModelAttributeMethodProcessor(ApplicationContext applicationContext) {
super ( true );
this .applicationContext=applicationContext;
}
@Override
protected void bindRequestParameters(WebDataBinder binder, NativeWebRequest request) {
AliasDataBinder aliasBinder = new AliasDataBinder(binder.getTarget(), binder.getObjectName());
RequestMappingHandlerAdapter requestMappingHandlerAdapter = this .applicationContext.getBean(RequestMappingHandlerAdapter. class );
requestMappingHandlerAdapter.getWebBindingInitializer().initBinder(aliasBinder);
aliasBinder.bind(request.getNativeRequest(ServletRequest. class ));
}
}
|
3.自己的数据处理类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
/**
* 重新数据处理类
* @author lw
*/
public class AliasDataBinder extends ExtendedServletRequestDataBinder {
public AliasDataBinder(Object target, String objectName) {
super (target, objectName);
}
/**
* 复写addBindValues方法
* @param mpvs 这里面存的就是请求参数的key-value对
* @param request 请求本身, 这里没有用到
*/
@Override
protected void addBindValues(MutablePropertyValues mpvs, ServletRequest request) {
super .addBindValues(mpvs, request);
// 处理要绑定参数的对象
Class<?> targetClass = getTarget().getClass();
// 获取对象的所有字段(拿到Test类的字段)
Field[] fields = targetClass.getDeclaredFields();
// 处理所有字段
for (Field field : fields) {
// 原始字段上的注解
ValueFrom valueFromAnnotation = field.getAnnotation(ValueFrom. class );
// 若参数中包含原始字段或者字段没有别名注解, 则跳过该字段
if (mpvs.contains(field.getName()) || valueFromAnnotation == null ) {
continue ;
}
// 参数中没有原始字段且字段上有别名注解, 则依次取别名列表中的别名, 在参数中最先找到的别名的值赋值给原始字段
for (String alias : valueFromAnnotation.value()) {
// 若参数中包含该别名, 则把别名的值赋值给原始字段
if (mpvs.contains(alias)) {
// 给原始字段赋值
mpvs.add(field.getName(), mpvs.getPropertyValue(alias).getValue());
// 跳出循环防止取其它别名
break ;
}
}
}
}
}
|
4.注册到spring中
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
/**
* 为了获得context需要实现ApplicationContextAware接口
* @author lw
*/
@Configuration
public class WebmvcConfig implements ApplicationContextAware {
@Autowired
private RequestMappingHandlerAdapter adapter;
private ApplicationContext applicationContext = null ;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this .applicationContext=applicationContext;
}
/**
* 将自定义的processor添加到adapter中
*/
@PostConstruct
protected void injectSelfMethodArgumentResolver() {
List<HandlerMethodArgumentResolver> argumentResolvers = new ArrayList<>();
argumentResolvers.add( new AliasModelAttributeMethodProcessor( this .applicationContext));
argumentResolvers.addAll(adapter.getArgumentResolvers());
adapter.setArgumentResolvers(argumentResolvers);
}
}
|
补充知识:springboot - mybatis - 下划线与驼峰自动转换 mapUnderscoreToCamelCase
以前都是在mybatis.xml中来配置,但是spring boot不想再用xml配置文件。网上搜寻了好久,才找到设置办法:
sessionFactoryBean.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);
db配置文件源码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
package com.vip.qa.vop.config;
import com.alibaba.druid.pool.DruidDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
import java.util.Properties;
/**
* Created by danny.yao on 2017/10/25.
*/
@Configuration
@MapperScan (basePackages = VOPDataSourceConfig.PACKAGE, sqlSessionFactoryRef = "vopSqlSessionFactory" )
public class VOPDataSourceConfig {
static final String PACKAGE = "com.vip.qa.vop.mapper.vop" ;
@Value ( "${vop.datasource.url}" )
private String dbUrl;
@Value ( "${vop.datasource.username}" )
private String dbUser;
@Value ( "${vop.datasource.password}" )
private String dbPassword;
@Value ( "${vop.datasource.driver-class-name}" )
private String dbDriver;
@Bean (name = "vopDataSource" )
@Qualifier
@Primary
public DataSource vopDataSource() {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName(dbDriver);
dataSource.setUrl(dbUrl);
dataSource.setUsername(dbUser);
dataSource.setPassword(dbPassword);
return dataSource;
}
@Bean (name = "vopSqlSessionFactory" )
@Qualifier
@Primary
public SqlSessionFactory vopSqlSessionFactory( @Qualifier ( "vopDataSource" ) DataSource scepDataSource) throws Exception {
final SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
sessionFactoryBean.setDataSource(scepDataSource);
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
sessionFactoryBean.setMapperLocations(resolver.getResources( "classpath:/mybatis/vop/*.xml" ));
sessionFactoryBean.getObject().getConfiguration().setMapUnderscoreToCamelCase( true );
return sessionFactoryBean.getObject();
}
// @Bean(name = "vopTransactionManager")
// @Qualifier
// public DataSourceTransactionManager testDataTransactionManager() {
// return new DataSourceTransactionManager(vopDataSource());
// }
}
|
以上这篇为spring get请求添加自定义的参数处理操作(如下划线转驼峰)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/qq_36752632/article/details/90665221