如何使用Kotlin将枚举数据存储为数据库中的数组列表



我有一个包含多个数据的枚举类,我想存储它将数据枚举为数据库中的列表。例如它包含的Developer表技术列,该列存储枚举数据,假设一个用户想要存储在许多技术上工作的Developer1,.Net等这些技术都属于enum类,如何存储。与我有一个包含多个主题名称的Subject枚举类相同,当我注册新老师时,我想存储多少科目老师知道,如果他知道我们列举的多个科目班级列表然后存储教师已知的科目id。但是我我无法在subjectId的一列中存储多个数据,It在SubjectId列中只存储一个数据,如果我在中传递多个数据poster中的subjectId列抛出错误400教师实体类

package com.nilmani.jpqlprojectexample.entity
import com.nilmani.jpqlprojectexample.enum.Subject
import com.nilmani.jpqlprojectexample.enum.University
import java.util.*
import javax.persistence.Entity
import javax.persistence.GeneratedValue
import javax.persistence.GenerationType
import javax.persistence.Id
@Entity
data class Teacher(
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
val id:Long=-1,
val nameOfTeacher:String="",
val price:Long=-1,
val subject:Int = Subject.MATH.type,//List<Int> = listOf(Subject.MATH.type)
val university:Int=University.OTHER.type
)

主题枚举类

package com.nilmani.jpqlprojectexample.enum
enum class Subject(val type:Int) {
MATH(1),
PHYSIC(2),
ACCOUNTING(3),
ZOOLOGY(4),
BIOLOGY(5),
PROGRAMMING(6),
STATICS(7),
CHEMISTRY(8),
HISTORY(9)
}

ReqTeacher模型类

package com.nilmani.jpqlprojectexample.model.request
import com.nilmani.jpqlprojectexample.enum.Subject import com.nilmani.jpqlprojectexample.enum.University
data class ReqTeacher(
val nameOfTeacher:String="",
val price:Long=-1,
val subject:Int= Subject.MATH.type,
val university:Int= University.OTHER.type, )

响应教师模型类

package com.nilmani.jpqlprojectexample.model.response
import com.nilmani.jpqlprojectexample.enum.Subject
import com.nilmani.jpqlprojectexample.enum.University
data class ResTeacher(
val nameOfTeacher:String="",
val price:Long=-1,
val subject:Int= Subject.MATH.type,
val university:Int= University.OTHER.type,
)

教师控制器类

package com.nilmani.jpqlprojectexample.controller
import com.nilmani.jpqlprojectexample.entity.Teacher
import com.nilmani.jpqlprojectexample.model.request.ReqTeacher
import com.nilmani.jpqlprojectexample.model.response.ResTeacher
import com.nilmani.jpqlprojectexample.repository.TeacherRepository
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.*
@RestController
@RequestMapping("/teachTest")
class TeacherController {
@Autowired
private lateinit var teachRopo:TeacherRepository
@PostMapping("/add")
fun addTeacher(@ModelAttribute request:ReqTeacher):ResponseEntity<*>{
//        val newTeacher = teachRopo.findById(request.)
var newTeacher = Teacher(
nameOfTeacher = request.nameOfTeacher,
price = request.price,
subject = request.subject,
university = request.university,
)
val saveTeacher = teachRopo.save(newTeacher)
val respTeacher = ResTeacher(
saveTeacher.nameOfTeacher,
saveTeacher.price,
saveTeacher.subject,
saveTeacher.university
)
return ResponseEntity(respTeacher,HttpStatus.OK)
}
}

我想将多个数据或单个数据存储在特定的teacherId,但我的代码只存储特定的一个数据教师ID

您应该有一个新的表(实体(来表示教师和科目之间的一对多关系,例如teacher_subject。这是正确的做法。如果您试图将主题作为空格/逗号分隔的值存储在单个单元格中;数学、物理、生物学;那将是不好的做法。

最新更新