文章目录
- 🌞 Sun Frame:SpringBoot 的轻量级开发框架(个人开源项目推荐)
- 🌟 亮点功能
- 📦 spring cloud模块概览
- 常用工具
 
- 🔗 更多信息
- 1.easycode生成基础代码
- 1.配置
- 2.将dao层代码剪切到mapper层
- 3.AuthRole.java使用lombok优化
- 4.AuthRoleDao.java 删除Pageable
- 5.AuthRoleService.java 删除分页查询接口
- 6.AuthRoleServiceImpl.java 删除分页查询实现类
 
 
- 2.新增角色
- 1.编写DTO和BO以及基础转换器
- 1.sun-club-auth-application-controller模块
- 1.AuthRoleDTO.java
- 2.AuthRoleDTOConverter.java
 
- 2.sun-club-auth-domain模块
- 1.AuthRoleBO.java
- 2.AuthRoleBOConverter.java
 
 
- 2.sun-club-auth-application-controller
- 1.RoleController.java
 
- 3.sun-club-auth-domain
- 1.AuthRoleDomainService.java
- 2.AuthRoleDomainServiceImpl.java
 
- 4.sun-club-auth-infra
- 1.AuthRoleService.java
- 2.AuthRoleServiceImpl.java
 
- 5.测试
- 1.接口设计
- 2.测试
 
 
 
- 3.更新角色
- 1.sun-club-auth-application-controller
- 1.RoleController.java
 
- 2.sun-club-auth-domain
- 1.AuthRoleDomainService.java
- 2.AuthRoleDomainServiceImpl.java
 
- 3.sun-club-auth-infra
- 1.AuthRoleService.java
- 2.AuthRoleServiceImpl.java
 
- 4.测试
- 1.接口设计
- 2.测试
 
 
 
- 4.删除角色
- 1.sun-club-auth-application-controller
- 1.RoleController.java
 
- 2.sun-club-auth-domain
- 1.AuthRoleDomainService.java
- 2.AuthRoleDomainServiceImpl.java
 
- 3.测试
- 1.接口设计
- 2.测试
 
 
 
- 5.用户角色关联
- 1.easycode生成用户-角色关联表代码
- 1.配置
- 2.将AuthUserRoleDao.java从dao移动到mapper
- 3.AuthUserRole.java 使用lombok简化
- 4.AuthUserRoleDao.java删除Pageable
- 5.AuthUserRoleService.java删除分页查询接口
- 6.AuthUserRoleServiceImpl.java删除分页查询实现
 
- 2.sun-club-auth-domain
- 1.AuthConstant.java
- 2.AuthUserDomainServiceImpl.java 新增逻辑,进行用户和角色关联
- 3.AuthRoleDao.xml 添加selectKey逻辑,插入后将id返回
 
- 3.测试
 
 
 
🌞 Sun Frame:SpringBoot 的轻量级开发框架(个人开源项目推荐)

轻松高效的现代化开发体验
Sun Frame 是我个人开源的一款基于 SpringBoot 的轻量级框架,专为中小型企业设计。它提供了一种快速、简单且易于扩展的开发方式。
 
 我们的开发文档记录了整个项目从0到1的任何细节,实属不易,请给我们一个Star!🌟
您的支持是我们持续改进的动力。
 
您的支持是我们持续改进的动力。
🌟 亮点功能
- 组件化开发:灵活选择,简化流程。
- 高性能:通过异步日志和 Redis 缓存提升性能。
- 易扩展:支持多种数据库和消息队列。
📦 spring cloud模块概览
- Nacos 服务:高效的服务注册与发现。
- Feign 远程调用:简化服务间通信。
- 强大网关:路由与限流。
常用工具
- 日志管理:异步处理与链路追踪。
- Redis 集成:支持分布式锁与缓存。
- Swagger 文档:便捷的 API 入口。
- 测试支持:SpringBoot-Test 集成。
- EasyCode:自定义EasyCode模板引擎,一键生成CRUD。
🔗 更多信息
- 开源地址:Gitee Sun Frame
- 详细文档:语雀文档
  
1.easycode生成基础代码
1.配置

2.将dao层代码剪切到mapper层

3.AuthRole.java使用lombok优化

4.AuthRoleDao.java 删除Pageable

