db change

master
wenfei 8 months ago
parent 6a31dd29f9
commit 5e24f88221

@ -4,9 +4,23 @@
<option name="autoReloadType" value="ALL" /> <option name="autoReloadType" value="ALL" />
</component> </component>
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="9f8c3ce4-c378-4191-b7a4-3d5acbc0ebbc" name="Changes" comment="rr"> <list default="true" id="9f8c3ce4-c378-4191-b7a4-3d5acbc0ebbc" name="Changes" comment="rrddf">
<change beforePath="$PROJECT_DIR$/.gitignore" beforeDir="false" afterPath="$PROJECT_DIR$/.gitignore" afterDir="false" /> <change beforePath="$PROJECT_DIR$/.gitignore" beforeDir="false" afterPath="$PROJECT_DIR$/.gitignore" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/go_mqtt.iml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/go_mqtt.iml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/modules.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/modules.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/vcs.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/vcs.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/README.md" beforeDir="false" afterPath="$PROJECT_DIR$/README.md" afterDir="false" />
<change beforePath="$PROJECT_DIR$/go.mod" beforeDir="false" afterPath="$PROJECT_DIR$/go.mod" afterDir="false" />
<change beforePath="$PROJECT_DIR$/go.sum" beforeDir="false" afterPath="$PROJECT_DIR$/go.sum" afterDir="false" />
<change beforePath="$PROJECT_DIR$/main.go" beforeDir="false" afterPath="$PROJECT_DIR$/main.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/models/Order.go" beforeDir="false" afterPath="$PROJECT_DIR$/models/Order.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/models/Product.go" beforeDir="false" afterPath="$PROJECT_DIR$/models/Product.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/models/Temperature.go" beforeDir="false" afterPath="$PROJECT_DIR$/models/Temperature.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/models/User.go" beforeDir="false" afterPath="$PROJECT_DIR$/models/User.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/mydb/ManageDb.go" beforeDir="false" afterPath="$PROJECT_DIR$/mydb/ManageDb.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/mydb/initialize.go" beforeDir="false" afterPath="$PROJECT_DIR$/mydb/initialize.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/utils/RandomNumber.go" beforeDir="false" afterPath="$PROJECT_DIR$/utils/RandomNumber.go" afterDir="false" />
</list> </list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" /> <option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -52,14 +66,25 @@
<property name="RunOnceActivity.OpenProjectViewOnStart" value="true" /> <property name="RunOnceActivity.OpenProjectViewOnStart" value="true" />
<property name="RunOnceActivity.ShowReadmeOnStart" value="true" /> <property name="RunOnceActivity.ShowReadmeOnStart" value="true" />
<property name="WebServerToolWindowFactoryState" value="false" /> <property name="WebServerToolWindowFactoryState" value="false" />
<property name="configurable..is.expanded" value="false" />
<property name="configurable.GoLibrariesConfigurable.is.expanded" value="true" />
<property name="go.formatter.settings.were.checked" value="true" /> <property name="go.formatter.settings.were.checked" value="true" />
<property name="go.import.settings.migrated" value="true" /> <property name="go.import.settings.migrated" value="true" />
<property name="go.modules.go.list.on.any.changes.was.set" value="true" /> <property name="go.modules.go.list.on.any.changes.was.set" value="true" />
<property name="go.sdk.automatically.set" value="true" /> <property name="go.sdk.automatically.set" value="true" />
<property name="last_opened_file_path" value="$USER_HOME$" /> <property name="last_opened_file_path" value="$PROJECT_DIR$" />
<property name="settings.editor.selected.configurable" value="go.sdk" /> <property name="settings.editor.selected.configurable" value="preferences.lookFeel" />
</component> </component>
<component name="RunManager"> <component name="RunManager">
<configuration default="true" type="GoApplicationRunConfiguration" factoryName="Go Application">
<module name="go_mqtt" />
<working_directory value="$PROJECT_DIR$" />
<go_parameters value="-i" />
<kind value="FILE" />
<directory value="$PROJECT_DIR$" />
<filePath value="$PROJECT_DIR$" />
<method v="2" />
</configuration>
<configuration name="go build main.go" type="GoApplicationRunConfiguration" factoryName="Go Application" temporary="true" nameIsGenerated="true"> <configuration name="go build main.go" type="GoApplicationRunConfiguration" factoryName="Go Application" temporary="true" nameIsGenerated="true">
<module name="go_mqtt" /> <module name="go_mqtt" />
<working_directory value="$PROJECT_DIR$" /> <working_directory value="$PROJECT_DIR$" />
@ -69,6 +94,16 @@
<filePath value="$PROJECT_DIR$/main.go" /> <filePath value="$PROJECT_DIR$/main.go" />
<method v="2" /> <method v="2" />
</configuration> </configuration>
<configuration default="true" type="GoTestRunConfiguration" factoryName="Go Test">
<module name="go_mqtt" />
<working_directory value="$PROJECT_DIR$" />
<go_parameters value="-i" />
<kind value="DIRECTORY" />
<directory value="$PROJECT_DIR$" />
<filePath value="$PROJECT_DIR$" />
<framework value="gotest" />
<method v="2" />
</configuration>
<recent_temporary> <recent_temporary>
<list> <list>
<item itemvalue="Go Build.go build main.go" /> <item itemvalue="Go Build.go build main.go" />
@ -95,7 +130,8 @@
<option name="ADD_EXTERNAL_FILES_SILENTLY" value="true" /> <option name="ADD_EXTERNAL_FILES_SILENTLY" value="true" />
<MESSAGE value="数据库改用orm框架 gorm" /> <MESSAGE value="数据库改用orm框架 gorm" />
<MESSAGE value="rr" /> <MESSAGE value="rr" />
<option name="LAST_COMMIT_MESSAGE" value="rr" /> <MESSAGE value="rrddf" />
<option name="LAST_COMMIT_MESSAGE" value="rrddf" />
</component> </component>
<component name="VgoProject"> <component name="VgoProject">
<integration-enabled>true</integration-enabled> <integration-enabled>true</integration-enabled>

