我已经进行了大量研究,以找出使用Spring JDBC框架返回结果集的方法。我是Spring的新手,我正在开发一个实现,该实现调用使用多个输入参数并返回ref游标的存储函数,但是我似乎无法找出执行此操作的正确方法。我现在拥有的代码不断返回一个空数组。有人可以建议我什么是最好的处理方式吗?
以下是我的一些代码段:
public class StandardRodsDAOImplementation implements StandardRodsDAO {
@Autowired
private JdbcTemplate jdbcTemplate;
private final String schema_name = "xxlt";
private final String procedure_catalog = "xxlt_bpg_std_width_pkg";
private final String procedure_name = "get_std_rods";
public List<StandardRods> getDefaultRodMatAndColor(String series, String style, String material, String color)
{
SimpleJdbcCall simpleJdbcCall = new SimpleJdbcCall(jdbcTemplate).withSchemaName(schema_name).withCatalogName(procedure_catalog).withFunctionName(procedure_name)
.declareParameters(new SqlOutParameter("lref", OracleTypes.CURSOR,
new StandardRodsMapper()),
new SqlParameter("p_series_ind", Types.VARCHAR),
new SqlParameter("p_style_ind", Types.VARCHAR),
new SqlParameter("p_material_ind", Types.VARCHAR),
new SqlParameter("p_color_ind", Types.VARCHAR));
simpleJdbcCall.compile();
Map<String, Object> inParams = new HashMap<>();
inParams.put("p_series_ind", Types.VARCHAR);
inParams.put("p_style_ind", Types.VARCHAR);
inParams.put("p_material_ind", Types.VARCHAR);
inParams.put("p_color_ind", Types.VARCHAR);
Map output = simpleJdbcCall.execute(inParams);
return (List) output.get("lref"); }
映射器功能:
public class StandardRodsMapper implements RowMapper<StandardRods> {
public StandardRods mapRow(ResultSet rs, int rowNum) throws SQLException
{
StandardRods standardRods = new StandardRods();
standardRods.setSeries(rs.getString("SERIES_IND"));
standardRods.setStyle(rs.getString("BELT_STYLE_IND"));
standardRods.setMaterial(rs.getString("MATERIAL_IND"));
standardRods.setColor(rs.getString("COLOR_IND"));
return standardRods;
}}
接口:
public interface StandardRodsDAO {
List<StandardRods> getDefaultRodMatAndColor(String series, String style, String material, String color);}
Junit测试:
@Configuration
@ComponentScan
public class SpringDemoTestingApplication
{
StandardRodsDAOImplementation standardRodsDAOImplementation;
@Before
public void setUp()
{
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
"spring-call-proc-simplejdbccall.xml");
standardRodsDAOImplementation = (StandardRodsDAOImplementation) applicationContext.getBean("standardRodsDao");
}
@Test
public void testGetStandardRod()
{
List<StandardRods> standardRods = standardRodsDAOImplementation.getDefaultRodMatAndColor("550", "TIGHT TRANSFER FLAT TOP", "NYLON", "DARK BROWN");
System.out.println(standardRods);
}
程序:
FUNCTION get_std_rods (p_series_ind IN varchar2, p_style_ind IN varchar2, p_material_ind IN varchar2, p_color_ind IN varchar2)
return SYS_REFCURSOR IS lref SYS_REFCURSOR;
BEGIN
OPEN lref FOR
SELECT bbv.default_rod_mat, bbv.default_rod_color
FROM xxlt.xxlt_bpg_belt_val bbv
WHERE bbv.series_ind = p_series_ind AND
bbv.belt_style_ind = p_style_ind AND
bbv.material_ind = p_material_ind AND
bbv.color_ind = p_color_ind;
RETURN lref;
END;