MyBatis知多少(24)存储过程
使用MyBatis配置来调用存储过程。为了理解这一章,首先需要了解我们是如何在MySQL中创建一个存储过程。
在继续对本节学习之前,可以自行学习MySQL存储过程。
我们已经在MySQL下有EMPLOYEE表:
CREATE TABLE EMPLOYEE (
id INT NOT NULL auto_increment,
first_name VARCHAR(20) default NULL,
last_name VARCHAR(20) default NULL,
salary INT default NULL,
PRIMARY KEY (id)
);
让我们在MySQL数据库中创建以下存储过程。
DELIMITER $$ DROP PROCEDURE IF EXISTS `testdb`.`getEmp` $$
CREATE PROCEDURE `testdb`.`getEmp`
(IN empid INT)
BEGIN
SELECT * FROM EMPLOYEE
WHERE ID = empid;
END $$ DELIMITER;
考虑EMPLOYEE表是有两条记录如下:
mysql> select * from EMPLOYEE;
+----+------------+-----------+--------+
| id | first_name | last_name | salary |
+----+------------+-----------+--------+
| 1 | Zara | Ali | 5000 |
| 2 | Roma | Ali | 3000 |
+----+------------+-----------+--------+
2 row in set (0.00 sec)
Employee POJO 类:
使用存储过程,你就需要修改Employee.java文件。因此,让我们保持它,因为它是在前一章。
public class Employee {
private int id;
private String first_name;
private String last_name;
private int salary;
/* Define constructors for the Employee class. */
public Employee() {}
public Employee(String fname, String lname, int salary) {
this.first_name = fname;
this.last_name = lname;
this.salary = salary;
}
/* Here are the required method definitions */
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return first_name;
}
public void setFirstName(String fname) {
this.first_name = fname;
}
public String getLastName() {
return last_name;
}
public void setlastName(String lname) {
this.last_name = lname;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
} /* End of Employee */
Employee.xml 类:
在这里,我们将修改Employee.xml文件介绍<procedure></procedure>和<parameterMap></parameterMap>标记。这里<procedure></procedure>标签将有一个id,我们会用我们的应用程序来调用存储过程。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap
PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-2.dtd"> <sqlMap namespace="Employee"> <!-- Perform Insert Operation -->
<insert id="insert" parameterClass="Employee">
INSERT INTO EMPLOYEE(first_name, last_name, salary)
values (#first_name#, #last_name#, #salary#) <selectKey resultClass="int" keyProperty="id">
select last_insert_id() as id
</selectKey> </insert> <!-- Perform Read Operation -->
<select id="getAll" resultClass="Employee">
SELECT * FROM EMPLOYEE
</select> <!-- Perform Update Operation -->
<update id="update" parameterClass="Employee">
UPDATE EMPLOYEE
SET first_name = #first_name#
WHERE id = #id#
</update> <!-- Perform Delete Operation -->
<delete id="delete" parameterClass="int">
DELETE FROM EMPLOYEE
WHERE id = #id#
</delete> <!-- To call stored procedure. -->
<procedure id="getEmpInfo" resultClass="Employee"
parameterMap="getEmpInfoCall">
{ call getEmp( #acctID# ) }
</procedure>
<parameterMap id="getEmpInfoCall" class="map">
<parameter property="acctID" jdbcType="INT"
javaType="java.lang.Integer" mode="IN"/>
</parameterMap> </sqlMap>
IbatisSP.java 文件:
文件将应用程序级别的逻辑读取使用结果映射Employee表员工的姓名name:
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 IbatisSP{
public static void main(String[] args)
throws IOException,SQLException{
Reader rd = Resources.getResourceAsReader("SqlMapConfig.xml");
SqlMapClient smc = SqlMapClientBuilder.buildSqlMapClient(rd); int id = 1;
System.out.println("Going to read employee name.....");
Employee e = (Employee)smc.queryForObject
("Employee.getEmpInfo", id); System.out.println("First Name: " + e.getFirstName()); System.out.println("Record name Successfully "); }
}
编译和运行:
下面是步骤来编译并运行上述应用程序。请确保您在进行的编译和执行之前,适当地设置PATH和CLASSPATH。
创建Employee.xml如上所示。
创建Employee.java如上图所示,并编译它。
创建IbatisSP.java如上图所示,并编译它。
执行IbatisSP二进制文件来运行程序。
得到以下结果:
Going to read record.....
ID: 1
First Name: Zara
Last Name: Ali
Salary: 5000
Record read Successfully
系列文章:
MyBatis知多少(13)MyBatis如何解决数据库的常见问题
MyBatis知多少(24)存储过程的更多相关文章
- MyBatis知多少(26)MyBatis和Hibernate区别
iBatis和Hibernate之间有着较大的差异,但两者解决方案很好,因为他们有特定的领域.我个人建议使用MyBatis的,如果: 你想创建自己的SQL,并愿意维持他们. 你的环境是由关系数据模型驱 ...
- MyBatis知多少(26)调试
这是很容易,同时与iBATIS的工作程序进行调试. iBATIS有内置的日志支持,并适用于下列日志库,并在这个顺序搜索他们. Jakarta Commons日志记录(JCL). Log4J JDK 日 ...
- MyBatis知多少(25)动态SQL
使用动态查询是MyBatis一个非常强大的功能.有时你已经改变WHERE子句条件的基础上你的参数对象的状态.在这种情况下的MyBatis提供了一组可以映射语句中使用,以提高SQL语句的重用性和灵活性的 ...
- MyBatis知多少(15)数据模型
瘦数据模型是一种最为臭名昭著并且问题多多的对关系数据库系统的滥用.不幸的是,有时又的确需要瘦数据模型.所谓瘦数据模型,就是简单地将每张表都设计为一种通用数据结构,用于存储名值对的集合.这非常像Java ...
- MyBatis知多少(4)MyBatis的优势
MyBatis是一个混合型解决方案.它汲取了所有这些解决方案中最有价值的思想并将它们融会贯通.下表总结了MyBatis从我们之前讨论的那些方案中所汲取的思想. 方 案 相同的优点 解决的问题 存储过程 ...
- MyBatis知多少(3)
解决存储过程固有限制的方法之一就是将SQL嵌入到更加通用的语言中去.与存储过程将业务逻辑移入数据库相反,内联SQL将SQL从数据库移入了应用程序代码.这就使得SQL语句可以直接与语言进行交互.从某种意 ...
- MyBatis知多少(2)
MyBatis从目前最流行的关系数据库访问方法中吸收了大量的优秀特征和思想,并找出其中的协同增效作用.下图展示了MyBatis框架是如何吸收我们在多年使用不同方式进行数据库集成的 开发过程中所学到的知 ...
- MyBatis知多少(23)MyBatis结果映射
resultMap的元素是在MyBatis的最重要和最强大的元素.您可以通过使用MyBatis的结果映射减少高达90%的JDBC编码,在某些情况下,可以让你做JDBC不支持的事情. ResultMap ...
- MyBatis知多少(22)MyBatis删除操作
本节从表中使用MyBatis删除记录. 我们已经在MySQL下有EMPLOYEE表: CREATE TABLE EMPLOYEE ( id INT NOT NULL auto_increment, f ...
随机推荐
- Linux内核启动分析过程-《Linux内核分析》week3作业
环境搭建 环境的搭建参考课件,主要就是编译内核源码和生成镜像 start_kernel 从start_kernel开始,才真正进入了Linux内核的启动过程.我们可以把start_kernel看做平时 ...
- Android:改变Activity切换方式(转载)
overridePendingTransition(enterAnim, exitAnim); Intent intent =new Intent(this,item2.class); startAc ...
- 修復jquery的tablesorter对加了千分位的数字无法正确排序的bug
找到函数: function getElementText(config, node) { var text = ""; if (!node) return "" ...
- Multiplexing SDIO Devices Using MAX II or CoolRunner-II CPLD
XAPP906 Supporting Multiple SD Devices with CoolRunner-II CPLDs There has been an increasing demand ...
- spring和ehcache整合,实现基于注解的缓存实现
要实现基于注解的缓存实现,要求Spring的版本在3.1或以上版本. 首先需要在spring的配置文件中添加对缓存注解的实现: <?xml version="1.0" enc ...
- Ubuntu server 搭建Git server
Ubuntu server 搭建Git server,git相比svn,最主要就是分布式了,每个客户端用户的本地都是一个版本管理控制器. Ubuntu server 版本为12.04 搭建步骤如下: ...
- linux 下 取进程占用内存(MEM)最高的前10个进程
# linux 下 取进程占用 cpu 最高的前10个进程ps aux|head -1;ps aux|grep -v PID|sort -rn -k +3|head # linux 下 取进程占用内存 ...
- Entity Framework: Joining in memory data with DbSet
转载自:https://ilmatte.wordpress.com/2013/01/06/entity-framework-joining-in-memory-data-with-dbset/ The ...
- Spring3系列10- Spring AOP——Pointcut,Advisor拦截指定方法
Spring3系列10- Spring AOP——Pointcut,Advisor 上一篇的Spring AOP Advice例子中,Class(CustomerService)中的全部method都 ...
- lua中得栈
如果你看了LUA的文档,那么就应该很清楚LUA与C交互数据时都是用到LUA中所谓的stack.那么当我调用lua_open函数之后栈是什么样的呢?空的(luaopen_base等会往栈上加进一些东西) ...