Search in sources :

Example 1 with Wrapper

use of com.baomidou.mybatisplus.core.conditions.Wrapper in project doorkeeper by limbo-world.

the class GroupRoleService method batchUpdate.

@Transactional
public void batchUpdate(Long groupId, GroupRoleBatchUpdateParam param) {
    switch(param.getType()) {
        case // 新增
        SAVE:
            if (CollectionUtils.isEmpty(param.getRoles())) {
                return;
            }
            List<GroupRolePO> addGroupRoles = new ArrayList<>();
            for (GroupRoleAddParam role : param.getRoles()) {
                GroupRolePO groupRole = new GroupRolePO();
                groupRole.setGroupId(groupId);
                groupRole.setRoleId(role.getRoleId());
                groupRole.setIsExtend(role.getIsExtend());
                addGroupRoles.add(groupRole);
            }
            groupRoleMapper.batchInsertIgnore(addGroupRoles);
            break;
        case // 删除
        DELETE:
            if (CollectionUtils.isEmpty(param.getRoleIds())) {
                return;
            }
            groupRoleMapper.delete(Wrappers.<GroupRolePO>lambdaQuery().eq(GroupRolePO::getGroupId, groupId).in(GroupRolePO::getRoleId, param.getRoleIds()));
            break;
        case // 更新
        UPDATE:
            if (CollectionUtils.isEmpty(param.getRoles())) {
                return;
            }
            List<Wrapper<GroupRolePO>> updateWrappers = new ArrayList<>();
            for (GroupRoleAddParam role : param.getRoles()) {
                updateWrappers.add(Wrappers.<GroupRolePO>lambdaUpdate().set(GroupRolePO::getIsExtend, role.getIsExtend()).eq(GroupRolePO::getGroupId, groupId).eq(GroupRolePO::getRoleId, role.getRoleId()));
            }
            MyBatisPlusUtils.batchUpdate(updateWrappers, GroupRolePO.class);
            break;
        default:
            break;
    }
}
Also used : Wrapper(com.baomidou.mybatisplus.core.conditions.Wrapper) GroupRoleAddParam(org.limbo.doorkeeper.api.model.param.add.GroupRoleAddParam) ArrayList(java.util.ArrayList) GroupRolePO(org.limbo.doorkeeper.server.infrastructure.po.GroupRolePO) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with Wrapper

use of com.baomidou.mybatisplus.core.conditions.Wrapper in project dynamic-threadpool by acmenlt.

the class UserServiceImpl method getUser.

@Override
public UserRespDTO getUser(UserReqDTO reqDTO) {
    Wrapper queryWrapper = Wrappers.lambdaQuery(UserInfo.class).eq(UserInfo::getUserName, reqDTO.getUserName());
    UserInfo userInfo = userMapper.selectOne(queryWrapper);
    UserRespDTO respUser = Optional.ofNullable(userInfo).map(each -> BeanUtil.toBean(each, UserRespDTO.class)).orElseThrow(() -> new ServiceException("查询无此用户, 可以尝试清空缓存或退出登录."));
    return respUser;
}
Also used : Wrappers(com.baomidou.mybatisplus.core.toolkit.Wrappers) BCryptPasswordEncoder(org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder) RoleService(cn.hippo4j.auth.service.RoleService) StringUtil(cn.hippo4j.common.toolkit.StringUtil) UserRespDTO(cn.hippo4j.auth.model.biz.user.UserRespDTO) UserInfo(cn.hippo4j.auth.model.UserInfo) LambdaQueryWrapper(com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper) BeanUtil(cn.hutool.core.bean.BeanUtil) ServiceException(cn.hippo4j.common.web.exception.ServiceException) Collectors(java.util.stream.Collectors) UserQueryPageReqDTO(cn.hippo4j.auth.model.biz.user.UserQueryPageReqDTO) UserReqDTO(cn.hippo4j.auth.model.biz.user.UserReqDTO) StrUtil(cn.hutool.core.util.StrUtil) List(java.util.List) Wrapper(com.baomidou.mybatisplus.core.conditions.Wrapper) UserMapper(cn.hippo4j.auth.mapper.UserMapper) LambdaUpdateWrapper(com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper) Service(org.springframework.stereotype.Service) UserService(cn.hippo4j.auth.service.UserService) Optional(java.util.Optional) AllArgsConstructor(lombok.AllArgsConstructor) IPage(com.baomidou.mybatisplus.core.metadata.IPage) LambdaQueryWrapper(com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper) Wrapper(com.baomidou.mybatisplus.core.conditions.Wrapper) LambdaUpdateWrapper(com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper) ServiceException(cn.hippo4j.common.web.exception.ServiceException) UserRespDTO(cn.hippo4j.auth.model.biz.user.UserRespDTO) UserInfo(cn.hippo4j.auth.model.UserInfo)

