Closed MariaWf closed 6 years ago
这样,测试了一下,是可以的,@qiangxue ,强哥验证下正确与否,刚学go不久。
package main
import(
"fmt"
"github.com/gin-gonic/gin"
"net/http"
"github.com/go-ozzo/ozzo-dbx"
_ "github.com/go-sql-driver/mysql"
model "github.com/fecshopsoft/go-blog/model"
//mysqlPool "github.com/fecshopsoft/golang-db/mysql"
//testMysql "github.com/fecshopsoft/golang-db/test/mysql"
)
func dbPool() *dbx.DB{
host := `127.0.0.1:3306`
database := `go_test`
user := `root`
password := `xxx`
charset := `utf8`
// 用于设置最大打开的连接数
maxOpenConns := 100
// 用于设置闲置的连接数
maxIdleConns := 10
dataSourceName := fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=%s&autocommit=true", user, password, host, database, charset)
db, _ := dbx.Open("mysql", dataSourceName)
db.DB().SetMaxOpenConns(maxOpenConns)
db.DB().SetMaxIdleConns(maxIdleConns)
return db
}
func main() {
db := dbPool();
r := gin.Default()
v2 := r.Group("/v2")
{
// 查询部分
v2.GET("/customers", func(c *gin.Context) {
data := model.Customer.List(db);
c.JSON(http.StatusOK, data)
})
}
r.Run("120.24.37.249:3000") // 这里改成您的ip和端口
}
Thanks, @fancyecommerce !
在您的库包中,没有看到做数据库连接池的设置选项,您可以把这个封装进去吗?