Spring Boot下如何校验Spring MVC的请求参数及如何自定义校验注解

一、前言

小编最近在经历后端框架的迁移,虽然不是小编来做,但是有个分页的情况让小编和一个同事去搞。说一下小编这边的需求:原来框架使用​Mybatis-plus​进行分页,要更换的新框架若依是使用Pagehelper​。所以现在需求让我们把若依的干掉,使用Mybatis-plus,Mybatis-plus​的生态还是挺好的,方便,最重要的是和原来的框架一样,不需要更改。存在问题:需要把若依以前的分页全部改成​Mybatis-plus的分页,那我们就按个换喽,谁让咱们喜欢搬砖!

先说一下问题出现的原因:Mybatis和Mybatis-plus存在冲突,​Pagehelper依赖于Mybatis,所以冲突了!!

解决方案:删​Pagehelper和Mybatis​的依赖,然后一点点的改若依一些基本配置的分页就好,最后在加上Mybatis-plus的分页插件配置!最最重要的是要扫描到写的分页插件,不然不生效!​

二、删依赖

1、删除根目录的依赖

复制

<!-- Mybatis 依赖配置 -->
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>${spring-boot.mybatis}</version>
</dependency>

<!-- pagehelper 分页插件 -->
<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper-spring-boot-starter</artifactId>
    <version>${pagehelper.boot.version}</version>
</dependency>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.

复制

<spring-boot.mybatis>2.2.2</spring-boot.mybatis>
  • 1.

2、根目录添加依赖

复制

<!--   mybatis-plus     -->
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>${spring-boot.mybatis-plus}</version>
</dependency>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

复制

<spring-boot.mybatis-plus>3.5.1</spring-boot.mybatis-plus>
  • 1.

3、ruoyi-common-core模块删除依赖

复制

<!-- Pagehelper -->
<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper-spring-boot-starter</artifactId>
</dependency>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

三、修改文件

1、注释PageUtils

整个类全部注释!

复制

/**
 * 分页工具类
 * 
 * @author ruoyi
 */
public class PageUtils extends PageHelper{}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

2、注释BaseController分页方法

复制

/**
 * 设置请求分页数据
 */
protected void startPage(){
    PageUtils.startPage();
}

/**
 * 清理分页的线程变量
 */
protected void clearPage(){
    PageUtils.clearPage();
}

/**
 * 响应请求分页数据
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
protected TableDataInfo getDataTable(List<?> list){
    TableDataInfo rspData = new TableDataInfo();
    rspData.setCode(HttpStatus.SUCCESS);
    rspData.setRows(list);
    rspData.setMsg("查询成功");
    rspData.setTotal(new PageInfo(list).getTotal());
    return rspData;
}
  • 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.

四、配置Mybatis-plus分页

1、在ruoyi-common-core中新建配置类

复制

@Configuration
public class MybatisPlusConfig {

    /**
     * 新的分页插件,一缓和二缓遵循mybatis的规则,需要设置 MybatisConfiguration#useDeprecatedExecutor = false 避免缓存出现问题(该属性会在旧插件移除后一同移除)
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }

}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.

2、配置上可以扫描的

我们发现在core中已经给了我们提示,他配置了一个,我们只需要把我们刚刚写的配置类加上去,就可以扫描到这配置,然后生效了!!

不配置不生效(切记切记)!

我们找到所在位置,添加上全路径即可,这里我们对若依的架构修改了名称,也就是若依的core包下的!

五、修改ruoyi-modules-system

我们的宗旨是不影响之前的使用,需要我们新写一个分页,因为他们的export接口都使用了原来的分页,虽然分页没了,但是只要不调用还是不会报错的!
我们以一个controller的改造为例:

1、SysConfigController改造

原来的方法:

复制

/**
 * 获取参数配置列表
 */
@RequiresPermissions("system:config:list")
@GetMapping("/list")
public TableDataInfo list(SysConfig config){
    startPage();
    List<SysConfig> list = configService.selectConfigList(config);
    return getDataTable(list);
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.

修改后的方法:

这里统一返回值我是使用我们以前架构的,大家也可以使用若依自带的AjaxResult,只需要添加上Page即可,原来的方法我们不动,重新写一个两个参数的方法。

复制

/**
 * 获取参数配置列表
 */
@RequiresPermissions("system:config:list")
@GetMapping("/list")
public R list(Page page, SysConfig config) {
    return R.ok(configService.selectConfigList(page, config));
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.

2、ISysConfigService新增分页方法

复制

/**
 * 新分页
 * @param page
 * @param config
 * @return
 */
Page<SysConfig> selectConfigList(Page page,SysConfig config);
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

3、SysConfigServiceImpl新增分页实现方法

复制

@Override
public Page<SysConfig> selectConfigList(Page page, SysConfig config) {
    return configMapper.selectConfigList(page,config);
}
  • 1.
  • 2.
  • 3.
  • 4.

4、SysConfigMapper新增分页接口

复制

/**
 * 新分页
 * @param page
 * @param config
 * @return
 */
Page<SysConfig> selectConfigList(Page page,@Param("config") SysConfig config);
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

5、总结

这样依次对ruoyi-modules-system项目进行修改,还有一些job和gen,不要和不用的就注释掉,只要不报错,原来的项目分页就可以展示出来,原来不改造之前是total和pages都是0,改造后恢复正常。

总的来说就是删依赖,加依赖,注释一些不要的,添加一个新的分页方法即可,都是搬砖的活,哈哈!!

六、补充

这样之后我们发现system项目中的分页是有问题,是因为xml文件里没有指定对象.属性。于是把xml的一个例子修改了,现在分享给大家:

复制

<select id="selectDeptList" resultMap="SysDeptResult">
    <include refid="selectDeptVo"/>
    where d.del_flag = '0'
    <if test="dept.deptId != null and dept.deptId != 0">
        AND dept_id = #{dept.deptId}
    </if>
    <if test="dept.parentId != null and dept.parentId != 0">
        AND parent_id = #{dept.parentId}
    </if>
    <if test="dept.deptName != null and dept.deptName != ''">
        AND dept_name like concat('%', #{dept.deptName}, '%')
    </if>
    <if test="dept.status != null and dept.status != ''">
        AND status = #{dept.status}
    </if>
    <!-- 数据范围过滤 -->
    ${dept.params.dataScope}
    order by d.parent_id, d.order_num
</select>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot中,可以使用Spring Boot Validation来对参数名称进行校验。通过引入spring-boot-starter-validation依赖,可以使用Spring Validator对参数进行校验Spring Validator是对Hibernate Validator的进一步封装,同时支持Spring MVC的自动校验。你可以在Spring官方文档中找到更多关于Spring Boot Validation的详细信息\[2\]。 在使用Spring Boot Validation时,可以使用@Validated注解来标记需要校验的类型、方法和方法参数。@Validated注解属于org.springframework.validation.annotation包,是Spring校验机制之一。它具有分组校验的功能,可以用于类型、方法和方法参数上,但不能用于成员属性(field)\[3\]。 通过使用Spring Boot Validation,你可以简化参数校验的代码,提高代码的可读性和美观性。 #### 引用[.reference_title] - *1* *3* [Spring Boot参数校验](https://blog.csdn.net/qq1929892209/article/details/126133350)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Spring Boot 参数校验校验工具类](https://blog.csdn.net/WEDUEST/article/details/121594610)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值