无法在spring MVC中为变量设置值

时间:2017-07-19 04:06:58

标签: java spring-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;
    }   

}

但是对于身份和工资&#34;薪水无法解决为变量&#34;。 这有什么问题..请帮帮我......

抱歉我的英语很差。

3 个答案:

答案 0 :(得分: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;

答案 1 :(得分:2)

mapRow()中没有任何id或薪金参数。您必须从ResultSet对象中获取它。

所以,不要这样做:

employee.setId(id); //or (1)
employee.setSalary(salary); //or (3)

尝试:

employee.setId(rs.getInt("id")); //or (1)
employee.setSalary(rs.getInt("salary")); //or (3)

答案 2 :(得分:0)

编译器无法解析该范围内的符号idsalary。简单来说,当您尝试访问idsalary的内存位置时,编译器不知道您的意思。

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

有关详细信息,请访问:https://docs.oracle.com/javase/7/docs/api/java/sql/ResultSet.html

Sajal Gupta有正确的想法