gobatis

package module
v0.3.2-beta Latest Latest
Warning

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

Go to latest
Published: Sep 8, 2020 License: Apache-2.0 Imports: 13 Imported by: 0

README

gobatis

Introduction

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

Features

  • Pure native
  • No third dependencies
  • Multiple DataSource supports
  • Multiple Database supports
  • Dynamic SQL supports & Func (Using text/template)

Supports database

  1. MySQL(Default dialect)
  2. SQLite/SQLite3
  3. MSSQL/SQLServer

Choose dialect

import _ "github.com/mattn/go-sqlite3"

batis.Choose(batis.SQLite3)

Install

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

cd $GOPATH/src/gitee.com/billcoding

git clone https://gitee.com/billcoding/gobatis.git gobatis
  • Go mod
require gitee.com/billcoding/gobatis 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().Call(func(rows *sql.Rows) {
    if rows.Next() {
        t := ""
        rows.Scan(&t)
        fmt.Printf("time is %v\n", t)
    }
    rows.Close()
})

  • Select Struct
var batis = Default().Init().RegisterDS("master", dsn)
type User struct {
    Id   int    `db:"id"`
    Name string `db:"name"`
}
userList := batis.Mapper("user").Select("selectStruct").Exec().List(new(User))
})

XML Definition

  • CUD defines Update node
<?xml version="1.0" encoding="UTF-8"?>
<batis-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>
 
</batis-mapper>
  • R defines Select node
<?xml version="1.0" encoding="UTF-8"?>
<batis-mapper binding="user">

    <select id="select">
        select * from user
    </update>
 
</batis-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
BATIS_SHOW_SQL

e.g.

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

e.g.

BATIS_MAPPER_PATH=/tmp/myapp/mapper
  • Dsn
BATIS_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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Batis

type Batis struct {
	// contains filtered or unexported fields
}

Define batis struct

func Default

func Default() *Batis

Return batis

func New

func New() *Batis

Return new batis

func (*Batis) Choose

func (b *Batis) Choose(dialect Dialect) *Batis

Choose dialect

func (*Batis) DBConfig

func (b *Batis) DBConfig(dbConfig *DBConfig) *Batis

Set dbConfig

func (*Batis) Init

func (b *Batis) Init() *Batis

Init batis

func (*Batis) LogFatal

func (b *Batis) LogFatal(message string, args ...interface{})

Log fatal

func (*Batis) LogInfo

func (b *Batis) LogInfo(message string, args ...interface{})

Log info

func (*Batis) LogWarn

func (b *Batis) LogWarn(message string, args ...interface{})

Log warn

func (*Batis) Mapper

func (b *Batis) Mapper(binding string) *mapper

Get mapper

func (*Batis) MapperPaths

func (b *Batis) MapperPaths(mapperPaths ...string) *Batis

Set mapper path

func (*Batis) RegisterDS

func (b *Batis) RegisterDS(name, dsn string) *Batis

Register datasource

func (*Batis) RegisterDSWithConfig

func (b *Batis) RegisterDSWithConfig(name, dsn string, dbConfig *DBConfig) *Batis

Register datasource with config

func (*Batis) ShowSql

func (b *Batis) ShowSql(showSql bool) *Batis

Set showSql

func (*Batis) TxMapper

func (b *Batis) TxMapper(binding string) *txMapper

Get txMapper

type DB

type DB struct {
	// contains filtered or unexported fields
}

Define DB struct

func (*DB) Begin

func (db *DB) Begin() *Tx

Begin a tx

type DBConfig

type DBConfig struct {
	MaxIdleConns    int
	MaxOpenConns    int
	ConnMaxLifetime time.Duration
}

Define DBConfig struct

type Dialect

type Dialect string
const (
	MySQL   Dialect = "mysql"     //see  https://github.com/go-sql-driver/mysql
	SQLite  Dialect = "sqlite3"   //see  https://github.com/mattn/go-sqlite3
	SQLite3 Dialect = "sqlite3"   //see  https://github.com/mattn/go-sqlite3
	MSSQL   Dialect = "sqlserver" //see  https://github.com/denisenkom/go-mssqldb
)

type NamedParam

type NamedParam struct {
	// contains filtered or unexported fields
}

Define namedParam struct

func NewNamedParam

func NewNamedParam(name string, val interface{}) *NamedParam

New namedParam

type Tx

type Tx struct {
	// contains filtered or unexported fields
}

Define Tx struct

Jump to

Keyboard shortcuts

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