【企业级SpringBoot单体项目模板 】—— 一些开发规范

  • 😜           是江迪呀
  • ✒️本文关键词SpringBoot项目模版企业级
  • ☀️每日   一言种一棵树最好的时间是十年前,其次是现在!

上一回我们已经搭建了一个单体SpringBoot项目并且做了一些全局的配置、比如全局异常处理、返回体配置。下面我们需要说的是在企业开发中提高代码可读性的一些规范。

本节最终目录结构

在这里插入图片描述

一、命名

1.1 类的命名和定义:

  • 实体类:和数据库名称保持一致,使用驼峰。
  • 出入参类: 入参查询使用QO结尾,其余使用DTO结尾。出参使用VO结尾。查询使用Query开头、新增使用Add开头、删除使用Delete开头、修改使用Update开头,如下所示:
    在这里插入图片描述
@Data
public class QueryUserQO {
    /**
     * 姓名
     */
    private String name;

    /**
     * 年龄
     */
    private Integer age;
}

@Data
public class DeleteUserQO {
    private Long id;
}
@Data
public class UpdateUserQO {
    private String name;
    private Integer age;
}

@Data
public class AddUserQO {
    private String name;
    private Integer age;
}

1.2 类的命名不要太长:

比如我需要定义一个企业微信发消息给用户的类命名为:QywxSendMessageToUserQO,感觉太长了如果缩减又不能达到见名之意的效果。

怎么办呢?

我们可以将QywxSendMessage往上提出来,作为文件夹的名字,那么这个文件夹下面就表示都是企业微信发消息的类,那么我们 可以将QywxSendMessageToUserQO缩减为ToUserQO在这里插入图片描述
当然如果你感觉qywxsendmessage作为文件夹的名字太长,你可以再分一级:在这里插入图片描述

二、使用RESTful风格

2.1 RESFul

如果你经常在定义接口的url时犯难,不知道如何定义这个url的名字才比较合适,起早贪黑想终于想好了结果又和其他url名称重复了。这太让人无语了!

那么RESTful风格绝对值得拥有!做法很简单,你只需要遵循一下两点:

  • RESTful规则: GET(获取资源)POST(创建资源)PUT(更新资源)DELETE(删除资源)等。
  • 以资源为驱动规则: 一切请求都被视为资源,每个资源都有一个唯一的标识符(通常是一个 URL)。这些资源可以是物理实体,也可以是虚拟概念,如数据、服务、文件等。
@RestController
@RequestMapping("/api")
public class UserController {
    
    private final List<User> userList = new ArrayList<>(); // 用于存放用户的列表
    // 获取所有用户列表
    @GetMapping("/users")
    public ResponseEntity<List<User>> getAllUsers() {
        return ResponseEntity.ok(userList);
    }
    // 获取单个用户信息
    @GetMapping("/user/{id}")
    public ResponseEntity<User> getUser(@PathVariable Long id) {
        User user = findUserById(id);
        if (user != null) {
            return ResponseEntity.ok(user);
        } else {
            return ResponseEntity.notFound().build();
        }
    }
    // 创建新用户
    @PostMapping
    public ResponseEntity<User> createUser(@RequestBody User user) {
        userList.add(user);
        return ResponseEntity.status(HttpStatus.CREATED).body(user);
    }
    // 更新用户信息
    @PutMapping("/user/{id}")
    public ResponseEntity<User> updateUser(@PathVariable Long id, @RequestBody User updatedUser) {
        User user = findUserById(id);
        if (user != null) {
            user.setUsername(updatedUser.getUsername());
            user.setEmail(updatedUser.getEmail());
            return ResponseEntity.ok(user);
        } else {
            return ResponseEntity.notFound().build();
        }
    }
    // 删除用户
    @DeleteMapping("/user/{id}")
    public ResponseEntity<Void> deleteUser(@PathVariable Long id) {
        User user = findUserById(id);
        if (user != null) {
            userList.remove(user);
            return ResponseEntity.noContent().build();
        } else {
            return ResponseEntity.notFound().build();
        }
    }
}

2.2 关于控制层命名

既然我们使用了RESTful并以资源为驱动的url命名风格,那么我们将控制层的名称也可以修改为Resource结尾,如:在这里插入图片描述

三、按照模块分类

每个模块都要创建属于它自己的文件夹,这样非常方便后期维护管理,会使整个项目看起来更加简洁。我们拿用户模块和订单模块创建的实体类举例:在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
- chapter1:[基本项目构建(可作为工程脚手架),引入web模块,完成一个简单的RESTful API](http://blog.didispace.com/spring-boot-learning-1/) - [使用Intellij中的Spring Initializr来快速构建Spring Boot/Cloud工程](http://blog.didispace.com/spring-initializr-in-intellij/) ### 工程配置 - chapter2-1-1:[配置文件详解:自定义属性、随机数、多环境配置等](http://blog.didispace.com/springbootproperties/) ### Web开发 - chapter3-1-1:[构建一个较为复杂的RESTful API以及单元测试](http://blog.didispace.com/springbootrestfulapi/) - chapter3-1-2:[使用Thymeleaf模板引擎渲染web视图](http://blog.didispace.com/springbootweb/) - chapter3-1-3:[使用Freemarker模板引擎渲染web视图](http://blog.didispace.com/springbootweb/) - chapter3-1-4:[使用Velocity模板引擎渲染web视图](http://blog.didispace.com/springbootweb/) - chapter3-1-5:[使用Swagger2构建RESTful API](http://blog.didispace.com/springbootswagger2/) - chapter3-1-6:[统一异常处理](http://blog.didispace.com/springbootexception/) ### 数据访问 - chapter3-2-1:[使用JdbcTemplate](http://blog.didispace.com/springbootdata1/) - chapter3-2-2:[使用Spring-data-jpa简化数据访问层(推荐)](http://blog.didispace.com/springbootdata2/) - chapter3-2-3:[多数据源配置(一):JdbcTemplate](http://blog.didispace.com/springbootmultidatasource/) - chapter3-2-4:[多数据源配置(二):Spring-data-jpa](http://blog.didispace.com/springbootmultidatasource/) - chapter3-2-5:[使用NoSQL数据库(一):Redis](http://blog.didispace.com/springbootredis/) - chapter3-2-6:[使用NoSQL数据库(二):MongoDB](http://blog.didispace.com/springbootmongodb/) - chapter3-2-7:[整合MyBatis](http://blog.didispace.com/springbootmybatis/) - chapter3-2-8:[MyBatis注解配置详解](http://blog.didispace.com/mybatisinfo/) ### 事务管理 - chapter3-3-1:[使用事务管理](http://blog.didispace.com/springboottransactional/) - chapter3-3-2:[分布式事务(未完成)] ### 其他内容 - chapter4-1-1:[使用@Scheduled创建定时任务](http://blog.didispace.com/springbootscheduled/) - chapter4-1-2:[使用@Async实现异步调用](http://blog.didispace.com/springbootasync/) #### 日志管理 - chapter4-2-1:[默认日志的配置](http://blog.didispace.com/springbootlog/) - chapter4-2-2:[使用log4j记录日志](http://blog.didispace.com/springbootlog4j/) - chapter4-2-3:[对log4j进行多环境不同日志级别的控制](http://blog
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

是江迪呀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值