为spring get请求添加自定义的参数处理(如下划线转驼峰)

本文介绍了如何为Spring的GET请求添加自定义处理,将URL参数中的下划线自动转换为驼峰命名。首先创建了一个自定义注解,接着实现ServletModelAttributeMethodProcessor来处理参数,然后编写数据处理类进行实际转换,最后将这些组件正确注册到Spring应用上下文中。

1.生成自己的注解(为了确定在哪些位置使用)

/**
 * 关闭patch delete的model处理,否则会报错
 */
@Target({
   
   ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface AliasProcessor {
   
   
}
/**
 * 处理Get 请求参数的驼峰问题
 * @author lw
 */
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ValueFrom {
   
   
    /**
     * 参数名(别名)列表
     */
    String[] value();
}

2.实现自己的ServletModelAttributeMethodProcessor


/**
 * 为了减少使用 @RequestPath  将get参数封装到实体类中 重写ModelAttributeMethodProcessor
 * 注:由于get请求为非raw请求,spring默认使用@ModelArrtribute注解,不会自动将下划线的数据转为驼峰数据
 * 所以需要自定义一个处理器,进行该操作 *
 * @author lw
 */

public class AliasModelAttributeMethodProcessor  extends ServletModelAttributeMethodProcessor {
   
   

    private ApplicationContext applicationContext;

    /**
     * 过滤掉patch请求,防止报错
     */
    @Override
    public boolean supportsParameter(MethodParameter parameter) {
   
   
        return parameter.getMethodAnnotation(AliasProcessor.class
Spring和MyBatis整合的情况下,可以通过在MyBatis的配置文件中配置一个插件来实现下划线驼峰。 具体操作如下: 1. 在MyBatis的配置文件中添加以下插件配置: ``` <plugins> <plugin interceptor="org.apache.ibatis.plugin.Interceptor"> <property name="properties"> <property name="helperDialect" value="mysql"/> <property name="offsetAsPageNum" value="true"/> <property name="rowBoundsWithCount" value="true"/> <property name="reasonable" value="true"/> </property> <property name="typeAliases"> <typeAlias type="com.example.domain.User" alias="User"/> </property> <property name="mapperLocations" value="classpath:com/example/mapper/*.xml"/> </plugin> </plugins> ``` 2. 在Spring的配置文件中配置一个Bean,将上一步中配置的插件添加到SqlSessionFactory中: ``` <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="mapperLocations" value="classpath:com/example/mapper/*.xml"/> <property name="plugins"> <array> <bean class="com.example.plugin.CamelCaseInterceptor"/> </array> </property> </bean> ``` 3. 编写一个插件类,实现下划线驼峰的逻辑: ``` public class CamelCaseInterceptor implements Interceptor { @Override public Object intercept(Invocation invocation) throws Throwable { Object[] args = invocation.getArgs(); MappedStatement ms = (MappedStatement) args[0]; Object parameter = args[1]; BoundSql boundSql = ms.getBoundSql(parameter); String sql = boundSql.getSql(); // 下划线驼峰 sql = CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, sql); BoundSql newBoundSql = new BoundSql(ms.getConfiguration(), sql, boundSql.getParameterMappings(), boundSql.getParameterObject()); MappedStatement newMs = copyFromMappedStatement(ms, new BoundSqlSqlSource(newBoundSql)); args[0] = newMs; return invocation.proceed(); } @Override public Object plugin(Object target) { return Plugin.wrap(target, this); } @Override public void setProperties(Properties properties) { // do nothing } private MappedStatement copyFromMappedStatement(MappedStatement ms, SqlSource newSqlSource) { MappedStatement.Builder builder = new MappedStatement.Builder(ms.getConfiguration(), ms.getId(), newSqlSource, ms.getSqlCommandType()); builder.resource(ms.getResource()); builder.fetchSize(ms.getFetchSize()); builder.statementType(ms.getStatementType()); builder.keyGenerator(ms.getKeyGenerator()); builder.keyProperty(StringUtils.join(ms.getKeyProperties(), ",")); builder.timeout(ms.getTimeout()); builder.parameterMap(ms.getParameterMap()); builder.resultMaps(ms.getResultMaps()); builder.cache(ms.getCache()); builder.useCache(ms.isUseCache()); return builder.build(); } } ``` 通过以上步骤,即可实现在Spring和MyBatis整合的情况下将数据库中下划线命名的字段换为驼峰命名的字段。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值