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

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

基于Spring JDBC框架与SimpleJdbcCall调用带有IN和OUT参数的存储过程

本文将通过一个完整的示例,展示如何利用Spring JDBC框架和SimpleJdbcCall类来调用带有IN和OUT参数的存储程序。该示例将涵盖从存储程序的创建到实际应用程序的集成,适用于MySQL、Oracle等多种数据库。

1. 创建存储程序

首先,我们需要为Student表创建一个存储程序getRecord,该存储程序接受一个IN参数和返回两个OUT参数。

DELIMITER $$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. 定义实体类

创建一个Student实体类来表示数据库中的学生记录。

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接口

StudentDAO接口定义了与学生数据相关的数据库操作方法。

package com.tutorialspoint;import java.util.List;import javax.sql.DataSource;public interface StudentDAO {    void setDataSource(DataSource ds);    void create(String name, Integer age);    Student getStudent(Integer id);    List
listStudents();}

4. 实现StudentMapper

StudentMapper用于将数据库结果映射到Student对象。

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

StudentJDBCTemplate实现了StudentDAO接口,负责执行具体的数据库操作。

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 jdbcTemplate = new JdbcTemplate(dataSource);        String SQL = "insert into Student (name, age) values (?, ?)";        jdbcTemplate.update(SQL, name, age);        System.out.println("Created Record Name = " + name + " Age = " + age);    }    public Student getStudent(Integer id) {        Map
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 jdbcTemplate = new JdbcTemplate(dataSource); String SQL = "select * from Student"; List
students = jdbcTemplate.query(SQL, new StudentMapper()); return students; }}

6. 主应用程序MainApp

MainApp类用于演示如何使用StudentJDBCTemplate进行数据库操作。

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("------创建记录--------" );        studentJDBCTemplate.create("Zara", 11);        studentJDBCTemplate.create("Nuha", 2);        studentJDBCTemplate.create("Ayan", 15);                System.out.println("------列出多个记录--------" );        List
students = studentJDBCTemplate.listStudents(); for (Student record : students) { System.out.print("ID : " + record.getId()); System.out.print(", 姓名 : " + record.getName()); System.out.println(", 年龄 : " + record.getAge()); } System.out.println("----列出ID=2的记录----" ); Student student = studentJDBCTemplate.getStudent(2); System.out.print("ID : " + student.getId()); System.out.print(", 姓名 : " + student.getName()); System.out.println(", 年龄 : " + student.getAge()); }}

7. 配置文件Beans.xml

Spring配置文件,定义数据源和StudentJDBCTemplate bean。

8. 运行主应用程序

运行MainApp.java主方法,查看输出结果。

------创建记录--------Created Record Name = Zara, Age = 11Created Record Name = Nuha, Age = 2Created Record Name = Ayan, Age = 15------列出多个记录--------ID : 1, 姓名 : Zara, 年龄 : 11ID : 2, 姓名 : Nuha, 年龄 : 2ID : 3, 姓名 : Ayan, 年龄 : 15----列出ID=2的记录----ID : 2, 姓名 : Nuha, 年龄 : 2

通过上述步骤,可以看到如何通过Spring JDBC框架和SimpleJdbcCall类来调用带有IN和OUT参数的存储程序,并实现与数据库的交互。

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

你可能感兴趣的文章
PyInstaller图标选项在Mac上不起作用
查看>>
pyinstaller的使用
查看>>
pyinstaller超级加密 (加壳和转c)
查看>>
pyinstaller错误:OSError:找不到Python库:libpython3.4mu.so.1.0、libpython3.4m.so.1.0、libpython3.4.so.1.0
查看>>
Pyinstaller:‘;Fiona‘;没有属性‘;_loading‘;(很可能是由于循环导入)
查看>>
pyinstaller:更改应用程序图标
查看>>
Pylint“未解决的导入“;Visual Studio 代码中的错误
查看>>
pyLoad远程代码执行漏洞复现(CVE-2023-0297)
查看>>
pymongo update_one(),upsert=True,不使用$运算符
查看>>
pymongo 中的快速或批量更新
查看>>
pymongo删除操作
查看>>
PyMongo按多个关键点分组
查看>>
Pympress:强大的双屏PDF阅读器
查看>>
pymysql.err.InternalError: (1054, "Unknown column '27D24A3B' in 'where clause'")之错误解决
查看>>
pymysql.err.OperationalError: (1364, “Field ‘id‘ doesn‘t have a default value“)
查看>>
Pytorch Tensor 维度操作的形象理解 Tensor.unsqueeze() Tensor.squeeze()
查看>>
PyMySQL库对Mysql数据库进行增删改查与工具类封装
查看>>
pynput的基本介绍和使用
查看>>
pyodbc 通过 IIS7 连接到 MSSQL 2005 服务器
查看>>
PyPI 存储库中的 JarkaStealer:深入解析与防范措施
查看>>