接口创建模板
基于代码生成的代码增加新接口模板例子
controller
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
*
* 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.
*/
package top.continew.admin.generator.controller;
import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.validation.Valid; // 保留导入
import top.continew.starter.web.model.R;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import top.continew.admin.generator.model.req.GetAnnouncementNameReq;
import top.continew.admin.generator.model.req.GetNameQueryListReq;
import top.continew.starter.core.exception.BusinessException;
import top.continew.starter.extension.crud.enums.Api;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;
import top.continew.starter.extension.crud.annotation.CrudRequestMapping;
import top.continew.starter.extension.crud.controller.BaseController;
import top.continew.admin.generator.model.query.NoticeSQuery;
import top.continew.admin.generator.model.req.NoticeSReq;
import top.continew.admin.generator.model.resp.NoticeSDetailResp;
import top.continew.admin.generator.model.resp.NoticeSResp;
import top.continew.admin.generator.service.NoticeSService;
import java.util.List;
import java.util.HashMap;
import java.util.Map;
/**
* 公告管理 API
*
* @author cx
* @since 2024/09/15 16:26
*/
@Tag(name = "公告管理 API")
@RestController
@CrudRequestMapping(value = "/generator/noticeS", api = {Api.PAGE, Api.GET, Api.ADD, Api.UPDATE, Api.DELETE,
Api.EXPORT})
@Validated // 启用参数验证
public class NoticeSController extends BaseController<NoticeSService, NoticeSResp, NoticeSDetailResp, NoticeSQuery, NoticeSReq> {
private final NoticeSService noticeSService; // 声明公告服务实例
// 构造函数,依赖注入
public NoticeSController(NoticeSService noticeSService) {
this.noticeSService = noticeSService; // 将服务实例赋值给类成员变量
}
/**
* 根据名称获取公告的详细信息
*
* @param request 请求体,包含公告名称
* @return 返回公告详情
*/
@PostMapping("/getAnnouncementName")
@SaCheckPermission("system:user:list")
@Operation(summary = "根据名称精准查询公告-测试开发", description = "根据名称精准查询公告-测试开发")
public Map<String, Object> getAnnouncementName(@Valid @RequestBody GetAnnouncementNameReq request) {
try {
Map<String, Object> response = new HashMap<>(); // 创建响应数据的Map对象
String name = request.getName(); // 从请求体中获取公告名称
// 调用服务层的方法,根据公告名称查询公告的详细信息
NoticeSDetailResp result = noticeSService.getAnnouncementName(name);
response.put("data", result);
return response; // 返回响应Map
} catch (Exception e) {
throw new BusinessException(e.getMessage());
}
}
/**
* 根据名称模糊查询公告列表
*
* @param request 请求体,包含分页参数和搜索条件
* @return 返回公告列表及总条数
*/
@PostMapping("/getNameQueryList")
@Operation(summary = "根据名称模糊查询公告列表-测试开发", description = "根据名称模糊查询公告列表-测试开发")
public Map<String, Object> getNameQueryList(@Valid @RequestBody GetNameQueryListReq request) {
Map<String, Object> response = new HashMap<>(); // 创建响应数据的Map对象
// 从请求体中获取分页参数和搜索条件
String name = request.getName(); // 获取模糊搜索的名称
Integer page = request.getPage(); // 获取当前页码
Integer size = request.getSize(); // 获取每页的条数
// 判断 page 和 size 是否大于 0
if (page <= 0) {
throw new BusinessException("分页参数 page 必须大于 0");
}
if (size <= 0) {
throw new BusinessException("分页参数 size 必须大于 0");
}
page = page - 1; // 将页码转换为从 0 开始
try {
// 调用服务层进行处理
List<NoticeSResp> notices = noticeSService.getNameQueryList(name, page, size);
// 设置返回的公告列表和总数
response.put("data", notices);
response.put("total", notices.size());
} catch (Exception e) {
throw new BusinessException("查询过程中发生错误: " + e.getMessage());
}
return response; // 返回响应Map
}
}
mapper
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
*
* 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.
*/
package top.continew.admin.generator.mapper;
import top.continew.starter.data.mp.base.BaseMapper;
import top.continew.admin.generator.model.entity.NoticeSDO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import java.util.List;
/**
* 公告 Mapper
*/
public interface NoticeSMapper extends BaseMapper<NoticeSDO> {
@Select("SELECT * FROM notice_s WHERE name = #{name}")
NoticeSDO getAnnouncementName(@Param("name") String name);
@Select("SELECT * FROM notice_s WHERE name LIKE CONCAT('%', #{name}, '%') LIMIT #{page}, #{size}")
List<NoticeSDO> getNameQueryList(@Param("name") String name, @Param("page") int page, @Param("size") int size); // 模糊查询公告
}
model
req
GetAnnouncementNameReq
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
*
* 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.
*/
package top.continew.admin.generator.model.req;
import jakarta.validation.constraints.*;
import lombok.Data;
import io.swagger.v3.oas.annotations.media.Schema;
import org.hibernate.validator.constraints.Length;
import top.continew.starter.extension.crud.model.req.BaseReq;
/**
* 根据 名称 获取公告请求体
*/
@Data
@Schema(description = "根据 名称 获取公告请求体")
public class GetAnnouncementNameReq extends BaseReq {
@Schema(description = "公告名称")
@NotNull(message = "公告名称不能为空") // 验证公告名称不能为空
@Size(min = 1, message = "公告名称不能为空") // 字段长度至少为 1
@Length(max = 3, message = "描述长度不能超过 {max} 个字符")
private String name; // 公告名称
}权限与用户信息获取与使用
GetNameQueryListReq
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
*
* 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.
*/
package top.continew.admin.generator.model.req;
import jakarta.validation.constraints.*;
import lombok.Data;
import io.swagger.v3.oas.annotations.media.Schema;
import top.continew.starter.extension.crud.model.req.BaseReq;
/**
* 根据 name 获取公告请求体
*/
@Data
@Schema(description = "根据 name 获取公告请求体")
public class GetNameQueryListReq extends BaseReq {
@Schema(description = "公告名称")
private String name; // 公告名称
@Schema(description = "页码")
@NotNull(message = "页码不能为空")
private Integer page; // 公告名称
@Schema(description = "条数")
@NotNull(message = "条数不能为空")
private Integer size; // 公告名称
}
service
impl
NoticeSServiceImpl
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
*
* 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.
*/
package top.continew.admin.generator.service.impl;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.stream.Collectors;
import top.continew.admin.common.model.dto.LoginUser;
import top.continew.admin.common.util.helper.LoginHelper;
import top.continew.starter.core.exception.BusinessException;
import top.continew.starter.extension.crud.service.impl.BaseServiceImpl;
import top.continew.admin.generator.mapper.NoticeSMapper;
import top.continew.admin.generator.model.entity.NoticeSDO;
import top.continew.admin.generator.model.query.NoticeSQuery;
import top.continew.admin.generator.model.req.NoticeSReq;
import top.continew.admin.generator.model.resp.NoticeSDetailResp;
import top.continew.admin.generator.model.resp.NoticeSResp;
import top.continew.admin.generator.service.NoticeSService;
/**
* 公告业务实现
*
* @author cx
* @since 2024/09/15 16:26
*/
@Service
@RequiredArgsConstructor
public class NoticeSServiceImpl extends BaseServiceImpl<NoticeSMapper, NoticeSDO, NoticeSResp, NoticeSDetailResp, NoticeSQuery, NoticeSReq> implements NoticeSService {
@Override
public NoticeSDetailResp getAnnouncementName(String name) {
// 使用 mapper 根据名称查询公告,假设这里的 mapper 是与数据库交互的对象
NoticeSDO notice = this.baseMapper.getAnnouncementName(name); // 使用名称查询公告
// 检查查询结果,如果公告存在
if (notice != null) {
// 创建一个 NoticeSDetailResp 对象,用于封装公告的详细信息
NoticeSDetailResp detailResp = new NoticeSDetailResp();
// 从 NoticeSDO 对象中提取公告的各个属性
detailResp.setId(notice.getId()); // 设置公告的 ID
detailResp.setName(notice.getName()); // 设置公告的名称
detailResp.setDescription(notice.getDescription()); // 设置公告的描述
detailResp.setSort(notice.getSort()); // 设置公告的排序字段
detailResp.setStatus(notice.getStatus()); // 设置公告的状态
return detailResp;
} else {
// 如果没有找到公告,返回 HTTP 404 (Not Found) 状态
throw new BusinessException("公告未找到");
}
}
@Override
public List<NoticeSResp> getNameQueryList(String name, int page, int size) {
// 使用 mapper 查询公告
List<NoticeSDO> notices = this.baseMapper.getNameQueryList(name, page, size);
// 转换为 NoticeSResp 对象
return notices.stream().map(notice -> {
NoticeSResp resp = new NoticeSResp();
resp.setId(notice.getId()); // 设置公告ID
resp.setName(notice.getName()); // 设置公告名称
// 添加其他属性的设置
return resp; // 返回公告响应对象
}).collect(Collectors.toList()); // 返回公告响应列表
}
}
NoticeSService
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
*
* 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.
*/
package top.continew.admin.generator.service;
import java.util.List;
import org.springframework.http.ResponseEntity;
import top.continew.starter.extension.crud.service.BaseService;
import top.continew.admin.generator.model.query.NoticeSQuery;
import top.continew.admin.generator.model.req.NoticeSReq;
import top.continew.admin.generator.model.resp.NoticeSDetailResp;
import top.continew.admin.generator.model.resp.NoticeSResp;
/**
* 公告业务接口
*
* @author cx
* @since 2024/09/15 16:26
*/
public interface NoticeSService extends BaseService<NoticeSResp, NoticeSDetailResp, NoticeSQuery, NoticeSReq> {
/**
* 根据 名称 获取公告
*
* @param name 公告 名称
* @return 返回成功的消息
*/
NoticeSDetailResp getAnnouncementName(String name);
/**
* 根据名称模糊查询公告列表
*
* @param name 模糊搜索的名称
* @param page 当前页码
* @param size 每页条数
* @return 返回包含公告列表的 ResponseEntity
*/
List<NoticeSResp> getNameQueryList(String name, int page, int size);
}
获取登录信息与权限
获取当前登录信息
NoticeSDO notice = this.baseMapper.getAnnouncementName(name); // 使用名称查询公告
LoginUser loginUser = LoginHelper.getLoginUser();
// 获取当前用户 ID
Long userId = loginUser.getId();
// 你可以在这里使用 userId
System.out.println("当前用户 ID: " + userId);
System.out.println("当前用户: " + loginUser);
//这是打印的信息
当前用户 ID: 547889293968801831
当前用户: LoginUser(id=547889293968801831, username=test, deptId=547888483713155087, permissions=[system:user:list, system:user:delete, system:user:add, system:user:update], roleCodes=[test], roles=[RoleDTO(id=547888897925840928, code=test, dataScope=CUSTOM)], token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJsb2dpblR5cGUiOiJsb2dpbiIsImxvZ2luSWQiOjU0Nzg4OTI5Mzk2ODgwMTgzMSwicm5TdHIiOiIzVGozMXlKNVMwNHVlTlNLQ3Z4Rml0alEzeERwbUxZTSJ9.H5tFqom-MAd9C0WEZDGBcz-whYGegs1KdNMYvjBikGU, ip=0:0:0:0:0:0:0:1, address=内网IP, browser=Chrome 130.0.0.0, os=OSX, loginTime=2024-10-31T16:01:38, pwdResetTime=2024-10-31T16:01:14, passwordExpirationDays=0)
增加功能权限
在controller方法里增加权限注解
@SaCheckPermission("system:user:list")
启动数据权限
mapper中更改BaseMapper为DataPermissionMapper注解
//例子
public interface NoticeSMapper extends DataPermissionMapper<NoticeSDO> {
}