使用Spring org.springframework.jdbc.core.simple.SimpleJdbcInsert获得table的元数据的时候,报错:
org.springframework.dao.InvalidDataAccessApiUsageException: Configuration can't be altered once the class has been compiled or used
找了资料,是这么说的:
写道
compiled means you have instantiated a simpleJdbcInsert instance and set up data source and table name already. Once an simpleJdbcInsert instance is compiled, you should not re-config it again; for instance, set another table name. Create a new simpleJdbcInsert instace if you need to do so.
也就意味着,不能再spring里面像使用JdbcTemplate一样在一个spring上下文中使用一个单例。要想多次重复使用SimpleJdbcInsert,就需要每次都创建一个SimpleJdbcInsert的实例。也就是为一个数据源下面的一张表要单独创建一个SimpleJdbcInsert。
我将代码修改了,每次调用方法的时候,如下:
SimpleJdbcInsert simpleJdbcInsert = new SimpleJdbcInsert(jdbcTemplate);
就可以解决这个问题。