FROM: http://developer.51cto.com/art/200907/138332.htm
通过iBatis我们可以在数据库表中执行内嵌的insert , delete, update SQL命令。本文中你将看到如何在iBatis中调用存储过程.
我们使用MySQL数据库,并且使用和上一个例子中一样的Contact表.
我们在数据库"vin"中创建了一个叫showData()的存储过程,它将显示Contract表中的所有的contact信息.为了创建存储过程,我们打开MySQL并创建如下定义的过程 :
DELIMITER $
DROP
PROCEDURE
IF EXISTS `vin`.`showData`$
CREATE
PROCEDURE
`vin`.`showData`()BEGINselect *
from
Contact;
END$ DELIMITER ;
"Contact.java"和"SqlMapConfig.xml"与上一个例子中的是一样的 :
public class Contact { private String firstName; private String lastName; private String email; private int id; public Contact() {} public Contact( String firstName, String lastName, String email) { this.firstName = firstName; this.lastName = lastName; this.email = email; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } }
SqlMapConfig.xml
- <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE sqlMapConfigPUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
- "http://ibatis.apache.org/dtd/sql-map-config-2.dtd"> <sqlMapConfig> <settings useStatementNamespaces="true"/>
- <transactionManager type="JDBC">
- <dataSource type="SIMPLE">
- <property name="JDBC.Driver" value="com.mysql.jdbc.Driver"/>
- <property name="JDBC.ConnectionURL" value="jdbc:mysql://192.168.10.112:3306/vin"/>
- <property name="JDBC.Username" value="root"/>
- <property name="JDBC.Password" value="root"/>
- </dataSource>
- </transactionManager>
- <sqlMap resource="Contact.xml"/> </sqlMapConfig>
我们只需修改"Contact.xml"并使用标签来在iBatis中调用存储过程
- <procedure id="storedInfo" resultClass="Contact">
- { call showData() } </procedure>
上面几行代码调用了存储过程并集合了contract表.下面是Contact.xml的代码 :
- <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE sqlMapPUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
- "http://ibatis.apache.org/dtd/sql-map-2.dtd">
- <sqlMap namespace="Contact">
- <!--- Calling stored procedure -->
- <procedure id="storedInfo" resultClass="Contact">
- { call showData()} </procedure></sqlMap>
现在我们可以这样在iBatis中调用存储过程 :
sqlMap.queryForList("Contact.storedInfo",null); "sqlMap"是SqlMapClient类的一个对象. IbatisStoredProcedure.java的代码如下 :
- import com.ibatis.common.resources.Resources;
- import com.ibatis.sqlmap.client.SqlMapClient;
- import com.ibatis.sqlmap.client.SqlMapClientBuilder;
- import java.io.*;import java.sql.SQLException;
- import java.util.*;
- public class IbatisStoredProcedure{ public static void main(String[] args)
- throws IOException,SQLException{ Reader reader = Resources.getResourceAsReader("SqlMapConfig.xml");
- SqlMapClient sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader); System.out.println("All Contacts");
- List<Contact> contacts = (List<Contact>)
- sqlMap.queryForList("Contact.storedInfo",null);
- Contact contact = null;
- for (Contact c : contacts) { System.out.print(" " + c.getId());
- System.out.print(" " + c.getFirstName());
- System.out.print(" " + c.getLastName());
- System.out.print(" " + c.getEmail());
- contact = c;
- System.out.println(""); } }}
请依照如下步骤执行在iBatis中调用存储过程 :
创建Contact.xml和SqlMapConfig.xml
创建Contact.java并将其编译
创建IbatisStoredProcedure.java并将其编译
执行IbatisStoredProcedure类文件,所有的Contract信息将在你的命令提示符下显示 :
【编辑推荐】