2016-04-22 166 views
0

时,我已经通过下面的Spring教程调用存储过程

http://www.tutorialspoint.com/spring/calling_stored_procedure.htm

一切工作我唯一的问题接受消息无法找到相应的参数是,我收到以下消息。

Apr 22, 2016 3:50:51 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh 
INFO: Refreshing org[email protected]5910e440: startup date [Fri Apr 22 15:50:51 MDT 2016]; root of context hierarchy 
Apr 22, 2016 3:50:51 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 
INFO: Loading XML bean definitions from class path resource [Beans.xml] 
Apr 22, 2016 3:50:51 PM org.springframework.jdbc.datasource.DriverManagerDataSource setDriverClassName 
INFO: Loaded JDBC driver: net.sourceforge.jtds.jdbc.Driver 
----Listing Record with ID = 2 ----- 
Apr 22, 2016 3:50:52 PM org.springframework.jdbc.core.metadata.CallMetaDataContext matchInParameterValuesWithCallParameters 
WARNING: Unable to locate the corresponding parameter value for 'outName' within the parameter values provided: [inID] 
Apr 22, 2016 3:50:52 PM org.springframework.jdbc.core.metadata.CallMetaDataContext matchInParameterValuesWithCallParameters 
WARNING: Unable to locate the corresponding parameter value for 'outAge' within the parameter values provided: [inID] 
ID : 3, Name : Ayan, Age : 15 

唯一的区别是我使用MSSQL。正如你可以看到结果返回。我甚至通过了一个不同的参数,值改变,所以我知道它的工作。我只是不确定为什么我收到这些消息。我已验证参数名称与该类中的内容相匹配。

package com.tutorialspoint; 
 
import java.util.List; 
 
import org.springframework.context.ApplicationContext; 
 
import org.springframework.context.support.ClassPathXmlApplicationContext; 
 
import com.tutorialspoint.StudentJDBCTemplate; 
 

 
public class MainApp { 
 
    public static void main(String[] args) { 
 
     ApplicationContext context = 
 
      new ClassPathXmlApplicationContext("Beans.xml"); 
 
     
 
     
 
     StudentJDBCTemplate studentJDBCTemplate = 
 
     (StudentJDBCTemplate)context.getBean("studentJDBCTemplate"); 
 
     /* 
 
     System.out.println("------Records Creation--------"); 
 
     studentJDBCTemplate.create("Zara", 11); 
 
     studentJDBCTemplate.create("Nuha", 2); 
 
     studentJDBCTemplate.create("Ayan", 15); 
 

 
     System.out.println("------Listing Multiple Records--------"); 
 
     List<Student> students = studentJDBCTemplate.listStudents(); 
 
     for (Student record : students) { 
 
     System.out.print("ID : " + record.getId()); 
 
     System.out.print(", Name : " + record.getName()); 
 
     System.out.println(", Age : " + record.getAge()); 
 
     } 
 
\t */ 
 
     
 
     System.out.println("----Listing Record with ID = 2 -----"); 
 
     
 
     Student student = studentJDBCTemplate.getStudent(3); 
 
     System.out.print("ID : " + student.getId()); 
 
     System.out.print(", Name : " + student.getName()); 
 
     System.out.println(", Age : " + student.getAge()); 
 
\t 
 
    } 
 
}

package com.tutorialspoint; 
 

 
import java.sql.ResultSet; 
 
import java.sql.SQLException; 
 
import org.springframework.jdbc.core.RowMapper; 
 

 
public class StudentMapper implements RowMapper<Student> { 
 
    public Student mapRow(ResultSet rs, int rowNum) throws SQLException { 
 
     Student student = new Student(); 
 
     student.setId(rs.getInt("id")); 
 
     student.setName(rs.getString("name")); 
 
     student.setAge(rs.getInt("age")); 
 
     return student; 
 
    } 
 
}

package com.tutorialspoint; 
 

 
import java.util.Map; 
 
import java.util.List; 
 

 
import javax.sql.DataSource; 
 
import org.springframework.jdbc.core.JdbcTemplate; 
 
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource; 
 
