?? go-xorm封裝公共CURD類

go-xorm封裝公共CURD類

$ 前言

  • go-xorm是一個(gè)簡(jiǎn)單而強(qiáng)大的Go語言O(shè)RM庫. 通過它可以使數(shù)據(jù)庫操作非常簡(jiǎn)便。

$ 配置目標(biāo)

  • 封裝一個(gè)公共的父級(jí)操作類,
  • 通過繼承可讓子類帶有簡(jiǎn)單的操作CRUD分頁查詢數(shù)據(jù)庫的能力
  • 可減少很多冗余,重復(fù)相似性很高的查詢的代碼編寫
  • 提高代碼整潔性,可維護(hù)性

$ 參考項(xiàng)目地址

http://www.reibang.com/p/ed748eae96c9

app.ini配置

[mysql]
url=root:abc.123@tcp(localhost:3306)/foot?charset=utf8
maxIdle=10
maxConn=50

utils工具類源碼(用于讀取配置信息)

package utils

import (
    "gopkg.in/ini.v1"
    "tesou.io/platform/foot-parent/foot-api/common/base"
)

var (
    //配置信息
    iniFile *ini.File
)

func init() {
    file, e := ini.Load("conf/app.ini")
    if e != nil {
        base.Log.Info("Fail to load conf/app.ini" + e.Error())
        return
    }
    iniFile = file
}

func GetSection(sectionName string) *ini.Section {
    section, e := iniFile.GetSection(sectionName)
    if e != nil {
        base.Log.Info("未找到對(duì)應(yīng)的配置信息:" + sectionName + e.Error())
        return nil
    }
    return section
}

func GetSectionMap(sectionName string) map[string]string {
    section, e := iniFile.GetSection(sectionName)
    if e != nil {
        base.Log.Info("未找到對(duì)應(yīng)的配置信息:" + sectionName + e.Error())
        return nil
    }
    section_map := make(map[string]string, 0)
    for _, e := range section.Keys() {
        section_map[e.Name()] = e.Value()
    }
    return section_map
}

func GetVal(sectionName string, key string) string {
    var temp_val string
    section := GetSection(sectionName)
    if nil != section {
        temp_val = section.Key(key).Value()
    }
    return temp_val;
}

Page源碼(分頁結(jié)構(gòu)體)

package pojo

type Page struct {
    //記錄總數(shù)
    Counts int64
    //每頁顯示記錄數(shù)
    PageSize int64
    //總頁數(shù)
    TotalPage int64
    //當(dāng)前頁
    CurPage int64
    //頁面顯示開始記錄數(shù)
    FirstResult int64
    //頁面顯示最后記錄數(shù)
    LastResult int64
    //排序類型
    OrderType string
    //排序名稱
    OrderName string
}

func (this *Page) Build(counts int64, pageSize int64) {
    this.Counts = counts
    this.PageSize = pageSize
    if (counts%pageSize == 0) {
        this.TotalPage = this.Counts / this.PageSize
    } else {
        this.TotalPage = this.Counts/this.PageSize + 1
    }
}

func (this *Page) GetCounts() int64 {
    return this.Counts
}

/**
 *  Counts
 *            the Counts to set
 */
func (this *Page) SetCounts(counts int64) {
    // 計(jì)算所有的頁面數(shù)
    this.Counts = counts
    // this.TotalPage = (int)Math.ceil((this.Counts + this.perPageSize - 1)
    // / this.perPageSize)
    if (counts%this.PageSize == 0) {
        this.TotalPage = this.Counts / this.PageSize
    } else {
        this.TotalPage = this.Counts/this.PageSize + 1
    }
}

func (this *Page) GetPageSize() int64 {
    return this.PageSize
}

func (this *Page) SetPageSize(pageSize int64) {
    this.PageSize = pageSize
}

/**
 *  the TotalPage
 */
func (this *Page) GetTotalPage() int64 {
    if this.TotalPage < 1 {
        return 1
    }
    return this.TotalPage
}

/**
 *  TotalPage
 *            the TotalPage to set
 */
func (this *Page) SetTotalPage(totalPage int64) {
    this.TotalPage = totalPage
}

func (this *Page) GetCurPage() int64 {
    return this.CurPage
}

func (this *Page) SetCurPage(curPage int64) {
    this.CurPage = curPage
}

/**
 *  the FirstResult
 */
func (this *Page) GetFirstResult() int64 {
    temp := this.CurPage - 1
    if (temp <= 0) {
        return 0
    }
    this.FirstResult = (this.CurPage - 1) * this.PageSize
    return this.FirstResult
}

/**
 *  FirstResult
 *            the FirstResult to set
 */
func (this *Page) SetFirstResult(firstResult int64) {
    this.FirstResult = firstResult
}

/**
 *  the LastResult
 */
func (this *Page) GetLastResult() int64 {
    this.LastResult = this.FirstResult + this.PageSize
    return this.LastResult
}

/**
 *  LastResult
 *            the LastResult to set
 */
func (this *Page) SetLastResult(lastResult int64) {
    this.LastResult = lastResult
}

/**
 *  the OrderName
 */
func (this *Page) GetOrderName() string {
    return this.OrderName
}

/**
 *  OrderName
 *            the OrderName to set
 */
func (this *Page) SetOrderName(orderName string) {
    this.OrderName = orderName
}

/**
 *  the orderBy
 */
func (this *Page) getOrderType() string {
    return this.OrderType
}

/**
 *  orderBy
 *            the orderBy to set
 */
func (this *Page) SetOrderType(orderType string) {
    this.OrderType = orderType
}

/**
 *  the orderBy
 */
func (this *Page) GetOrderBy() string {
    if len(this.GetOrderName()) <= 0 {
        return ""
    }
    orderBy := " order by " + this.GetOrderName() + " " + this.getOrderType()
    return orderBy
}

BaseService源碼(公共的CRUD類)

package mysql

import (
    "container/list"
    _ "github.com/go-sql-driver/mysql"
    "github.com/go-xorm/core"
    "github.com/go-xorm/xorm"
    "gopkg.in/mgo.v2/bson"
    "reflect"
    "strconv"
    "tesou.io/platform/foot-parent/foot-api/common/base"
    "tesou.io/platform/foot-parent/foot-api/common/base/pojo"
    "tesou.io/platform/foot-parent/foot-core/common/utils"
)

type BaseService struct {
}

var (
    engine *xorm.Engine
)

func GetEngine() *xorm.Engine {
    if nil == engine {
        setEngine()
    }
    return engine
}

func ShowSQL(show bool) {
    engine := GetEngine()
    engine.ShowSQL(show)
    engine.ShowExecTime(show)

}

func setEngine() *xorm.Engine {
    url := utils.GetVal("mysql", "url")
    maxIdle, _ := strconv.Atoi(utils.GetVal("mysql", "maxIdle"))
    maxConn, _ := strconv.Atoi(utils.GetVal("mysql", "maxConn"))

    var err error
    engine, err = xorm.NewEngine("mysql", url)
    if nil != err {
        base.Log.Error("init" + err.Error())
    }

    //engine.ShowExecTime(true)
    //則會(huì)在控制臺(tái)打印出生成的SQL語句
    //則會(huì)在控制臺(tái)打印調(diào)試及以上的信息
    engine.ShowSQL(true)
    //engine.Logger().SetLevel(core.LOG_DEBUG)
    engine.SetMaxIdleConns(maxIdle)
    engine.SetMaxOpenConns(maxConn)
    tbMapper := core.NewPrefixMapper(core.SnakeMapper{}, "t_")
    engine.SetTableMapper(tbMapper)
    engine.SetColumnMapper(core.SameMapper{})
    /**
    當(dāng)使用了Distinct,Having,GroupBy方法將不會(huì)使用緩存
    在Get或者Find時(shí)使用了Cols,Omit方法旗芬,則在開啟緩存后此方法無效,系統(tǒng)仍舊會(huì)取出這個(gè)表中的所有字段柒凉。
    在使用Exec方法執(zhí)行了方法之后柬唯,可能會(huì)導(dǎo)致緩存與數(shù)據(jù)庫不一致的地方揭北。因此如果啟用緩存,盡量避免使用Exec芥喇。
    如果必須使用西采,則需要在使用了Exec之后調(diào)用ClearCache手動(dòng)做緩存清除的工作。比如:
    engine.Exec("update user set name = ? where id = ?", "xlw", 1)
    engine.ClearCache(new(User))
    */
    //cacher := xorm.NewLRUCacher(xorm.NewMemoryStore(), 999)
    //engine.SetDefaultCacher(cacher)

    return engine
}

