Spring 中 SQL 的存储过程
在运行MainApp时报错:
--------记录列表--------
ID:1,Name黑熊,Age11
ID:2,Name加菲猫,Age13
ID:3,Name阿丽,Age15
--------ID为2的记录信息--------
Exception in thread "main" org.springframework.dao.InvalidDataAccessApiUsageException:
Unable to determine the correct call signature - no procedure/function/signature for 'getrecord'
at org.springframework.jdbc.core.metadata.GenericCallMetaDataProvider.processProcedureColumns(GenericCallMetaDataProvider.java:347)
at org.springframework.jdbc.core.metadata.GenericCallMetaDataProvider.initializeWithProcedureColumnMetaData(GenericCallMetaDataProvider.java:112)
at org.springframework.jdbc.core.metadata.CallMetaDataProviderFactory$1.processMetaData(CallMetaDataProviderFactory.java:133)
at org.springframework.jdbc.support.JdbcUtils.extractDatabaseMetaData(JdbcUtils.java:336)
at org.springframework.jdbc.core.metadata.CallMetaDataProviderFactory.createMetaDataProvider(CallMetaDataProviderFactory.java:73)
at org.springframework.jdbc.core.metadata.CallMetaDataContext.initializeMetaData(CallMetaDataContext.java:242)
at org.springframework.jdbc.core.simple.AbstractJdbcCall.compileInternal(AbstractJdbcCall.java:303)
at org.springframework.jdbc.core.simple.AbstractJdbcCall.compile(AbstractJdbcCall.java:288)
at org.springframework.jdbc.core.simple.AbstractJdbcCall.checkCompiled(AbstractJdbcCall.java:348)
at org.springframework.jdbc.core.simple.AbstractJdbcCall.doExecute(AbstractJdbcCall.java:363)
at org.springframework.jdbc.core.simple.SimpleJdbcCall.execute(SimpleJdbcCall.java:198)
at com.joey.JdbcCall.StudentJDBCTemplate.getStudent(StudentJDBCTemplate.java:37)
at com.joey.JdbcCall.MainApp.main(MainApp.java:27)
MainApp.java
package com.joey.JdbcCall;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("jdbcCallBeans.xml");
StudentJDBCTemplate studentJDBCTemplate = (StudentJDBCTemplate) context.getBean("studentJDBCTemplate");
/*System.out.println("--------添加记录--------");
studentJDBCTemplate.create("黑熊", 11);
studentJDBCTemplate.create("加菲猫", 13);
studentJDBCTemplate.create("阿丽", 15);*/
System.out.println("--------记录列表--------");
List<Student> students=studentJDBCTemplate.listStudent();
for (Student student : students) {
System.out.print("ID:"+student.getId());
System.out.print(",Name"+student.getName());
System.out.println(",Age"+student.getAge());
}
System.out.println("--------ID为2的记录信息--------");
Student student=studentJDBCTemplate.getStudent(2);
System.out.print("ID:"+student.getId());
System.out.print(",Name"+student.getName());
System.out.println(",Age"+student.getAge());
}
}
StudentJDBCTemplate.java
package com.joey.JdbcCall;
import java.util.List;
import java.util.Map;
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{
private DataSource dataSource;
private SimpleJdbcCall jdbcCall;
@Override
public void setDataSource(DataSource dataSource) {
this.dataSource=dataSource;
this.jdbcCall=new SimpleJdbcCall(dataSource)
.withProcedureName("getRecord");
}
@Override
public void create(String name, Integer age) {
JdbcTemplate jdbcTemplateObject=new JdbcTemplate(dataSource);
String SQL="insert into Student(name,age) values(?,?)";
jdbcTemplateObject.update(SQL,name,age);
System.out.println("Created Record Name = "+name+" Age = "+age);
return;
}
@Override
public Student getStudent(Integer id) {
SqlParameterSource in=new MapSqlParameterSource().addValue("in_id", id);
Map<String, Object>out=jdbcCall.execute(in);
Student student = new Student();
student.setId(id);
student.setName((String) out.get("out_name"));
student.setAge((Integer) out.get("out_age"));
return student;
}
@Override
public List<Student> listStudent() {
JdbcTemplate jdbcTemplateObject=new JdbcTemplate(dataSource);
String SQL="select * from Student";
List<Student> students=jdbcTemplateObject.query(SQL,new StudentMapper());
return students;
}
}
完~