Files

1 line
34 KiB
JavaScript

"use strict";(globalThis.webpackChunkblog=globalThis.webpackChunkblog||[]).push([[6473],{19175:(e,n,r)=>{r.r(n),r.d(n,{assets:()=>c,contentTitle:()=>d,default:()=>x,frontMatter:()=>i,metadata:()=>t,toc:()=>a});const t=JSON.parse('{"id":"\u4e2a\u4eba\u77e5\u8bc6\u5e93/\u8bed\u8a00\u77e5\u8bc6/Go/Go\u57fa\u7840\u7f16\u7a0b/Golang-\u64cd\u4f5cMySQL","title":"Golang-\u64cd\u4f5cMySQL","description":"|\u4f5c\u8005|\u4fee\u8ba2\u65f6\u95f4|","source":"@site/docs/\u4e2a\u4eba\u77e5\u8bc6\u5e93/05.\u8bed\u8a00\u77e5\u8bc6/03.Go/01.Go\u57fa\u7840\u7f16\u7a0b/12.Golang-\u64cd\u4f5cMySQL.md","sourceDirName":"\u4e2a\u4eba\u77e5\u8bc6\u5e93/05.\u8bed\u8a00\u77e5\u8bc6/03.Go/01.Go\u57fa\u7840\u7f16\u7a0b","slug":"/\u4e2a\u4eba\u77e5\u8bc6\u5e93/\u8bed\u8a00\u77e5\u8bc6/Go/Go\u57fa\u7840\u7f16\u7a0b/Golang-\u64cd\u4f5cMySQL","permalink":"/docs/\u4e2a\u4eba\u77e5\u8bc6\u5e93/\u8bed\u8a00\u77e5\u8bc6/Go/Go\u57fa\u7840\u7f16\u7a0b/Golang-\u64cd\u4f5cMySQL","draft":false,"unlisted":false,"editUrl":"https://github.com/facebook/docusaurus/tree/main/packages/create-docusaurus/templates/shared/docs/\u4e2a\u4eba\u77e5\u8bc6\u5e93/05.\u8bed\u8a00\u77e5\u8bc6/03.Go/01.Go\u57fa\u7840\u7f16\u7a0b/12.Golang-\u64cd\u4f5cMySQL.md","tags":[],"version":"current","sidebarPosition":12,"frontMatter":{},"sidebar":"tutorialSidebar","previous":{"title":"Go\u8bed\u8a00\u6587\u4ef6\u64cd\u4f5c","permalink":"/docs/\u4e2a\u4eba\u77e5\u8bc6\u5e93/\u8bed\u8a00\u77e5\u8bc6/Go/Go\u57fa\u7840\u7f16\u7a0b/GoLang-\u6587\u4ef6io"},"next":{"title":"Redis","permalink":"/docs/\u4e2a\u4eba\u77e5\u8bc6\u5e93/\u8bed\u8a00\u77e5\u8bc6/Go/Go\u57fa\u7840\u7f16\u7a0b/GoLang-\u64cd\u4f5cRedis"}}');var s=r(74848),l=r(28453);const i={},d=void 0,c={},a=[{value:"\u4e0b\u8f7d\u4f9d\u8d56",id:"\u4e0b\u8f7d\u4f9d\u8d56",level:3},{value:"\u4f7f\u7528MySQL\u9a71\u52a8",id:"\u4f7f\u7528mysql\u9a71\u52a8",level:3},{value:"\u521d\u59cb\u5316\u8fde\u63a5",id:"\u521d\u59cb\u5316\u8fde\u63a5",level:3},{value:"SetMaxOpenConns",id:"setmaxopenconns",level:3},{value:"SetMaxIdleConns",id:"setmaxidleconns",level:3},{value:"CRUD",id:"crud",level:2},{value:"\u5efa\u5e93\u5efa\u8868",id:"\u5efa\u5e93\u5efa\u8868",level:3},{value:"\u67e5\u8be2",id:"\u67e5\u8be2",level:3},{value:"\u5355\u884c\u67e5\u8be2",id:"\u5355\u884c\u67e5\u8be2",level:4},{value:"\u591a\u884c\u67e5\u8be2",id:"\u591a\u884c\u67e5\u8be2",level:4},{value:"\u63d2\u5165\u6570\u636e",id:"\u63d2\u5165\u6570\u636e",level:3},{value:"\u66f4\u65b0\u6570\u636e",id:"\u66f4\u65b0\u6570\u636e",level:3},{value:"\u5220\u9664\u6570\u636e",id:"\u5220\u9664\u6570\u636e",level:3},{value:"MySQL\u9884\u5904\u7406",id:"mysql\u9884\u5904\u7406",level:2},{value:"\u4ec0\u4e48\u662f\u9884\u5904\u7406\uff1f",id:"\u4ec0\u4e48\u662f\u9884\u5904\u7406",level:3},{value:"\u4e3a\u4ec0\u4e48\u8981\u9884\u5904\u7406\uff1f",id:"\u4e3a\u4ec0\u4e48\u8981\u9884\u5904\u7406",level:3},{value:"Go\u5b9e\u73b0MySQL\u9884\u5904\u7406",id:"go\u5b9e\u73b0mysql\u9884\u5904\u7406",level:3},{value:"SQL\u6ce8\u5165\u95ee\u9898",id:"sql\u6ce8\u5165\u95ee\u9898",level:3},{value:"Go\u5b9e\u73b0MySQL\u4e8b\u52a1",id:"go\u5b9e\u73b0mysql\u4e8b\u52a1",level:2},{value:"\u4ec0\u4e48\u662f\u4e8b\u52a1\uff1f",id:"\u4ec0\u4e48\u662f\u4e8b\u52a1",level:3},{value:"\u4e8b\u52a1\u7684ACID",id:"\u4e8b\u52a1\u7684acid",level:3},{value:"\u4e8b\u52a1\u76f8\u5173\u65b9\u6cd5",id:"\u4e8b\u52a1\u76f8\u5173\u65b9\u6cd5",level:3},{value:"\u4e8b\u52a1\u793a\u4f8b",id:"\u4e8b\u52a1\u793a\u4f8b",level:3}];function o(e){const n={a:"a",code:"code",h2:"h2",h3:"h3",h4:"h4",img:"img",li:"li",ol:"ol",p:"p",pre:"pre",strong:"strong",table:"table",tbody:"tbody",td:"td",th:"th",thead:"thead",tr:"tr",...(0,l.R)(),...e.components};return(0,s.jsxs)(s.Fragment,{children:[(0,s.jsxs)(n.table,{children:[(0,s.jsx)(n.thead,{children:(0,s.jsxs)(n.tr,{children:[(0,s.jsx)(n.th,{style:{textAlign:"left"},children:"\u4f5c\u8005"}),(0,s.jsx)(n.th,{style:{textAlign:"left"},children:"\u4fee\u8ba2\u65f6\u95f4"})]})}),(0,s.jsx)(n.tbody,{children:(0,s.jsxs)(n.tr,{children:[(0,s.jsx)(n.td,{style:{textAlign:"left"},children:(0,s.jsx)(n.img,{src:"https://img.shields.io/badge/wjlin0-%E4%B8%AA%E4%BA%BA%E7%9F%A5%E8%AF%86%E5%BA%93-green",alt:"wjlin0"})}),(0,s.jsx)(n.td,{style:{textAlign:"left"},children:"2025-10-14 23:58:22"})]})})]}),"\n",(0,s.jsxs)(n.p,{children:["Go\u8bed\u8a00\u4e2d\u7684",(0,s.jsx)(n.code,{children:"database/sql"}),"\u5305\u63d0\u4f9b\u4e86\u4fdd\u8bc1SQL\u6216\u7c7bSQL\u6570\u636e\u5e93\u7684\u6cdb\u7528\u63a5\u53e3\uff0c\u5e76\u4e0d\u63d0\u4f9b\u5177\u4f53\u7684\u6570\u636e\u5e93\u9a71\u52a8\u3002\u4f7f\u7528",(0,s.jsx)(n.code,{children:"database/sql"}),"\u5305\u65f6\u5fc5\u987b\u6ce8\u5165\uff08\u81f3\u5c11\uff09\u4e00\u4e2a\u6570\u636e\u5e93\u9a71\u52a8\u3002"]}),"\n",(0,s.jsxs)(n.p,{children:["\u6211\u4eec\u5e38\u7528\u7684\u6570\u636e\u5e93\u57fa\u672c\u4e0a\u90fd\u6709\u5b8c\u6574\u7684\u7b2c\u4e09\u65b9\u5b9e\u73b0\u3002\u4f8b\u5982\uff1a",(0,s.jsx)(n.a,{href:"https://github.com/go-sql-driver/mysql",children:"MySQL\u9a71\u52a8"})]}),"\n",(0,s.jsx)(n.h3,{id:"\u4e0b\u8f7d\u4f9d\u8d56",children:"\u4e0b\u8f7d\u4f9d\u8d56"}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-bash",children:"go get -u github.com/go-sql-driver/mysql\n"})}),"\n",(0,s.jsx)(n.h3,{id:"\u4f7f\u7528mysql\u9a71\u52a8",children:"\u4f7f\u7528MySQL\u9a71\u52a8"}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-go",children:"func Open(driverName, dataSourceName string) (*DB, error)\n"})}),"\n",(0,s.jsx)(n.p,{children:"Open\u6253\u5f00\u4e00\u4e2adirverName\u6307\u5b9a\u7684\u6570\u636e\u5e93\uff0cdataSourceName\u6307\u5b9a\u6570\u636e\u6e90\uff0c\u4e00\u822c\u81f3\u5c11\u5305\u62ec\u6570\u636e\u5e93\u6587\u4ef6\u540d\u548c\u5176\u5b83\u8fde\u63a5\u5fc5\u8981\u7684\u4fe1\u606f\u3002"}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-go",children:'import (\n\t"database/sql"\n\n\t_ "github.com/go-sql-driver/mysql"\n)\n\nfunc main() {\n // DSN:Data Source Name\n\tdsn := "user:password@tcp(127.0.0.1:3306)/dbname"\n\tdb, err := sql.Open("mysql", dsn)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\tdefer db.Close() // \u6ce8\u610f\u8fd9\u884c\u4ee3\u7801\u8981\u5199\u5728\u4e0a\u9762err\u5224\u65ad\u7684\u4e0b\u9762\n}\n'})}),"\n",(0,s.jsxs)(n.p,{children:[(0,s.jsx)(n.strong,{children:"\u601d\u8003\u9898"}),"\uff1a \u4e3a\u4ec0\u4e48\u4e0a\u9762\u4ee3\u7801\u4e2d\u7684",(0,s.jsx)(n.code,{children:"defer db.Close()"}),"\u8bed\u53e5\u4e0d\u5e94\u8be5\u5199\u5728",(0,s.jsx)(n.code,{children:"if err != nil"}),"\u7684\u524d\u9762\u5462\uff1f"]}),"\n",(0,s.jsx)(n.h3,{id:"\u521d\u59cb\u5316\u8fde\u63a5",children:"\u521d\u59cb\u5316\u8fde\u63a5"}),"\n",(0,s.jsx)(n.p,{children:"Open\u51fd\u6570\u53ef\u80fd\u53ea\u662f\u9a8c\u8bc1\u5176\u53c2\u6570\u683c\u5f0f\u662f\u5426\u6b63\u786e\uff0c\u5b9e\u9645\u4e0a\u5e76\u4e0d\u521b\u5efa\u4e0e\u6570\u636e\u5e93\u7684\u8fde\u63a5\u3002\u5982\u679c\u8981\u68c0\u67e5\u6570\u636e\u6e90\u7684\u540d\u79f0\u662f\u5426\u771f\u5b9e\u6709\u6548\uff0c\u5e94\u8be5\u8c03\u7528Ping\u65b9\u6cd5\u3002"}),"\n",(0,s.jsx)(n.p,{children:"\u8fd4\u56de\u7684DB\u5bf9\u8c61\u53ef\u4ee5\u5b89\u5168\u5730\u88ab\u591a\u4e2agoroutine\u5e76\u53d1\u4f7f\u7528\uff0c\u5e76\u4e14\u7ef4\u62a4\u5176\u81ea\u5df1\u7684\u7a7a\u95f2\u8fde\u63a5\u6c60\u3002\u56e0\u6b64\uff0cOpen\u51fd\u6570\u5e94\u8be5\u4ec5\u88ab\u8c03\u7528\u4e00\u6b21\uff0c\u5f88\u5c11\u9700\u8981\u5173\u95ed\u8fd9\u4e2aDB\u5bf9\u8c61\u3002"}),"\n",(0,s.jsxs)(n.p,{children:["\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5b9a\u4e49\u4e00\u4e2a\u5168\u5c40\u53d8\u91cf",(0,s.jsx)(n.code,{children:"db"}),"\uff0c\u7528\u6765\u4fdd\u5b58\u6570\u636e\u5e93\u8fde\u63a5\u5bf9\u8c61\u3002\u5c06\u4e0a\u9762\u7684\u793a\u4f8b\u4ee3\u7801\u62c6\u5206\u51fa\u4e00\u4e2a\u72ec\u7acb\u7684",(0,s.jsx)(n.code,{children:"initDB"}),"\u51fd\u6570\uff0c\u53ea\u9700\u8981\u5728\u7a0b\u5e8f\u542f\u52a8\u65f6\u8c03\u7528\u4e00\u6b21\u8be5\u51fd\u6570\u5b8c\u6210\u5168\u5c40\u53d8\u91cfdb\u7684\u521d\u59cb\u5316\uff0c\u5176\u4ed6\u51fd\u6570\u4e2d\u5c31\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u5168\u5c40\u53d8\u91cf",(0,s.jsx)(n.code,{children:"db"}),"\u4e86\u3002\uff08",(0,s.jsx)(n.strong,{children:"\u6ce8\u610f\u4e0b\u65b9\u7684\u6ce8\u610f"}),"\uff09"]}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-go",children:'// \u5b9a\u4e49\u4e00\u4e2a\u5168\u5c40\u5bf9\u8c61db\nvar db *sql.DB\n\n// \u5b9a\u4e49\u4e00\u4e2a\u521d\u59cb\u5316\u6570\u636e\u5e93\u7684\u51fd\u6570\nfunc initDB() (err error) {\n\t// DSN:Data Source Name\n\tdsn := "user:password@tcp(127.0.0.1:3306)/sql_test?charset=utf8mb4&parseTime=True"\n\t// \u4e0d\u4f1a\u6821\u9a8c\u8d26\u53f7\u5bc6\u7801\u662f\u5426\u6b63\u786e\n\t// \u6ce8\u610f\uff01\uff01\uff01\u8fd9\u91cc\u4e0d\u8981\u4f7f\u7528:=\uff0c\u6211\u4eec\u662f\u7ed9\u5168\u5c40\u53d8\u91cf\u8d4b\u503c\uff0c\u7136\u540e\u5728main\u51fd\u6570\u4e2d\u4f7f\u7528\u5168\u5c40\u53d8\u91cfdb\n\tdb, err = sql.Open("mysql", dsn)\n\tif err != nil {\n\t\treturn err\n\t}\n\t// \u5c1d\u8bd5\u4e0e\u6570\u636e\u5e93\u5efa\u7acb\u8fde\u63a5\uff08\u6821\u9a8cdsn\u662f\u5426\u6b63\u786e\uff09\n\terr = db.Ping()\n\tif err != nil {\n\t\treturn err\n\t}\n\treturn nil\n}\n\nfunc main() {\n\terr := initDB() // \u8c03\u7528\u8f93\u51fa\u5316\u6570\u636e\u5e93\u7684\u51fd\u6570\n\tif err != nil {\n\t\tfmt.Printf("init db failed,err:%v\\n", err)\n\t\treturn\n\t}\n}\n'})}),"\n",(0,s.jsxs)(n.p,{children:["\u5176\u4e2d",(0,s.jsx)(n.code,{children:"sql.DB"}),"\u662f\u8868\u793a\u8fde\u63a5\u7684\u6570\u636e\u5e93\u5bf9\u8c61\uff08\u7ed3\u6784\u4f53\u5b9e\u4f8b\uff09\uff0c\u5b83\u4fdd\u5b58\u4e86\u8fde\u63a5\u6570\u636e\u5e93\u76f8\u5173\u7684\u6240\u6709\u4fe1\u606f\u3002\u5b83\u5185\u90e8\u7ef4\u62a4\u7740\u4e00\u4e2a\u5177\u6709\u96f6\u5230\u591a\u4e2a\u5e95\u5c42\u8fde\u63a5\u7684\u8fde\u63a5\u6c60\uff0c\u5b83\u53ef\u4ee5\u5b89\u5168\u5730\u88ab\u591a\u4e2agoroutine\u540c\u65f6\u4f7f\u7528\u3002"]}),"\n",(0,s.jsx)(n.h3,{id:"setmaxopenconns",children:"SetMaxOpenConns"}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-go",children:"func (db *DB) SetMaxOpenConns(n int)\n"})}),"\n",(0,s.jsxs)(n.p,{children:[(0,s.jsx)(n.code,{children:"SetMaxOpenConns"}),"\u8bbe\u7f6e\u4e0e\u6570\u636e\u5e93\u5efa\u7acb\u8fde\u63a5\u7684\u6700\u5927\u6570\u76ee\u3002 \u5982\u679cn\u5927\u4e8e0\u4e14\u5c0f\u4e8e\u6700\u5927\u95f2\u7f6e\u8fde\u63a5\u6570\uff0c\u4f1a\u5c06\u6700\u5927\u95f2\u7f6e\u8fde\u63a5\u6570\u51cf\u5c0f\u5230\u5339\u914d\u6700\u5927\u5f00\u542f\u8fde\u63a5\u6570\u7684\u9650\u5236\u3002 \u5982\u679c",(0,s.jsx)(n.code,{children:"n<=0"}),"\uff0c\u4e0d\u4f1a\u9650\u5236\u6700\u5927\u5f00\u542f\u8fde\u63a5\u6570\uff0c\u9ed8\u8ba4\u4e3a0\uff08\u65e0\u9650\u5236\uff09\u3002"]}),"\n",(0,s.jsx)(n.h3,{id:"setmaxidleconns",children:"SetMaxIdleConns"}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-go",children:"func (db *DB) SetMaxIdleConns(n int)\n"})}),"\n",(0,s.jsxs)(n.p,{children:["SetMaxIdleConns\u8bbe\u7f6e\u8fde\u63a5\u6c60\u4e2d\u7684\u6700\u5927\u95f2\u7f6e\u8fde\u63a5\u6570\u3002 \u5982\u679cn\u5927\u4e8e\u6700\u5927\u5f00\u542f\u8fde\u63a5\u6570\uff0c\u5219\u65b0\u7684\u6700\u5927\u95f2\u7f6e\u8fde\u63a5\u6570\u4f1a\u51cf\u5c0f\u5230\u5339\u914d\u6700\u5927\u5f00\u542f\u8fde\u63a5\u6570\u7684\u9650\u5236\u3002 \u5982\u679c",(0,s.jsx)(n.code,{children:"n<="}),"0\uff0c\u4e0d\u4f1a\u4fdd\u7559\u95f2\u7f6e\u8fde\u63a5\u3002"]}),"\n",(0,s.jsx)(n.h2,{id:"crud",children:"CRUD"}),"\n",(0,s.jsx)(n.h3,{id:"\u5efa\u5e93\u5efa\u8868",children:"\u5efa\u5e93\u5efa\u8868"}),"\n",(0,s.jsxs)(n.p,{children:["\u6211\u4eec\u5148\u5728MySQL\u4e2d\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a",(0,s.jsx)(n.code,{children:"sql_test"}),"\u7684\u6570\u636e\u5e93"]}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-sql",children:"CREATE DATABASE sql_test;\n"})}),"\n",(0,s.jsx)(n.p,{children:"\u8fdb\u5165\u8be5\u6570\u636e\u5e93:"}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-sql",children:"use sql_test;\n"})}),"\n",(0,s.jsx)(n.p,{children:"\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\u521b\u5efa\u4e00\u5f20\u7528\u4e8e\u6d4b\u8bd5\u7684\u6570\u636e\u8868\uff1a"}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-sql",children:"CREATE TABLE `user` (\n `id` BIGINT(20) NOT NULL AUTO_INCREMENT,\n `name` VARCHAR(20) DEFAULT '',\n `age` INT(11) DEFAULT '0',\n PRIMARY KEY(`id`)\n)ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4;\n"})}),"\n",(0,s.jsx)(n.h3,{id:"\u67e5\u8be2",children:"\u67e5\u8be2"}),"\n",(0,s.jsx)(n.p,{children:"\u4e3a\u4e86\u65b9\u4fbf\u67e5\u8be2\uff0c\u6211\u4eec\u4e8b\u5148\u5b9a\u4e49\u597d\u4e00\u4e2a\u7ed3\u6784\u4f53\u6765\u5b58\u50a8user\u8868\u7684\u6570\u636e\u3002"}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-go",children:"type user struct {\n\tid int\n\tage int\n\tname string\n}\n"})}),"\n",(0,s.jsx)(n.h4,{id:"\u5355\u884c\u67e5\u8be2",children:"\u5355\u884c\u67e5\u8be2"}),"\n",(0,s.jsxs)(n.p,{children:["\u5355\u884c\u67e5\u8be2",(0,s.jsx)(n.code,{children:"db.QueryRow()"}),"\u6267\u884c\u4e00\u6b21\u67e5\u8be2\uff0c\u5e76\u671f\u671b\u8fd4\u56de\u6700\u591a\u4e00\u884c\u7ed3\u679c\uff08\u5373Row\uff09\u3002QueryRow\u603b\u662f\u8fd4\u56de\u975enil\u7684\u503c\uff0c\u76f4\u5230\u8fd4\u56de\u503c\u7684Scan\u65b9\u6cd5\u88ab\u8c03\u7528\u65f6\uff0c\u624d\u4f1a\u8fd4\u56de\u88ab\u5ef6\u8fdf\u7684\u9519\u8bef\u3002\uff08\u5982\uff1a\u672a\u627e\u5230\u7ed3\u679c\uff09"]}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-go",children:"func (db *DB) QueryRow(query string, args ...interface{}) *Row\n"})}),"\n",(0,s.jsx)(n.p,{children:"\u5177\u4f53\u793a\u4f8b\u4ee3\u7801\uff1a"}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-go",children:'// \u67e5\u8be2\u5355\u6761\u6570\u636e\u793a\u4f8b\nfunc queryRowDemo() {\n\tsqlStr := "select id, name, age from user where id=?"\n\tvar u user\n\t// \u975e\u5e38\u91cd\u8981\uff1a\u786e\u4fddQueryRow\u4e4b\u540e\u8c03\u7528Scan\u65b9\u6cd5\uff0c\u5426\u5219\u6301\u6709\u7684\u6570\u636e\u5e93\u94fe\u63a5\u4e0d\u4f1a\u88ab\u91ca\u653e\n\terr := db.QueryRow(sqlStr, 1).Scan(&u.id, &u.name, &u.age)\n\tif err != nil {\n\t\tfmt.Printf("scan failed, err:%v\\n", err)\n\t\treturn\n\t}\n\tfmt.Printf("id:%d name:%s age:%d\\n", u.id, u.name, u.age)\n}\n'})}),"\n",(0,s.jsx)(n.h4,{id:"\u591a\u884c\u67e5\u8be2",children:"\u591a\u884c\u67e5\u8be2"}),"\n",(0,s.jsxs)(n.p,{children:["\u591a\u884c\u67e5\u8be2",(0,s.jsx)(n.code,{children:"db.Query()"}),"\u6267\u884c\u4e00\u6b21\u67e5\u8be2\uff0c\u8fd4\u56de\u591a\u884c\u7ed3\u679c\uff08\u5373Rows\uff09\uff0c\u4e00\u822c\u7528\u4e8e\u6267\u884cselect\u547d\u4ee4\u3002\u53c2\u6570args\u8868\u793aquery\u4e2d\u7684\u5360\u4f4d\u53c2\u6570\u3002"]}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-go",children:"func (db *DB) Query(query string, args ...interface{}) (*Rows, error)\n"})}),"\n",(0,s.jsx)(n.p,{children:"\u5177\u4f53\u793a\u4f8b\u4ee3\u7801\uff1a"}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-go",children:'// \u67e5\u8be2\u591a\u6761\u6570\u636e\u793a\u4f8b\nfunc queryMultiRowDemo() {\n\tsqlStr := "select id, name, age from user where id > ?"\n\trows, err := db.Query(sqlStr, 0)\n\tif err != nil {\n\t\tfmt.Printf("query failed, err:%v\\n", err)\n\t\treturn\n\t}\n\t// \u975e\u5e38\u91cd\u8981\uff1a\u5173\u95edrows\u91ca\u653e\u6301\u6709\u7684\u6570\u636e\u5e93\u94fe\u63a5\n\tdefer rows.Close()\n\n\t// \u5faa\u73af\u8bfb\u53d6\u7ed3\u679c\u96c6\u4e2d\u7684\u6570\u636e\n\tfor rows.Next() {\n\t\tvar u user\n\t\terr := rows.Scan(&u.id, &u.name, &u.age)\n\t\tif err != nil {\n\t\t\tfmt.Printf("scan failed, err:%v\\n", err)\n\t\t\treturn\n\t\t}\n\t\tfmt.Printf("id:%d name:%s age:%d\\n", u.id, u.name, u.age)\n\t}\n}\n'})}),"\n",(0,s.jsx)(n.h3,{id:"\u63d2\u5165\u6570\u636e",children:"\u63d2\u5165\u6570\u636e"}),"\n",(0,s.jsxs)(n.p,{children:["\u63d2\u5165\u3001\u66f4\u65b0\u548c\u5220\u9664\u64cd\u4f5c\u90fd\u4f7f\u7528",(0,s.jsx)(n.code,{children:"Exec"}),"\u65b9\u6cd5\u3002"]}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-go",children:"func (db *DB) Exec(query string, args ...interface{}) (Result, error)\n"})}),"\n",(0,s.jsx)(n.p,{children:"Exec\u6267\u884c\u4e00\u6b21\u547d\u4ee4\uff08\u5305\u62ec\u67e5\u8be2\u3001\u5220\u9664\u3001\u66f4\u65b0\u3001\u63d2\u5165\u7b49\uff09\uff0c\u8fd4\u56de\u7684Result\u662f\u5bf9\u5df2\u6267\u884c\u7684SQL\u547d\u4ee4\u7684\u603b\u7ed3\u3002\u53c2\u6570args\u8868\u793aquery\u4e2d\u7684\u5360\u4f4d\u53c2\u6570\u3002"}),"\n",(0,s.jsx)(n.p,{children:"\u5177\u4f53\u63d2\u5165\u6570\u636e\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a"}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-go",children:'// \u63d2\u5165\u6570\u636e\nfunc insertRowDemo() {\n\tsqlStr := "insert into user(name, age) values (?,?)"\n\tret, err := db.Exec(sqlStr, "\u738b\u4e94", 38)\n\tif err != nil {\n\t\tfmt.Printf("insert failed, err:%v\\n", err)\n\t\treturn\n\t}\n\ttheID, err := ret.LastInsertId() // \u65b0\u63d2\u5165\u6570\u636e\u7684id\n\tif err != nil {\n\t\tfmt.Printf("get lastinsert ID failed, err:%v\\n", err)\n\t\treturn\n\t}\n\tfmt.Printf("insert success, the id is %d.\\n", theID)\n}\n'})}),"\n",(0,s.jsx)(n.h3,{id:"\u66f4\u65b0\u6570\u636e",children:"\u66f4\u65b0\u6570\u636e"}),"\n",(0,s.jsx)(n.p,{children:"\u5177\u4f53\u66f4\u65b0\u6570\u636e\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a"}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-go",children:'// \u66f4\u65b0\u6570\u636e\nfunc updateRowDemo() {\n\tsqlStr := "update user set age=? where id = ?"\n\tret, err := db.Exec(sqlStr, 39, 3)\n\tif err != nil {\n\t\tfmt.Printf("update failed, err:%v\\n", err)\n\t\treturn\n\t}\n\tn, err := ret.RowsAffected() // \u64cd\u4f5c\u5f71\u54cd\u7684\u884c\u6570\n\tif err != nil {\n\t\tfmt.Printf("get RowsAffected failed, err:%v\\n", err)\n\t\treturn\n\t}\n\tfmt.Printf("update success, affected rows:%d\\n", n)\n}\n'})}),"\n",(0,s.jsx)(n.h3,{id:"\u5220\u9664\u6570\u636e",children:"\u5220\u9664\u6570\u636e"}),"\n",(0,s.jsx)(n.p,{children:"\u5177\u4f53\u5220\u9664\u6570\u636e\u7684\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a"}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-go",children:'// \u5220\u9664\u6570\u636e\nfunc deleteRowDemo() {\n\tsqlStr := "delete from user where id = ?"\n\tret, err := db.Exec(sqlStr, 3)\n\tif err != nil {\n\t\tfmt.Printf("delete failed, err:%v\\n", err)\n\t\treturn\n\t}\n\tn, err := ret.RowsAffected() // \u64cd\u4f5c\u5f71\u54cd\u7684\u884c\u6570\n\tif err != nil {\n\t\tfmt.Printf("get RowsAffected failed, err:%v\\n", err)\n\t\treturn\n\t}\n\tfmt.Printf("delete success, affected rows:%d\\n", n)\n}\n'})}),"\n",(0,s.jsx)(n.h2,{id:"mysql\u9884\u5904\u7406",children:"MySQL\u9884\u5904\u7406"}),"\n",(0,s.jsx)(n.h3,{id:"\u4ec0\u4e48\u662f\u9884\u5904\u7406",children:"\u4ec0\u4e48\u662f\u9884\u5904\u7406\uff1f"}),"\n",(0,s.jsx)(n.p,{children:"\u666e\u901aSQL\u8bed\u53e5\u6267\u884c\u8fc7\u7a0b\uff1a"}),"\n",(0,s.jsxs)(n.ol,{children:["\n",(0,s.jsx)(n.li,{children:"\u5ba2\u6237\u7aef\u5bf9SQL\u8bed\u53e5\u8fdb\u884c\u5360\u4f4d\u7b26\u66ff\u6362\u5f97\u5230\u5b8c\u6574\u7684SQL\u8bed\u53e5\u3002"}),"\n",(0,s.jsx)(n.li,{children:"\u5ba2\u6237\u7aef\u53d1\u9001\u5b8c\u6574SQL\u8bed\u53e5\u5230MySQL\u670d\u52a1\u7aef"}),"\n",(0,s.jsx)(n.li,{children:"MySQL\u670d\u52a1\u7aef\u6267\u884c\u5b8c\u6574\u7684SQL\u8bed\u53e5\u5e76\u5c06\u7ed3\u679c\u8fd4\u56de\u7ed9\u5ba2\u6237\u7aef\u3002"}),"\n"]}),"\n",(0,s.jsx)(n.p,{children:"\u9884\u5904\u7406\u6267\u884c\u8fc7\u7a0b\uff1a"}),"\n",(0,s.jsxs)(n.ol,{children:["\n",(0,s.jsx)(n.li,{children:"\u628aSQL\u8bed\u53e5\u5206\u6210\u4e24\u90e8\u5206\uff0c\u547d\u4ee4\u90e8\u5206\u4e0e\u6570\u636e\u90e8\u5206\u3002"}),"\n",(0,s.jsx)(n.li,{children:"\u5148\u628a\u547d\u4ee4\u90e8\u5206\u53d1\u9001\u7ed9MySQL\u670d\u52a1\u7aef\uff0cMySQL\u670d\u52a1\u7aef\u8fdb\u884cSQL\u9884\u5904\u7406\u3002"}),"\n",(0,s.jsx)(n.li,{children:"\u7136\u540e\u628a\u6570\u636e\u90e8\u5206\u53d1\u9001\u7ed9MySQL\u670d\u52a1\u7aef\uff0cMySQL\u670d\u52a1\u7aef\u5bf9SQL\u8bed\u53e5\u8fdb\u884c\u5360\u4f4d\u7b26\u66ff\u6362\u3002"}),"\n",(0,s.jsx)(n.li,{children:"MySQL\u670d\u52a1\u7aef\u6267\u884c\u5b8c\u6574\u7684SQL\u8bed\u53e5\u5e76\u5c06\u7ed3\u679c\u8fd4\u56de\u7ed9\u5ba2\u6237\u7aef\u3002"}),"\n"]}),"\n",(0,s.jsx)(n.h3,{id:"\u4e3a\u4ec0\u4e48\u8981\u9884\u5904\u7406",children:"\u4e3a\u4ec0\u4e48\u8981\u9884\u5904\u7406\uff1f"}),"\n",(0,s.jsxs)(n.ol,{children:["\n",(0,s.jsx)(n.li,{children:"\u4f18\u5316MySQL\u670d\u52a1\u5668\u91cd\u590d\u6267\u884cSQL\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u63d0\u5347\u670d\u52a1\u5668\u6027\u80fd\uff0c\u63d0\u524d\u8ba9\u670d\u52a1\u5668\u7f16\u8bd1\uff0c\u4e00\u6b21\u7f16\u8bd1\u591a\u6b21\u6267\u884c\uff0c\u8282\u7701\u540e\u7eed\u7f16\u8bd1\u7684\u6210\u672c\u3002"}),"\n",(0,s.jsx)(n.li,{children:"\u907f\u514dSQL\u6ce8\u5165\u95ee\u9898\u3002"}),"\n"]}),"\n",(0,s.jsx)(n.h3,{id:"go\u5b9e\u73b0mysql\u9884\u5904\u7406",children:"Go\u5b9e\u73b0MySQL\u9884\u5904\u7406"}),"\n",(0,s.jsxs)(n.p,{children:[(0,s.jsx)(n.code,{children:"database/sql"}),"\u4e2d\u4f7f\u7528\u4e0b\u9762\u7684",(0,s.jsx)(n.code,{children:"Prepare"}),"\u65b9\u6cd5\u6765\u5b9e\u73b0\u9884\u5904\u7406\u64cd\u4f5c\u3002"]}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-go",children:"func (db *DB) Prepare(query string) (*Stmt, error)\n"})}),"\n",(0,s.jsxs)(n.p,{children:[(0,s.jsx)(n.code,{children:"Prepare"}),"\u65b9\u6cd5\u4f1a\u5148\u5c06sql\u8bed\u53e5\u53d1\u9001\u7ed9MySQL\u670d\u52a1\u7aef\uff0c\u8fd4\u56de\u4e00\u4e2a\u51c6\u5907\u597d\u7684\u72b6\u6001\u7528\u4e8e\u4e4b\u540e\u7684\u67e5\u8be2\u548c\u547d\u4ee4\u3002\u8fd4\u56de\u503c\u53ef\u4ee5\u540c\u65f6\u6267\u884c\u591a\u4e2a\u67e5\u8be2\u548c\u547d\u4ee4\u3002"]}),"\n",(0,s.jsx)(n.p,{children:"\u67e5\u8be2\u64cd\u4f5c\u7684\u9884\u5904\u7406\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a"}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-go",children:'// \u9884\u5904\u7406\u67e5\u8be2\u793a\u4f8b\nfunc prepareQueryDemo() {\n\tsqlStr := "select id, name, age from user where id > ?"\n\tstmt, err := db.Prepare(sqlStr)\n\tif err != nil {\n\t\tfmt.Printf("prepare failed, err:%v\\n", err)\n\t\treturn\n\t}\n\tdefer stmt.Close()\n\trows, err := stmt.Query(0)\n\tif err != nil {\n\t\tfmt.Printf("query failed, err:%v\\n", err)\n\t\treturn\n\t}\n\tdefer rows.Close()\n\t// \u5faa\u73af\u8bfb\u53d6\u7ed3\u679c\u96c6\u4e2d\u7684\u6570\u636e\n\tfor rows.Next() {\n\t\tvar u user\n\t\terr := rows.Scan(&u.id, &u.name, &u.age)\n\t\tif err != nil {\n\t\t\tfmt.Printf("scan failed, err:%v\\n", err)\n\t\t\treturn\n\t\t}\n\t\tfmt.Printf("id:%d name:%s age:%d\\n", u.id, u.name, u.age)\n\t}\n}\n'})}),"\n",(0,s.jsx)(n.p,{children:"\u63d2\u5165\u3001\u66f4\u65b0\u548c\u5220\u9664\u64cd\u4f5c\u7684\u9884\u5904\u7406\u5341\u5206\u7c7b\u4f3c\uff0c\u8fd9\u91cc\u4ee5\u63d2\u5165\u64cd\u4f5c\u7684\u9884\u5904\u7406\u4e3a\u4f8b\uff1a"}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-go",children:'// \u9884\u5904\u7406\u63d2\u5165\u793a\u4f8b\nfunc prepareInsertDemo() {\n\tsqlStr := "insert into user(name, age) values (?,?)"\n\tstmt, err := db.Prepare(sqlStr)\n\tif err != nil {\n\t\tfmt.Printf("prepare failed, err:%v\\n", err)\n\t\treturn\n\t}\n\tdefer stmt.Close()\n\t_, err = stmt.Exec("\u5c0f\u738b\u5b50", 18)\n\tif err != nil {\n\t\tfmt.Printf("insert failed, err:%v\\n", err)\n\t\treturn\n\t}\n\t_, err = stmt.Exec("\u6c99\u6cb3\u5a1c\u624e", 18)\n\tif err != nil {\n\t\tfmt.Printf("insert failed, err:%v\\n", err)\n\t\treturn\n\t}\n\tfmt.Println("insert success.")\n}\n'})}),"\n",(0,s.jsx)(n.h3,{id:"sql\u6ce8\u5165\u95ee\u9898",children:"SQL\u6ce8\u5165\u95ee\u9898"}),"\n",(0,s.jsx)(n.p,{children:(0,s.jsx)(n.strong,{children:"\u6211\u4eec\u4efb\u4f55\u65f6\u5019\u90fd\u4e0d\u5e94\u8be5\u81ea\u5df1\u62fc\u63a5SQL\u8bed\u53e5\uff01"})}),"\n",(0,s.jsx)(n.p,{children:"\u8fd9\u91cc\u6211\u4eec\u6f14\u793a\u4e00\u4e2a\u81ea\u884c\u62fc\u63a5SQL\u8bed\u53e5\u7684\u793a\u4f8b\uff0c\u7f16\u5199\u4e00\u4e2a\u6839\u636ename\u5b57\u6bb5\u67e5\u8be2user\u8868\u7684\u51fd\u6570\u5982\u4e0b\uff1a"}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-go",children:'// sql\u6ce8\u5165\u793a\u4f8b\nfunc sqlInjectDemo(name string) {\n\tsqlStr := fmt.Sprintf("select id, name, age from user where name=\'%s\'", name)\n\tfmt.Printf("SQL:%s\\n", sqlStr)\n\tvar u user\n\terr := db.QueryRow(sqlStr).Scan(&u.id, &u.name, &u.age)\n\tif err != nil {\n\t\tfmt.Printf("exec failed, err:%v\\n", err)\n\t\treturn\n\t}\n\tfmt.Printf("user:%#v\\n", u)\n}\n'})}),"\n",(0,s.jsx)(n.p,{children:"\u6b64\u65f6\u4ee5\u4e0b\u8f93\u5165\u5b57\u7b26\u4e32\u90fd\u53ef\u4ee5\u5f15\u53d1SQL\u6ce8\u5165\u95ee\u9898\uff1a"}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-go",children:'sqlInjectDemo("xxx\' or 1=1#")\nsqlInjectDemo("xxx\' union select * from user #")\nsqlInjectDemo("xxx\' and (select count(*) from user) <10 #")\n'})}),"\n",(0,s.jsx)(n.p,{children:"**\u8865\u5145\uff1a**\u4e0d\u540c\u7684\u6570\u636e\u5e93\u4e2d\uff0cSQL\u8bed\u53e5\u4f7f\u7528\u7684\u5360\u4f4d\u7b26\u8bed\u6cd5\u4e0d\u5c3d\u76f8\u540c\u3002"}),"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n",(0,s.jsxs)(n.table,{children:[(0,s.jsx)(n.thead,{children:(0,s.jsxs)(n.tr,{children:[(0,s.jsx)(n.th,{style:{textAlign:"center"},children:"\u6570\u636e\u5e93"}),(0,s.jsx)(n.th,{style:{textAlign:"center"},children:"\u5360\u4f4d\u7b26\u8bed\u6cd5"})]})}),(0,s.jsxs)(n.tbody,{children:[(0,s.jsxs)(n.tr,{children:[(0,s.jsx)(n.td,{style:{textAlign:"center"},children:"MySQL"}),(0,s.jsx)(n.td,{style:{textAlign:"center"},children:(0,s.jsx)(n.code,{children:"?"})})]}),(0,s.jsxs)(n.tr,{children:[(0,s.jsx)(n.td,{style:{textAlign:"center"},children:"PostgreSQL"}),(0,s.jsxs)(n.td,{style:{textAlign:"center"},children:[(0,s.jsx)(n.code,{children:"$1"}),", ",(0,s.jsx)(n.code,{children:"$2"}),"\u7b49"]})]}),(0,s.jsxs)(n.tr,{children:[(0,s.jsx)(n.td,{style:{textAlign:"center"},children:"SQLite"}),(0,s.jsxs)(n.td,{style:{textAlign:"center"},children:[(0,s.jsx)(n.code,{children:"?"})," \u548c",(0,s.jsx)(n.code,{children:"$1"})]})]}),(0,s.jsxs)(n.tr,{children:[(0,s.jsx)(n.td,{style:{textAlign:"center"},children:"Oracle"}),(0,s.jsx)(n.td,{style:{textAlign:"center"},children:(0,s.jsx)(n.code,{children:":name"})})]})]})]}),"\n",(0,s.jsx)(n.h2,{id:"go\u5b9e\u73b0mysql\u4e8b\u52a1",children:"Go\u5b9e\u73b0MySQL\u4e8b\u52a1"}),"\n",(0,s.jsx)(n.h3,{id:"\u4ec0\u4e48\u662f\u4e8b\u52a1",children:"\u4ec0\u4e48\u662f\u4e8b\u52a1\uff1f"}),"\n",(0,s.jsx)(n.p,{children:"\u4e8b\u52a1\uff1a\u4e00\u4e2a\u6700\u5c0f\u7684\u4e0d\u53ef\u518d\u5206\u7684\u5de5\u4f5c\u5355\u5143\uff1b\u901a\u5e38\u4e00\u4e2a\u4e8b\u52a1\u5bf9\u5e94\u4e00\u4e2a\u5b8c\u6574\u7684\u4e1a\u52a1(\u4f8b\u5982\u94f6\u884c\u8d26\u6237\u8f6c\u8d26\u4e1a\u52a1\uff0c\u8be5\u4e1a\u52a1\u5c31\u662f\u4e00\u4e2a\u6700\u5c0f\u7684\u5de5\u4f5c\u5355\u5143)\uff0c\u540c\u65f6\u8fd9\u4e2a\u5b8c\u6574\u7684\u4e1a\u52a1\u9700\u8981\u6267\u884c\u591a\u6b21\u7684DML(insert\u3001update\u3001delete)\u8bed\u53e5\u5171\u540c\u8054\u5408\u5b8c\u6210\u3002A\u8f6c\u8d26\u7ed9B\uff0c\u8fd9\u91cc\u9762\u5c31\u9700\u8981\u6267\u884c\u4e24\u6b21update\u64cd\u4f5c\u3002"}),"\n",(0,s.jsxs)(n.p,{children:["\u5728MySQL\u4e2d\u53ea\u6709\u4f7f\u7528\u4e86",(0,s.jsx)(n.code,{children:"Innodb"}),"\u6570\u636e\u5e93\u5f15\u64ce\u7684\u6570\u636e\u5e93\u6216\u8868\u624d\u652f\u6301\u4e8b\u52a1\u3002\u4e8b\u52a1\u5904\u7406\u53ef\u4ee5\u7528\u6765\u7ef4\u62a4\u6570\u636e\u5e93\u7684\u5b8c\u6574\u6027\uff0c\u4fdd\u8bc1\u6210\u6279\u7684SQL\u8bed\u53e5\u8981\u4e48\u5168\u90e8\u6267\u884c\uff0c\u8981\u4e48\u5168\u90e8\u4e0d\u6267\u884c\u3002"]}),"\n",(0,s.jsx)(n.h3,{id:"\u4e8b\u52a1\u7684acid",children:"\u4e8b\u52a1\u7684ACID"}),"\n",(0,s.jsx)(n.p,{children:"\u901a\u5e38\u4e8b\u52a1\u5fc5\u987b\u6ee1\u8db34\u4e2a\u6761\u4ef6\uff08ACID\uff09\uff1a\u539f\u5b50\u6027\uff08Atomicity\uff0c\u6216\u79f0\u4e0d\u53ef\u5206\u5272\u6027\uff09\u3001\u4e00\u81f4\u6027\uff08Consistency\uff09\u3001\u9694\u79bb\u6027\uff08Isolation\uff0c\u53c8\u79f0\u72ec\u7acb\u6027\uff09\u3001\u6301\u4e45\u6027\uff08Durability\uff09\u3002"}),"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n",(0,s.jsxs)(n.table,{children:[(0,s.jsx)(n.thead,{children:(0,s.jsxs)(n.tr,{children:[(0,s.jsx)(n.th,{style:{textAlign:"center"},children:"\u6761\u4ef6"}),(0,s.jsx)(n.th,{style:{textAlign:"center"},children:"\u89e3\u91ca"})]})}),(0,s.jsxs)(n.tbody,{children:[(0,s.jsxs)(n.tr,{children:[(0,s.jsx)(n.td,{style:{textAlign:"center"},children:"\u539f\u5b50\u6027"}),(0,s.jsx)(n.td,{style:{textAlign:"center"},children:"\u4e00\u4e2a\u4e8b\u52a1\uff08transaction\uff09\u4e2d\u7684\u6240\u6709\u64cd\u4f5c\uff0c\u8981\u4e48\u5168\u90e8\u5b8c\u6210\uff0c\u8981\u4e48\u5168\u90e8\u4e0d\u5b8c\u6210\uff0c\u4e0d\u4f1a\u7ed3\u675f\u5728\u4e2d\u95f4\u67d0\u4e2a\u73af\u8282\u3002\u4e8b\u52a1\u5728\u6267\u884c\u8fc7\u7a0b\u4e2d\u53d1\u751f\u9519\u8bef\uff0c\u4f1a\u88ab\u56de\u6eda\uff08Rollback\uff09\u5230\u4e8b\u52a1\u5f00\u59cb\u524d\u7684\u72b6\u6001\uff0c\u5c31\u50cf\u8fd9\u4e2a\u4e8b\u52a1\u4ece\u6765\u6ca1\u6709\u6267\u884c\u8fc7\u4e00\u6837\u3002"})]}),(0,s.jsxs)(n.tr,{children:[(0,s.jsx)(n.td,{style:{textAlign:"center"},children:"\u4e00\u81f4\u6027"}),(0,s.jsx)(n.td,{style:{textAlign:"center"},children:"\u5728\u4e8b\u52a1\u5f00\u59cb\u4e4b\u524d\u548c\u4e8b\u52a1\u7ed3\u675f\u4ee5\u540e\uff0c\u6570\u636e\u5e93\u7684\u5b8c\u6574\u6027\u6ca1\u6709\u88ab\u7834\u574f\u3002\u8fd9\u8868\u793a\u5199\u5165\u7684\u8d44\u6599\u5fc5\u987b\u5b8c\u5168\u7b26\u5408\u6240\u6709\u7684\u9884\u8bbe\u89c4\u5219\uff0c\u8fd9\u5305\u542b\u8d44\u6599\u7684\u7cbe\u786e\u5ea6\u3001\u4e32\u8054\u6027\u4ee5\u53ca\u540e\u7eed\u6570\u636e\u5e93\u53ef\u4ee5\u81ea\u53d1\u6027\u5730\u5b8c\u6210\u9884\u5b9a\u7684\u5de5\u4f5c\u3002"})]}),(0,s.jsxs)(n.tr,{children:[(0,s.jsx)(n.td,{style:{textAlign:"center"},children:"\u9694\u79bb\u6027"}),(0,s.jsx)(n.td,{style:{textAlign:"center"},children:"\u6570\u636e\u5e93\u5141\u8bb8\u591a\u4e2a\u5e76\u53d1\u4e8b\u52a1\u540c\u65f6\u5bf9\u5176\u6570\u636e\u8fdb\u884c\u8bfb\u5199\u548c\u4fee\u6539\u7684\u80fd\u529b\uff0c\u9694\u79bb\u6027\u53ef\u4ee5\u9632\u6b62\u591a\u4e2a\u4e8b\u52a1\u5e76\u53d1\u6267\u884c\u65f6\u7531\u4e8e\u4ea4\u53c9\u6267\u884c\u800c\u5bfc\u81f4\u6570\u636e\u7684\u4e0d\u4e00\u81f4\u3002\u4e8b\u52a1\u9694\u79bb\u5206\u4e3a\u4e0d\u540c\u7ea7\u522b\uff0c\u5305\u62ec\u8bfb\u672a\u63d0\u4ea4\uff08Read uncommitted\uff09\u3001\u8bfb\u63d0\u4ea4\uff08read committed\uff09\u3001\u53ef\u91cd\u590d\u8bfb\uff08repeatable read\uff09\u548c\u4e32\u884c\u5316\uff08Serializable\uff09\u3002"})]}),(0,s.jsxs)(n.tr,{children:[(0,s.jsx)(n.td,{style:{textAlign:"center"},children:"\u6301\u4e45\u6027"}),(0,s.jsx)(n.td,{style:{textAlign:"center"},children:"\u4e8b\u52a1\u5904\u7406\u7ed3\u675f\u540e\uff0c\u5bf9\u6570\u636e\u7684\u4fee\u6539\u5c31\u662f\u6c38\u4e45\u7684\uff0c\u5373\u4fbf\u7cfb\u7edf\u6545\u969c\u4e5f\u4e0d\u4f1a\u4e22\u5931\u3002"})]})]})]}),"\n",(0,s.jsx)(n.h3,{id:"\u4e8b\u52a1\u76f8\u5173\u65b9\u6cd5",children:"\u4e8b\u52a1\u76f8\u5173\u65b9\u6cd5"}),"\n",(0,s.jsx)(n.p,{children:"Go\u8bed\u8a00\u4e2d\u4f7f\u7528\u4ee5\u4e0b\u4e09\u4e2a\u65b9\u6cd5\u5b9e\u73b0MySQL\u4e2d\u7684\u4e8b\u52a1\u64cd\u4f5c\u3002 \u5f00\u59cb\u4e8b\u52a1"}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-go",children:"func (db *DB) Begin() (*Tx, error)\n"})}),"\n",(0,s.jsx)(n.p,{children:"\u63d0\u4ea4\u4e8b\u52a1"}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-go",children:"func (tx *Tx) Commit() error\n"})}),"\n",(0,s.jsx)(n.p,{children:"\u56de\u6eda\u4e8b\u52a1"}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-go",children:"func (tx *Tx) Rollback() error\n"})}),"\n",(0,s.jsx)(n.h3,{id:"\u4e8b\u52a1\u793a\u4f8b",children:"\u4e8b\u52a1\u793a\u4f8b"}),"\n",(0,s.jsx)(n.p,{children:"\u4e0b\u9762\u7684\u4ee3\u7801\u6f14\u793a\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u4e8b\u52a1\u64cd\u4f5c\uff0c\u8be5\u4e8b\u7269\u64cd\u4f5c\u80fd\u591f\u786e\u4fdd\u4e24\u6b21\u66f4\u65b0\u64cd\u4f5c\u8981\u4e48\u540c\u65f6\u6210\u529f\u8981\u4e48\u540c\u65f6\u5931\u8d25\uff0c\u4e0d\u4f1a\u5b58\u5728\u4e2d\u95f4\u72b6\u6001\u3002"}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-go",children:'// \u4e8b\u52a1\u64cd\u4f5c\u793a\u4f8b\nfunc transactionDemo() {\n\ttx, err := db.Begin() // \u5f00\u542f\u4e8b\u52a1\n\tif err != nil {\n\t\tif tx != nil {\n\t\t\ttx.Rollback() // \u56de\u6eda\n\t\t}\n\t\tfmt.Printf("begin trans failed, err:%v\\n", err)\n\t\treturn\n\t}\n\tsqlStr1 := "Update user set age=30 where id=?"\n\tret1, err := tx.Exec(sqlStr1, 2)\n\tif err != nil {\n\t\ttx.Rollback() // \u56de\u6eda\n\t\tfmt.Printf("exec sql1 failed, err:%v\\n", err)\n\t\treturn\n\t}\n\taffRow1, err := ret1.RowsAffected()\n\tif err != nil {\n\t\ttx.Rollback() // \u56de\u6eda\n\t\tfmt.Printf("exec ret1.RowsAffected() failed, err:%v\\n", err)\n\t\treturn\n\t}\n\n\tsqlStr2 := "Update user set age=40 where id=?"\n\tret2, err := tx.Exec(sqlStr2, 3)\n\tif err != nil {\n\t\ttx.Rollback() // \u56de\u6eda\n\t\tfmt.Printf("exec sql2 failed, err:%v\\n", err)\n\t\treturn\n\t}\n\taffRow2, err := ret2.RowsAffected()\n\tif err != nil {\n\t\ttx.Rollback() // \u56de\u6eda\n\t\tfmt.Printf("exec ret1.RowsAffected() failed, err:%v\\n", err)\n\t\treturn\n\t}\n\n\tfmt.Println(affRow1, affRow2)\n\tif affRow1 == 1 && affRow2 == 1 {\n\t\tfmt.Println("\u4e8b\u52a1\u63d0\u4ea4\u5566...")\n\t\ttx.Commit() // \u63d0\u4ea4\u4e8b\u52a1\n\t} else {\n\t\ttx.Rollback()\n\t\tfmt.Println("\u4e8b\u52a1\u56de\u6eda\u5566...")\n\t}\n\n\tfmt.Println("exec trans success!")\n}\n'})}),"\n",(0,s.jsx)(n.p,{children:(0,s.jsx)(n.a,{href:"https://www.liwenzhou.com/posts/Go/sqlx/",children:"\u66f4\u5f3a\u5927\u3001\u66f4\u597d\u7528\u7684sqlx\u5e93"})})]})}function x(e={}){const{wrapper:n}={...(0,l.R)(),...e.components};return n?(0,s.jsx)(n,{...e,children:(0,s.jsx)(o,{...e})}):o(e)}},28453:(e,n,r)=>{r.d(n,{R:()=>i,x:()=>d});var t=r(96540);const s={},l=t.createContext(s);function i(e){const n=t.useContext(l);return t.useMemo(function(){return"function"==typeof e?e(n):{...n,...e}},[n,e])}function d(e){let n;return n=e.disableParentContext?"function"==typeof e.components?e.components(s):e.components||s:i(e.components),t.createElement(l.Provider,{value:n},e.children)}}}]);