import org.springframework.jdbc.core.namedparam.SqlParameterSource; 
 
import org.springframework.jdbc.core.simple.SimpleJdbcCall; 
 

 
public class StudentJDBCTemplate implements StudentDAO { 
 
\t 
 
\t private DataSource dataSource; 
 
\t private SimpleJdbcCall jdbcCall; 
 
\t 
 
\t public void setDataSource(DataSource dataSource){ \t 
 
\t \t this.dataSource = dataSource; 
 
\t \t this.jdbcCall = new SimpleJdbcCall(dataSource).withProcedureName("getRecord"); 
 
\t } 
 
\t 
 
\t public void create(String name, Integer age){ 
 
\t \t JdbcTemplate jdbcTemplateObject = new JdbcTemplate(dataSource); 
 
\t \t String SQL = "INSERT INTO Student(name,age) VALUES (?,?)"; 
 
\t \t jdbcTemplateObject.update(SQL,name,age); 
 
\t \t System.out.println("Created Record Name = " + name + " Age = " + age); 
 
\t \t return; 
 
\t } 
 
\t 
 
\t public Student getStudent(Integer id){ 
 
\t \t 
 
\t \t SqlParameterSource in = new MapSqlParameterSource().addValue("inID", id); 
 
\t \t Map<String, Object> out = jdbcCall.execute(in); 
 
\t \t 
 
\t \t Student student = new Student(); 
 
\t \t student.setId(id); 
 
\t \t student.setName((String) out.get("outName")); 
 
\t \t student.setAge((Integer) out.get("outAge")); 
 

 
\t \t return student; 
 
\t } 
 
\t 
 
\t public List<Student> listStudents(){ 
 
\t \t String SQL = "SELECT * FROM Student"; 
 
\t \t JdbcTemplate jdbcTemplateObject = new JdbcTemplate(dataSource); 
 
\t \t List <Student> students = jdbcTemplateObject.query(SQL, \t new StudentMapper()); \t \t 
 
\t \t return students; 
 
\t } 
 

 
}

package com.tutorialspoint; 
 

 
public class Student { 
 
    private Integer age; 
 
    private String name; 
 
    private Integer id; 
 

 
    public void setAge(Integer age) { 
 
     this.age = age; 
 
    } 
 
    public Integer getAge() { 
 
     return age; 
 
    } 
 

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

 
    public void setId(Integer id) { 
 
     this.id = id; 
 
    } 
 
    public Integer getId() { 
 
     return id; 
 
    } 
 
}

+0

请包括您在问题中使用的代码。 –

+0

您还可以将存储过程签名添加到您的问题中吗? – Igor

回答

0

尝试宣告在这个例子中使用SqlOutParameter等的输出参数,也许是警告将消失:

SimpleJdbcCall caller = new SimpleJdbcCall(getDataSource()) 
.withCatalogName("PKG_REG_REVIEW") 
.withFunctionName("fn_get_task_complete_date") 
.withoutProcedureColumnMetaDataAccess() 
.declareParameters(
new SqlOutParameter("v_maxdate", Types.DATE), 
new SqlParameter("p_case_seq", Types.INTEGER), 
new SqlParameter("p_task_seq", Types.INTEGER) 
); 
1

它为我添加参数声明过,但略有不同:

public Student getStudent(Integer id) { 
    SqlParameterSource in = new MapSqlParameterSource().addValue("id", id, Types.INTEGER) 
      .addValue("name",Types.VARCHAR) 
      .addValue("age", Types.INTEGER); 
    Map<String, Object> out = jdbcCall.execute(in); 

    Student student = new Student(); 
    student.setId(id); 
    student.setName((String) out.get("name")); 
    student.setAge((Integer) out.get("age")); 

    return student; 
} 

请注意我使用MS SQL Server而不是MySQL;并创建了程序略有不同:

CREATE PROCEDURE getRecord 
    @id INT, 
    @name VARCHAR(20) OUTPUT , 
    @age INT OUTPUT 
AS 
    SELECT @name = name, @age = AGE 
    FROM Student where id = @id 

但它应该是非常相似。