美文网首页
MyBatis调用存储过程

MyBatis调用存储过程

作者: zheting | 来源:发表于2018-01-22 21:44 被阅读458次

    SQL Script

    
    CREATE TABLE users(
            id INT,
            NAME VARCHAR(20),
             age INT);
    INSERT INTO users(id,NAME, age) VALUES(100,'Tom', 12);
    INSERT INTO users(id,NAME, age) VALUES(200,'Jack', 11);
    
    select * from users for update;
    
            
    create or replace procedure pro_hello(p_user_name in varchar2,p_result out varchar2) is  
    begin  
      p_result := 'hello,' || p_user_name;  
    end;   
    
    
    CREATE OR REPLACE PROCEDURE user_selectList(p_age in INTEGER, p_CURSOR out sys_refcursor) is
    BEGIN
        OPEN p_CURSOR FOR SELECT * FROM users where age = p_age;
    END user_selectList;
    
    
    package com.zheting;
    
    public class User {
        private int id;
        private String name;
        private int age;
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }
        
    }
    
    

    conf.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
    <configuration>
        <environments default="development">
            <environment id="development">
                <transactionManager type="JDBC" />
                <dataSource type="POOLED">
                    <property name="driver" value="oracle.jdbc.driver.OracleDriver" />
                    <property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl" />
                    <property name="username" value="scott" />
                    <property name="password" value="tiger" />
                </dataSource>
            </environment>
        </environments>
        
        <mappers>
            <mapper resource="com/zheting/userMapper.xml"/>
        </mappers>
        
    </configuration>
    
    <?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.zheting.userMapper">
     
        <select id="getUser" parameterType="int" resultType="com.zheting.User">
            select * from users where id=#{id}
        </select>
        
        
        <select id="proHello" statementType="CALLABLE">  
            <![CDATA[  
                {call pro_hello (#{name,mode=IN,jdbcType=VARCHAR},#{result,mode=OUT,jdbcType=VARCHAR})}  
            ]]>  
        </select>  
        
        
        <resultMap type ="java.util.HashMap" id= "cursorMap">
             <result column ="id" property="id" />  
             <result column ="name" property="name"  />  
              <result column ="age" property="age" /> 
        </resultMap > 
        
        <select id="execProcedure" statementType="CALLABLE" parameterType="java.util.Map">
            <![CDATA[  
                {call user_selectList(#{age,mode=IN,jdbcType=INTEGER},#{p_CURSOR,mode=OUT,jdbcType=CURSOR,resultMap=cursorMap})} 
            ]]>
        </select>
    </mapper>
    
    
    package com.zheting;
    
    import java.io.IOException;
    import java.io.Reader;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    import org.apache.ibatis.io.Resources;
    import org.apache.ibatis.session.SqlSession;
    import org.apache.ibatis.session.SqlSessionFactory;
    import org.apache.ibatis.session.SqlSessionFactoryBuilder;
    
    public class Test {
        public static void main(String[] args) throws IOException {
            String resource = "conf.xml"; 
            //加载mybatis的配置文件(它也加载关联的映射文件)
            Reader reader = Resources.getResourceAsReader(resource); 
            //构建sqlSession的工厂
            SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);
            //创建能执行映射文件中sql的sqlSession
            SqlSession session = sessionFactory.openSession();
            //映射sql的标识字符串 命名空间 + sqlid
            String statement = "com.zheting.userMapper"+".getUser";
            //执行查询返回一个唯一user对象的sql
            User user = session.selectOne(statement, 200);
            //System.out.println("***" + user);
    
            
            /* try {  
                   Map<String, String> param = new HashMap<String, String>();  
                   param.put("name", "zhangsan");
                   session.selectOne("com.zheting.userMapper.proHello", param);  
                   System.out.println("result=" + param.get("result"));  
             } catch (Exception e) {  
                  e.printStackTrace();  
             } finally {  
                session.close();  
            }  */
            
              try {  
                  Map<String, Object> param = new HashMap<String, Object>();  
                  param.put("age", "12");
                  //传入一个jdbc游标,用于接收返回参数  
                  param.put("p_CURSOR", new ArrayList<Map<String, Object>>());
                  session.selectOne("com.zheting.userMapper.execProcedure", param); 
                  System.out.println("result=" + param.get("p_CURSOR")); 
                  List<Map<String, Object>> userList = (ArrayList<Map<String, Object>>)param.get("p_CURSOR");
                  System.out.println(((Map<String, Object>) userList.get(0)).get("age"));
              } catch (Exception e) {  
                  e.printStackTrace();  
              } finally {  
                  session.close();  
              }  
        }
    }
    

    相关文章

      网友评论

          本文标题:MyBatis调用存储过程

          本文链接:https://www.haomeiwen.com/subject/bzbraxtx.html