Spring MVC Preparedstatementcreator:从内部类访问局部变量对象

时间:2019-01-08 06:11:10

标签: java spring-mvc anonymous-inner-class


我正在尝试使用Customer在这里保存preparedstatementcreator记录。

public Customer saveRecord(Customer customer) {
        int result = 0;

        try {
            KeyHolder keyHolder = new GeneratedKeyHolder();
            result = jdbcTemplate.update(new PreparedStatementCreator() {
                @Override
                public PreparedStatement createPreparedStatement(Connection connection)
                        throws SQLException {
                    PreparedStatement preparedStatement = connection.prepareStatement(SQL_INSERT_CUSTOMER_MASTER_WITH_AUTO_INCREMENT, Statement.RETURN_GENERATED_KEYS);
                    preparedStatement.setString(1, customer.getFirstname());
                    return preparedStatement;
                }
            }, keyHolder);
        } catch (DataAccessException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result > 0 ? customer : null;
    }

这是我的客户对象。

public class Customer implements Serializable{

    private long id;
    private String firstname;
    private String secondname;
    private int age;
    private String address;
    private Country country;
    private String[] language;

    public Customer() {
    }

    public Customer(String firstname, String secondname, int age, String address, Country country, String[] language) {
        this.firstname = firstname;
        this.secondname = secondname;
        this.age = age;
        this.address = address;
        this.country = country;
        this.language = language;
    }

    //getters setters
}

我了解这里的原因是我们无法从createPreparedStatement()内部访问客户对象。


我可以对原始的Customer对象类进行某种修改,以使其在此内部类中可见吗?

0 个答案:

没有答案