博客
关于我
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/

你可能感兴趣的文章
MySQL查询优化之索引
查看>>
mysql查询储存过程,函数,触发过程
查看>>
mysql查询总成绩的前3名学生信息
查看>>
mysql查询慢排查
查看>>
MySQL查询报错ERROR:No query specified
查看>>
mysql查询数据库储存数据的占用容量大小
查看>>
MySQL查询数据库所有表名及其注释
查看>>
MySQL查询数据表中数据记录(包括多表查询)
查看>>
MySQL查询结果排序
查看>>
MYSQL查询语句优化
查看>>
mysql查询语句能否让一个字段不显示出来_天天写order by,你知道Mysql底层执行原理吗?
查看>>
MySQL查询语句:揭秘专家秘籍,让你秒变数据库达人!
查看>>
mysql查询超时对PHP执行的影响
查看>>
mysql查询输出到excel文件_如何保存mysql查询输出到excel或.txt文件?
查看>>
mysql查询过程
查看>>
MySQL模拟Oracle序列sequence
查看>>
Mysql模糊查询like效率,以及更高效的写法
查看>>
MySQL死锁套路:一次诡异的批量插入死锁问题分析
查看>>
Mysql死锁问题Deadlock found when trying to get lock;try restarting transaction
查看>>
mysql每个数据库的最大连接数_MySQL数据库最大连接数
查看>>