Browse Source

【升级】新增dict值校验注解

fengshuonan 4 years ago
parent
commit
a56415cadb

+ 8 - 0
guns-base-support/guns-core/src/main/java/cn/stylefeng/guns/core/context/system/SystemContext.java

@ -106,4 +106,12 @@ public interface SystemContext {
     * @date 2020/8/4 20:56
     * @date 2020/8/4 20:56
     */
     */
    boolean isRole(Long userOrRoleId);
    boolean isRole(Long userOrRoleId);
    /**
     * 根据字典类型获取字典的code值
     *
     * @author fengshuonan
     * @date 2020/8/9 14:18
     */
    List<String> getDictCodesByDictTypeCode(String... dictTypeCodes);
}
}

+ 66 - 0
guns-base-support/guns-core/src/main/java/cn/stylefeng/guns/core/validation/dict/DictValue.java

@ -0,0 +1,66 @@
/*
Copyright [2020] [https://www.stylefeng.cn]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
  http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Guns采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意以下几点:
1.请不要删除和修改根目录下的LICENSE文件。
2.请不要删除和修改Guns源码头部的版权声明。
3.请保留源码和相关描述文件的项目出处,作者声明等。
4.分发源码时候,请注明软件出处 https://gitee.com/stylefeng/guns-separation
5.在修改包名,模块名称,项目代码等时,请注明软件出处 https://gitee.com/stylefeng/guns-separation
6.若您的项目无法满足以上几点,可申请商业授权,获取Guns商业授权许可,请在官网购买授权,地址为 https://www.stylefeng.cn
 */
package cn.stylefeng.guns.core.validation.dict;
import javax.validation.Constraint;
import javax.validation.Payload;
import java.lang.annotation.*;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
/**
 * 检验值是否为字典值,验证sys_dict_data表中有没有相关的字典项
 * <p>
 * 本注解用的时候,一定要加dictType参数,用来表明验证的哪个字典类型中的值
 * <p>
 * dictType值来自数据库中sys_dict_type表的code值
 *
 * @author stylefeng
 * @date 2020/4/14 23:49
 */
