Score
0
Watch 2 Star 6 Fork 0

@Billcoding / bgbatisGoApache-2.0

Join us
Explore and code with more than 5 million developers,Free private repositories !:)
Sign up
A sample db tools with binding between sqls and xml nodes, similar like mybatis style. spread retract

Clone or download
Cancel
Notice: Creating folder will generate an empty file .keep, because not support in Git
Loading...
README.md

bgbatis

Introduction

A sample db tools with binding between sqls and xml nodes, similar like mybatis style.

Features

  • Pure native
  • No third dependencies
  • Easy and simple
  • Multiple DataSource supports
  • Tx supports
  • SQL cache
  • DB configurable
  • Query optimized

Improvement

  • Select single row
selectMapper.Exec().Single(&PTR1, &PTR2 ...)
  • Select multiple rows
selectMapper.Exec().List(&StructPTR{})

Install

  • GOPATH
mkdir -p $GOPATH/src/gitee.com/billcoding/bgbatis

cd $GOPATH/src/gitee.com/billcoding

git clone https://gitee.com/billcoding/bgbatis.git bgbatis
  • Go mod
require gitee.com/billcoding/bgbatis latest

Usage

  • Insert
userMapper := Default().Init().RegisterDS("master", dsn).Mapper("user")
err := userMapper.Update("insert").Exec("inserted")
  • Delete
userMapper := Default().Init().RegisterDS("master", dsn).Mapper("user")
err := userMapper.Update("delete").Exec(1)
  • Update
userMapper := Default().Init().RegisterDS("master", dsn).Mapper("user")
err := userMapper.Update("update").Exec("updated", 1)
  • Select Simple
var batis = Default().Init().RegisterDS("master", dsn)
userMapper := batis.Mapper("user")
userMapper.Select("selectSimple").Exec(func(rows *sql.Rows) {
    if rows.Next() {
        t := ""
        rows.Scan(&t)
        fmt.Printf("time is %v\n", t)
    }
})
  • Select Struct
var batis = Default().Init().RegisterDS("master", dsn)
type User struct {
    Id   int    `db:"id"`
    Name string `db:"name"`
}
var userList []User
batis.Mapper("user").Select("selectStruct").Exec(func(rows *sql.Rows) {
    for rows.Next() {
        user := User{}
        ScanStruct(rows, &user)
        userList = append(userList, user)
    }
})

XML Definition

  • CUD defines Update node
<?xml version="1.0" encoding="UTF-8"?>
<bgbatis-mapper binding="user">

    <update id="insert">
        insert into user(`name`) values (?)
    </update>
 
    <update id="delete">
        delete from user where id = ?
    </update>
 
    <update id="update">
        update user set name = ? where id = ?
    </update>
 
</bgbatis-mapper>
  • R defines Select node
<?xml version="1.0" encoding="UTF-8"?>
<bgbatis-mapper binding="user">

    <select id="select">
        select * from user
    </update>
 
</bgbatis-mapper>

Multiple DataSource Support

  • Register DataSource
batis.RegisterDS(DSNAME, DSN)
  • Select DataSource
mapper.SelectDS(DSNAME)

Transaction Support

  • Begin tx
batis.TxMapper(BINDING)
  • Commit tx
txMapper.Commit()
  • Rollback tx
txMapper.Rollback()

Env Support

  • Show SQL
BGBATIS_SHOW_SQL

e.g.

BGBATIS_SHOW_SQL=1
BGBATIS_SHOW_SQL=on|ON
BGBATIS_SHOW_SQL=true|TRUE
  • Mapper path
BGBATIS_MAPPER_PATH

e.g.

BGBATIS_MAPPER_PATH=/tmp/myapp/mapper
  • Dsn
BGBATIS_DSN

e.g.

1. BGBATIS_DSN=root:123@tcp(192.168.1.8:3306)/test
2. BGBATIS_DSN=_,root:123@tcp(192.168.1.8:3306)/test
3. BGBATIS_DSN=master,root:123@tcp(192.168.1.8:3306)/test|slave,root:123@tcp(192.168.1.9:3306)/test

Comments ( 0 )

Sign in for post a comment

Go
1
https://gitee.com/billcoding/bgbatis.git
git@gitee.com:billcoding/bgbatis.git
billcoding
bgbatis
bgbatis
master

Help Search