前言
为自己搭建一个可以自我思考的平台,其核心为“心想事成”。
一、思考过程?
后端框架搭建完成之后,如果不能允许以及访问不了接口也是不完美的。那么接着继续!
二、完善
接口设计思路
1、首先我要完成的是“心想事成”的第一步,其名为:记忆输入搜索
2、构建一个记录关键字、关键字关联句子的表,在第一步完成了
3、搭建完基本框架后就是接口的开发。
4、第一个接口,没有数据咋办,当然是生成数据。
5、以User表为例子,先写一个生成用户的接口,如何实现呢?我思路是先使用chatGPT获取姓名的数组。然后通过随机数去组合起来生成用户名字。
接口代码
1、编写业务实现:UserServiceImpl
private final String[] prefixName = {
"阿", "白", "蔡", "陈", "成", "程", "崔", "戴", "邓", "丁", "董", "杜", "范", "方", "樊", "房", "高", "郭", "韩", "郝", "贺", "侯", "胡", "华", "黄", "韩", "江", "姜", "蒋", "解", "江", "金", "康", "孔", "赖", "劳", "黎", "李", "连", "梁", "廖", "林", "刘", "龙", "卢", "马", "毛", "莫", "孟", "欧阳", "潘", "庞", "裴", "彭", "齐", "钱", "乔", "秦", "丘", "曲", "屈", "任", "沈", "盛", "施", "石", "史", "苏", "孙", "邵", "沈", "田", "汪", "王", "危", "韦", "卫", "尉迟", "魏", "魏", "温", "文", "翁", "巫", "吴", "伍", "武", "徐", "许", "徐", "薛", "杨", "叶", "尹", "俞", "虞", "于", "余", "俞", "宇文", "岳", "袁", "曾", "查", "柴", "常", "长孙", "陈", "成", "程", "楚", "褚", "崔", "戴", "邓", "丁", "董", "杜", "范", "方", "樊", "房", "高", "郭", "韩", "郝", "贺", "侯", "胡", "华", "黄", "韩", "江", "姜", "蒋", "解", "江", "金", "康", "孔", "赖", "劳", "黎", "李", "连", "梁", "廖", "林", "刘", "龙", "卢", "马", "毛", "莫", "孟", "欧阳", "潘", "庞", "裴", "彭", "齐", "钱", "乔", "秦", "丘", "曲", "屈", "任"
};
private final String[] suffixName = {
"安琪", "白雪", "蔡琳", "陈晨", "程悦", "崔雨", "戴瑶", "邓洁", "丁丁", "董璐", "杜娟", "范雪", "方方", "樊梅", "房宁", "高婷", "郭莉", "韩丽", "郝丹", "贺瑶", "侯敏", "胡静", "华娜", "黄璐", "韩雨", "江洁", "姜丽", "蒋洋", "解敏", "江玲", "金燕", "康霞", "孔悦", "赖娜", "劳丽", "黎萍", "李娜", "连婷", "梁洁", "廖敏", "林娟", "刘雪", "龙丽", "卢霞", "马丽", "毛梅", "莫宁", "孟娜", "欧阳静", "潘瑶", "庞丹", "裴敏", "彭洋", "齐敏", "钱玲", "乔燕", "秦霞", "丘悦", "曲娟", "屈洁", "任丽", "沈雪", "盛丽", "施萍", "石娜", "史婷", "苏敏", "孙洋", "邵敏", "沈玲", "田霞", "汪霞", "王瑶", "危娟", "韦洁", "卫敏", "尉迟丽", "魏霞", "魏丽", "温萍", "文娟", "翁洁", "巫敏", "吴洋", "伍敏", "武婷", "徐霞", "许瑶", "徐丽", "薛洁", "杨敏", "叶丽", "尹洋", "俞敏", "虞洁", "于丽", "余敏", "俞宇文", "岳霞", "袁丹", "曾敏", "查洁", "柴敏", "常丽", "长孙洋", "陈敏", "成玲", "程萍", "楚悦", "褚娟", "崔洁", "戴丽", "邓洋", "丁敏", "董玲", "杜霞", "范洁", "方丽", "樊敏", "房洋", "高敏", "郭洁", "韩丽", "郝悦", "贺娟", "侯洁", "胡丽", "华悦", "黄娜", "韩洋", "江敏", "姜瑶", "蒋洁", "解敏", "江玲", "金燕", "康霞", "孔悦", "赖娟", "劳丽", "黎萍", "李娜", "连婷", "梁洁", "廖敏", "林娟", "刘雪", "龙丽", "卢霞", "马丽", "毛梅", "莫宁", "孟娜", "欧阳静", "潘瑶", "庞丹", "裴敏", "彭洋", "齐敏", "钱玲", "乔燕", "秦霞", "丘悦", "曲娟", "屈洁", "任丽"
};
public static void main(String[] args) {
Random random = new Random();
UserServiceImpl userService = new UserServiceImpl();
for (int i = 0; i < 10; i++) {
String name = userService.prefixName[random.nextInt(167)] +
userService.suffixName[random.nextInt(165)];
System.out.println("生成名字:" + name);
}
}
@Override
public boolean generatorUser(Integer total) {
List<User> userList = new ArrayList<>();
Random random = new Random();
for (int i = 0; i < total; i++) {
String name = prefixName[random.nextInt(prefixName.length - 1)] +
suffixName[random.nextInt(prefixName.length - 1)];
String password = "123456";
User user = new User();
user.setName(name);
user.setPassword(password);
user.setCreateTime(LocalDateTime.now());
userList.add(user);
}
return this.saveBatch(userList);
}
2、编写Controller层
@Tag(name="用户管理")
@RestController
@AllArgsConstructor
@RequestMapping("/user")
public class UserController {
private final UserService userService;
@Operation(tags = "生成用户", description = "生成指定用户的用户信息")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "成功", content = {
@Content(mediaType = "application/json")
}),
@ApiResponse(responseCode = "500", description = "失败", content = {
@Content(mediaType = MediaType.APPLICATION_JSON_VALUE)
}),
})
@GetMapping("/generatedData/{total}")
public R generatedData(@PathVariable Integer total) {
return R.ok(userService.generatorUser(total));
}
}
3、补充其中使用到的工具类
package com.etp.sustainable.util;
import com.etp.sustainable.constant.CommonConstants;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*;
import lombok.experimental.Accessors;
import java.io.Serializable;
/**
* 响应信息主体
*
* @param <T>
* @author etp
*/
@Schema(name = "R", title = "返回对象")
@Builder
@ToString
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class R<T> implements Serializable {
private static final long serialVersionUID = 1L;
@Getter
@Setter
@Schema(name = "code", title = "编码")
private int code;
@Getter
@Setter
@Schema(name = "msg", title = "消息")
private String msg;
@Getter
@Setter
@Schema(name = "data", title = "数据")
private T data;
public static <T> R<T> ok() {
return restResult(null, CommonConstants.SUCCESS, null);
}
public static <T> R<T> ok(T data) {
return restResult(data, CommonConstants.SUCCESS, null);
}
public static <T> R<T> ok(T data, String msg) {
return restResult(data, CommonConstants.SUCCESS, msg);
}
public static <T> R<T> failed() {
return restResult(null, CommonConstants.FAIL, null);
}
public static <T> R<T> failed(String msg) {
return restResult(null, CommonConstants.FAIL, msg);
}
public static <T> R<T> failed(T data) {
return restResult(data, CommonConstants.FAIL, null);
}
public static <T> R<T> failed(T data, String msg) {
return restResult(data, CommonConstants.FAIL, msg);
}
private static <T> R<T> restResult(T data, int code, String msg) {
R<T> apiResult = new R<>();
apiResult.setCode(code);
apiResult.setData(data);
apiResult.setMsg(msg);
return apiResult;
}
}
package com.etp.sustainable.config;
import io.swagger.v3.oas.models.ExternalDocumentation;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author ETP
* @since 2023/10/12 10:27
*/
@Configuration
public class SwaggerConfig {
@Bean
public OpenAPI springShopOpenAPI() {
return new OpenAPI()
.info(new Info().title("标题")
.description("我的API文档")
.version("v1")
.license(new License().name("Apache 2.0").url("http://springdoc.org")))
.externalDocs(new ExternalDocumentation()
.description("外部文档")
.url("https://springshop.wiki.github.org/docs"));
}
}
package com.etp.sustainable.constant;
/**
* @author etp
* @date 2023-10-11
*/
public interface CommonConstants {
/**
* 成功标记
*/
Integer SUCCESS = 200;
/**
* 失败标记
*/
Integer FAIL = 500;
}
4、最后一步配置启动类:在启动类中增加注解
package com.etp.sustainable;
import lombok.SneakyThrows;
import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.Environment;
import java.net.InetAddress;
@SpringBootApplication
@MapperScan("com.etp.sustainable.mapper")
public class SustainableApplication {
private static final Logger log = LoggerFactory.getLogger(SustainableApplication.class);
@SneakyThrows
public static void main(String[] args) {
ConfigurableApplicationContext application = SpringApplication.run(SustainableApplication.class, args);
Environment env = application.getEnvironment();
log.info("\n----------------------------------------------------------\n\t" +
"应用 '{}' 运行成功! 访问连接:\n\t" +
"Swagger文档: \t\thttp://{}:{}/swagger-ui.html\n\t" +
"----------------------------------------------------------",
env.getProperty("spring.application.name"),
InetAddress.getLocalHost().getHostAddress(),
env.getProperty("server.port")
);
}
}
总结
启动后就是对框架的生华!
备注启动: