当前位置: 首页 > 工具软件 > go-xorm > 使用案例 >

golang[go-xorm辅助组件]

柳高卓
2023-12-01

博主写了个比较通用的,不多说,直接上代码,如果有机会能集成到xorm库里,也可以~!

主要是以POJO实体直接映射字段,请注意需要配置json~

QueryString因为不带数据类型,拿出来转换比较麻烦~

如果联表筛选,请仔细写SELECT出来的字段对应关系~

主要实现,curd常规操作【删除不做硬删】,分页等...

 

具体请看 : https://github.com/billmi/xorm-helper

package main

import (
	_ "github.com/go-sql-driver/mysql"
	"github.com/go-xorm/xorm"
	"github.com/billmi/xorm-helper"
	"fmt"
	"time"
	"log"
)

func main() {

	engine, err := xorm.NewEngine("mysql",
		"root"+":"+"root"+"@tcp("+"127.0.0.1"+":"+"3306"+")/"+"bill"+"?charset=utf8",
	)
	if err != nil {
		fmt.Print(err.Error())
	}
	var XormHelper = xormhelper.XormHelper{}

	//Set xorm engine
	XormHelper.SetDatasource(engine)

	var (
		tableName  = "test"
		currTime   = int(time.Now().Unix())
		inSertData = map[string]interface{}{
			"device_type":          1,
			"device_type_name":     "test device",
			"device_type_title":    "test device",
			"r_id":                 1,
			"device_screen_width":  300,
			"device_screen_height": 500,
			"create_time":          currTime,
		}
		editData = map[string]interface{}{
			"device_type_name": "update device",
			"update_time":      currTime,
		}
	)
	//insert
	XormHelper.InsertRow(tableName, inSertData)

	//Edit
	condi := fmt.Sprintf("id = %d ", 1)
	XormHelper.EditRow(tableName, condi, editData)

	/*
		Search base on XormEngine
		The demo : Only Demo
		You can create the model by reverse.
	*/
	var _model = XXModel{}
	has, err := engine.Where(condi).Get(&_model)
	if err != nil {
		log.Fatal(err.Error())
	}
	if has {
		fmt.Print(_model)
	} else {
		fmt.Print("Not Find !")
	}
}

 

 类似资料: