{"id":10,"date":"2025-06-29T19:37:37","date_gmt":"2025-06-29T11:37:37","guid":{"rendered":"https:\/\/www.rbfwq.com\/blog\/?p=10"},"modified":"2025-06-29T19:37:37","modified_gmt":"2025-06-29T11:37:37","slug":"spring-boot-%e6%98%af%e4%bb%80%e4%b9%88%e4%bb%a5%e5%8f%8a%e6%9c%89%e4%bb%80%e4%b9%88%e7%89%b9%e7%82%b9","status":"publish","type":"post","link":"https:\/\/www.idc61.com\/blog\/10.html","title":{"rendered":"Spring Boot \u662f\u4ec0\u4e48\u4ee5\u53ca\u6709\u4ec0\u4e48\u7279\u70b9"},"content":{"rendered":"<p><h2>Spring Boot \u662f\u4ec0\u4e48<\/h2>\n<\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/www.idc61.com\/blog\/wp-content\/uploads\/2025\/06\/R8V2657eKo.jpg\" alt=\"Spring Boot \u662f\u4ec0\u4e48\u4ee5\u53ca\u6709\u4ec0\u4e48\u7279\u70b9\" title=\"Spring Boot \u662f\u4ec0\u4e48\u4ee5\u53ca\u6709\u4ec0\u4e48\u7279\u70b9\"><\/p>\n<p><p>Spring Boot \u662f\u4e00\u4e2a\u5f00\u6e90\u7684 Java \u57fa\u4e8e Spring \u6846\u67b6\u7684\u6846\u67b6\uff0c\u5176\u76ee\u7684\u662f\u4e3a\u4e86\u7b80\u5316 Spring \u5e94\u7528\u7684\u914d\u7f6e\u548c\u90e8\u7f72\u3002\u901a\u8fc7\u7b80\u5316\u7684\u8bbe\u7f6e\u3001\u81ea\u52a8\u914d\u7f6e\u4ee5\u53ca\u5916\u90e8\u5316\u914d\u7f6e\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u5feb\u901f\u6784\u5efa\u548c\u751f\u4ea7\u7ea7\u522b\u7684\u5e94\u7528\u7a0b\u5e8f\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd Spring Boot \u7684\u5b89\u88c5\u3001\u57fa\u672c\u4f7f\u7528\u53ca\u5176\u9ad8\u7ea7\u7279\u6027\uff0c\u64cd\u4f5c\u6b65\u9aa4\u3001\u547d\u4ee4\u793a\u4f8b\u53ca\u6ce8\u610f\u4e8b\u9879\u3002<\/p>\n<\/p>\n<p><h2>\u5b89\u88c5 Spring Boot<\/h2>\n<\/p>\n<p><h3>\u524d\u63d0\u6761\u4ef6<\/h3>\n<\/p>\n<ul>\n<li>\u786e\u4fdd\u5df2\u5b89\u88c5 JDK 8 \u6216\u66f4\u9ad8\u7248\u672c\uff08\u53ef\u4ee5\u901a\u8fc7\u5728\u547d\u4ee4\u884c\u4e2d\u8f93\u5165 <code>java -version<\/code> \u6765\u786e\u8ba4\uff09\u3002<\/li>\n<li>\u786e\u4fdd\u5df2\u5b89\u88c5 Apache Maven\uff08\u8f93\u5165 <code>mvn -version<\/code> \u6765\u786e\u8ba4\uff09\u3002<\/li>\n<\/ul>\n<p><h3>\u521b\u5efa Spring Boot \u9879\u76ee<\/h3>\n<\/p>\n<p><h4>\u4f7f\u7528 Spring Initializr<\/h4>\n<\/p>\n<ol>\n<li>\u8bbf\u95ee <a rel=\"nofollow\" href=\"https:\/\/start.spring.io\/\">Spring Initializr<\/a> \u7f51\u7ad9\u3002<\/li>\n<li>\u9009\u62e9\u9879\u76ee\u5143\u6570\u636e\uff1a<\/p>\n<ul>\n<li>Project: Maven Project<\/li>\n<li>Language: Java<\/li>\n<li>Spring Boot: \u9009\u62e9\u6700\u65b0\u7a33\u5b9a\u7248\u672c<\/li>\n<li>Project Metadata:<\/p>\n<ul>\n<li>Group: com.example<\/li>\n<li>Artifact: demo<\/li>\n<li>Name: demo<\/li>\n<li>Description: Demo project for Spring Boot<\/li>\n<li>Package name: com.example.demo<\/li>\n<li>Packaging: Jar<\/li>\n<li>Java: \u9009\u62e9\u4e0e\u5b89\u88c5\u7684 JDK \u7248\u672c\u76f8\u5339\u914d<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<li>\u5728 Dependencies \u90e8\u5206\u6dfb\u52a0\u9700\u8981\u7684\u4f9d\u8d56\uff0c\u4f8b\u5982\uff1a<\/p>\n<ul>\n<li>Spring Web<\/li>\n<li>Spring Data JPA<\/li>\n<li>H2 Database<\/li>\n<\/ul>\n<\/li>\n<li>\u70b9\u51fb <strong>GENERATE<\/strong> \u6309\u94ae\u4e0b\u8f7d\u9879\u76ee\u538b\u7f29\u5305\u3002<\/li>\n<\/ol>\n<p><h3>\u5bfc\u5165\u9879\u76ee<\/h3>\n<\/p>\n<ul>\n<li>\u4f7f\u7528 IDE\uff08\u5982 IntelliJ IDEA \u6216 Eclipse\uff09\u5bfc\u5165\u4e0b\u8f7d\u7684\u9879\u76ee\u3002<\/li>\n<li>\u786e\u4fdd Maven \u4f9d\u8d56\u9879\u5df2\u6210\u529f\u4e0b\u8f7d\u3002<\/li>\n<\/ul>\n<p><h2>\u8fd0\u884c Spring Boot \u5e94\u7528<\/h2>\n<\/p>\n<p><h3>\u4f7f\u7528 Maven \u547d\u4ee4<\/h3>\n<\/p>\n<p><pre><code>.\/mvnw spring-boot:run<\/code><\/pre>\n<\/p>\n<p><p>\u8be5\u547d\u4ee4\u4f1a\u542f\u52a8\u5d4c\u5165\u5f0f\u670d\u52a1\u5668\u5e76\u8fd0\u884c\u5e94\u7528\u7a0b\u5e8f\u3002\u9ed8\u8ba4\u7aef\u53e3\u4e3a8080\uff0c\u53ef\u4ee5\u5728\u6d4f\u89c8\u5668\u4e2d\u8bbf\u95ee <code>http:\/\/localhost:8080<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u521b\u5efa\u7b80\u5355\u7684 RESTful API<\/h3>\n<\/p>\n<p><h4>\u6dfb\u52a0 Controller \u7c7b<\/h4>\n<\/p>\n<p><pre><code><\/p>\r\n<p>package com.example.demo.controller;<\/p>\r\n\r\n<p>import org.springframework.web.bind.annotation.GetMapping;<\/p>\r\n<p>import org.springframework.web.bind.annotation.RestController;<\/p>\r\n\r\n<p>@RestController<\/p>\r\n<p>public class HelloController {<\/p>\r\n\r\n<p>    @GetMapping(\"\/hello\")<\/p>\r\n<p>    public String sayHello() {<\/p>\r\n<p>        return \"Hello, Spring Boot!\";<\/p>\r\n<p>    }<\/p>\r\n<p>}<\/p>\r\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u5b9a\u4e49\u4e86\u4e00\u4e2a\u7b80\u5355\u7684 RESTful API\uff0c\u5f53\u8bbf\u95ee <code>\/hello<\/code> \u65f6\u8fd4\u56de &#8220;Hello, Spring Boot!&#8221;\u3002<\/p>\n<\/p>\n<p><h2>\u914d\u7f6e Spring Boot<\/h2>\n<\/p>\n<p><h3>\u4f7f\u7528 application.properties<\/h3>\n<\/p>\n<p><p>Spring Boot \u5141\u8bb8\u901a\u8fc7 <code>src\/main\/resources\/application.properties<\/code> \u6587\u4ef6\u8fdb\u884c\u914d\u7f6e\u3002\u4f8b\u5982\uff0c\u8bbe\u7f6e\u670d\u52a1\u5668\u7aef\u53e3\uff1a<\/p>\n<\/p>\n<p><pre><code>server.port=8081<\/code><\/pre>\n<\/p>\n<p><p>\u4ee5\u4e0a\u914d\u7f6e\u4f1a\u5c06\u670d\u52a1\u5668\u7aef\u53e3\u66f4\u6539\u4e3a 8081\u3002<\/p>\n<\/p>\n<p><h3>\u5916\u90e8\u5316\u914d\u7f6e<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u73af\u5883\u53d8\u91cf\u6216\u547d\u4ee4\u884c\u53c2\u6570\u6765\u5916\u90e8\u5316\u914d\u7f6e\u3002\u4f8b\u5982\uff0c\u542f\u52a8\u5e94\u7528\u65f6\u6307\u5b9a\u7aef\u53e3\uff1a<\/p>\n<\/p>\n<p><pre><code>.\/mvnw spring-boot:run -Dspring-boot.run.arguments=\"--server.port=8082\"<\/code><\/pre>\n<\/p>\n<p><h2>\u4f7f\u7528\u6570\u636e\u5e93<\/h2>\n<\/p>\n<p><h3>\u914d\u7f6e H2 \u5185\u5b58\u6570\u636e\u5e93<\/h3>\n<\/p>\n<p><p>\u5728 <code>application.properties<\/code> \u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u914d\u7f6e\uff1a<\/p>\n<\/p>\n<p><pre><code><\/p>\r\n<p>spring.datasource.url=jdbc:h2:mem:testdb<\/p>\r\n<p>spring.datasource.driverClassName=org.h2.Driver<\/p>\r\n<p>spring.datasource.username=sa<\/p>\r\n<p>spring.datasource.password=password<\/p>\r\n<p>spring.h2.console.enabled=true<\/p>\r\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u521b\u5efa\u5b9e\u4f53\u7c7b\u548c JPA \u4ed3\u5e93<\/h3>\n<\/p>\n<p><h4>\u6dfb\u52a0 Entity \u7c7b<\/h4>\n<\/p>\n<p><pre><code><\/p>\r\n<p>package com.example.demo.entity;<\/p>\r\n\r\n<p>import javax.persistence.Entity;<\/p>\r\n<p>import javax.persistence.GeneratedValue;<\/p>\r\n<p>import javax.persistence.GenerationType;<\/p>\r\n<p>import javax.persistence.Id;<\/p>\r\n\r\n<p>@Entity<\/p>\r\n<p>public class User {<\/p>\r\n    \r\n<p>    @Id<\/p>\r\n<p>    @GeneratedValue(strategy = GenerationType.IDENTITY)<\/p>\r\n<p>    private Long id;<\/p>\r\n<p>    private String name;<\/p>\r\n    \r\n<p>    \/\/ Getters and Setters<\/p>\r\n<p>}<\/p>\r\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u6dfb\u52a0 Repository \u63a5\u53e3<\/h4>\n<\/p>\n<p><pre><code><\/p>\r\n<p>package com.example.demo.repository;<\/p>\r\n\r\n<p>import com.example.demo.entity.User;<\/p>\r\n<p>import org.springframework.data.jpa.repository.JpaRepository;<\/p>\r\n\r\n<p>public interface UserRepository extends JpaRepository<User, Long> {<\/p>\r\n<p>}<\/p>\r\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4f7f\u7528 JPA \u64cd\u4f5c\u6570\u636e\u5e93<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u5728 Controller \u4e2d\u6ce8\u5165 UserRepository\uff0c\u5e76\u5b9e\u73b0\u589e\u5220\u6539\u67e5\uff1a<\/p>\n<\/p>\n<p><pre><code><\/p>\r\n<p>package com.example.demo.controller;<\/p>\r\n\r\n<p>import com.example.demo.entity.User;<\/p>\r\n<p>import com.example.demo.repository.UserRepository;<\/p>\r\n<p>import org.springframework.beans.factory.annotation.Autowired;<\/p>\r\n<p>import org.springframework.web.bind.annotation.*;<\/p>\r\n\r\n<p>import java.util.List;<\/p>\r\n\r\n<p>@RestController<\/p>\r\n<p>@RequestMapping(\"\/users\")<\/p>\r\n<p>public class UserController {<\/p>\r\n    \r\n<p>    @Autowired<\/p>\r\n<p>    private UserRepository userRepository;<\/p>\r\n\r\n<p>    @PostMapping<\/p>\r\n<p>    public User createUser(@RequestBody User user) {<\/p>\r\n<p>        return userRepository.save(user);<\/p>\r\n<p>    }<\/p>\r\n\r\n<p>    @GetMapping<\/p>\r\n<p>    public List<User> getAllUsers() {<\/p>\r\n<p>        return userRepository.findAll();<\/p>\r\n<p>    }<\/p>\r\n<p>}<\/p>\r\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u6ce8\u610f\u4e8b\u9879\u4e0e\u5b9e\u7528\u6280\u5de7<\/h2>\n<\/p>\n<ul>\n<li><strong>\u4f9d\u8d56\u7ba1\u7406<\/strong>: \u4f7f\u7528 <code>pom.xml<\/code> \u7ba1\u7406\u4f9d\u8d56\uff0c\u786e\u4fdd\u4f7f\u7528\u517c\u5bb9\u7684\u7248\u672c\u3002<\/li>\n<li><strong>\u8c03\u8bd5\u6a21\u5f0f<\/strong>: \u53ef\u4ee5\u901a\u8fc7\u4fee\u6539 <code>application.properties<\/code> \u4e2d\u7684 <code>logging.level.root=DEBUG<\/code> \u5f00\u542f\u8c03\u8bd5\u65e5\u5fd7\u3002<\/li>\n<li><strong>\u73af\u5883\u5207\u6362<\/strong>: \u4f7f\u7528 Spring Profiles \u8fdb\u884c\u4e0d\u540c\u73af\u5883\u4e0b\u7684\u914d\u7f6e\u5207\u6362\uff0c\u4f8b\u5982 <code>application-dev.properties<\/code> \u7528\u4e8e\u5f00\u53d1\u73af\u5883\u3002<\/li>\n<li><strong>\u5065\u5eb7\u68c0\u67e5<\/strong>: \u53ef\u4ee5\u4f7f\u7528 Spring Boot Actuator \u63d0\u4f9b\u7684\u5065\u5eb7\u68c0\u67e5\u548c\u76d1\u63a7\u529f\u80fd\uff0c\u7b80\u5316\u8fd0\u7ef4\u3002<\/li>\n<\/ul>\n<p><h2>\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u60a8\u5e94\u8be5\u80fd\u591f\u5feb\u901f\u5165\u95e8 Spring Boot\uff0c\u5e76\u638c\u63e1\u521b\u5efa RESTful \u5e94\u7528\u548c\u96c6\u6210\u6570\u636e\u5e93\u7684\u57fa\u7840\u77e5\u8bc6\u3002\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\uff0c\u8fd8\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u8fdb\u4e00\u6b65\u6269\u5c55\u5e94\u7528\u7684\u529f\u80fd\u548c\u914d\u7f6e\uff0c\u5229\u7528 Spring Boot \u63d0\u4f9b\u7684\u8bf8\u591a\u7279\u6027\u6765\u63d0\u5347\u5f00\u53d1\u6548\u7387\u548c\u5e94\u7528\u6027\u80fd\u3002<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Spring Boot \u662f\u4ec0\u4e48 Spring Boot \u662f\u4e00\u4e2a\u5f00\u6e90\u7684 Java \u57fa\u4e8e Spring \u6846\u67b6\u7684\u6846\u67b6\uff0c\u5176\u76ee\u7684\u662f\u4e3a\u4e86\u7b80\u5316 Spring \u5e94\u7528\u7684\u914d\u7f6e\u548c\u90e8\u7f72\u3002\u901a\u8fc7\u7b80\u5316\u7684\u8bbe\u7f6e\u3001\u81ea\u52a8\u914d\u7f6e\u4ee5\u53ca\u5916\u90e8\u5316\u914d\u7f6e\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u5feb\u901f\u6784\u5efa\u548c\u751f\u4ea7\u7ea7\u522b\u7684\u5e94\u7528\u7a0b\u5e8f&#8230;<\/p>\n","protected":false},"author":1,"featured_media":11,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"topic":[],"class_list":["post-10","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-jishu"],"_links":{"self":[{"href":"https:\/\/www.idc61.com\/blog\/wp-json\/wp\/v2\/posts\/10","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.idc61.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.idc61.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.idc61.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.idc61.com\/blog\/wp-json\/wp\/v2\/comments?post=10"}],"version-history":[{"count":1,"href":"https:\/\/www.idc61.com\/blog\/wp-json\/wp\/v2\/posts\/10\/revisions"}],"predecessor-version":[{"id":13,"href":"https:\/\/www.idc61.com\/blog\/wp-json\/wp\/v2\/posts\/10\/revisions\/13"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.idc61.com\/blog\/wp-json\/wp\/v2\/media\/11"}],"wp:attachment":[{"href":"https:\/\/www.idc61.com\/blog\/wp-json\/wp\/v2\/media?parent=10"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.idc61.com\/blog\/wp-json\/wp\/v2\/categories?post=10"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.idc61.com\/blog\/wp-json\/wp\/v2\/tags?post=10"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.idc61.com\/blog\/wp-json\/wp\/v2\/topic?post=10"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}