2016-02-26 43 views
0

我已经硬编码了下面的ID为1串连,但我需要从EMPLOYEE数据库的employeedetails表中提取ID以ID从MySQL表表springboot与URL

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=? "); 
} 

我怎样才能做到这一点与春季启动java配置?

+0

在employeee中写入一个查询方法。其中将查找员工表中的所有条目。然后在上面的代码中调用该方法。然后设置为employee.getid(ID),并通过它在任何你想要 –

+0

我在DAO这个代码,但我无法从主要的Java文件调用此方法:我如何可以调用上面的Java代码 这种方法'公Integer getidforuserId(String userId){ \t \t return(Integer)entityManager.createQuery(“从雇员中选择id,其中userId是:userId”).setParameter(“userId”,userId).getSingleResult(); \t}' – Vikram

+0

我无法正常得到你的代码,但如果你想那么我可以分享我如何使用这样的ID从不同的表 –

回答

0

无论我从你的问题了解我有一个解决方案 在写一个你的员工查询EmployeeDAO
这里的员工对所有的getter setter方法实体类

public Employee getByUserId(Integer userId) { 

//return your query for userId here 

} 

然后调用这个类在你的用户类别,您必须使用员工编号

@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。

+0

我EmployeeDAOImpl '公共整数getidforuserId(字符串userid){ \t \t回报(整数)entityManager.createQuery( “选择employee ID其中UserID,如:用户id ”).setParameter(“ 用户id”,用户id).getSingleResult() ; \t}' 我的类代码: '@Autowired \t私有静态EmployeeDaoImpl employeedaoimpl;' '公共无效addViewControllers(ViewControllerRegistry注册表){ 雇员雇员= employeedaoimpl.getByUserID( “[email protected]” ); \t \t ID = employee.getId();' 但我得到以下异常: – Vikram

+0

'公务员getidforuserId(字符串userid){...}'声明所有的getter setter方法在'Employee'类,让你可以用另一种方法 –

+0

我的getter setter方法中已经Employee类使用它的价值,但我得到一个例外,每当我用 '员工= employeedaoimpl.getByUserID(“[email protected]”); ID = employee.getId();' – Vikram

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

谢谢你们!