首页 > 代码库 > mybatis调用存储过程实现
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调用存储过程实现
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。