5.AuthRoleService.java 删除分页查询接口
6.AuthRoleServiceImpl.java 删除分页查询实现类
2.新增角色
1.编写DTO和BO以及基础转换器
1.sun-club-auth-application-controller模块
1.AuthRoleDTO.java
package com.sunxiansheng.auth.application.dto;
import lombok.Data;
import java.io.Serializable;
/**
 * (AuthRole)dto
 *
 * @author makejava
 * @since 2024-06-06 14:30:38
 */
@Data
public class AuthRoleDTO implements Serializable {
    private Long id;
    /**
     * 角色名称
     */
    private String roleName;
    /**
     * 角色唯一标识
     */
    private String roleKey;
    /**
     * 是否被删除 0未删除 1已删除
     */
    private Integer isDeleted;
}
2.AuthRoleDTOConverter.java
package com.sunxiansheng.auth.application.convert;
import com.sunxiansheng.auth.application.dto.AuthRoleDTO;
import com.sunxiansheng.auth.domain.entity.AuthRoleBO;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
import java.util.List;
/**
 * Description: DTO转换
 * @Author sun
 * @Create 2024/6/5 15:39
 * @Version 1.0
 */
@Mapper
public interface AuthRoleDTOConverter {
    AuthRoleDTOConverter INSTANCE = Mappers.getMapper(AuthRoleDTOConverter.class);
    // 将DTO转换为BO
    AuthRoleBO convertDTO2BO(AuthRoleDTO authRoleDTO);
    // 将BO转换为DTO
    AuthRoleDTO convertBO2DTO(AuthRoleBO authRoleBO);
    // 将DTO集合转换为BO集合
    List<AuthRoleBO> convertDTOList2BOList(List<AuthRoleDTO> authRoleDTOList);
    // 将BO集合转换为DTO集合
    List<AuthRoleDTO> convertBOList2DTOList(List<AuthRoleBO> authRoleBOList);
}
2.sun-club-auth-domain模块
1.AuthRoleBO.java
package com.sunxiansheng.auth.domain.entity;
import lombok.Data;
import java.io.Serializable;
/**
 * (AuthRole)bo
 *
 * @author makejava
 * @since 2024-06-06 14:30:38
 */
@Data
public class AuthRoleBO implements Serializable {
    private Long id;
    /**
     * 角色名称
     */
    private String roleName;
    /**
     * 角色唯一标识
     */
    private String roleKey;
    /**
     * 是否被删除 0未删除 1已删除
     */
    private Integer isDeleted;
}
2.AuthRoleBOConverter.java
package com.sunxiansheng.auth.domain.convert;
import com.sunxiansheng.auth.basic.entity.AuthRole;
import com.sunxiansheng.auth.domain.entity.AuthRoleBO;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
import java.util.List;
/**
 * Description: BO转换
 * @Author sun
 * @Create 2024/5/24 9:18
 * @Version 1.0
 */
@Mapper // mapstruct的注解
public interface AuthRoleBOConverter {
    AuthRoleBOConverter INSTANCE = Mappers.getMapper(AuthRoleBOConverter.class);
    // 将BO转换Entity
    AuthRole convertBO2Entity(AuthRoleBO authRoleBO);
    // 将Entity转换BO
    AuthRoleBO convertEntity2BO(AuthRole authRole);
    // 将BO集合转换Entity集合
    List<AuthRole> convertBOList2EntityList(List<AuthRoleBO> authRoleBOList);
    // 将Entity集合转换BO集合
    List<AuthRoleBO> convertEntityList2BOList(List<AuthRole> authRoleList);
}
2.sun-club-auth-application-controller
1.RoleController.java
package com.sunxiansheng.auth.application.controller;
import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.sunxiansheng.auth.application.convert.AuthRoleDTOConverter;
import com.sunxiansheng.auth.application.dto.AuthRoleDTO;
import com.sunxiansheng.auth.common.eneity.Result;
import com.sunxiansheng.auth.domain.entity.AuthRoleBO;
import com.sunxiansheng.auth.domain.service.AuthRoleDomainService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
/**
 * Description:
 * @Author sun
 * @Create 2024/6/2 17:25
 * @Version 1.0
 */
