使用 spring boot 从 mysql table table 中获取 id 并与 url 连接

Take id from mysql table table with springboot and concatenate with url

我已将下面的 ID 硬编码为 1,但我需要从 EMPLOYEE 数据库的雇员详细信息 table 中获取 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 引导 java 配置实现此目的?

无论我从你的问题中了解到什么,我都有一个解决方案 在 EmployeeDAO
中写一个你的员工查询 这里的 Employee 是所有 getter setter 方法的实体 class

public Employee getByUserId(Integer userId) {

//return your query for userId here

}

然后在您的用户 class 中调用此 class,您必须在其中使用员工 ID

@Autowired
EmployeeDAO employeeDAO;

public void addViewControllers(ViewControllerRegistry registry) {

  Employee employee = employeeDAO.getByUserId(//pass your user id here)
   id = employee.getId();

   registry.addViewController("/home").setViewName("home");
   registry.addRedirectViewController("/", "/applyleave/"+ id);
}

这样就可以在这里使用employeeId了

这个方法是控制器解决的问题。

@RequestMapping(value = "/", method = RequestMethod.GET)
public String getid() {
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        String userid = auth.getName();
        int id=employeeDaoImpl.getIdByUserId(userid);
        return "redirect:/applyleave/"+id;
    }

谢谢大家!!!