/ch13-seckill/pkg/mysql/mysql.go

https://github.com/longjoy/micro-go-book · Go · 36 lines · 27 code · 6 blank · 3 comment · 2 complexity · 0b3b1d79844ee381e75ca9fb9ba389b7 MD5 · raw file

  1. package mysql
  2. import (
  3. "fmt"
  4. _ "github.com/go-sql-driver/mysql"
  5. "github.com/gohouse/gorose/v2"
  6. )
  7. var engin *gorose.Engin
  8. var err error
  9. func InitMysql(hostMysql, portMysql, userMysql, pwdMysql, dbMysql string) {
  10. fmt.Printf(userMysql)
  11. fmt.Printf(dbMysql)
  12. DbConfig := gorose.Config{
  13. // Default database configuration
  14. Driver: "mysql", // Database driver(mysql,sqlite,postgres,oracle,mssql)
  15. Dsn: userMysql + ":" + pwdMysql + "@tcp(" + hostMysql + ":" + portMysql + ")/" + dbMysql + "?charset=utf8&parseTime=true", // 数据库链接
  16. Prefix: "", // Table prefix
  17. // (Connection pool) Max open connections, default value 0 means unlimit.
  18. SetMaxOpenConns: 300,
  19. // (Connection pool) Max idle connections, default value is 1.
  20. SetMaxIdleConns: 10,
  21. }
  22. engin, err = gorose.Open(&DbConfig)
  23. if err != nil {
  24. fmt.Println(err)
  25. return
  26. }
  27. }
  28. func DB() gorose.IOrm {
  29. return engin.NewOrm()
  30. }