以下内容引用自:
SimpleJdbcCall类可以被用于调用一个包含IN和OUT参数的存储过程。你可以在处理任何一个RDBMS时使用这个方法,支持数据库有Apache Derby,DB2,MySQL,Microsoft SQL Server,Oracle,和Sybase等等。
实践前提:
1、创建数据库,脚本如下所示:
---- 数据库: `test`---- ------------------------------------------------------------ 表的结构 `student`--CREATE TABLE IF NOT EXISTS `student` ( `ID` int(11) NOT NULL AUTO_INCREMENT, `NAME` varchar(20) NOT NULL, `AGE` int(11) NOT NULL, PRIMARY KEY (`ID`)) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=10 ;---- 转存表中的数据 `student`--INSERT INTO `student` (`ID`, `NAME`, `AGE`) VALUES(1, 'Zara', 11),(2, 'Nuha', 2),(3, 'Ayan', 15);
2、创建存储过程,实现获取单个学生信息,脚本如下所示:
DELIMITER $$DROP PROCEDURE IF EXISTS `TEST`.`getRecord` $$CREATE PROCEDURE `TEST`.`getRecord` (IN in_id INTEGER,OUT out_name VARCHAR(20),OUT out_age INTEGER)BEGIN SELECT name, age INTO out_name, out_age FROM Student where id = in_id;END $$DELIMITER ;
3、由于使用的是MySql数据库,需要引入mysql-connector-java.jar包,POM如下所示:
mysql mysql-connector-java 5.1.38
实践例子:
pom.xml:
4.0.0 com.jsoft.testspring teststoredprocedure 0.0.1-SNAPSHOT jar teststoredprocedure http://maven.apache.org UTF-8 junit junit 3.8.1 test org.springframework spring-core 4.1.4.RELEASE org.springframework spring-context 4.1.4.RELEASE org.springframework spring-jdbc 4.1.4.RELEASE org.springframework spring-tx 4.1.4.RELEASE mysql mysql-connector-java 5.1.38
Student.java:
package com.jsoft.testspring.teststoredprocedure;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; }}
StudentDAO.java:
package com.jsoft.testspring.teststoredprocedure;import java.util.List;import javax.sql.DataSource;public interface StudentDAO { public void setDataSource(DataSource ds); public void create(String name, Integer age); public Student getStudent(Integer id); public ListlistStudents(); public void delete(Integer id); public void update(Integer id, Integer age);}
StudentMapper.java:
package com.jsoft.testspring.teststoredprocedure;import java.sql.ResultSet;import java.sql.SQLException;import org.springframework.jdbc.core.RowMapper;public class StudentMapper implements RowMapper{ @Override public Student mapRow(ResultSet rs, int arg1) throws SQLException { // TODO Auto-generated method stub Student student = new Student(); student.setId(rs.getInt("id")); student.setName(rs.getString("name")); student.setAge(rs.getInt("age")); return student; }}
StudentJDBCTemplate.java:
package com.jsoft.testspring.teststoredprocedure;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 JdbcTemplate jdbcTemplateObject; private SimpleJdbcCall jdbcCall; @Override public void setDataSource(DataSource dataSource) { this.dataSource = dataSource; this.jdbcTemplateObject = new JdbcTemplate(dataSource); this.jdbcCall = new SimpleJdbcCall(dataSource).withProcedureName("getRecord"); } @Override public void create(String name, Integer age) { 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); Mapout = 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 listStudents() { String SQL = "select * from Student"; List students = jdbcTemplateObject.query(SQL, new StudentMapper()); return students; } @Override public void delete(Integer id) { String SQL = "delete from Student where id = ?"; jdbcTemplateObject.update(SQL, id); System.out.println("Deleted Record with ID = " + id); return; } @Override public void update(Integer id, Integer age) { String SQL = "update Student set age = ? where id = ?"; jdbcTemplateObject.update(SQL, age, id); System.out.println("Updated Record with ID = " + id); return; }}
beans.xml:
App.java:
package com.jsoft.testspring.teststoredprocedure;import java.util.List;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;/** * Hello world! * */public class App { 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--------"); Liststudents = studentJDBCTemplate.listStudents(); for (Student record : students) { System.out.print("ID : " + record.getId()); System.out.print(", Name : " + record.getName()); System.out.println(", Age : " + record.getAge()); } System.out.println("----Updating Record with ID = 2 -----"); studentJDBCTemplate.update(2, 20); System.out.println("----Listing Record with 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()); }}
测试结果:
测试工程: