博客
关于我
Spring(十三)之SQL存储过程
阅读量:458 次
发布时间:2019-03-06

本文共 5396 字,大约阅读时间需要 17 分钟。

SimpleJdbcCall 类可以被用于调用一个包含 IN 和 OUT 参数的存储过程。你可以在处理任何一个 RDBMS 时使用这个方法,就像 Apache Derby, DB2, MySQL, Microsoft SQL Server, Oracle,和 Sybase。

 

还是基于Spring之JDBC框架这个例子继续

 

(1)基于Test库创建对应的存储过程

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 ;

 

(2)创建实体

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;   }}

 

(3)编写StudentDAO

package com.tutorialspoint;import java.util.List;import javax.sql.DataSource;public interface StudentDAO {   /**     * This is the method to be used to initialize    * database resources ie. connection.    */   public void setDataSource(DataSource ds);   /**     * This is the method to be used to create    * a record in the Student table.    */   public void create(String name, Integer age);   /**     * This is the method to be used to list down    * a record from the Student table corresponding    * to a passed student id.    */   public Student getStudent(Integer id);   /**     * This is the method to be used to list down    * all the records from the Student table.    */   public List
listStudents();}

 

(4)编写StudentMapper.java

package com.tutorialspoint;import java.sql.ResultSet;import java.sql.SQLException;import org.springframework.jdbc.core.RowMapper;public class StudentMapper implements RowMapper
{ 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; }}

 

(5)编写StudentJDBCTemplate .java

package com.tutorialspoint;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;   public void setDataSource(DataSource dataSource) {      this.dataSource = dataSource;      this.jdbcCall =  new SimpleJdbcCall(dataSource).                       withProcedureName("getRecord");   }   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;   }   public Student getStudent(Integer id) {      SqlParameterSource in = new MapSqlParameterSource().                              addValue("in_id", id);      Map
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; } public List
listStudents() { JdbcTemplate jdbcTemplateObject = new JdbcTemplate(dataSource); String SQL = "select * from Student"; List
students = jdbcTemplateObject.query(SQL, new StudentMapper()); return students; }}

 

(6)编写对应的MainApp.java

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
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()); } 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()); }}

 

(7)编写Beans.xml

 

(8)运行MainApp.java中的main方法

结果如图:

 

转载地址:http://moobz.baihongyu.com/

你可能感兴趣的文章
maven仓库设置,windows和mac
查看>>
Android错误收集
查看>>
浙大机器学习课程-8-支持向量机(原问题转化为对偶问题)
查看>>
css3之路- 结构性伪类
查看>>
239_自定义View画圆环环形
查看>>
流媒体音视频服务云管理平台EasyNVS平台中视频播放页面出现错误码的问题解决
查看>>
渗透测试学习笔记之案例五
查看>>
Pentest Wiki Part4 后渗透(二)
查看>>
tomcat配置虚拟目录后出现404的解决方法
查看>>
/usr/bin/ld: cannot find -ltcmalloc 错误的解决方法
查看>>
【python】protobuf 使用学习记录
查看>>
php许愿墙
查看>>
攻防世界 secret-galaxy-300
查看>>
两张图帮你更好理解git常用指令
查看>>
【Lintcode】452. Remove Linked List Elements
查看>>
IDEA中JavaWeb项目成功部署运行,但在浏览器访问时依然报404错误
查看>>
视频课程:CMOS模拟集成电路设计--已上线
查看>>
砂原良徳创作的“日本媒体艺术分散式博物馆”主题曲《Nihon no Sugata》在官网发布
查看>>
实习复习--mysql视图17
查看>>
数据结构与算法【Java版】:第一课
查看>>