mybatis调用存储过程实现

1.创建存储过程create or replace
procedure pro_student_info_byId(
in_Id in t_student_info.id%type,out_custInfo out sys_refcursor
)is
begin
open out_custInfo for select a.*  from t_student_info a where a.id = in_Id;

end ;
public interface StudentMapper {
	public void getStudents(Map<String,Object> param);
}

  

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.study.mybatis.mapper.StudentMapper">
    <resultMap type="student" id="studentMap">
        <id column="id" property="id" />
        <result column="student_name" property="studentName" />
        <result column="sex" property="sex"
            typeHandler="org.apache.ibatis.type.EnumOrdinalTypeHandler" />
        <result column="student_note" property="studentNote" />
        <result column="student_num" property="studentNum" />
    </resultMap>
    <select id="getStudents" statementType="CALLABLE" parameterType="java.util.Map">
        {call
        pro_student_info_byId(
        #{id,jdbcType=VARCHAR,mode=IN},
        #{studentInfoList,jdbcType=CURSOR,mode=OUT,javaType=ResultSet,resultMap=studentMap})}
    </select>

</mapper>
package com.study.mybatis.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

/**
 * mybatis工具类
 *
 * @author Administrator
 *
 */
public class MybatisUtil {

    private static SqlSessionFactory sqlSessionFactory;
    // 类线程锁
    private static final Class CLASS_LOCK = MybatisUtil.class;

    public static SqlSessionFactory initSqlSessionFactory() {
        String resource = "mybatis-config.xml";
        InputStream inputStream = null;
        try {
            inputStream = Resources.getResourceAsStream(resource);
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        } catch (IOException e) {
            Logger.getLogger(MybatisUtil.class.getName()).log(Level.SEVERE, null, e);
            synchronized (CLASS_LOCK) {
                if (sqlSessionFactory == null) {
                    sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
                }
                return sqlSessionFactory;
            }
        }
        return sqlSessionFactory;

    }

    public static SqlSession openSqlsession() {
        if (sqlSessionFactory == null) {
            initSqlSessionFactory();
        }
        return sqlSessionFactory.openSession();
    }

    public static void closeSqlSession(SqlSession session) {
        if (session != null) {
            session.close();
        }
    }
}
public static void main(String[] args) {
        SqlSession session = null;
        try {
            session = MybatisUtil.openSqlsession();
            StudentMapper studentMapper = session.getMapper(StudentMapper.class);
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("id", 1);
            params.put("studentInfoList", OracleTypes.CURSOR);
            System.out.println(params);
            studentMapper.getStudents(params);
            System.out.println(params);
            session.commit();
        } catch (Exception e) {
            System.out.println(e.getMessage());
            session.rollback();
        } finally {
            MybatisUtil.closeSqlSession(session);
        }
    }
时间: 2024-08-02 02:50:22

mybatis调用存储过程实现的相关文章

mybatis调用存储过程

最近在研究mybatis调用存储过程,发表一下自己的学习心得 其中最好用的就是通过map传值,结果还用这个map接收,因为map是一个非常强大的东西,有时候存储过程要操作多张表. 比如说:往多张表插入数据,用JavaBean的方式就很难做到,参数对应多个实体类不好解决,用map就能完美的解决这个问题. Map<String, Object> map=new HashMap<String, Object>把需要的参数往里面一放,就能很容易解决问题 dao层(dao接口),在dao接口

mybatis 调用存储过程 返回游标 实例

存储过程示例: create or replace procedure Fsp_Plan_CheckPrj(v_grantno varchar2, v_deptcode number, v_cursor out sys_refcursor) is ……………… ---返回统计结果 open v_Cursor for select s.plan_code, s.plan_dept, s.plan_amount, s.exec_amount, p.cname as plan_name, d.cnam

MyBatis——调用存储过程

原文:http://www.cnblogs.com/xdp-gacl/p/4270352.html 一.提出需求 查询得到男性或女性的数量, 如果传入的是0就女性否则是男性 二.准备数据库表和存储过程 create table p_user( id int primary key auto_increment, name varchar(10), sex char(2) ); insert into p_user(name,sex) values('A',"男"); insert in

mybatis调用存储过程 无参、带有输入输出参数,输出游标类型的 存储

存储过程在小公司用的不多,但是如果业务比较复杂或者性能要求比较苛刻的时候存储过程就派上用场了,ibatis的前期的一些版本貌似不支持存储过程因此我选择了mybatis来做实验. 1.无输入和输出参数的存储过程,我写了一个比较简单的,需要注意的是Oracle无参存储过程不能写括号 [sql] view plain copy CREATE OR REPLACE Procedure cascadeoperation As Begin Delete From teacher Where id=1; Up

Springmvc+mybatis 调用存储过程源码

刚开始学springmvc . 期间参考jeesite ,等优秀开源框架. 谢谢作者开源精神. 看图 源码打包了   http://yunpan.cn/cctLmadujMfEV (提取码:2cff) 具体调用过程可以参考上一篇帖子!

MyBatis 调用存储过程

sqlxml,这里面的call 与后面不能换行: <select id="getPersonCount" parameterMap="getPCMap" statementType="CALLABLE"> CALL test.get_user_count(?,?) </select> <parameterMap type="java.util.Map" id="getPCMap"

【Mybatis】MyBatis调用带有返回结果、output参数的存储过程上与ibatis的区别

用过mybatis的应该都知道它是ibatis被Google收购后重新命名的一个工程,因此也做了大量升级.本文就来介绍下两者在调用存储过程上的一点区别,ibatis有一个专门的标签<procedure>,在mybatis里面已经没有这标签了,而是通过一个参数statementType="CALLABLE"来区分. 存储过程: ALTER PROCEDURE [dbo].[Pro_Create_Number] @ChannelNo VARCHAR(10) ,--渠道号 @Nu

Mybatis调用Oracle中的存储过程和function

一.Mybatis调用存储过程 1 在数据库中创建以下的存储过程create or replace procedure pro_hello(p_user_name in varchar2,p_result out varchar2) isbegin  p_result := 'hello,' || p_user_name;end; 2 编写SQL映射文件mapper.xmlstatementType里的CALLABLE是标注此sql为存储过程.parameterType是标注要传的参数,看了一些

mybatis如何在后台调用存储过程

-- mybatis调用存储过程-- 数据库:mysql5.5一.提出需求 查询得到男性或女性的数量, 如果传入的是2就女性,否则是男性二.准备数据库表和存储过程create table p_user( id int primary key auto_increment, name varchar(10),sex char(2)); insert into p_user(name,sex,code) values('A',"男",1); insert into p_user(name,