码迷,mamicode.com
首页 > 其他好文 > 详细

mybatis调用存储过程实现

时间:2017-04-30 12:25:06      阅读:309      评论:0      收藏:0      [点我收藏+]

标签:tsql   stat   shm   state   encoding   on()   tno   mapper   exce   

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);
        }
    }

 

mybatis调用存储过程实现

标签:tsql   stat   shm   state   encoding   on()   tno   mapper   exce   

原文地址:http://www.cnblogs.com/youlangta/p/6789096.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!