2017-07-19 53 views
0

我在我的应用程序中为表员工创建了一个类。这里是这个类无法在春季MVC中设置值给变量MVC

package com.bct.internal.form.model; 

public class Employee { 
int id; 
String name; 
int salary; 
String designation; 



public String getName() { 
    return name; 
} 

public void setName(String name) { 
    this.name = name; 
} 


public String getDesignation() { 
    return designation; 
} 

public int getId() { 
    return id; 
} 

public void setId(int id) { 
    this.id = id; 
} 

public int getSalary() { 
    return salary; 
} 

public void setSalary(int salary) { 
    this.salary = salary; 
} 

public void setDesignation(String designation) { 
    this.designation = designation; 
} 
@Override 
public String toString() { 
    return "Employee [ID=" + id + ", NAME=" + name 
      + ", SALARY=" + salary + ", DESIGNATION=" + designation + "]"; 
} 

} 

而且我将该列的值传递给控制器​​。

public static final class EmployeeMapper implements RowMapper<Employee> 
    { 

    public Employee mapRow(ResultSet rs, int rowNum) throws SQLException { 
     Employee employee = new Employee(); 
     employee.setId(id); 
     employee.setName(rs.getString("name")); 
     employee.setSalary(salary); 
     employee.setDesignation(rs.getString("designation")); 
     return employee; 
    } 

} 

但是对于id和薪水“工资无法解析为变量”。 这里有什么问题..请帮助我..

对不起,我英文很差。

回答

2

应该

Employee employee = new Employee(); 
    employee.setId(rs.getInt("id")); //or (1) 
    employee.setName(rs.getString("name"));//or (2) 
    employee.setSalary(rs.getInt("salary"));//or (3) 
    employee.setDesignation(rs.getString("designation"));//or (4) 
    return employee; 
+0

感谢ü回答..我用这样那样的错误了。但现在它显示错误,如“错误创建名为'authenticationDaoImpl'的bean:通过字段'id'表示的不满意的依赖项;嵌套的异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:” – Shanmugapriya

+0

您必须将id标记为主键@你的模型类中的注释。 –

0

编译器不能解析符号id和范围内salary。用最简单的术语来说,当您尝试访问内存位置idsalary时,编译器不知道您的意思。

对于您的情况,您必须使用适当的ResultSet getter方法从该特定行中检索列值。

欲了解更多信息,请访问:https://docs.oracle.com/javase/7/docs/api/java/sql/ResultSet.html

Sajal古普塔有正确的想法