simplejdbccall 类可以被用于调用一个包含 in 和 out 参数的存储过程。你可以在处理任何一个 rdbms 时使用这个方法,就像 apache derby, db2, mysql, microsoft sql server, oracle,和 sybase。
为了了解这个方法,我们使用 student 表,它可以在 mysql test 数据库中使用下面的 ddl 进行创建:
create table student(
id int not null auto_increment,
name varchar(20) not null,
age int not null,
primary key (id)
);
下一步,考虑接下来的 mysql 存储过程,该过程使用 学生 id 并且使用 out 参数返回相应的学生的姓名和年龄。所以让我们在你的 test 数据库中使用 mysql 命令提示符创建这个存储过程:
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 ;
现在,让我们编写我们的 spring jdbc 应用程序,它可以实现对我们的 student 数据库表的创建和读取操作。让我们使 eclipse ide 处于工作状态,然后按照如下步骤创建一个 spring 应用程序:
步骤 | 描述 |
---|---|
1 | 创建一个名为 springexample 的项目,并且在所创建项目的 src 文件夹下创建一个名为 com.tutorialspoint 的包。 |
2 | 使用 add external jars 选项添加所需的 spring 库文件,就如在 spring hello world example 章节中解释的那样。 |
3 | 在项目中添加 spring jdbc 指定的最新的库文件 mysql-connector-java.jar, org.springframework.jdbc.jar 和 org.springframework.transaction.jar。如果你还没有这些所需要的库文件,你可以下载它们。 |
4 | 创建 dao 接口 studentdao 并且列出所有需要的方法。 即使他不是必需的,你可以直接编写 studentjdbctemplate 类,但是作为一个良好的实践,让我们编写它。 |
5 | 在 com.tutorialspoint 包下创建其他所需要的 java 类 student, studentmapper, studentjdbctemplate 和 mainapp。 |
6 | 确保你已经在 test 数据库中创建了 student 表。同样确保你的 mysql 服务器是正常工作的,并且保证你可以使用给定的用户名和密码对数据库有读取/写入的权限。 |
7 | 在 src 文件夹下创建 beans 配置文件 beans.xml。 |
8 | 最后一步是创建所有 java 文件和 bean 配置文件的内容,并且按如下解释的那样运行应用程序。 |
下面是数据访问对象接口文件 studentdao.java 的内容:
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<student> liststudents();
}
下面是 student.java 文件的内容:
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;
}
}
下面是 studentmapper.java 文件的内容:
package com.tutorialspoint;
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;
}
}
下面是实现类文件 studentjdbctemplate.java,定义了 dao 接口 studentdao:
package com.tutorialspoint;
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<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"));
return student;
}
public list<student> liststudents() {
string sql = "select * from student";
list <student> students = jdbctemplateobject.query(sql,
new studentmapper());
return students;
}
}
关于上述项目的几句话:你编写的课调用执行的代码涉及创建一个包含 in 参数的 sqlparametersource。名称的匹配是很重要的,该名称可以使用在存储过程汇总声明的参数名称来提供输入值。execute 方法利用 in 参数返回一个包含在存储过程中由名称指定的任何外部参数键的映射。现在让我们移动主应用程序文件 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<student> 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());
}
}
下面是配置文件 beans.xml:
<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
xsi:schemalocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd ">
<!-- initialization for data source -->
<bean id="datasource"
class="org.springframework.jdbc.datasource.drivermanagerdatasource">
<property name="driverclassname" value="com.mysql.jdbc.driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test"/>
<property name="username" value="root"/>
<property name="password" value="password"/>
</bean>
<!-- definition for studentjdbctemplate bean -->
<bean id="studentjdbctemplate"
class="com.tutorialspoint.studentjdbctemplate">
<property name="datasource" ref="datasource" />
</bean>
</beans>
一旦你已经完成的创建了源文件和 bean 配置文件,让我们运行一下应用程序。如果你的应用程序一切都正常的话,这将会输出以下消息:
------records creation--------
created record name = zara age = 11
created record name = nuha age = 2
created record name = ayan age = 15
------listing multiple records--------
id : 1, name : zara, age : 11
id : 2, name : nuha, age : 2
id : 3, name : ayan, age : 15
----listing record with id = 2 -----
id : 2, name : nuha, age : 2