博客
关于我
Spring(十三)之SQL存储过程
阅读量:469 次
发布时间: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/

你可能感兴趣的文章
MariaDB的简单使用
查看>>
MaterialForm对tab页进行隐藏
查看>>
Member var and Static var.
查看>>
memcached高速缓存学习笔记001---memcached介绍和安装以及基本使用
查看>>
memcached高速缓存学习笔记003---利用JAVA程序操作memcached crud操作
查看>>
Memcached:Node.js 高性能缓存解决方案
查看>>
memcache、redis原理对比
查看>>
memset初始化高维数组为-1/0
查看>>
Metasploit CGI网关接口渗透测试实战
查看>>
Metasploit Web服务器渗透测试实战
查看>>
MFC模态对话框和非模态对话框
查看>>
Moment.js常见用法总结
查看>>
MongoDB出现Error parsing command line: unrecognised option ‘--fork‘ 的解决方法
查看>>
mxGraph改变图形大小重置overlay位置
查看>>
MongoDB可视化客户端管理工具之NoSQLbooster4mongo
查看>>
Mongodb学习总结(1)——常用NoSql数据库比较
查看>>
MongoDB学习笔记(8)--索引及优化索引
查看>>
mongodb定时备份数据库
查看>>
mppt算法详解-ChatGPT4o作答
查看>>
mpvue的使用(一)必要的开发环境
查看>>