Example 3 with Wrapper

use of com.baomidou.mybatisplus.core.conditions.Wrapper in project chao-cloud by chaojunzi.

the class MybatisUtil method buildSql.

/**
 * 根据wrapper生成sql 如:a=1 and b=2
 *
 * @param <T>       实体类型
 * @param wrapper   条件
 * @param beanClass bean类型
 * @return sql条件
 */
static <T> String buildSql(LambdaQueryWrapper<T> wrapper, Class<T> beanClass) {
    // 判断bean是否加载
    TableInfo info = FunctionUtil.buildTableInfo(beanClass);
    Assert.notNull(info, "无效的数据实体 beanClass={}", beanClass.getName());
    String targetSql = wrapper.getTargetSql();
    if (StrUtil.isBlank(targetSql)) {
        return null;
    }
    int count = StrUtil.count(targetSql, MybatisConstant.ASK);
    Map<String, Object> valuePairs = wrapper.getParamNameValuePairs();
    Object[] vals = // 
    IntStream.range(1, count + 1).mapToObj(i -> {
        Object obj = valuePairs.get("MPGENVAL" + i);
        if (obj instanceof Date) {
            obj = DateUtil.formatDateTime((Date) obj);
        }
        return obj;
    }).toArray();
    // 去掉首尾括号
    return StrUtil.format(StrUtil.replace(targetSql, MybatisConstant.ASK, "'{}'"), vals);
}
Also used : SFunction(com.baomidou.mybatisplus.core.toolkit.support.SFunction) DateUtil(cn.hutool.core.date.DateUtil) ApplySql(com.chao.cloud.common.extra.mybatis.common.ApplySql) MybatisConstant(com.chao.cloud.common.extra.mybatis.constant.MybatisConstant) Date(java.util.Date) MergeSegments(com.baomidou.mybatisplus.core.conditions.segments.MergeSegments) LambdaQueryWrapper(com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper) ClassUtil(cn.hutool.core.util.ClassUtil) DbType(com.baomidou.mybatisplus.annotation.DbType) Constants(com.baomidou.mybatisplus.core.toolkit.Constants) SqlKeyword(com.baomidou.mybatisplus.core.enums.SqlKeyword) Wrapper(com.baomidou.mybatisplus.core.conditions.Wrapper) Map(java.util.Map) ApplySqlSegment(com.chao.cloud.common.extra.mybatis.common.ApplySql.ApplySqlSegment) FuncExps(com.chao.cloud.common.extra.mybatis.common.FuncExps) Method(java.lang.reflect.Method) Wrappers(com.baomidou.mybatisplus.core.toolkit.Wrappers) AbstractISegmentList(com.baomidou.mybatisplus.core.conditions.segments.AbstractISegmentList) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ReUtil(cn.hutool.core.util.ReUtil) Collectors(java.util.stream.Collectors) TableInfoHelper(com.baomidou.mybatisplus.core.metadata.TableInfoHelper) List(java.util.List) StrUtil(cn.hutool.core.util.StrUtil) QueryCondition(com.chao.cloud.common.extra.mybatis.annotation.QueryCondition) ReflectUtil(cn.hutool.core.util.ReflectUtil) ArrayUtil(cn.hutool.core.util.ArrayUtil) Optional(java.util.Optional) Convert(cn.hutool.core.convert.Convert) IPage(com.baomidou.mybatisplus.core.metadata.IPage) IntStream(java.util.stream.IntStream) ParamValue(com.chao.cloud.common.extra.mybatis.common.ApplySql.ParamValue) IService(com.baomidou.mybatisplus.extension.service.IService) ArrayList(java.util.ArrayList) TableInfo(com.baomidou.mybatisplus.core.metadata.TableInfo) WeakReference(java.lang.ref.WeakReference) SerializedLambda(com.baomidou.mybatisplus.core.toolkit.support.SerializedLambda) SqlTemplate(com.chao.cloud.common.extra.mybatis.common.SqlTemplate) AnnotationUtil(cn.hutool.core.annotation.AnnotationUtil) Assert(cn.hutool.core.lang.Assert) Field(java.lang.reflect.Field) TableField(com.baomidou.mybatisplus.annotation.TableField) Consumer(java.util.function.Consumer) AbstractWrapper(com.baomidou.mybatisplus.core.conditions.AbstractWrapper) Page(com.baomidou.mybatisplus.extension.plugins.pagination.Page) CollUtil(cn.hutool.core.collection.CollUtil) ISqlSegment(com.baomidou.mybatisplus.core.conditions.ISqlSegment) TableFieldInfo(com.baomidou.mybatisplus.core.metadata.TableFieldInfo) SpringUtil(cn.hutool.extra.spring.SpringUtil) Collections(java.util.Collections) TableInfo(com.baomidou.mybatisplus.core.metadata.TableInfo) Date(java.util.Date)

