update 修改表名的硬编码问题
This commit is contained in:
1 parent
f091f8c24d
commit
7ee48cf4a9
9 files changed
+144
-139
No files matched your search
+13
-13
@@ -6,12 +6,12 @@ import (
|
||||
)
|
||||
|
||||
type Post struct {
|
||||
PostId int `gorm:"primary_key;AUTO_INCREMENT" json:"postId"` //岗位编号
|
||||
PostName string `gorm:"type:varchar(128);" json:"postName"` //岗位名称
|
||||
PostCode string `gorm:"type:varchar(128);" json:"postCode"` //岗位代码
|
||||
Sort int `gorm:"type:int(4);" json:"sort"` //岗位排序
|
||||
Status string `gorm:"type:int(1);" json:"status"` //状态
|
||||
Remark string `gorm:"type:varchar(255);" json:"remark"` //描述
|
||||
PostId int `gorm:"primary_key;AUTO_INCREMENT" json:"postId"` //岗位编号
|
||||
PostName string `gorm:"type:varchar(128);" json:"postName"` //岗位名称
|
||||
PostCode string `gorm:"type:varchar(128);" json:"postCode"` //岗位代码
|
||||
Sort int `gorm:"type:int(4);" json:"sort"` //岗位排序
|
||||
Status string `gorm:"type:int(1);" json:"status"` //状态
|
||||
Remark string `gorm:"type:varchar(255);" json:"remark"` //描述
|
||||
CreateBy string `gorm:"type:varchar(128);" json:"createBy"`
|
||||
UpdateBy string `gorm:"type:varchar(128);" json:"updateBy"`
|
||||
DataScope string `gorm:"-" json:"dataScope"`
|
||||
@@ -25,7 +25,7 @@ func (Post) TableName() string {
|
||||
|
||||
func (e *Post) Create() (Post, error) {
|
||||
var doc Post
|
||||
result := orm.Eloquent.Table("sys_post").Create(&e)
|
||||
result := orm.Eloquent.Table(e.TableName()).Create(&e)
|
||||
if result.Error != nil {
|
||||
err := result.Error
|
||||
return doc, err
|
||||
@@ -37,7 +37,7 @@ func (e *Post) Create() (Post, error) {
|
||||
func (e *Post) Get() (Post, error) {
|
||||
var doc Post
|
||||
|
||||
table := orm.Eloquent.Table("sys_post")
|
||||
table := orm.Eloquent.Table(e.TableName())
|
||||
if e.PostId != 0 {
|
||||
table = table.Where("post_id = ?", e.PostId)
|
||||
}
|
||||
@@ -60,7 +60,7 @@ func (e *Post) Get() (Post, error) {
|
||||
func (e *Post) GetList() ([]Post, error) {
|
||||
var doc []Post
|
||||
|
||||
table := orm.Eloquent.Table("sys_post")
|
||||
table := orm.Eloquent.Table(e.TableName())
|
||||
if e.PostId != 0 {
|
||||
table = table.Where("post_id = ?", e.PostId)
|
||||
}
|
||||
@@ -83,7 +83,7 @@ func (e *Post) GetList() ([]Post, error) {
|
||||
func (e *Post) GetPage(pageSize int, pageIndex int) ([]Post, int, error) {
|
||||
var doc []Post
|
||||
|
||||
table := orm.Eloquent.Select("*").Table("sys_post")
|
||||
table := orm.Eloquent.Select("*").Table(e.TableName())
|
||||
if e.PostId != 0 {
|
||||
table = table.Where("post_id = ?", e.PostId)
|
||||
}
|
||||
@@ -109,20 +109,20 @@ func (e *Post) GetPage(pageSize int, pageIndex int) ([]Post, int, error) {
|
||||
}
|
||||
|
||||
func (e *Post) Update(id int) (update Post, err error) {
|
||||
if err = orm.Eloquent.Table("sys_post").First(&update, id).Error; err != nil {
|
||||
if err = orm.Eloquent.Table(e.TableName()).First(&update, id).Error; err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
//参数1:是要修改的数据
|
||||
//参数2:是修改的数据
|
||||
if err = orm.Eloquent.Table("sys_post").Model(&update).Updates(&e).Error; err != nil {
|
||||
if err = orm.Eloquent.Table(e.TableName()).Model(&update).Updates(&e).Error; err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (e *Post) Delete(id int) (success bool, err error) {
|
||||
if err = orm.Eloquent.Table("sys_post").Where("post_id = ?", id).Delete(&Post{}).Error; err != nil {
|
||||
if err = orm.Eloquent.Table(e.TableName()).Where("post_id = ?", id).Delete(&Post{}).Error; err != nil {
|
||||
success = false
|
||||
return
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user