@RestController
@RequestMapping("/role/")
@Slf4j
public class RoleController {
    @Resource
    private AuthRoleDomainService authRoleDomainService;
    /**
     * 新增角色
     * @param authRoleDTO
     * @return
     */
    @RequestMapping("add")
    public Result<Boolean> add(@RequestBody AuthRoleDTO authRoleDTO) {
        try {
            // 日志
            if (log.isInfoEnabled()) {
                log.info("RoleController add AuthRoleDTO, authRoleDTO:{}", JSON.toJSONString(authRoleDTO));
            }
            // 参数校验
            Preconditions.checkArgument(!StringUtils.isBlank(authRoleDTO.getRoleKey()), "角色唯一标识不能为空");
            Preconditions.checkArgument(!StringUtils.isBlank(authRoleDTO.getRoleName()), "角色名称不能为空");
            // 转换DTO为BO
            AuthRoleBO authRoleBO = AuthRoleDTOConverter.INSTANCE.convertDTO2BO(authRoleDTO);
            // 调用领域服务
            // 新增角色
            Boolean res = authRoleDomainService.add(authRoleBO);
            return Result.ok(res);
        } catch (Exception e) {
            // error日志
            log.error("RoleController add error:{}", e.getMessage(), e);
            return Result.fail("新增角色失败");
        }
    }
}
3.sun-club-auth-domain
1.AuthRoleDomainService.java
package com.sunxiansheng.auth.domain.service;
import com.sunxiansheng.auth.domain.entity.AuthRoleBO;
/**
 * Description:
 * @Author sun
 * @Create 2024/5/24 9:03
 * @Version 1.0
 */
public interface AuthRoleDomainService {
    /**
     * 添加
     * @param authRoleBO
     * @return
     */
    Boolean add(AuthRoleBO authRoleBO);
}
2.AuthRoleDomainServiceImpl.java
package com.sunxiansheng.auth.domain.service.impl;
import com.sunxiansheng.auth.basic.entity.AuthRole;
import com.sunxiansheng.auth.basic.service.AuthRoleService;
import com.sunxiansheng.auth.common.enums.IsDeleteFlagEnum;
import com.sunxiansheng.auth.domain.convert.AuthRoleBOConverter;
import com.sunxiansheng.auth.domain.entity.AuthRoleBO;
import com.sunxiansheng.auth.domain.service.AuthRoleDomainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
/**
 * Description:
 * @Author sun
 * @Create 2024/5/24 9:03
 * @Version 1.0
 */
@Service
@Slf4j
public class AuthRoleDomainServiceImpl implements AuthRoleDomainService {
    @Resource
    private AuthRoleService authRoleService;
    @Override
    public Boolean add(AuthRoleBO authRoleBO) {
        // BO转换为Entity
        AuthRole authRole = AuthRoleBOConverter.INSTANCE.convertBO2Entity(authRoleBO);
        // 调用基础服务
        // 设置逻辑删除
        authRole.setIsDeleted(IsDeleteFlagEnum.UN_DELETED.getCode());
        // 新增角色
        Integer count = authRoleService.insert(authRole);
        return count > 0;
    }
}
4.sun-club-auth-infra
1.AuthRoleService.java

2.AuthRoleServiceImpl.java

5.测试
1.接口设计

2.测试


3.更新角色
1.sun-club-auth-application-controller
1.RoleController.java
/**
 * 修改角色信息
 * @param authRoleDTO
 * @return
 */
@RequestMapping("update")
public Result<Boolean> update(@RequestBody AuthRoleDTO authRoleDTO) {
    try {
        // 日志
        if (log.isInfoEnabled()) {
            log.info("RoleController update AuthRoleDTO, authRoleDTO:{}", JSON.toJSONString(authRoleDTO));
        }
        // 参数校验
        Preconditions.checkNotNull(authRoleDTO.getId(), "角色ID不能为空");
        // 转换DTO为BO
        AuthRoleBO authRoleBO = AuthRoleDTOConverter.INSTANCE.convertDTO2BO(authRoleDTO);
        // 调用领域服务
        // 更新用户信息
        Boolean res = authRoleDomainService.update(authRoleBO);
        return Result.ok(res);
    } catch (Exception e) {
        // error日志
        log.error("RoleController update error:{}", e.getMessage(), e);
        return Result.fail("修改角色信息失败");
    }
}
2.sun-club-auth-domain
1.AuthRoleDomainService.java
/**
 * 修改
 * @param authRoleBO
 * @return
 */