@Documented
@Constraint(validatedBy = DictValueValidator.class)
@Target({ElementType.FIELD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface DictValue {
    String message() default "不正确的字典值,请检查数据库中是否录入该字典项";
    Class[] groups() default {};
    Class<? extends Payload>[] payload() default {};
    /**
     * 字典的类型
     */
    String[] dictType();
    @Target({ElementType.FIELD, ElementType.PARAMETER})
    @Retention(RUNTIME)
    @Documented
    @interface List {
        DictValue[] value();
    }
}

+ 57 - 0
guns-base-support/guns-core/src/main/java/cn/stylefeng/guns/core/validation/dict/DictValueValidator.java

@ -0,0 +1,57 @@
/*
Copyright [2020] [https://www.stylefeng.cn]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
  http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Guns采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意以下几点:
1.请不要删除和修改根目录下的LICENSE文件。
2.请不要删除和修改Guns源码头部的版权声明。
3.请保留源码和相关描述文件的项目出处,作者声明等。
4.分发源码时候,请注明软件出处 https://gitee.com/stylefeng/guns-separation
5.在修改包名,模块名称,项目代码等时,请注明软件出处 https://gitee.com/stylefeng/guns-separation
6.若您的项目无法满足以上几点,可申请商业授权,获取Guns商业授权许可,请在官网购买授权,地址为 https://www.stylefeng.cn
 */
package cn.stylefeng.guns.core.validation.dict;
import cn.stylefeng.guns.core.context.system.SystemContextHolder;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.List;
/**
 * 字典值校验
 *
 * @author stylefeng
 * @date 2020/4/14 23:49
 */
public class DictValueValidator implements ConstraintValidator<DictValue, String> {
    private String[] dictType;
    @Override
    public void initialize(DictValue constraintAnnotation) {
        this.dictType = constraintAnnotation.dictType();
    }
    @Override
    public boolean isValid(String dictValue, ConstraintValidatorContext context) {
        List<String> dictCodes = SystemContextHolder.me().getDictCodesByDictTypeCode(dictType);
        if (dictCodes != null && dictCodes.size() > 0) {
            return dictCodes.contains(dictValue);
        } else {
            return false;
        }
    }
}

+ 10 - 0
guns-base-support/guns-system/src/main/java/cn/stylefeng/guns/sys/core/context/SystemContextImpl.java

@ -29,6 +29,7 @@ import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.core.context.system.SystemContext;
import cn.stylefeng.guns.core.context.system.SystemContext;
import cn.stylefeng.guns.core.pojo.login.SysLoginUser;
import cn.stylefeng.guns.core.pojo.login.SysLoginUser;
import cn.stylefeng.guns.sys.modular.auth.service.AuthService;
import cn.stylefeng.guns.sys.modular.auth.service.AuthService;
import cn.stylefeng.guns.sys.modular.dict.service.SysDictDataService;
import cn.stylefeng.guns.sys.modular.role.entity.SysRole;
import cn.stylefeng.guns.sys.modular.role.entity.SysRole;
import cn.stylefeng.guns.sys.modular.role.param.SysRoleParam;
import cn.stylefeng.guns.sys.modular.role.param.SysRoleParam;
import cn.stylefeng.guns.sys.modular.role.service.SysRoleService;
import cn.stylefeng.guns.sys.modular.role.service.SysRoleService;
@ -58,6 +59,9 @@ public class SystemContextImpl implements SystemContext {
    @Resource
    @Resource
    private SysRoleService sysRoleService;
    private SysRoleService sysRoleService;
    @Resource
    private SysDictDataService sysDictDataService;
    @Override
    @Override
    public String getNameByUserId(Long userId) {
    public String getNameByUserId(Long userId) {
        return sysUserService.getNameByUserId(userId);
        return sysUserService.getNameByUserId(userId);
@ -102,4 +106,10 @@ public class SystemContextImpl implements SystemContext {
        SysRole sysRole = sysRoleService.getById(userOrRoleId);
        SysRole sysRole = sysRoleService.getById(userOrRoleId);
        return !ObjectUtil.isNull(sysRole);
        return !ObjectUtil.isNull(sysRole);
    }
    }
    @Override
    public List<String> getDictCodesByDictTypeCode(String... dictTypeCodes) {
        return sysDictDataService.getDictCodesByDictTypeCode(dictTypeCodes);
    }
}
}

+ 11 - 0
guns-base-support/guns-system/src/main/java/cn/stylefeng/guns/sys/modular/dict/mapper/SysDictDataMapper.java

@ -27,6 +27,8 @@ package cn.stylefeng.guns.sys.modular.dict.mapper;
import cn.stylefeng.guns.sys.modular.dict.entity.SysDictData;
import cn.stylefeng.guns.sys.modular.dict.entity.SysDictData;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import java.util.List;
/**
/**
 * 系统字典值mapper接口
 * 系统字典值mapper接口
 *
 *
@ -34,4 +36,13 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
 * @date 2020/3/13 16:12
 * @date 2020/3/13 16:12
 */
 */
public interface SysDictDataMapper extends BaseMapper<SysDictData> {
public interface SysDictDataMapper extends BaseMapper<SysDictData> {
    /**
     * 通过字典类型code获取字典编码值列表
     *
     * @author fengshuonan
     * @date 2020/8/9 14:27
     */
    List<String> getDictCodesByDictTypeCode(String[] dictTypeCodes);
}
}

+ 12 - 0
guns-base-support/guns-system/src/main/java/cn/stylefeng/guns/sys/modular/dict/mapper/mapping/SysDictDataMapper.xml

@ -2,4 +2,16 @@
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.stylefeng.guns.sys.modular.dict.mapper.SysDictDataMapper">
<mapper namespace="cn.stylefeng.guns.sys.modular.dict.mapper.SysDictDataMapper">
    <select id="getDictCodesByDictTypeCode" resultType="java.lang.String">
        SELECT
        dict.`code`
        FROM
        sys_dict_data dict
        INNER JOIN sys_dict_type type ON dict.type_id = type.id
        where type.code in
        <foreach collection="array" index="index" item="i" open="(" separator="," close=")">
            #{i}
        </foreach>
    </select>
</mapper>
</mapper>

+ 8 - 0
guns-base-support/guns-system/src/main/java/cn/stylefeng/guns/sys/modular/dict/service/SysDictDataService.java

@ -124,4 +124,12 @@ public interface SysDictDataService extends IService<SysDictData> {
     * @date 2020/5/1 9:44
     * @date 2020/5/1 9:44
     */
     */
    void changeStatus(SysDictDataParam sysDictDataParam);
    void changeStatus(SysDictDataParam sysDictDataParam);
    /**
     * 根据字典类型获取字典的code值
     *
     * @author fengshuonan
     * @date 2020/8/9 14:18
     */
    List<String> getDictCodesByDictTypeCode(String... dictTypeCodes);
}
}

+ 8 - 3
guns-base-support/guns-system/src/main/java/cn/stylefeng/guns/sys/modular/dict/service/impl/SysDictDataServiceImpl.java

@ -60,7 +60,7 @@ public class SysDictDataServiceImpl extends ServiceImpl<SysDictDataMapper, SysDi
        //构造条件
        //构造条件
        LambdaQueryWrapper<SysDictData> queryWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<SysDictData> queryWrapper = new LambdaQueryWrapper<>();
        if(ObjectUtil.isNotNull(sysDictDataParam)) {
        if (ObjectUtil.isNotNull(sysDictDataParam)) {
            //根据字典类型查询
            //根据字典类型查询
            if (ObjectUtil.isNotEmpty(sysDictDataParam.getTypeId())) {
            if (ObjectUtil.isNotEmpty(sysDictDataParam.getTypeId())) {
                queryWrapper.eq(SysDictData::getTypeId, sysDictDataParam.getTypeId());
                queryWrapper.eq(SysDictData::getTypeId, sysDictDataParam.getTypeId());
@ -85,8 +85,8 @@ public class SysDictDataServiceImpl extends ServiceImpl<SysDictDataMapper, SysDi
    public List<SysDictData> list(SysDictDataParam sysDictDataParam) {
    public List<SysDictData> list(SysDictDataParam sysDictDataParam) {
        //构造条件,查询某个字典类型下的
        //构造条件,查询某个字典类型下的
        LambdaQueryWrapper<SysDictData> queryWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<SysDictData> queryWrapper = new LambdaQueryWrapper<>();
        if(ObjectUtil.isNotNull(sysDictDataParam)) {
            if(ObjectUtil.isNotEmpty(sysDictDataParam.getTypeId())) {
        if (ObjectUtil.isNotNull(sysDictDataParam)) {
            if (ObjectUtil.isNotEmpty(sysDictDataParam.getTypeId())) {
                queryWrapper.eq(SysDictData::getTypeId, sysDictDataParam.getTypeId());
                queryWrapper.eq(SysDictData::getTypeId, sysDictDataParam.getTypeId());
            }
            }
        }
        }
@ -201,6 +201,11 @@ public class SysDictDataServiceImpl extends ServiceImpl<SysDictDataMapper, SysDi
        }
        }
    }
    }
    @Override
    public List<String> getDictCodesByDictTypeCode(String... dictTypeCodes) {
        return this.baseMapper.getDictCodesByDictTypeCode(dictTypeCodes);
    }
    /**
    /**
     * 校验参数,校验是否存在相同的编码
     * 校验参数,校验是否存在相同的编码
     *
     *