@ -1,20 +1,22 @@
module go_mqtt module go_mqtt
go 1.21.3 go 1.24.1
require github.com/eclipse/paho.mqtt.golang v1.4.2 require (
github.com/eclipse/paho.mqtt.golang v1.4.2
github.com/go-sql-driver/mysql v1.9.0
github.com/google/uuid v1.6.0
github.com/shopspring/decimal v1.4.0
gorm.io/driver/mysql v1.5.7
gorm.io/gorm v1.25.12
)
require ( require (
filippo.io/edwards25519 v1.1.0 // indirect filippo.io/edwards25519 v1.1.0 // indirect
github.com/go-sql-driver/mysql v1.9.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/gorilla/websocket v1.4.2 // indirect github.com/gorilla/websocket v1.4.2 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect github.com/jinzhu/now v1.1.5 // indirect
github.com/shopspring/decimal v1.4.0 // indirect
golang.org/x/net v0.0.0-20200425230154-ff2c4b7c35a0 // indirect golang.org/x/net v0.0.0-20200425230154-ff2c4b7c35a0 // indirect
golang.org/x/sync v0.11.0 // indirect golang.org/x/sync v0.11.0 // indirect
golang.org/x/text v0.22.0 // indirect golang.org/x/text v0.22.0 // indirect
gorm.io/driver/mysql v1.5.7 // indirect
gorm.io/gorm v1.25.12 // indirect
) )

@ -17,8 +17,10 @@ var DB *gorm.DB
func initDatabase() { func initDatabase() {
dsn := "root:Skyinno251,@tcp(192.168.2.18:3306)/gorm?charset=utf8mb4&parseTime=True&loc=Local" //dsn := "root:Skyinno251,@tcp(192.168.2.18:3306)/gorm?charset=utf8mb4&parseTime=True&loc=Local"
//dsn := "root:Skyinno251,@tcp(47.242.184.139:3306)/appserver?charset=utf8mb4&parseTime=True&loc=Local" //dsn := "root:Skyinno251,@tcp(47.242.184.139:3306)/appserver?charset=utf8mb4&parseTime=True&loc=Local"
//dsn := "root:Skyinno251,@tcp(localhost:3306)/gorm?charset=utf8mb4&parseTime=True&loc=Local"
dsn := "root:Skyinno251,@tcp(192.168.3.29:3306)/gorm?charset=utf8mb4&parseTime=True&loc=Local"
db, err := gorm.Open(mysql.New(mysql.Config{ db, err := gorm.Open(mysql.New(mysql.Config{
DSN: dsn, // DSN data source name DSN: dsn, // DSN data source name
@ -41,16 +43,16 @@ func initDatabase() {
} }
// SetMaxIdleConns 用于设置连接池中空闲连接的最大数量。 // SetMaxIdleConns 用于设置连接池中空闲连接的最大数量。
sqlDB.SetMaxIdleConns(10) sqlDB.SetMaxIdleConns(5)
// SetMaxOpenConns 设置打开数据库连接的最大数量。 // SetMaxOpenConns 设置打开数据库连接的最大数量。
sqlDB.SetMaxOpenConns(50) sqlDB.SetMaxOpenConns(20)
// SetConnMaxLifetime 设置了连接可复用的最大时间。 // SetConnMaxLifetime 设置了连接可复用的最大时间。
sqlDB.SetConnMaxLifetime(time.Minute * 5) // 连接最大生命周期 sqlDB.SetConnMaxLifetime(time.Minute * 5) // 连接最大生命周期
err = sqlDB.Ping() // 检查连接是否成功建立。如果失败,则返回错误。 err = sqlDB.Ping() // 检查连接是否成功建立。如果失败,则返回错误。
if err != nil { if err != nil {
// 处理连接失败的情况。例如,打印错误信息并退出程序。 // 处理连接失败的情况。例如,打印错误信息并退出程序。
log.Println("192.168.2.18:3306连接失败") log.Println("数据库连接失败")
fmt.Println("Connection failed:", err) fmt.Println("Connection failed:", err)
return return
} }

Loading…
Cancel
Save