Boolean update(AuthRoleBO authRoleBO);
2.AuthRoleDomainServiceImpl.java
@Override
public Boolean update(AuthRoleBO authRoleBO) {
    // BO转换为Entity
    AuthRole authRole = AuthRoleBOConverter.INSTANCE.convertBO2Entity(authRoleBO);
    // 调用基础服务
    // 修改角色
    Integer count = authRoleService.update(authRole);
    // 有任何的更新,都要与缓存进行同步的修改,要把当前用户的角色和权限都放到redis里
    return count > 0;
}
3.sun-club-auth-infra
1.AuthRoleService.java

2.AuthRoleServiceImpl.java

4.测试
1.接口设计

2.测试


4.删除角色
1.sun-club-auth-application-controller
1.RoleController.java
/**
 * 删除角色信息
 * @param authRoleDTO
 * @return
 */
@RequestMapping("delete")
public Result<Boolean> delete(@RequestBody AuthRoleDTO authRoleDTO) {
    try {
        // 日志
        if (log.isInfoEnabled()) {
            log.info("RoleController delete AuthRoleDTO, authRoleDTO:{}", JSON.toJSONString(authRoleDTO));
        }
        // 参数校验
        Preconditions.checkNotNull(authRoleDTO.getId(), "角色ID不能为空");
        // 转换DTO为BO
        AuthRoleBO authRoleBO = AuthRoleDTOConverter.INSTANCE.convertDTO2BO(authRoleDTO);
        // 调用领域服务
        // 删除用户信息
        Boolean res = authRoleDomainService.delete(authRoleBO);
        return Result.ok(res);
    } catch (Exception e) {
        // error日志
        log.error("RoleController delete error:{}", e.getMessage(), e);
        return Result.fail("删除角色信息失败");
    }
}
2.sun-club-auth-domain
1.AuthRoleDomainService.java
/**
 * 删除
 * @param authRoleBO
 * @return
 */
Boolean delete(AuthRoleBO authRoleBO);
2.AuthRoleDomainServiceImpl.java
@Override
public Boolean delete(AuthRoleBO authRoleBO) {
    // BO转换为Entity
    AuthRole authRole = AuthRoleBOConverter.INSTANCE.convertBO2Entity(authRoleBO);
    // 设置逻辑删除
    authRole.setIsDeleted(IsDeleteFlagEnum.DELETED.getCode());
    // 更新
    Integer count = authRoleService.update(authRole);
    return count > 0;
}
3.测试
1.接口设计

2.测试


5.用户角色关联
1.easycode生成用户-角色关联表代码
1.配置

2.将AuthUserRoleDao.java从dao移动到mapper

3.AuthUserRole.java 使用lombok简化

4.AuthUserRoleDao.java删除Pageable

5.AuthUserRoleService.java删除分页查询接口
6.AuthUserRoleServiceImpl.java删除分页查询实现
2.sun-club-auth-domain
1.AuthConstant.java
package com.sunxiansheng.auth.domain.constants;
/**
 * Description:
 * @Author sun
 * @Create 2024/6/6 15:54
 * @Version 1.0
 */
public enum AuthConstant {
    /*
    普通用户
     */
    NORMAL_USER("normal_user"),
    ;
    private String value;
    AuthConstant(String value) {
        this.value = value;
    }
    public String getValue() {
        return value;
    }
}
2.AuthUserDomainServiceImpl.java 新增逻辑,进行用户和角色关联

3.AuthRoleDao.xml 添加selectKey逻辑,插入后将id返回
# 在新增之后会将id返回给对象
<selectKey resultType="java.lang.Long" keyProperty="id" order="AFTER">
    SELECT LAST_INSERT_ID()
</selectKey>

3.测试




![[Web服务器] 简易静态Web服务器的搭建](https://i-blog.csdnimg.cn/direct/c1e9d7a7d25b4af4aff2dbb453066f00.png)




![[C++] 模板进阶:特化与编译链接全解析](https://img-blog.csdnimg.cn/img_convert/2e8c3698ed1db09596b1e95d40ec2984.png)










