spring-boot - 我想通过电子邮件、年份和学期(学期)从数据库中检索数据
问题描述
我想检索当前用户在出勤表中通过他的电子邮件搜索的出勤情况。接下来,我必须确切地知道我想要检索哪一年和哪一学期的出勤率,所以我尝试通过在控制器中输入“日期”作为参数来执行此操作,但它没有帮助。它给了我下一个
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
我也试过没有参数,有我的代码:
考勤控制器.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);
}
出勤存储库.java
public interface AttendanceRepository extends JpaRepository<Attendance, Integer> {
List<Attendance> findAttendanceByEmail(String email, String year_and_term);
}
出勤服务.java
@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);
}
出勤实体.java
@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中有我的表出勤:
+----+------------+--------+--------+--------+-----------+---------------+
| 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) |
+----+------------+--------+--------+--------+-----------+---------------+
解决方案
实际上,您在这里有两个问题,第一个是您使用 spring jpa 关键字创建了变量名称,即变量名称 year_and_term 中的“和”,并且您将两个不同的值相互组合在一起,将来您可能需要同时查询两者year 和 sem 分开,那么你会遇到麻烦。
所以现在你有两个解决方案: -
如果您仍然想使用clubbed,请重构变量名称以喜欢 yearNterm 即只需将 and 替换为 N 或您想要的东西。那么您的查询将类似于:-
List findByEmailAndYearNterm(String email, String year_and_term);
或者您可以按照我的方法进行,即将 2 它与 2 个变量分开,例如 1 是年份,2 是术语,那么您的查询将如下所示:-
List findByEmailAndYearAndTerm(String email, String year, String term);
推荐阅读
- python - 烧瓶容器异常行为
- sql - R:如何在 R 中使用“NOT IN”逻辑
- google-apps-script - 将文档中的占位符文本替换为工作表单元格中的文本块作为项目符号列表
- javascript - 导出变量时出现错误。有人可以帮我吗?
- java - 通过 WinAPI 提取光标大小 (Windows 10)
- hybris - 在集群环境中,如果 items.xml 发生变化,是否可以在一个节点中更新运行系统并清除其他节点的缓存?
- javascript - 如何仅添加一个特定页面的链接?
- javascript - JSON 属性具有动态名称
- v4l2loopback - OBS Studio 中的 v4l2loopback“格式不支持”错误
- r - 如何从文件夹中导入多个 csv 文件并将它们分类为不同的数据框