Spring JDBC SimpleJdbcCall类示例

org.springframework.jdbc.core.SimpleJdbcCall类是表示对存储过程或存储函数的调用的多线程,可重用的对象。 它提供元数据处理以简化访问基本存储过程/函数所需的代码。 所有需要提供的是程序/函数的名称和包含执行调用时参数的Map对象。 提供的参数的名称将与创建存储过程时声明的输入和输出参数相匹配。

使用到的 Student 表的结构如下 -

CREATE TABLE Student(
   ID   INT NOT NULL AUTO_INCREMENT, NAME VARCHAR(20) NOT NULL, AGE INT NOT NULL, PRIMARY KEY (ID) ); 
SQL

类的声明

以下是org.springframework.jdbc.core.SimpleJdbcCall接口的声明 -

public class SimpleJdbcCall
   extends AbstractJdbcCall implements SimpleJdbcCallOperations 
Java

以下示例将演示如何使用Spring的SimpleJdbcCall对象调用存储过程。通过调用存储过程来读取Student表中的一条记录。传递一个学生编号作为参数并读取学生记录。

语法

SimpleJdbcCall jdbcCall = new SimpleJdbcCall(dataSource).withProcedureName("getRecord"); SqlParameterSource in = new MapSqlParameterSource().addValue("in_id", id); Map<String, Object> 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")); 
Java

在上代码中,

  • jdbcCall - SimpleJdbcCall对象来表示存储过程。
  • in - SqlParameterSource对象将参数传递给存储过程。
  • student - 学生(Student)对象。
  • out - Map对象来表示存储过程调用结果的输出。

实例项目

要了解上述与Spring JDBC相关的概念,下面我们编写一个使用SimpleJdbcCall进行存储过程调用。打开Eclipse IDE,并按照以下步骤创建一个Spring应用程序,这里创建一个名称为:SimpleJdbcCall 的项目。

步骤说明

  1. 参考第一个Spring JDBC项目来创建一个Maven项目 - http://www.yiibai.com/springjdbc/first_application.html
  2. 更新bean配置并运行应用程序。

完整的项目结构如下所示 -

要了解如何调用数据库中的存储过程,请创建以下MySQL存储过程,该过程需要学生ID作为参数,并使用OUT参数返回相应的学生姓名和年龄。所以首先打开MySQL命令提示符,并在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 ; 
SQL

以下是Maven配置文件:pom.xml的代码内容:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.yiibai</groupId> <artifactId>SimpleJdbcCall</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>SimpleJdbcCall</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.40</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.1.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.1.4.RELEASE</version> </dependency> </dependencies> </project> 
XML

以下是数据访问对象接口文件:StudentDAO.java的内容:

package com.yiibai;

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 all the records from the * Student table. */ public List<Student> listStudents(); /** * 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); } 
Java

以下是文件:Student.java的代码内容:

package com.yiibai;

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

以下是文件:StudentMapper.java的代码内容:

package com.yiibai;

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

实现类StudentJDBCTemplate.java实现了定义的DAO接口 - StudentDAO.java,以下是文件:StudentJDBCTemplate.java的代码内容:

package com.yiibai;

import java.util.HashMap; 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; import org.springframework.jdbc.core.simple.SimpleJdbcInsert; public class StudentJDBCTemplate implements StudentDAO { private DataSource dataSource; private JdbcTemplate jdbcTemplateObject; SimpleJdbcInsert jdbcInsert; public void setDataSource(DataSource dataSource) { this.dataSource = dataSource; this.jdbcTemplateObject = new JdbcTemplate(dataSource); this.jdbcInsert = new SimpleJdbcInsert(dataSource).withTableName("Student"); } public void create(String name, Integer age) { Map<String, Object> parameters = new HashMap<String, Object>(); parameters.put("name", name); parameters.put("age", age); jdbcInsert.execute(parameters); System.out.println("Created Record Name = " + name + " Age = " + age); return; } public List<Student> listStudents() { String SQL = "select * from Student"; List<Student> students = jdbcTemplateObject.query(SQL, new StudentMapper()); return students; } public Student getStudent(Integer id) { SimpleJdbcCall jdbcCall = new SimpleJdbcCall(dataSource).withProcedureName("getRecord"); SqlParameterSource in = new MapSqlParameterSource().addValue("in_id", id); Map<String, Object> 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")

转载于:https://www.cnblogs.com/borter/p/9608499.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值