{"id":808,"date":"2024-03-19T00:55:56","date_gmt":"2024-03-18T16:55:56","guid":{"rendered":"https:\/\/blog.yiming1234.cn\/?p=808"},"modified":"2024-03-19T09:09:24","modified_gmt":"2024-03-19T01:09:24","slug":"php%e4%b8%8espring-boot%e5%9c%a8%e5%ae%9e%e7%8e%b0%e5%8a%9f%e8%83%bd%e4%b8%8a%e7%9a%84%e6%af%94%e8%be%83","status":"publish","type":"post","link":"https:\/\/blog.yiming1234.cn\/index.php\/2024\/php%e4%b8%8espring-boot%e5%9c%a8%e5%ae%9e%e7%8e%b0%e5%8a%9f%e8%83%bd%e4%b8%8a%e7%9a%84%e6%af%94%e8%be%83\/","title":{"rendered":"PHP\u4e0eSpring Boot\u5728\u5b9e\u73b0\u529f\u80fd\u4e0a\u7684\u6bd4\u8f83"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u524d\u8a00<\/h2>\n\n\n\n<p>\u6700\u8fd1\u5728\u5b66Spring Boot\u540e\u7aef\u6846\u67b6\uff0c\u5728\u529f\u80fd\u5b9e\u73b0\u4e0a\u53d1\u73b0\u4e86\u4e0ePHP\u5b58\u5728\u7740\u4e00\u5b9a\u7684\u53ef\u6bd4\u6027\u3002<\/p>\n\n\n\n<p>\u8fd9\u7bc7\u5c31\u5355\u72ec\u62ce\u51fa\u6765\u8fdb\u884c\u6bd4\u8f83\u4e00\u4e0b\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u6b63\u6587<\/h2>\n\n\n\n<p>\u5c31\u4f8b\u4e3e\u4e00\u4e9b\u529f\u80fd\u4e0a\u7684\u91cd\u53e0\u4ee5\u53ca\u5927\u81f4\u7684\u4ee3\u7801\u5b9e\u73b0\u601d\u8def<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u53d8\u91cf\u7c7b\u578b\u7684\u8f6c\u6362<\/h3>\n\n\n\n<p>\u663e\u793a\u7c7b\u578b\u7684\u8f6c\u6362\uff0c\u4f7f\u7528\u5f3a\u5236\u8f6c\u6362\u7684\u8bed\u6cd5<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">php\u4e2d<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>$stringVar = \"10\"; \/\/ \u5b57\u7b26\u4e32\u53d8\u91cf\r\n$intVar = (int) $stringVar;\r\necho $intVar;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">SpringBoot\u4e2d<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>String stringVar = \"10\";\r\nint intVar = Integer.parseInt(stringVar);\r\nSystem.out.println(intVar);\r<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u952e\u503c\u5bf9\u6570\u636e\u7ed3\u6784<\/h3>\n\n\n\n<p>php\u4e2d\u7684\u5173\u8054\u6570\u7ec4\u548cSpringBoot\u4e2d\u7684Map\u7c7b\u578b\u5b58\u5728\u4e00\u5b9a\u7684\u76f8\u4f3c\u6027<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">php\u4e2d<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>$map = array(\r\n    \"key1\" => \"value1\",\r\n    \"key2\" => 123,\r\n    \"key3\" => true\r\n);\r\necho $map&#91;\"key1\"]; \/\/ \u8f93\u51fa\uff1avalue1\r\necho $map&#91;\"key2\"]; \/\/ \u8f93\u51fa\uff1a123\r\necho $map&#91;\"key3\"]; \/\/ \u8f93\u51fa\uff1atrue\r<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">SpringBoot\u4e2d<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>Map&lt;String, Object> map = new HashMap&lt;>();\r\nmap.put(\"key1\", \"value1\");\r\nmap.put(\"key2\", 123);\r\nmap.put(\"key3\", true);\r\nSystem.out.println(map.get(\"key1\")); \/\/ \u8f93\u51fa\uff1avalue1\r\nSystem.out.println(map.get(\"key2\")); \/\/ \u8f93\u51fa\uff1a123\r\nSystem.out.println(map.get(\"key3\")); \/\/ \u8f93\u51fa\uff1atrue\r<\/code><\/pre>\n\n\n\n<p>\u4e0d\u540c\u8def\u5f84\u7684\u8bbf\u95ee\u4e0e\u54cd\u5e94<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u8fd4\u56deHTTP\u54cd\u5e94<\/h3>\n\n\n\n<p>\u5728\u529f\u80fd\u4e0aphp\u548cSpringBoot\u90fd\u652f\u6301\u8fd4\u56deHTTP\u54cd\u5e94\uff0c\u4e0b\u9762\u4e3e\u51e0\u4e2a\u5177\u4f53\u7684\u4f8b\u5b50\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">php\u4e2d<\/h4>\n\n\n\n<p>\u8bbe\u7f6e\u72b6\u6001\u7801<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\r\nhttp_response_code(200);\r<\/code><\/pre>\n\n\n\n<p>\u8bbe\u7f6e\u91cd\u5b9a\u5411<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\r\nheader(\"Location: \/hello\", true, 302);\r<\/code><\/pre>\n\n\n\n<p>\u8bbe\u7f6e\u54cd\u5e94\u5934<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\r\nheader(\"Content-Type: text\/plain\");\r<\/code><\/pre>\n\n\n\n<p>\u8bbe\u7f6e\u54cd\u5e94\u5185\u5bb9<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\r\necho \"Hello, world!\";\r<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">SpringBoot\u4e2d<\/h4>\n\n\n\n<p>\u8bbe\u7f6e\u72b6\u6001\u7801<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import org.springframework.http.HttpStatus;\r\nimport org.springframework.web.bind.annotation.*;\r\n\r\n@RestController\r\npublic class MyController {\r\n    \r\n    @GetMapping(\"\/hello\")\r\n    public ResponseEntity&lt;String> hello() {\r\n        return new ResponseEntity&lt;>(\"Hello, world!\", HttpStatus.OK);\r\n    }\r\n}\r<\/code><\/pre>\n\n\n\n<p>\u8bbe\u7f6e\u91cd\u5b9a\u5411<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import org.springframework.web.bind.annotation.*;\r\n\r\n@RestController\r\npublic class MyController {\r\n    \r\n    @GetMapping(\"\/redirect\")\r\n    public void redirect(HttpServletResponse response) throws IOException {\r\n        response.sendRedirect(\"\/hello\");\r\n    }\r\n}\r<\/code><\/pre>\n\n\n\n<p>\u8bbe\u7f6e\u54cd\u5e94\u5934<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import org.springframework.http.MediaType;\r\nimport org.springframework.web.bind.annotation.*;\r\n\r\n@RestController\r\npublic class MyController {\r\n    \r\n    @GetMapping(\"\/hello\")\r\n    public ResponseEntity&lt;String> hello() {\r\n        HttpHeaders headers = new HttpHeaders();\r\n        headers.setContentType(MediaType.TEXT_PLAIN);\r\n        return new ResponseEntity&lt;>(\"Hello, world!\", headers, HttpStatus.OK);\r\n    }\r\n}\r<\/code><\/pre>\n\n\n\n<p>\u8bbe\u7f6e\u54cd\u5e94\u5185\u5bb9\uff08\u7565\uff09<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u51fd\u6570\u7684\u4f7f\u7528<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">php\u4e2d<\/h4>\n\n\n\n<p>\u51fd\u6570\u58f0\u660e<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function add($a, $b) {\r\n    return $a + $b;\r\n}<\/code><\/pre>\n\n\n\n<p>\u53c2\u6570\u4f20\u9012<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function greet($name) {\r\n    echo \"Hello, \" . $name . \"!\";\r\n}\r\ngreet(\"John\");<\/code><\/pre>\n\n\n\n<p>\u8fd4\u56de\u503c<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function multiply($a, $b) {\r\n    return $a * $b;\r\n}\r\n$result = multiply(2, 3);<\/code><\/pre>\n\n\n\n<p>\u51fd\u6570\u8c03\u7528<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$sum = add(3, 5);<\/code><\/pre>\n\n\n\n<p>\u51fd\u6570\u4f5c\u7528\u57df<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function myFunction() {\r\n    \/\/ \u5728\u51fd\u6570\u4e2d\u5b9a\u4e49\u7684\u51fd\u6570\u53ea\u80fd\u5728\u8be5\u51fd\u6570\u4e2d\u4f7f\u7528\r\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">SpringBoot\u4e2d<\/h4>\n\n\n\n<p>\u51fd\u6570\u58f0\u660e<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public int add(int a, int b) {\r\n    return a + b;\r\n}<\/code><\/pre>\n\n\n\n<p>\u53c2\u6570\u4f20\u9012<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public void greet(String name) {\r\n    System.out.println(\"Hello, \" + name + \"!\");\r\n}\r\ngreet(\"John\");<\/code><\/pre>\n\n\n\n<p>\u8fd4\u56de\u503c<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public int multiply(int a, int b) {\r\n    return a * b;\r\n}\r\nint result = multiply(2, 3);<\/code><\/pre>\n\n\n\n<p>\u51fd\u6570\u8c03\u7528<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int sum = add(3, 5);<\/code><\/pre>\n\n\n\n<p>\u51fd\u6570\u4f5c\u7528\u57df<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class MyService {\r\n    public void myMethod() {\r\n        \/\/ \u5728\u7c7b\u4e2d\u5b9a\u4e49\u7684\u51fd\u6570\u53ef\u4ee5\u5728\u6574\u4e2a\u7c7b\u4e2d\u4f7f\u7528\r\n    }\r\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u6570\u636e\u5e93\u64cd\u4f5c<\/h3>\n\n\n\n<p>php\u548cSpringBoot\u4e2d\u90fd\u5b58\u5728\u8fde\u63a5\u6570\u636e\u5e93\u7684\u64cd\u4f5c<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">php\u4e2d<\/h4>\n\n\n\n<p>\u6570\u636e\u5e93\u8fde\u63a5\u914d\u7f6e<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$servername = \"localhost\";\r\n$username = \"root\";\r\n$password = \"123456\";<\/code><\/pre>\n\n\n\n<p>\u6267\u884cSQL\u67e5\u8be2<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$stmt = $conn->prepare(\"SELECT * FROM my_table\");\r\n$stmt->execute();\r\n$rows = $stmt->fetchAll();<\/code><\/pre>\n\n\n\n<p>\u4e8b\u52a1\u7ba1\u7406\uff08\u7565\uff09<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">SpringBoot\u4e2d<\/h4>\n\n\n\n<p>\u6570\u636e\u5e93\u8fde\u63a5\u914d\u7f6e<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>spring.datasource.url=jdbc:mysql:\/\/localhost:3306\/mydb\r\nspring.datasource.username=root\r\nspring.datasource.password=123456<\/code><\/pre>\n\n\n\n<p>\u6267\u884cSQL\u67e5\u8be2<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>List&lt;Map&lt;String, Object>> rows = jdbcTemplate.queryForList(\"SELECT * FROM my_table\");<\/code><\/pre>\n\n\n\n<p>\u4e8b\u52a1\u7ba1\u7406<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@Transactional\r\npublic void performTransaction() {\r\n    \/\/ \u6267\u884c\u6570\u636e\u5e93\u64cd\u4f5c\r\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u50a8\u5b58\u7528\u6237\u767b\u5f55\u72b6\u6001<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">php\u4e2d<\/h4>\n\n\n\n<p>\u5728php\u4e2d\u901a\u5e38\u901a\u8fc7\u4f1a\u8bdd\u7ba1\u7406\u6216\u8005cookie\u8bbe\u7f6e\u6765\u50a8\u5b58\u7528\u6237\u7684\u767b\u5f55\u72b6\u6001\u3002\u5728\u4e0d\u540c\u8bf7\u6c42\u95f4\u5171\u4eab\u7528\u6237\u4fe1\u606f\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">SpringBoot\u4e2d<\/h4>\n\n\n\n<p>\u5728SpringBoot\u4e2d\u901a\u5e38\u901a\u8fc7\u62e6\u622a\u5668\u52a0\u4e0aThreadLocal\u6765\u5b9e\u73b0\u767b\u5f55\u7684\u62e6\u622a\u4ee5\u53ca\u7ebf\u7a0b\u8303\u56f4\u5185\u7528\u6237\u4fe1\u606f\u7684\u6570\u636e\u5171\u4eab\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u5c3e\u58f0<\/h2>\n\n\n\n<p>\u4f5c\u4e3a\u5b9e\u73b0\u540e\u7aef\u529f\u80fd\u4e24\u79cd\u8bed\u8a00\uff0c\u5728\u529f\u80fd\u548c\u4f7f\u7528\u4e0a\u80af\u5b9a\u8fd8\u6709\u5176\u4ed6\u7684\u76f8\u4f3c\u4e4b\u5904\uff0c\u4e0a\u9762\u662f\u6211\u76ee\u524d\u6574\u7406\u7684\u5185\u5bb9\u3002\u53e6\u5916\u7684\u6709\u5f85\u5927\u5bb6\u81ea\u5df1\u53d1\u6398\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u524d\u8a00 \u6700\u8fd1\u5728\u5b66Spring Boot\u540e\u7aef\u6846\u67b6\uff0c\u5728\u529f\u80fd\u5b9e\u73b0\u4e0a\u53d1\u73b0\u4e86\u4e0ePHP\u5b58\u5728\u7740\u4e00\u5b9a\u7684\u53ef\u6bd4\u6027\u3002 \u8fd9\u7bc7\u5c31\u5355\u72ec\u62ce\u51fa\u6765 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[11],"tags":[37],"class_list":["post-808","post","type-post","status-publish","format-standard","hentry","category-web-backend","tag-knowledge-understanding"],"_links":{"self":[{"href":"https:\/\/blog.yiming1234.cn\/index.php\/wp-json\/wp\/v2\/posts\/808","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.yiming1234.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.yiming1234.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.yiming1234.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.yiming1234.cn\/index.php\/wp-json\/wp\/v2\/comments?post=808"}],"version-history":[{"count":2,"href":"https:\/\/blog.yiming1234.cn\/index.php\/wp-json\/wp\/v2\/posts\/808\/revisions"}],"predecessor-version":[{"id":810,"href":"https:\/\/blog.yiming1234.cn\/index.php\/wp-json\/wp\/v2\/posts\/808\/revisions\/810"}],"wp:attachment":[{"href":"https:\/\/blog.yiming1234.cn\/index.php\/wp-json\/wp\/v2\/media?parent=808"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.yiming1234.cn\/index.php\/wp-json\/wp\/v2\/categories?post=808"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.yiming1234.cn\/index.php\/wp-json\/wp\/v2\/tags?post=808"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}