问题1:delete 方法无法识别到ids
解决:
问题2:idea(goland) 上传到gitee 步骤记录
1.项目setting->git->stash->create stash
2. 本地仓库下面查看项目分支
3.对比修改的位置和代码
git->compare with branch ->选择分支
4.commit 之前查看提交的内容和修改信息
commit ->commit - >push
5.push
学习记录点一:
多对多查询
需求:
1.通过请求时传入的studentId去查询,当前学生被哪几位老师教。
2.如法炮制,通过老师id查询教了哪几位学生
实现:
1.model层:
// student info
type SysStudent struct {
ms.M
Sid uint `gorm:"sid" json:"sid"`
Sname string `gorm:"sname" json:"sname"`
ParentId uint `gorm:"default:0;comment:'父菜单编号(编号为0时表示根菜单)'" json:"parentId"`
Courses []SysCourse `gorm:"foreignkey:StudentId;comment:'学生课程'" json:"courses"`
Teachers []SysTeacher `gorm:"many2many:sys_student_teacher_relation;" json:"teachers"` // 学生老师多对多关系
Children []SysStudent `gorm:"-" json:"children"`
}
// teacher info
type SysTeacher struct {
ms.M
Tnum uint `gorm:"comment:'工号'" json:"tnum"`
Password string `gorm:"comment:'密码'" json:"password"`
Tname string `gorm:"tname" json:"sname"`
CourseId uint `gorm:"comment:'角色Id外键'" json:"courseId"`
Course SysCourse `gorm:"foreignKey:StudentId" json:"course"` // 将SysUser.RoleId指定为外键
Students []SysStudent `gorm:"many2many:sys_student_teacher_relation;" json:"student"` // 教师学生多对多关系
}
// student relation teacher
type RelationStudentTeacher struct {
SysStudentId uint `json:"sysStudentId"`
SysTeacherId uint `json:"sysTeacherId"`
}
2.路由器
// 用户路由
func InitTeacherRouter(r *router.Router) {
router1 := r.Casbin("/teacher")
router2 := r.Idempotence("/teacher")
//router1.POST("/info", v1.GetTeacherInfo)
router1.GET("/list", v1.GetTeachers)
router2.POST("/create", v1.CreateTeacher)
router1.PATCH("/update/:studentId", v1.UpdateTeacherById)
router1.DELETE("/delete/batch", v1.BatchDeleteTeacherByIds)
router1.GET("/all/:id", v1.FindAllStudentsByTeacherId)//find students by techerID
}
3.控制层
// find students by techer id
func FindAllStudentsByTeacherId(c *gin.Context) {
var r request.StudentReq
req.ShouldBind(c, &r)
//id := req.UintId(c)
s := service.New(c)
students := s.FindStudentsByTeacherId(utils.Str2Uint(c.Param("id")))//service importe
resp.SuccessWithData(students)
}
4.业务层
// find student by teacher id
func (my MysqlService) FindStudentsByTeacherId(teacher uint) []models.SysStudent {
// query current student teacher relation
studentIds := make([]uint, 0)
my.Q.Tx.
Model(&models.StudenttionTeacherStudent{}).
Where("sys_teacher_id = ?", teacher).
Pluck("sys_student_id", &studentIds)
teachersStudent := make([]models.SysStudent, 0)
if len(studentIds) > 0 {
my.Q.Tx.
Model(&models.SysStudent{}).
Where("id IN (?)", studentIds).
Find(&teachersStudent)
}
return teachersStudent
}