Score
0
Watch 6 Star 21 Fork 7

springrain / readygoGoApache-2.0

Join us
Explore and code with more than 5 million developers,Free private repositories !:)
Sign up
golang开发脚手架 spread retract

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

readygo

介绍

golang开发脚手架

软件架构

基于gin , zormgowe
自带代码生成器

  • 基于原生sql语句编写,是springrain的精简和优化.
  • 代码精简,总计2000行左右,注释详细,方便定制修改.
  • 支持事务传播,这是zorm诞生的主要原因
  • 支持mysql,postgresql,oracle,mssql,sqlite
  • 支持数据库读写分离

生产使用参考 UserStructService.go

示例

  1. 生成实体类或手动编写,建议使用代码生成器 https://gitee.com/chunanyong/readygo/tree/master/codegenerator

//UserOrgStructTableName 表名常量,方便直接调用
const UserOrgStructTableName = "t_user_org"

// UserOrgStruct 用户部门中间表
type UserOrgStruct struct {
  //引入默认的struct,隔离IEntityStruct的方法改动
  zorm.EntityStruct

  //Id 编号
  Id string `column:"id"`

  //UserId 用户编号
  UserId string `column:"userId"`

  //OrgId 机构编号
  OrgId string `column:"orgId"`

  //ManagerType 0会员,1员工,2主管
  ManagerType int `column:"managerType"`

  //------------------数据库字段结束,自定义字段写在下面---------------//

}

//GetTableName 获取表名称
func (entity *UserOrgStruct) GetTableName() string {
  return UserOrgStructTableName
}

//GetPKColumnName 获取数据库表的主键字段名称.因为要兼容Map,只能是数据库的字段名称.
func (entity *UserOrgStruct) GetPKColumnName() string {
  return "id"
}
  1. 初始化zorm

    import _ "github.com/go-sql-driver/mysql"
    
    
    dataSourceConfig := zorm.DataSourceConfig{
    	DSN:        "root:root@tcp(127.0.0.1:3306)/readygo?charset=utf8&parseTime=true",
    	DriverName: "mysql",
    	DBType:     "mysql",
     }
     zorm.NewDBDao(&dataSourceConfig)
  2. var user permstruct.UserStruct
    err := zorm.Insert(context.Background(), &user)
  3. err := zorm.Delete(context.Background(),&user)
  4. err := zorm.Update(context.Background(),&user)
    //finder更新
    err := zorm.UpdateFinder(context.Background(),finder)
  5. finder := zorm.NewSelectFinder(permstruct.UserStructTableName)
    finder.Append(" order by id asc ")
    page := zorm.NewPage()
    var users = make([]permstruct.UserStruct, 0)
    err := zorm.QuerySlice(context.Background(), finder, &users, page)
  6. 事务传播

    //匿名函数return的error如果不为nil,事务就会回滚
    _, errSaveUserStruct := zorm.Transaction(ctx, func(ctx context.Context) (interface{}, error) {
    
    	//事务下的业务代码开始
    	errSaveUserStruct := zorm.Insert(ctx, userStruct)
    
    	if errSaveUserStruct != nil {
    		return nil, errSaveUserStruct
    	}
    
    	return nil, nil
    	//事务下的业务代码结束
    
    })
  7. 生产示例

    //FindUserOrgByUserId 根据userId查找部门UserOrg中间表对象
    func FindUserOrgByUserId(ctx context.Context, userId string, page *zorm.Page) ([]permstruct.UserOrgStruct, error) {
    if len(userId) < 1 {
    	return nil, errors.New("userId不能为空")
    }
    finder := zorm.NewFinder().Append("SELECT re.* FROM  ").Append(permstruct.UserOrgStructTableName).Append(" re ")
    finder.Append("   WHERE re.userId=?    order by re.managerType desc   ", userId)
    
    userOrgs := make([]permstruct.UserOrgStruct, 0)
    errQueryList := zorm.QuerySlice(ctx, finder, &userOrgs, page)
    if errQueryList != nil {
    	return nil, errQueryList
    }
    
    return userOrgs, nil
    }
  8. 测试

    //函数测试
    go test -run TestAdd2
    //性能测试
    go test -bench=.
    go test -v -bench=. -cpu=8 -benchtime="3s" -timeout="5s" -benchmem

Comments ( 0 )

Sign in for post a comment

Go
1
https://gitee.com/chunanyong/readygo.git
git@gitee.com:chunanyong/readygo.git
chunanyong
readygo
readygo
master

Help Search

205735 778617b6 1899542 205747 347fc4a1 1899542