博客
关于我
Springboot 第一讲 创建一个springboot项目
阅读量:600 次
发布时间:2019-03-12

本文共 2151 字,大约阅读时间需要 7 分钟。

Spring Boot快速入门指南

要使用Spring Boot进行项目开发,我们需要首先熟悉它的基本概念和创建项目的流程。

项目创建

使用IntelliJ IDEA IDE 创建Spring Boot项目

  • 打开IDE,选择 File > New Project,点击 Next 开始配置项目。
  • 在项目选择页面,找到Spring Boot项目模板,点击 Choose
  • 选择Spring Boot版本和需要依赖的项目骨架包,点击 Next
  • 项目创建完成后,可能会提示下载错误,检查 pom.xml 是否冲突。如果有问题,可以尝试降低Spring Boot版本。
  • 打启开发环境,通过 main.gradle 文件启动项目。
  • 注意:记得在项目的根目录下启动,确保你有最新版本的 JDK 软件安装。

    简单的示例说明

    完整的项目示例如下:

    项目结构

    src/  main/    java/      com/        myapp/          DemoApplication.java (启动类)          controller/            TestController.java (控制器类)            Person.java (实体类)

    编写代码

    创建一个实体类(与包名保持一致):

    @Data@AllArgsConstructor@NoArgsConstructorpublic class Person {    private String name;    private int age;    private Date createTime;}

    编写控制器类:

    @RestController@RequestMapping("/api")public class TestController {    @InitBinder    public void initBinder(WebDataBinder binder) {        binder.addCustomFormatter(new DateFormatter("yyyy-MM-dd"));    }    @GetMapping("/test persons")    public List
    findAllPersons() { List
    persons = new ArrayList<>(); // 数据库操作或其他数据处理 return persons; } @PostMapping("/test persons") public Person savePerson(@RequestBody Person person) { // 数据库存储逻辑 return person; } @PutMapping("/test persons/{id}") public Person updatePerson(@PathVariable Long id, @RequestBody Person person) { // 数据库更新逻辑 return person; } @DeleteMapping("/test persons/{id}") public void deletePerson(@PathVariable Long id) { // 数据库删除逻辑 }}

    启动类代码:

    @SpringBootApplicationpublic class DemoApplication {    public static void main(String[] args) {        SpringApplication.run(DemoApplication.class, args);    }}

    项目启动

  • 修改 MainApplication.java 文件,确保其在 src/main/java/ 目录下。
  • 打开终端,指令行运行 mvn spring-boot:run
  • 访问 http://localhost:8080/api/test persons 查看端口状态。
  • 核心知识点

  • @RestController/@RequestMapping:用于标注返回值和路径。
  • @InitBinder:用于自定义日期格式转换。
  • 线程安全集合使用:像 Collections.synchronizedList 增加默认线程安全。
  • 测试工具

    使用 Postman 测试:

    • GET 请求:http://localhost:8080/api/test persons
    • POST 请求:http://localhost:8080/api/test persons
    • PUT 和 DELETE 调试也需要测试。

    创建好项目后,可以直接通过 IDE 的自动复盖功能上传修改。

    希望这些信息能帮助初次接触Spring Boot的开发者顺利上手项目开发。

    转载地址:http://ulbxz.baihongyu.com/

    你可能感兴趣的文章
    MTK Android 如何获取系统权限
    查看>>
    MySQL - 4种基本索引、聚簇索引和非聚索引、索引失效情况、SQL 优化
    查看>>
    MySQL - ERROR 1406
    查看>>
    mysql - 视图
    查看>>
    MySQL - 解读MySQL事务与锁机制
    查看>>
    MTTR、MTBF、MTTF的大白话理解
    查看>>
    mt_rand
    查看>>
    mysql -存储过程
    查看>>
    mysql /*! 50100 ... */ 条件编译
    查看>>
    mudbox卸载/完美解决安装失败/如何彻底卸载清除干净mudbox各种残留注册表和文件的方法...
    查看>>
    mysql 1264_关于mysql 出现 1264 Out of range value for column 错误的解决办法
    查看>>
    mysql 1593_Linux高可用(HA)之MySQL主从复制中出现1593错误码的低级错误
    查看>>
    mysql 5.6 修改端口_mysql5.6.24怎么修改端口号
    查看>>
    MySQL 8.0 恢复孤立文件每表ibd文件
    查看>>
    MySQL 8.0开始Group by不再排序
    查看>>
    mysql ansi nulls_SET ANSI_NULLS ON SET QUOTED_IDENTIFIER ON 什么意思
    查看>>
    multi swiper bug solution
    查看>>
    MySQL Binlog 日志监听与 Spring 集成实战
    查看>>
    MySQL binlog三种模式
    查看>>
    multi-angle cosine and sines
    查看>>