mysql_orm

package module
v0.0.0-...-d34338c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 27, 2020 License: MIT Imports: 5 Imported by: 0

README

mysql-orm

go get github.com/go-package-lab/mysql-orm

demo mysql 表结构
CREATE TABLE `test` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `title` varchar(100) DEFAULT '',
  `uid` int(10) DEFAULT '0',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4;

CREATE TABLE `test_attr` (
  `id` int(10) DEFAULT '0',
  `content` text,
  UNIQUE KEY `id` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
demo.go
package main

import (
	DB "github.com/go-package-lab/mysql-orm"
	"fmt"
	"log"
	"os"
)
//连接池1
var db1 = DB.DbConfig{
	DriverName: "mysql",
	Addr:       "127.0.0.1",
	User:       "root",
	Passwd:     "root",
	Port:       "3306",
	DBName:     "test1",
	Debug:     true,
}
//连接池2
var db2 = DB.DbConfig{
	DriverName: "mysql",
	Addr:       "127.0.0.1",
	User:       "root",
	Passwd:     "root",
	Port:       "3306",
	DBName:     "test2",
	Debug:     true,
}

func init() {
	err := db1.Connect()
	if err != nil {
		fmt.Println("connect err:", err.Error())
		os.Exit(1)
	}

	err = db2.Connect()
	if err != nil {
		fmt.Println("connect err:", err.Error())
		os.Exit(1)
	}
}
func main() {
	defer db1.Close()

	datas := make(DB.DataStruct)
	datas["title"] = "今天天气不错"
	datas["uid"] = 666

	id, err := db1.Insert("test", datas)
	if err != nil {
		fmt.Println("insert err:", err.Error())
	}
	fmt.Println("连接池1: id = ", id)

	//----------------------------------------------------------

	defer db2.Close()

	datas2 := make(DB.DataStruct)
	datas2["title"] = "还是不错的"
	datas2["uid"] = 999
	//datas2["adddate"] = "2010-01-01"
	datas2.Set("adddate", "2010-01-02")

	id2, err := db2.Insert("test", datas2)
	if err != nil {
		fmt.Println("insert err:", err.Error())
	}
	fmt.Println("连接池2: id2 = ", id2)

	//get one 获取一条

	data, err := db2.GetOne("test", "*", "id > ? ORDER BY id DESC", 11)
	fmt.Println(data)
	fmt.Println(data["title"])
	fmt.Println(data["uid"])
	fmt.Println(data["adddate"])
	fmt.Println(DB.Format2String(data, "adddate"))

	//select
	data2, err := db2.Select("test", "*", "title LIKE ? ORDER BY id DESC Limit 0,5", "%人民%")
	for i, i2 := range data2 {
		//fmt.Println(i, i2)
		fmt.Println(i+1,DB.Format2String(i2, "title"))
	}
	//UPDATE
	datas3 := make(DB.DataStruct)
	datas3["title"] = "修改后的结果2"
	datas3["uid"] = 8

	rows, err := db2.Update("test", datas3, "id=?", 86)
	fmt.Println("影响行数:", rows)

	//delete
	deleteid := 86
	rows2, err := db2.Delete("test", "id=?", deleteid)
	fmt.Println("影响行数:", rows2)

	title := "----test---"
	rows3, err := db2.Delete("test", "title=?", title)
	fmt.Println("影响行数:", rows3)

	//count
	total, err := db2.Count("test", "id>? AND id<?", 190,195)
	fmt.Println("总数:", total)


	//批量插入
	data9 := []DB.DataStruct{
		DB.DataStruct{
			"title": "A1111",
			"uid": 100,
		},
		DB.DataStruct{
			"title": "A2222",
			"uid": 200,
		},
	}

	num, err :=db2.BatchInsert("test", data9)
	fmt.Println("成功插入:",num,"条",err)

	//自定义联合查询
	data10, err := db2.Query("SELECT * FROM test a,test_attr b where a.id=b.id AND a.uid=?",999)
	for i, i2 := range data10 {
		//fmt.Println(i, i2)
		fmt.Println(i+1,DB.Format2String(i2, "content"))
	}
	//采用底层方法执行
	var title2 string
	err = db2.Db.QueryRow("select title from test where id = ?", 1).Scan(&title2)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(title2)

	//

}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Format2String

func Format2String(datas map[string]interface{}, key string) string

Types

type DataStruct

type DataStruct map[string]interface{}
type SetField struct {
	FieldName string
	FieldData interface{}
}

type SqlValues []SetField

func (DataStruct) Get

func (d DataStruct) Get(key string) interface{}

获取数据

func (*DataStruct) Set

func (d *DataStruct) Set(key string, value interface{})

添加或者修改数据

type DbConfig

type DbConfig struct {
	Db           *sql.DB
	DriverName   string
	Addr         string
	User         string
	Passwd       string
	Port         string
	DBName       string
	MaxOpenConns int
	MaxIdleConns int
	Debug        bool
}

func (*DbConfig) BatchInsert

func (config *DbConfig) BatchInsert(table string, datas []DataStruct) (num int64, err error)

func (*DbConfig) Close

func (config *DbConfig) Close() error

func (*DbConfig) Connect

func (config *DbConfig) Connect() (err error)

func (*DbConfig) Count

func (config *DbConfig) Count(table string, where string, args ...interface{}) (total int64, err error)

func (*DbConfig) Delete

func (config *DbConfig) Delete(table string, where string, args ...interface{}) (num int64, err error)

func (*DbConfig) GetOne

func (config *DbConfig) GetOne(table, fields, where string, args ...interface{}) (map[string]interface{}, error)

获取一条

func (*DbConfig) Insert

func (config *DbConfig) Insert(table string, datas DataStruct) (id int64, err error)

插入数据

func (*DbConfig) Query

func (config *DbConfig) Query(sqlString string, args ...interface{}) ([]map[string]interface{}, error)

func (*DbConfig) Select

func (config *DbConfig) Select(table string, fields string, where string, args ...interface{}) ([]map[string]interface{}, error)

批量查询,不带分页计算

func (*DbConfig) Update

func (config *DbConfig) Update(table string, datas DataStruct, where string, args ...interface{}) (num int64, err error)

更新

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL