我正在使用 java spring 創建一個電子日記應用程式,但我遇到了一個小問題。我決定用狀態模式創建角色系統。所以這個系統的邏輯應該是這樣的:
- 在控制器中,接受請求后,它會創建背景關系類的物件。
- 背景關系類的建構式根據用戶角色獲取具體的RoleClass。(它使用工廠)
- 在控制器中,我們呼叫我們想要執行的背景關系的方法
- 在背景關系中,我們呼叫我們想要執行的角色的方法。
- 在角色類中,我們呼叫我們想要執行的服務方法。
但這就是問題所在。當我構建專案時,它向我顯示了這樣的錯誤:
java: cannot find symbol
symbol: method getRole()
location: variable user of type com.diary.diary.entity.UserEntity
我不明白為什么會這樣,因為所有方法都在 UserEntity 類中宣告。
所以這里是所有的代碼:
用戶控制器方法
@GetMapping("/marks")
public ResponseEntity<Object> getMarks() {
try {
UserContext userContext = new UserContext();
return ResponseEntity.ok(userContext.getMarks());
} catch (Exception e) {
return new ResponseEntity<>(e.getMessage(), HttpStatus.NOT_FOUND);
}
}
用戶背景關系
public class UserContext {
private final UserRole userRole;
public UserContext() throws UserNotFoundException {
UserEntity user = new UserService().getCurrentUser();
userRole = RoleFactory.getUserRole(user.getRole().getName());
}
public List<HomeworkGetModel> getHomework() throws UserNotFoundException {
return userRole.getHomework();
}
public List<MarkGetModel> getMarks() throws UserNotFoundException {
return userRole.getMarks();
}
}
角色工廠
public class RoleFactory {
private static UserRole userRole;
public static UserRole getUserRole(String roleName) {
if(userRole == null) {
userRole = buildUserRole(roleName);
}
return userRole;
}
private static UserRole buildUserRole(String roleString) {
switch (roleString) {
case RoleNames.DEFAULT -> userRole = new DefaultRole();
case RoleNames.STUDENT -> userRole = new StudentRole();
case RoleNames.TEACHER -> userRole = new TeacherRole();
case RoleNames.ADMIN -> userRole = new AdminRole();
}
return userRole;
}
}
用戶物體
@Entity
@Table(name = "users", schema = "working_schema")
@Getter @Setter
public class UserEntity {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private long id;
private String login;
private String password;
private String phoneNumber;
private String email;
private String name;
private String surname;
private String patronymic;
@ManyToOne
@JoinColumn(name = "roleid", nullable = false)
private RoleEntity role;
@ManyToOne
@JoinColumn(name = "classid")
private ClassEntity userClass;
@ManyToOne
@JoinColumn(name = "schoolid")
private SchoolEntity school;
@OneToMany(mappedBy = "student", cascade = CascadeType.ALL)
private List<MarkEntity> marks;
public UserEntity() {
}
}
角色物體
@Entity
@Table(name = "roles", schema = "working_schema")
@Getter @Setter
public class RoleEntity {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private long id;
private String name;
@OneToMany(mappedBy = "role", cascade = CascadeType.ALL)
private List<UserEntity> users;
public RoleEntity() {
}
}
用戶角色界面
public interface UserRole {
default UserGetModel getUser(long id) throws UserNotFoundException {
throw new NotImplementedException();
}
default List<UserGetModel> getAllUsers() {
throw new NotImplementedException();
}
default UserEntity updateUser(UserUpdateModel newUserData) throws UserNotFoundException {
throw new NotImplementedException();
}
default UserEntity deleteUser() throws UserNotFoundException {
throw new NotImplementedException();
}
default List<MarkGetModel> getMarks() throws UserNotFoundException {
throw new NotImplementedException();
}
default List<MarkGetModel> getMarksByDate(String date) throws UserNotFoundException, ParseException {
throw new NotImplementedException();
}
default List<MarkGetModel> getMarksBySubject(String subjectName) throws UserNotFoundException {
throw new NotImplementedException();
}
default List<MarkGetModel> getMarksByDateAndSubject(DateAndSubjectModel dateAndSubject) throws UserNotFoundException, ParseException {
throw new NotImplementedException();
}
default SubjectGetModel getSubject(long id) throws SubjectNotFoundException {
throw new NotImplementedException();
}
default SubjectGetModel getSubject(String name) throws SubjectNotFoundException {
throw new NotImplementedException();
}
default SchoolGetModel getSchoolById(long schoolId) throws SchoolNotFoundException {
throw new NotImplementedException();
}
default SchoolGetModel getSchoolByNumber(int schoolNumber) throws SchoolNotFoundException {
throw new NotImplementedException();
}
default List<SchoolGetModel> getSchools() {
throw new NotImplementedException();
}
default ClassGetModel getSchoolClass(ClassGetByNumberModel classData) throws com.diary.diary.exception.school_class.ClassNotFoundException, SchoolNotFoundException {
throw new NotImplementedException();
}
default List<ClassGetModel> getClasses() {
throw new NotImplementedException();
}
default List<HomeworkGetModel> getHomework() throws UserNotFoundException {
throw new NotImplementedException();
}
default List<HomeworkGetModel> getHomeworkByDate(String date) throws UserNotFoundException, ParseException {
throw new NotImplementedException();
}
default List<HomeworkGetModel> getHomeworkBySubject(String subjectName) {
throw new NotImplementedException();
}
default ClassEntity addClass(ClassAddModel classData) {
throw new NotImplementedException();
}
default ClassEntity addUserToClass(AdminAddUserToClassModel userAndClassModel) {
throw new NotImplementedException();
}
default ClassEntity deleteClass(long id) {
throw new NotImplementedException();
}
default UserEntity removeUserFromClass(AdminRemoveUserFromClassModel userClassModel) {
throw new NotImplementedException();
}
default SchoolEntity addSchool(SchoolAddModel schoolData) {
throw new NotImplementedException();
}
default SchoolEntity deleteSchool(long id) {
throw new NotImplementedException();
}
default UserEntity removeUserFromSchool(AdminRemoveUserFromSchoolModel userSchoolModel) {
throw new NotImplementedException();
}
default SubjectEntity addSubject(SubjectAddModel subjectData) {
throw new NotImplementedException();
}
default SubjectEntity updateSubject(SubjectUpdateModel newSubjectModel) {
throw new NotImplementedException();
}
default SubjectEntity deleteSubject(SubjectDeleteModel subjectDeleteData) {
throw new NotImplementedException();
}
default TimetableEntity addTimetable(TimetableAddModel timetableData) {
throw new NotImplementedException();
}
default TimetableEntity updateTimetable(TimeTableUpdateModel newTimetableData) {
throw new NotImplementedException();
}
default TimetableEntity deleteTimetable(long id) {
throw new NotImplementedException();
}
default ClassEntity addTimetableToClass(TimetableClassModel timetableClass) {
throw new NotImplementedException();
}
default ClassEntity deleteTimetableFromClass(long classId) {
throw new NotImplementedException();
}
}
uj5u.com熱心網友回復:
如果你使用 maven,你應該在插件中添加 pom.xml:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
</path>
</configuration>
</plugin>
</plugins>
</build>
你使用了@Getter 和@Setter,這意味著你已經有了依賴
您需要插件來重新生成帶有 lombok 注釋的類,以創建 getter、setter、contructors 等。
uj5u.com熱心網友回復:
好吧,我不知道為什么會這樣,但是在重新安裝 Lombok 插件后,我重建了我的專案,現在一切正常。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/517458.html
標籤:爪哇春天例外