func init() {
    //設(shè)置初始化數(shù)據(jù)庫引擎
    setEngine()
}

func beforeModify(entity interface{}) {
    //當(dāng)前時(shí)間
    //current_date := time.Now().Format("2006-01-02 15:04:05")
    //默認(rèn)更新者
    default_user := "100000"
    //對(duì)象操作
    entity_value := reflect.ValueOf(entity).Elem()

    /*//設(shè)置更新時(shí)間
    field_ModifyDate := entity_value.FieldByName("ModifyTime")
    if field_ModifyDate.String() == "" {
        field_ModifyDate.SetString(current_date)
    }*/
    //設(shè)置更新者
    field_ModifyUser := entity_value.FieldByName("ModifyUser")
    if field_ModifyUser.String() == "" {
        field_ModifyUser.SetString(default_user)
    }

}

func beforeDelete(entity interface{}) {
    //當(dāng)前時(shí)間
    //current_date := time.Now().Format("2006-01-02 15:04:05")
    //默認(rèn)刪除者
    default_user := "100000"
    //對(duì)象操作
    entity_value := reflect.ValueOf(entity).Elem()

    /*//設(shè)置更新時(shí)間
    field_ModifyDate := entity_value.FieldByName("ModifyTime")
    if field_ModifyDate.String() == "" {
        field_ModifyDate.SetString(current_date)
    }*/
    //設(shè)置刪除者
    field_DeleteUser := entity_value.FieldByName("DeleteUser")
    if field_DeleteUser.String() == "" {
        field_DeleteUser.SetString(default_user)
    }

}

func beforeSave(entity interface{}) interface{} {
    //當(dāng)前時(shí)間
    //current_date := time.Now().Format("2006-01-02 15:04:05")
    //默認(rèn)創(chuàng)建者
    default_user := "100000"
    //對(duì)象操作
    entity_value := reflect.ValueOf(entity).Elem()

    /*  //設(shè)置創(chuàng)建時(shí)間  通過`xorm:"created"`配置
        field_CreateDate := entity_value.FieldByName("CreateTime")
        if field_CreateDate.String() == "" {
            field_CreateDate.SetString(current_date)
        }*/
    //設(shè)置創(chuàng)建者
    field_CreateUser := entity_value.FieldByName("CreateUser")
    if field_CreateUser.String() == "" {
        field_CreateUser.SetString(default_user)
    }

    beforeModify(entity)

    var id interface{}
    //設(shè)置id
    field_Id := entity_value.FieldByName("Id")
    if field_Id.String() == "" {
        //使用bson.NewObject作為主鍵
        id = bson.NewObjectId().Hex()
        field_Id.Set(reflect.ValueOf(id))
    }
    return id
}

func (this *BaseService) SaveOrModify(entity interface{}) {
    b, err := engine.Exist(entity)
    if nil != err {
        base.Log.Info("SaveOrModify:" + err.Error())
    }
    if b {
        this.Modify(entity)
    } else {
        this.Save(entity)
    }
}
func (this *BaseService) Save(entity interface{}) interface{} {
    id := beforeSave(entity)
    _, err := engine.InsertOne(entity)
    if nil != err {
        base.Log.Info("Save:" + err.Error())
    }
    return id
}

func (this *BaseService) SaveList(entitys []interface{}) *list.List {
    if len(entitys) <= 0 {
        return nil
    }
    list_ids := list.New()
    for _, v := range entitys {
        id := beforeSave(v)
        list_ids.PushBack(id)
    }

    _, err := engine.Insert(entitys...)
    if nil != err {
        base.Log.Info("SaveList:" + err.Error())
    }
    return list_ids
}

func (this *BaseService) Del(entity interface{}) int64 {
    beforeDelete(entity)
    entity_value := reflect.ValueOf(entity).Elem()
    id_field := entity_value.FieldByName("Id")
    i, err := engine.Id(id_field.Interface()).Delete(entity)
    if err != nil {
        base.Log.Info("Del:", err)
    }
    return i
}

