1. <strong id="7actg"></strong>
    2. <table id="7actg"></table>

    3. <address id="7actg"></address>
      <address id="7actg"></address>
      1. <object id="7actg"><tt id="7actg"></tt></object>

        震驚!Gorm 終于可以操作 Oracle 了

        共 1741字,需瀏覽 4分鐘

         ·

        2020-12-07 01:55

        點擊上方藍(lán)色“Go語言中文網(wǎng)”關(guān)注,每天一起學(xué) Go

        前言:寫這篇文章的目的有兩個,一是希望找到志同道合的伙伴一起維護這個庫,提高這個庫的代碼質(zhì)量。二是為了推廣 gorm 的 oracle 驅(qū)動,避免因為操作 oracle 而編寫大量的樣板代碼。同時也要感謝Go 語言中文網(wǎng)的站長 polaris 能用它的公眾號為這個庫進行推廣。

        gorm-oracle 開源庫地址:https://github.com/cengsin/oracle

        歡迎大家積極參與維護 :)

        使用 gorm 連接 oracle

        前置條件

        • 首先安裝 oracle 數(shù)據(jù)庫客戶端和 go-oci8

          • oracle client: httpwww.oracle.com/technetwork/database/database-technologies/instant-client/downloads/index.html
          • go-oci8: https://github.com/mattn/go-oci8
        • 之后配置好環(huán)境變量 LD_LIBRARY_PATH

          • LD_LIBRAAY_PATH = ${oracle client path}

        獲取 Gorm-Oracle 驅(qū)動

        go?get?github.com/CengSin/oracle

        在文件加載的時候初始化數(shù)據(jù)庫連接

        func?init()?{
        ?con,?err?:=?gorm.Open(oracle.Open("system/[email protected]:1521/XE"),?&gorm.Config{
        ??Logger:?logger.New(log.New(os.Stdout,?"\r\n",?log.LstdFlags),?logger.Config{
        ???SlowThreshold:?1?*?time.Millisecond,
        ???LogLevel:??????logger.Warn,
        ???Colorful:??????true,
        ??}),
        ?})

        ?if?err?!=?nil?{
        ??log.Fatalln(err)
        ?}

        ?if?err?=?conn.AutoMigrate(&model.ClassInfo{});?err?!=?nil?{
        ??log.Fatalln(err.Error())
        ?}
        }

        定義結(jié)構(gòu)模型

        type?ClassInfo?struct?{
        ?ClassId???int64??`gorm:"column:CLASSID;primaryKey"`
        ?ClassName?string?`gorm:"column:CLASSNAME;size:50"`
        }

        func?(c?ClassInfo)?TableName()?string?{
        ?return?`CLASSINFO`
        }

        對數(shù)據(jù)庫進行操作

        func?QueryCaseTest()?{
        ?classInfo?:=?new(model.ClassInfo)
        ?//?獲取第一條記錄(主鍵升序)
        ?db.First(classInfo)
        ?fmt.Printf("獲取第一條記錄(主鍵升序)%+v\n",?classInfo)
        ?//?SELECT?*?FROM?users?ORDER?BY?id?LIMIT?1;

        ?//?獲取第一條記錄(主鍵升序)
        ?classInfo?=?new(model.ClassInfo)
        ?db.Take(classInfo)
        ?fmt.Printf("獲取第一條記錄(主鍵升序)%+v\n",?classInfo)
        ?//?SELECT?*?FROM?users?LIMIT?1;
        }


        推薦閱讀


        福利

        我為大家整理了一份從入門到進階的Go學(xué)習(xí)資料禮包,包含學(xué)習(xí)建議:入門看什么,進階看什么。關(guān)注公眾號 「polarisxu」,回復(fù) ebook 獲??;還可以回復(fù)「進群」,和數(shù)萬 Gopher 交流學(xué)習(xí)。

        瀏覽 292
        點贊
        評論
        收藏
        分享

        手機掃一掃分享

        分享
        舉報
        評論
        圖片
        表情
        推薦
        點贊
        評論
        收藏
        分享

        手機掃一掃分享

        分享
        舉報
        1. <strong id="7actg"></strong>
        2. <table id="7actg"></table>

        3. <address id="7actg"></address>
          <address id="7actg"></address>
          1. <object id="7actg"><tt id="7actg"></tt></object>
            偷拍一女多男做爰免费视频 | 国产精品海角社区在线观看 | 超级人人操 | 伊人久久婷婷 | 女同性毛片60分钟 | 国产精品白丝av久久网站 | 9999人体做爰大胆视频 | 国产ai换脸一区二区三区 | 天天操天天操天天操 | 麻豆chinese |