【发布时间】:2025-11-23 14:25:01
【问题描述】:
我已将下面的 id 硬编码为 1,但我需要从 EMPLOYEE 数据库的 employeedetails 表中获取 id
int id=1; //This id needs to come from table from column id
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/home").setViewName("home");
registry.addRedirectViewController("/", "/applyleave/"+ id);
}
@Autowired
public DataSource dataSource() {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/EMPLOYEE");
dataSource.setUsername("root");
dataSource.setPassword("password");
return dataSource;
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth
.jdbcAuthentication().dataSource(dataSource()).usersByUsernameQuery("select user_id, password, loginstatus from employeedetails where user_id=? ").authoritiesByUsernameQuery("select user_id, role from employeedetails where user_id=? ");
}
如何通过 Spring Boot Java 配置实现这一点?
【问题讨论】:
-
在employee中写一个查询方法。这将找出员工表中的所有条目。然后在上面的代码中调用该方法。然后将您的 id 设置为 employee.getid() 并将其传递到您想要的任何地方
-
我在 DAO 中有这个代码,但是我不能从主 java 文件调用这个方法:我如何在上面的 java 代码
public Integer getidforuserId(String userId) { return (Integer) entityManager.createQuery("select id from Employee where userId like :userId ").setParameter("userId", userId).getSingleResult(); }调用这个方法 -
我没有正确获取您的代码,但如果您愿意,我可以分享我关于如何使用不同表中的此类 id 的想法
-
您能帮我从数据库中获取员工 ID 并分配给变量
标签: mysql spring-security spring-boot spring-jdbc