func (this *BaseService) Modify(entity interface{}) int64 {
    beforeModify(entity)

    entity_value := reflect.ValueOf(entity).Elem()
    id_field := entity_value.FieldByName("Id")
    i, err := engine.Id(id_field.Interface()).AllCols().Update(entity)
    if err != nil {
        base.Log.Info("Modify:", err)
    }
    return i
}

func (this *BaseService) ModifyList(entitys []interface{}) int64 {
    if len(entitys) <= 0 {
        return 0
    }
    //i, err := engine.In("id",ids).Update(entitys)
    for _, v := range entitys {
        //entity_value := reflect.ValueOf(v).Elem()
        //id_field := entity_value.FieldByName("Id")
        this.Modify(v)
    }
    return 1
}

func (this *BaseService) Exist(entity interface{}) bool {
    //對(duì)象操作
    exist, err := engine.Exist(entity)
    if nil != err {
        base.Log.Info("ExistByName:" + err.Error())
    }
    return exist
}

func (this *BaseService) Find(entity interface{}) {
    engine.Find(entity)
}

func (this *BaseService) FindBySQL(sql string, entity interface{}) {
    engine.SQL(sql).Find(entity)
}

func (this *BaseService) FindAll(entity interface{}) {
    err := engine.Find(entity)
    if nil != err {
        base.Log.Info("FindAll: " + err.Error())
    }
}

/**
分頁查詢
*/
func (this *BaseService) Page(v interface{}, page *pojo.Page, dataList interface{}) error {
    tableName := engine.TableName(v)
    sql := "select t.* from " + tableName + " t where 1=1 "

    return this.PageSql(sql, page, dataList)
}

/**
分頁查詢
*/
func (this *BaseService) PageSql(sql string, page *pojo.Page, dataList interface{}) error {
    //聲明結(jié)果變量
    var err error
    var counts int64
    //獲取總記錄數(shù)處理
    countSql := " select count(1) from (" + sql + ") t"
    counts, err = engine.SQL(countSql).Count()
    if nil != err {
        return err
    } else {
        page.SetCounts(counts)
    }
    //排序處理
    orderBy := page.GetOrderBy()
    if len(orderBy) > 0 {
        sql += orderBy
    }
    sql += " limit " + strconv.FormatInt(page.GetFirstResult(), 10) + "," + strconv.FormatInt(page.GetPageSize(), 10)
    err = engine.SQL(sql).Find(dataList)
    return err
}


使用示例

package service

import (
    "tesou.io/platform/foot-parent/foot-api/common/base"
    "tesou.io/platform/foot-parent/foot-api/module/match/pojo"
    "tesou.io/platform/foot-parent/foot-core/common/base/service/mysql"
)

type MatchHisService struct {
    //引入公共類
    mysql.BaseService
}

func (this *MatchHisService) Exist(v *pojo.MatchHis) bool {
    has, err := mysql.GetEngine().Table("`t_match_his`").Where(" `Id` = ?  ", v.Id).Exist()
    if err != nil {
        base.Log.Error("Exist", err)
    }
    return has
}

func (this *MatchHisService) FindAll() []*pojo.MatchHis {
    dataList := make([]*pojo.MatchHis, 0)
    mysql.GetEngine().OrderBy("MatchDate").Find(&dataList)
    return dataList
}

func (this *MatchHisService) FindById(matchId string) *pojo.MatchHis {
    data := new(pojo.MatchHis)
    data.Id = matchId
    _, err := mysql.GetEngine().Get(data)
    if err != nil {
        base.Log.Error("FindById:", err)
    }
    return data
}

