博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring中使用存储过程
阅读量:6307 次
发布时间:2019-06-22

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

以下内容引用自:

SimpleJdbcCall类可以被用于调用一个包含IN和OUT参数的存储过程。你可以在处理任何一个RDBMS时使用这个方法,支持数据库有Apache Derby,DB2,MySQL,Microsoft SQL Server,Oracle,和Sybase等等。

实践前提:

1、创建数据库,脚本如下所示:

---- 数据库: `test`---- ------------------------------------------------------------ 表的结构 `student`--CREATE TABLE IF NOT EXISTS `student` (  `ID` int(11) NOT NULL AUTO_INCREMENT,  `NAME` varchar(20) NOT NULL,  `AGE` int(11) NOT NULL,  PRIMARY KEY (`ID`)) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=10 ;---- 转存表中的数据 `student`--INSERT INTO `student` (`ID`, `NAME`, `AGE`) VALUES(1, 'Zara', 11),(2, 'Nuha', 2),(3, 'Ayan', 15);

2、创建存储过程,实现获取单个学生信息,脚本如下所示:

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 ;

3、由于使用的是MySql数据库,需要引入mysql-connector-java.jar包,POM如下所示:

mysql
mysql-connector-java
5.1.38

实践例子:

pom.xml:

4.0.0
com.jsoft.testspring
teststoredprocedure
0.0.1-SNAPSHOT
jar
teststoredprocedure
http://maven.apache.org
UTF-8
junit
junit
3.8.1
test
org.springframework
spring-core
4.1.4.RELEASE
org.springframework
spring-context
4.1.4.RELEASE
org.springframework
spring-jdbc
4.1.4.RELEASE
org.springframework
spring-tx
4.1.4.RELEASE
mysql
mysql-connector-java
5.1.38

Student.java:

package com.jsoft.testspring.teststoredprocedure;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;    }}

StudentDAO.java:

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

StudentMapper.java:

package com.jsoft.testspring.teststoredprocedure;import java.sql.ResultSet;import java.sql.SQLException;import org.springframework.jdbc.core.RowMapper;public class StudentMapper implements RowMapper
{ @Override public Student mapRow(ResultSet rs, int arg1) throws SQLException { // TODO Auto-generated method stub Student student = new Student(); student.setId(rs.getInt("id")); student.setName(rs.getString("name")); student.setAge(rs.getInt("age")); return student; }}

StudentJDBCTemplate.java:

package com.jsoft.testspring.teststoredprocedure;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 JdbcTemplate jdbcTemplateObject;    private SimpleJdbcCall jdbcCall;    @Override    public void setDataSource(DataSource dataSource) {        this.dataSource = dataSource;        this.jdbcTemplateObject = new JdbcTemplate(dataSource);        this.jdbcCall = new SimpleJdbcCall(dataSource).withProcedureName("getRecord");    }    @Override    public void create(String name, Integer age) {        String SQL = "insert into Student (name, age) values (?, ?)";        jdbcTemplateObject.update(SQL, name, age);        System.out.println("Created Record Name = " + name + " Age = " + age);        return;    }    @Override    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; } @Override public List
listStudents() { String SQL = "select * from Student"; List
students = jdbcTemplateObject.query(SQL, new StudentMapper()); return students; } @Override public void delete(Integer id) { String SQL = "delete from Student where id = ?"; jdbcTemplateObject.update(SQL, id); System.out.println("Deleted Record with ID = " + id); return; } @Override public void update(Integer id, Integer age) { String SQL = "update Student set age = ? where id = ?"; jdbcTemplateObject.update(SQL, age, id); System.out.println("Updated Record with ID = " + id); return; }}

beans.xml:

App.java:

package com.jsoft.testspring.teststoredprocedure;import java.util.List;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;/** * Hello world! * */public class App {    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("----Updating Record with ID = 2 -----"); studentJDBCTemplate.update(2, 20); 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()); }}

测试结果:

 

测试工程:

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

你可能感兴趣的文章
Mysql explain
查看>>
初识闭包
查看>>
java tcp socket实例
查看>>
011 指针的算术运算
查看>>
hdu1874畅通工程续
查看>>
rails 字符串 转化为 html
查看>>
java-学习8
查看>>
AOP动态代理
查看>>
Oracle序列
查看>>
xcodebuild命令行编译错误问题解决
查看>>
Yii2.0 下的 load() 方法的使用
查看>>
华为畅玩5 (CUN-AL00) 刷入第三方twrp Recovery 及 root
查看>>
LeetCode----67. Add Binary(java)
查看>>
母版页 MasterPage
查看>>
[转] ReactNative Animated动画详解
查看>>
DNS原理及其解析过程
查看>>
记录自写AFNetWorking封装类
查看>>
没想到cnblog也有月经贴,其实C#值不值钱不重要。
查看>>
【转】LUA内存分析
查看>>
springboot使用schedule定时任务
查看>>