Example 4 with Wrapper

use of com.baomidou.mybatisplus.core.conditions.Wrapper in project diboot by dibo-software.

the class ProtectInterceptor method intercept.

@Override
public Object intercept(Invocation invocation) throws Throwable {
    Object[] args = invocation.getArgs();
    if (args.length > 1) {
        MappedStatement mappedStatement = (MappedStatement) args[0];
        SqlCommandType sqlType = mappedStatement.getSqlCommandType();
        if (SqlCommandType.INSERT == sqlType || SqlCommandType.UPDATE == sqlType) {
            Object entity = args[1];
            Configuration config = mappedStatement.getConfiguration();
            if (entity instanceof Map) {
                Set<Integer> set = new HashSet<>();
                for (Map.Entry<?, ?> entry : ((Map<?, ?>) entity).entrySet()) {
                    Object value = entry.getValue();
                    if (value == null || value instanceof Wrapper || set.contains(value.hashCode())) {
                        continue;
                    }
                    set.add(value.hashCode());
                    if (value instanceof List) {
                        for (Object obj : (List<?>) value) {
                            if (!encryptor(config, obj, IEncryptStrategy::encrypt)) {
                                break;
                            }
                        }
                    } else {
                        encryptor(config, value, IEncryptStrategy::encrypt);
                    }
                }
            } else {
                encryptor(config, entity, IEncryptStrategy::encrypt);
            }
        }
        return invocation.proceed();
    } else {
        ResultSetHandler resultSetHandler = (ResultSetHandler) invocation.getTarget();
        Field field = resultSetHandler.getClass().getDeclaredField("mappedStatement");
        field.setAccessible(true);
        List<?> list = (List<?>) invocation.proceed();
        if (list.isEmpty()) {
            return list;
        }
        Configuration config = ((MappedStatement) field.get(resultSetHandler)).getConfiguration();
        for (Object obj : list) {
            if (!encryptor(config, obj, IEncryptStrategy::decrypt)) {
                break;
            }
        }
        return list;
    }
}
Also used : Wrapper(com.baomidou.mybatisplus.core.conditions.Wrapper) Configuration(org.apache.ibatis.session.Configuration) Field(java.lang.reflect.Field) SqlCommandType(org.apache.ibatis.mapping.SqlCommandType) ResultSetHandler(org.apache.ibatis.executor.resultset.ResultSetHandler) MetaObject(org.apache.ibatis.reflection.MetaObject) List(java.util.List) MappedStatement(org.apache.ibatis.mapping.MappedStatement) Map(java.util.Map) HashSet(java.util.HashSet)

