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

你可能感兴趣的文章
MySQL5.7.18主从复制搭建(一主一从)
查看>>
MySQL5.7.19-win64安装启动
查看>>
mysql5.7.19安装图解_mysql5.7.19 winx64解压缩版安装配置教程
查看>>
MySQL5.7.37windows解压版的安装使用
查看>>
mysql5.7免费下载地址
查看>>
mysql5.7命令总结
查看>>
mysql5.7安装
查看>>
mysql5.7性能调优my.ini
查看>>
MySQL5.7新增Performance Schema表
查看>>
Mysql5.7深入学习 1.MySQL 5.7 中的新增功能
查看>>
Webpack 之 basic chunk graph
查看>>
Mysql5.7版本单机版my.cnf配置文件
查看>>
mysql5.7的安装和Navicat的安装
查看>>
mysql5.7示例数据库_Linux MySQL5.7多实例数据库配置
查看>>
Mysql8 数据库安装及主从配置 | Spring Cloud 2
查看>>
mysql8 配置文件配置group 问题 sql语句group不能使用报错解决 mysql8.X版本的my.cnf配置文件 my.cnf文件 能够使用的my.cnf配置文件
查看>>
MySQL8.0.29启动报错Different lower_case_table_names settings for server (‘0‘) and data dictionary (‘1‘)
查看>>
MYSQL8.0以上忘记root密码
查看>>
Mysql8.0以上重置初始密码的方法
查看>>
mysql8.0新特性-自增变量的持久化
查看>>