func (this *MatchHisService) FindBySeason(season string) []*pojo.MatchLast {
    sql_build := `
SELECT 
  la.* 
FROM
  foot.t_match_his la
WHERE 1=1
    `
    sql_build = sql_build + " AND la.MatchDate >= '" + season + "-01-01 00:00:00' AND la.MatchDate <= '" + season + "-12-31 23:59:59'"

    //結(jié)果值
    dataList := make([]*pojo.MatchLast, 0)
    //執(zhí)行查詢
    this.FindBySQL(sql_build, &dataList)
    return dataList
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末继控,一起剝皮案震驚了整個(gè)濱河市械馆,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌武通,老刑警劉巖霹崎,帶你破解...
    沈念sama閱讀 218,858評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異冶忱,居然都是意外死亡尾菇,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,372評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門囚枪,熙熙樓的掌柜王于貴愁眉苦臉地迎上來派诬,“玉大人,你說我怎么就攤上這事链沼∧福” “怎么了?”我有些...
    開封第一講書人閱讀 165,282評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵括勺,是天一觀的道長(zhǎng)缆八。 經(jīng)常有香客問我,道長(zhǎng)疾捍,這世上最難降的妖魔是什么奈辰? 我笑而不...
    開封第一講書人閱讀 58,842評(píng)論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮乱豆,結(jié)果婚禮上奖恰,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好房官,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,857評(píng)論 6 392
  • 文/花漫 我一把揭開白布趾徽。 她就那樣靜靜地躺著,像睡著了一般翰守。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上疲酌,一...
    開封第一講書人閱讀 51,679評(píng)論 1 305
  • 那天蜡峰,我揣著相機(jī)與錄音,去河邊找鬼朗恳。 笑死湿颅,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的粥诫。 我是一名探鬼主播油航,決...
    沈念sama閱讀 40,406評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼怀浆!你這毒婦竟也來了谊囚?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,311評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤执赡,失蹤者是張志新(化名)和其女友劉穎镰踏,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體沙合,經(jīng)...
    沈念sama閱讀 45,767評(píng)論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡奠伪,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,945評(píng)論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了首懈。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片绊率。...
    茶點(diǎn)故事閱讀 40,090評(píng)論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖究履,靈堂內(nèi)的尸體忽然破棺而出滤否,到底是詐尸還是另有隱情,我是刑警寧澤挎袜,帶...
    沈念sama閱讀 35,785評(píng)論 5 346
  • 正文 年R本政府宣布顽聂,位于F島的核電站,受9級(jí)特大地震影響盯仪,放射性物質(zhì)發(fā)生泄漏紊搪。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,420評(píng)論 3 331
  • 文/蒙蒙 一全景、第九天 我趴在偏房一處隱蔽的房頂上張望耀石。 院中可真熱鬧,春花似錦爸黄、人聲如沸滞伟。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,988評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽梆奈。三九已至野崇,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間亩钟,已是汗流浹背乓梨。 一陣腳步聲響...
    開封第一講書人閱讀 33,101評(píng)論 1 271
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留清酥,地道東北人扶镀。 一個(gè)月前我還...
    沈念sama閱讀 48,298評(píng)論 3 372
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像焰轻,于是被迫代替她去往敵國和親臭觉。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,033評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容

  • 一. Java基礎(chǔ)部分.................................................
    wy_sure閱讀 3,811評(píng)論 0 11
  • 本章我們首先從ThinkPHP5.0的數(shù)據(jù)庫訪問層架構(gòu)設(shè)計(jì)原理開始辱志,然后熟悉下數(shù)據(jù)庫的配置蝠筑,并掌握如何進(jìn)行基礎(chǔ)的查...
    寒冬夜行人_51a4閱讀 2,057評(píng)論 11 6
  • 我喜歡有個(gè)性的人,蘇明海就是其中一個(gè)荸频。 蘇明海有過三個(gè)女人菱肖,兩個(gè)是在小學(xué)談的,一個(gè)是現(xiàn)在談的老婆旭从,我到現(xiàn)在還記得...
    椽罔閱讀 769評(píng)論 1 2
  • 昨晚奶奶給我打來電話褒繁,問我什么時(shí)候回去過年。我說我們還有十多天才放假馍忽,電話那頭沉默了一會(huì)兒棒坏,我知道奶奶想急切見到我...
    羅志遠(yuǎn)閱讀 1,990評(píng)論 6 9
  • 做生意難是普遍現(xiàn)象 要求房東降房租,房東要求房地產(chǎn)遭笋,降價(jià)坝冕,房地產(chǎn)要求鋼筋,水泥瓦呼,人工喂窟,地皮降價(jià),最后發(fā)現(xiàn)是債務(wù)成本...
    1ce94d33416d閱讀 195評(píng)論 0 2