如何快速搭建springboot项目(新手入门)

本文详细介绍了使用SpringBoot进行项目开发的步骤,包括创建项目、配置编码、添加POM父依赖、集成SpringMVC、创建启动类和REST接口,以及配置Thymeleaf模板、添加Controller和HTML页面。指南还涉及了Maven的使用和项目部署的注意事项。
摘要由CSDN通过智能技术生成

一、创建项目

 

1.1、创建项目

 

e0c85530caa488d411eb37680f62c405.png

 

1.2、配置编码

 

4cf16cb0a79dad8220a495c2623d89a2.png

 

1.3、取消无用提示

 

a16f4e858b6c6dbcfa14aea31186bec4.png

 

1.4、取消无用参数提示

 

f4da782c885550a835dadf3784ac8048.png

 

二、添加POM父依赖

<!-- 两种方式添加父依赖或者import方式 -->
<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>2.5.7</version>
</parent>

maven的pom文件手动更新

添加完成maven的pom文件之后,会自动更新,也可能不会自动更新,那么我们需要手动更新它。

 

8e87a254fac0d994b4c275c615f40497.png

 

三、支持SpringMVC

<dependencies>
    <!-- 支持SpringMVC -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

 

四、创建启动类、rest接口

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class StartApplication {
    public static void main(String[] args) {
        SpringApplication.run(StartApplication.class, args);
    }
}

 

五、配置插件

配置完成后,maven打包可以生成可执行jar文件

<build>
  <plugins>
      <!-- 打包成可执行jar -->
      <plugin>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
      <!-- 配置跳过测试 -->
      <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <configuration>
              <skip>true</skip>
          </configuration>
      </plugin>
      <!-- 配置jdk版本11 -->
      <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-compiler-plugin</artifactId>
          <configuration>
              <source>11</source>
              <target>11</target>
              <encoding>utf-8</encoding>
          </configuration>
      </plugin>
  </plugins>
</build>

 

六、添加thymeleaf模板

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

 

七、添加配置

在resources文件夹下,创建application.properties

在resources文件夹下,创建templates文件夹

# 应用名称
spring.application.name=thymeleaf
# 应用服务 WEB 访问端口
server.port=8080
# THYMELEAF (ThymeleafAutoConfiguration)
# 开启模板缓存(默认值: true )
spring.thymeleaf.cache=false
# 检查模板是否存在,然后再呈现
spring.thymeleaf.check-template=true
# 检查模板位置是否正确(默认值 :true )
spring.thymeleaf.check-template-location=true
#Content-Type 的值(默认值: text/html )
spring.thymeleaf.content-type=text/html
# 开启 MVC Thymeleaf 视图解析(默认值: true )
spring.thymeleaf.enabled=true
# 模板编码
spring.thymeleaf.encoding=UTF-8
# 要被排除在解析之外的视图名称列表,⽤逗号分隔
spring.thymeleaf.excluded-view-names=
# 要运⽤于模板之上的模板模式。另⻅ StandardTemplate-ModeHandlers( 默认值: HTML5)
spring.thymeleaf.mode=HTML5
# 在构建 URL 时添加到视图名称前的前缀(默认值: classpath:/templates/ )
spring.thymeleaf.prefix=classpath:/templates/
# 在构建 URL 时添加到视图名称后的后缀(默认值: .html )
spring.thymeleaf.suffix=.html

 

八、添加controller

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class IndexController {
    @RequestMapping("/index")
    public ModelAndView index(){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("index");
        return mv;
    }
}
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
@RestController
public class HelloController {
    @GetMapping("/Hello")
    public String Hello(){
        return "haha";
    }
}
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
 * rest测试controller
 */
@RestController
public class RestIndexController {
    @GetMapping("/restIndex")
    public String index(){
        return "rest";
    }
}

 

九、添加html

在templates下创建index.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" >
<head>
    <meta charset="UTF-8">
    <title>index</title>
</head>
<body>
index
</body>
</html>

 

十、访问

需要maven执行编译,否则容易404

http://localhost:8080/index

 

  • 17
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
使用Spring Boot可以快速搭建一个基于RESTful API的用户增删改查功能。 首先,需要新建一个Spring Boot项目,可以通过Spring Initializr(https://start.spring.io/)进行初始化。 接下来,通过引入相关的依赖,创建用户实体类和数据库访问接口。 1. 创建用户实体类 ```java @Entity @Table(name = "users") public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String name; private String email; // 省略构造方法、getter和setter } ``` 2. 创建数据库访问接口 ```java @Repository public interface UserRepository extends JpaRepository<User, Long> { } ``` 3. 创建用户控制器类 ```java @RestController @RequestMapping("/users") public class UserController { @Autowired private UserRepository userRepository; @GetMapping("/") public List<User> getAllUsers() { return userRepository.findAll(); } @GetMapping("/{id}") public User getUserById(@PathVariable Long id) { return userRepository.findById(id) .orElseThrow(() -> new ResourceNotFoundException("User not found with id: " + id)); } @PostMapping("/") public User createUser(@RequestBody User user) { return userRepository.save(user); } @PutMapping("/{id}") public User updateUser(@PathVariable Long id, @RequestBody User userDetails) { User user = userRepository.findById(id) .orElseThrow(() -> new ResourceNotFoundException("User not found with id: " + id)); user.setName(userDetails.getName()); user.setEmail(userDetails.getEmail()); return userRepository.save(user); } @DeleteMapping("/{id}") public ResponseEntity<?> deleteUser(@PathVariable Long id) { User user = userRepository.findById(id) .orElseThrow(() -> new ResourceNotFoundException("User not found with id: " + id)); userRepository.delete(user); return ResponseEntity.ok().build(); } } ``` 以上代码中,`@RestController`注解表示这是一个RESTful API控制器类,`@RequestMapping("/users")`指定了API的基础路径为"/users"。 4. 配置数据库连接 在`application.properties`文件中配置数据库连接相关信息,例如: ```properties spring.datasource.url=jdbc:mysql://localhost:3306/mydatabase spring.datasource.username=root spring.datasource.password=123456 spring.jpa.hibernate.ddl-auto=create ``` 这里假设使用MySQL数据库,数据库名为"mydatabase",用户名为"root",密码为"123456"。 5. 运行应用程序 通过运行Spring Boot应用程序,应用程序将监听在默认端口(通常是8080)。可以使用Postman或其他HTTP客户端工具发送HTTP请求来测试API。 通过发送GET请求到`http://localhost:8080/users/`,可以获取所有用户的列表。 通过发送GET请求到`http://localhost:8080/users/{id}`,可以获取指定ID的用户信息。 通过发送POST请求到`http://localhost:8080/users/`,可以创建一个新用户。 通过发送PUT请求到`http://localhost:8080/users/{id}`,可以更新指定ID的用户信息。 通过发送DELETE请求到`http://localhost:8080/users/{id}`,可以删除指定ID的用户。 以上就是使用Spring Boot快速搭建用户增删改查功能的基本步骤。当然,还可以根据实际需求进行更多的定制和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

浮生若梦777

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

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

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

打赏作者

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

抵扣说明:

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

余额充值