12mybatis调用执行存储过程
Posted 归然
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了12mybatis调用执行存储过程相关的知识,希望对你有一定的参考价值。
mybatis 调用执行存储过程
mysql 声明建立存储过程 删除 在mysql中调用
-- 声明定义存储过程
delimiter $$
create procedure delbook(id int)
begin
delete from book where book_id=id;
end$$
delimiter ;
-- 删除存储过程
drop procedure delbook;
-- 调用执行存储过程
call delbook(4);
select * from book;
mybatis 调用删除存储过程
-- 声明定义存储过程--------------------------------------------
delimiter $$
create procedure delbook(id int)
begin
delete from book where book_id=id;
end$$
delimiter ;
BookMapper.java 映射接口文件
public int delbook(int id);
BookMapper.xml 映射配置文件
<parameterMap id="pm" type="map">
<parameter property="id" javaType="int" mode="IN"/>
</parameterMap>
<update id="delbook" statementType="CALLABLE" parameterMap="pm">
{call delbook(?)}
</update>
程序调用
int i = bdao.delbook(3);
-- 声明一个插入数据的存储过程--------------------------------------------
delimiter $$
create procedure addbook(name varchar(20),price decimal(8,2))
begin
insert into book(book_name,book_price) values(name,price);
end$$
delimiter ;
-- 调用一下
call addbook(‘《java开发》‘,80);
BookMapper.java 映射接口
public int addBook(Map<String,Object> map);
BookMapper.xml 映射文件
注册映射文件的参数顺序
<parameterMap id="am" type="map">
<parameter property="name" javaType="String" mode="IN"/>
<parameter property="price" javaType="double" mode="IN"/>
</parameterMap>
<update id="addBook" statementType="CALLABLE" parameterMap="am">
{call addbook(?,?)}
</update>
程序调用方法
Map<String,Object> m = new HashMap<String,Object>();
m.put("name","《项目开发》");
m.put("price",28d);
int i = bdao.addBook(m);
System.out.println(i);
-- 声明定义存储过程--------------------------------------------
delimiter $$
create procedure showbook()
begin
select * from book;
end$$
delimiter ;
-- 调用存储
call showbook;
call showbook();
BookMapper.java
public List<Book> showbook();
BookMapper.xml
<resultMap id="mp" type="book">
<id column="book_id" property="id"/>
<result column="book_name" property="name"/>
<result column="book_price" property="price"/>
</resultMap>
<select id="showbook" statementType="CALLABLE" resultMap="mp">
{call showbook()}
</select>
程序代码
List<Book> bks = bdao.showbook();
System.out.println(bks.size());
for(Book b : bks){
System.out.println(b.getName());
}
-- 声明定义存储过程
delimiter $$
create procedure bookcc(out r int)
begin
select count(*) into r from book;
end$$
delimiter ;
call bookcc(@r);
select @r;
BookMapper.java
public void bookcc(Map<String,Object> map);
BookMapper.xml 文件
<select id="bookcc" statementType="CALLABLE">
{call bookcc(#{r,mode=OUT,jdbcType=INTEGER})}
</select>
程序代码调用
Map<String,Object> m = new HashMap<String,Object>();
bdao.bookcc(m);
System.out.println(m.get("r"));
别一种方式
BookMapper.java 映射接口
public void bookcc(Map<String,Object> map);
BookMapper.xml 映射配置文件
<parameterMap id="cm" type="map">
<parameter property="r" jdbcType="INTEGER" mode="OUT"/>
</parameterMap>
<select id="bookcc" statementType="CALLABLE" parameterMap="cm">
{call bookcc(?)}
</select>
程序代码为
Map<String,Object> m = new HashMap<String,Object>();
bdao.bookcc(m);
System.out.println(m.get("r"));
总程序测试代码如下:
package com;
import com.fz.entity.Book;
import com.fz.mapper.BookMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by webrx on 2017-06-16.
*/
public class Demo {
protected SqlSessionFactory sf;
protected SqlSession ss;
protected BookMapper bdao;
@Before
public void init(){
try {
this.sf = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis-config.xml"));
this.ss = this.sf.openSession();
this.bdao = this.ss.getMapper(BookMapper.class);
} catch (IOException e) {
e.printStackTrace();
}
}
@After
public void close(){
this.ss.commit();
this.ss.close();
}
@Test
public void s1() throws SQLException {
Connection conn = this.ss.getConnection();
PreparedStatement pst = conn.prepareStatement("create database ddd");
pst.execute();
pst.close();
conn.close();
}
@Test
public void s2(){
//bdao.dropDb("ddd");
List<Map<String,Object>> dbs = bdao.showDbs();
for(Map<String,Object> map : dbs){
System.out.println(map.get("Database"));
}
}
@Test
public void s4(){
//int i = bdao.delbook(3);
//System.out.println(i);
//Map<String,Object> m = new HashMap<String,Object>();
//m.put("name","《项目开发》");
//m.put("price",28d);
//int i = bdao.addBook(m);
//System.out.println(i);
//List<Book> bks = bdao.showbook();
//System.out.println(bks.size());
//for(Book b : bks){
// System.out.println(b.getName());
//}
Map<String,Object> m = new HashMap<String,Object>();
bdao.bookcc(m);
System.out.println(m.get("r"));
}
@Test
public void s3(){
//bdao.dropDb("ddd");
List<String> dbs = bdao.showDbs2();
for(String n : dbs){
System.out.println(n);
}
}
}
以上是关于12mybatis调用执行存储过程的主要内容,如果未能解决你的问题,请参考以下文章
MyBatis从入门到放弃五:调用存储过程(SQLServer2012)