使用springboot从mysql表表中获取id并与url连接

时间:2016-02-26 12:00:02

标签: mysql spring-security spring-boot spring-jdbc

我将下面的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配置实现这一点?

2 个答案:

答案 0 :(得分:0)

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

public Employee getByUserId(Integer userId) {

//return your query for userId here

}

然后在您必须使用员工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。

答案 1 :(得分:0)

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

@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;
    }

谢谢你们!