2016-02-26 42 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; 
    } 

謝謝你們!