mybatis调用存储过程实现
Posted 游浪踏
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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); } }
以上是关于mybatis调用存储过程实现的主要内容,如果未能解决你的问题,请参考以下文章