Closed Anxiangchegu closed 4 days ago
The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question
template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking ✨
you can use Raw SQL, or just build it manually, here is a minimal demo
package main
import (
"fmt"
"math/rand"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
const SQL = `
SELECT ta.*
FROM sys_dictionary_data ta
LEFT JOIN sys_dictionary tb
ON ta.dict_id = tb.dict_id
AND tb.deleted = 0
WHERE ta.deleted = 0
AND tb.dict_code = "sex"`
const SQL2 = `
SELECT ta.*
FROM sys_dictionary_data ta
LEFT JOIN sys_dictionary tb
ON ta.dict_id = tb.dict_id
AND tb.deleted = 0
WHERE ta.deleted = 0
AND tb.dict_code = ?`
type SysDictionaryData struct {
ID uint64 `gorm:"column:id"`
DictID uint64 `gorm:"column:dict_id"`
Deleted int8 `gorm:"column:deleted"`
Data string `gorm:"column:data"`
}
func (SysDictionaryData) TableName() string {
return "sys_dictionary_data"
}
type SysDictionary struct {
ID uint64 `gorm:"column:id"`
DictID uint64 `gorm:"column:dict_id"`
Deleted int8 `gorm:"column:deleted"`
DictCode string `gorm:"column:dict_code"`
}
func (SysDictionary) TableName() string {
return "sys_dictionary"
}
func prepareForTest(db *gorm.DB) {
db.AutoMigrate(&SysDictionary{}, &SysDictionaryData{})
for i := uint64(1); i <= 10; i++ {
db.Create(&SysDictionary{DictID: i, DictCode: "sex"})
dataNum := rand.Int()%5 + 2
for j := 1; j <= dataNum; j++ {
db.Create(&SysDictionaryData{DictID: i, Data: fmt.Sprintf("dict %d data %d", i, j)})
}
}
}
func runByRawSQL(db *gorm.DB) {
fmt.Printf("run by raw sql\n\n")
var dest []SysDictionaryData
err := db.Raw(SQL).Find(&dest).Error
fmt.Printf("err is %+v\n", err)
fmt.Printf("data is %+v\n\n", dest)
}
func runByRawSQLWithParameter(db *gorm.DB, dict_code string) {
fmt.Printf("run by raw sql with parameter\n\n")
var dest []SysDictionaryData
err := db.Raw(SQL2, dict_code).Find(&dest).Error
fmt.Printf("err is %+v\n", err)
fmt.Printf("data is %+v\n\n", dest)
}
func runByManuallyBuildTheSQL(db *gorm.DB, dict_code string) {
fmt.Printf("run by manually build the SQL\n\n")
query := db.Table("sys_dictionary_data ta").
Joins("LEFT JOIN sys_dictionary tb ON ta.dict_id = tb.dict_id AND tb.deleted = ?", 0).
Where("ta.deleted = 0 AND tb.dict_code = ?", dict_code)
var dest []SysDictionaryData
err := query.Find(&dest).Error
fmt.Printf("err is %+v\n", err)
fmt.Printf("data is %+v\n\n", dest)
}
func main() {
db, _ := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{PrepareStmt: true})
prepareForTest(db)
db = db.Debug() // use debug to print sql to the console
runByRawSQL(db)
runByRawSQLWithParameter(db, "sex")
runByManuallyBuildTheSQL(db, "sex")
}
The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question
template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking ✨
这是从Java项目mapper.xml文件中获取得的,如果用转换为GORM?
`