Example 5 with Wrapper

use of com.baomidou.mybatisplus.core.conditions.Wrapper in project hippo4j by longtai-cn.

the class UserServiceImpl method getUser.

@Override
public UserRespDTO getUser(UserReqDTO reqDTO) {
    Wrapper queryWrapper = Wrappers.lambdaQuery(UserInfo.class).eq(UserInfo::getUserName, reqDTO.getUserName());
    UserInfo userInfo = userMapper.selectOne(queryWrapper);
    UserRespDTO respUser = Optional.ofNullable(userInfo).map(each -> BeanUtil.toBean(each, UserRespDTO.class)).orElseThrow(() -> new ServiceException("查询无此用户, 可以尝试清空缓存或退出登录."));
    return respUser;
}
Also used : Wrappers(com.baomidou.mybatisplus.core.toolkit.Wrappers) BCryptPasswordEncoder(org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder) RoleService(cn.hippo4j.auth.service.RoleService) StringUtil(cn.hippo4j.common.toolkit.StringUtil) UserRespDTO(cn.hippo4j.auth.model.biz.user.UserRespDTO) UserInfo(cn.hippo4j.auth.model.UserInfo) LambdaQueryWrapper(com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper) BeanUtil(cn.hutool.core.bean.BeanUtil) ServiceException(cn.hippo4j.common.web.exception.ServiceException) Collectors(java.util.stream.Collectors) UserQueryPageReqDTO(cn.hippo4j.auth.model.biz.user.UserQueryPageReqDTO) UserReqDTO(cn.hippo4j.auth.model.biz.user.UserReqDTO) StrUtil(cn.hutool.core.util.StrUtil) List(java.util.List) Wrapper(com.baomidou.mybatisplus.core.conditions.Wrapper) UserMapper(cn.hippo4j.auth.mapper.UserMapper) LambdaUpdateWrapper(com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper) Service(org.springframework.stereotype.Service) UserService(cn.hippo4j.auth.service.UserService) Optional(java.util.Optional) AllArgsConstructor(lombok.AllArgsConstructor) IPage(com.baomidou.mybatisplus.core.metadata.IPage) LambdaQueryWrapper(com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper) Wrapper(com.baomidou.mybatisplus.core.conditions.Wrapper) LambdaUpdateWrapper(com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper) ServiceException(cn.hippo4j.common.web.exception.ServiceException) UserRespDTO(cn.hippo4j.auth.model.biz.user.UserRespDTO) UserInfo(cn.hippo4j.auth.model.UserInfo)

Aggregations

Wrapper (com.baomidou.mybatisplus.core.conditions.Wrapper)6 List (java.util.List)4 StrUtil (cn.hutool.core.util.StrUtil)3 LambdaQueryWrapper (com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper)3 IPage (com.baomidou.mybatisplus.core.metadata.IPage)3 Wrappers (com.baomidou.mybatisplus.core.toolkit.Wrappers)3 Optional (java.util.Optional)3 Collectors (java.util.stream.Collectors)3 UserMapper (cn.hippo4j.auth.mapper.UserMapper)2 UserInfo (cn.hippo4j.auth.model.UserInfo)2 UserQueryPageReqDTO (cn.hippo4j.auth.model.biz.user.UserQueryPageReqDTO)2 UserReqDTO (cn.hippo4j.auth.model.biz.user.UserReqDTO)2 UserRespDTO (cn.hippo4j.auth.model.biz.user.UserRespDTO)2 RoleService (cn.hippo4j.auth.service.RoleService)2 UserService (cn.hippo4j.auth.service.UserService)2 StringUtil (cn.hippo4j.common.toolkit.StringUtil)2 ServiceException (cn.hippo4j.common.web.exception.ServiceException)2 BeanUtil (cn.hutool.core.bean.BeanUtil)2 LambdaUpdateWrapper (com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper)2 IService (com.baomidou.mybatisplus.extension.service.IService)2