Spring boot 我想通过电子邮件从数据库中检索数据,年份和学期(学期)
我想通过在考勤表中搜索当前用户的电子邮件来检索其考勤。下一步,我必须确定我想要检索哪一年和哪一学期的出勤率,所以我试着在控制器中输入“日期”作为参数,在服务中,但没有帮助。它给了我下一个机会Spring boot 我想通过电子邮件从数据库中检索数据,年份和学期(学期),spring-boot,spring-data-jpa,Spring Boot,Spring Data Jpa,我想通过在考勤表中搜索当前用户的电子邮件来检索其考勤。下一步,我必须确定我想要检索哪一年和哪一学期的出勤率,所以我试着在控制器中输入“日期”作为参数,在服务中,但没有帮助。它给了我下一个机会 Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'attendanceRepository': Invocation of init method failed
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'attendanceRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract java.util.List com.example.demo.Repositories.AttendanceRepository.findAttendanceByEmail(java.lang.String,java.lang.String)! At least 2 parameter(s) provided but only 1 parameter(s) present in query.
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1796) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:595) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:276) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1290) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1210) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:640) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
... 32 common frames omitted
我也尝试过不带参数,下面是我的代码:
AttendanceControl.java
@Autowired
AttendanceService attendanceService;
// Get User Attendance
@RequestMapping(value = "/attendance", method = RequestMethod.GET)
public List<Attendance> getUserAttendance(@AuthenticationPrincipal MyUserDetails myUserDetails, java.util.Date date) {
return attendanceService.getUserAttendanceByEmail(myUserDetails.getEmail(), date);
}
public interface AttendanceRepository extends JpaRepository<Attendance, Integer> {
List<Attendance> findAttendanceByEmail(String email, String year_and_term);
}
@Autowired
AttendanceRepository attendanceRepository;
// Finds in attendance table by email of the current user
public List<Attendance> getUserAttendanceByEmail(String email, Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int year = cal.get(Calendar.YEAR);
int term = cal.get(Calendar.MONTH);
if (term >= 1 && term <= 5)
term = 1;
else if (term >= 9 && term <= 12)
term = 2;
String year_and_term = year + "-" + (year+1) + " (" + term + ")";
System.out.println(year_and_term);
return attendanceRepository.findAttendanceByEmail(email, year_and_term);
}
@Entity
@Table(name = "attendance")
public class Attendance {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
@ManyToOne
@JoinColumn(name="subject_id", nullable=false)
private Subjects subjects;
private String email;
private Integer absent;
private Integer attend;
private Integer permitted;
private String year_and_term;
public Attendance(Integer id, Subjects subjects, String email, Integer absent, Integer attend, Integer permitted, String year_and_term) {
this.id = id;
this.subjects = subjects;
this.email = email;
this.absent = absent;
this.attend = attend;
this.permitted = permitted;
this.year_and_term = year_and_term;
}
public Attendance() {
}
// getters and setters..
在db中有我的表出席人数:
实际上,这里有两个问题,第一个问题是,在变量名year_和_术语中,变量名带有一个spring jpa关键字,即“and”,并且您将两个不同的值相互关联,可能将来您需要分别查询year和sem,那么您将遇到麻烦 现在有两种解决方案:-
实际上,这里有两个问题,第一个问题是,在变量名year_和_术语中,变量名带有一个spring jpa关键字,即“and”,并且您将两个不同的值相互关联,可能将来您需要分别查询year和sem,那么您将遇到麻烦 现在有两种解决方案:-
您希望基于两个字段获取数据,但只传递一个字段。因此,要使其工作,请进行以下更改
您希望基于两个字段获取数据,但只传递一个字段。因此,要使其工作,请进行以下更改
你能分享你的实体类吗?您不能像这样使用namedQuery FindAtEndanceByEmail(电子邮件、年份和术语)。使用FindAllAttendanceByEmail和YearAndTerm(电子邮件、year_和_term)我提供的实体。我将其重命名为gives“起因:org.springframework.data.mapping.PropertyReferenceException:找不到类型Attention的属性年份!”是的,这将给出一个错误作为编译器know关键字,因此从技术上讲,在和之前和之后应该有两个不同的变量,一个变量中有一个变量,请按照我下面的答案。你能分享你的实体类吗?您不能像这样使用namedQuery FindAtEndanceByEmail(电子邮件、年份和术语)。使用FindAllAttendanceByEmail和YearAndTerm(电子邮件、year_和_term)我提供的实体。我将其重命名为gives“起因:org.springframework.data.mapping.PropertyReferenceException:找不到类型Attention的属性年份!”是的,这将给出一个错误作为编译器know关键字,因此从技术上讲,在和之前和之后应该有两个不同的变量,一个变量中有一个变量,请按照我下面的回答。这将使派生查询异常为'And'is关键字,因此'term'变量将被视为查询的第三个变量。在这种情况下,他应该重命名字段。是的,他应该从变量名称中删除And,或者更合适的方法是将年份和term分解为两个变量,他将来肯定需要它。这将使派生查询异常为'And'is关键字,因此'term'变量将被视为查询的第三个变量。在这种情况下,他应该重命名字段。是的,他应该从变量名称中删除And,或者更合适的方法是将年份和term分解为两个变量,他将来肯定需要它。
+----+------------+--------+--------+--------+-----------+---------------+
| id | subject_id | email | absent | attend | permitted | year_and_term |
+----+------------+--------+--------+--------+-----------+---------------+
| 1 | 1 | damir | 1 | 1 | 1 | 2019-2020 (2) |
| 2 | 2 | damir | 0 | 5 | 2 | 2019-2020 (2) |
| 3 | 3 | rapkat | 0 | 10 | 0 | 2019-2020 (2) |
| 4 | 1 | damir | 10 | 20 | 5 | 2019-2020 (1) |
| 5 | 2 | damir | 2 | 20 | 0 | 2019-2020 (1) |
+----+------------+--------+--------+--------+-----------+---------------+