• 5.MyBaits调用存储过程


    1.创建一个javaweb项目MyBatis_Part4_Procedure

    2.在src下创建procedure.sql文件

     1 --创建表
     2 create table p_user(  
     3     id number primary key,  
     4     name varchar2(20),
     5     sex varchar2(2)
     6 ); 
     7 
     8 create sequence seq_puser;
     9 
    10 insert into p_user values(seq_puser.nextval,'A','');  
    11 insert into p_user values(seq_puser.nextval,'B','');  
    12 insert into p_user values(seq_puser.nextval,'C','');  
    13 
    14 commit;
    15 
    16 --统计男女的数量
    17 CREATE or replace PROCEDURE holly.get_user_count(sex_id IN number,user_count OUT number)
    18 is
    19 BEGIN 
    20 IF sex_id=0 THEN
    21  SELECT COUNT(*) INTO user_count FROM p_user WHERE p_user.sex='';
    22 ELSE
    23   SELECT COUNT(*) INTO user_count FROM p_user WHERE p_user.sex='' ;
    24 END IF;
    25 END ;
    26 /
    procedure.sql

    3.在src下创建jdbc.properties属性文件

    1 driver=oracle.jdbc.driver.OracleDriver
    2 url=jdbc:oracle:thin:@127.0.0.1:1521:orcl
    3 username=holly
    4 password=sys
    jdbc.properties

    4.在src下创建configuration.xml配置文件

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd" >
     3 <configuration>
     4 
     5   <!-- 1.引用jdbc链接数据库的属性文件 -->
     6   <properties resource="jdbc.properties"/>
     7   
     8   <!-- 2.类路径的别名 --><!--
     9   <typeAliases>
    10     <typeAlias type="com.entity.Classes" alias="Classes"/>
    11     <typeAlias type="com.entity.Teacher" alias="Teacher"/>
    12     <typeAlias type="com.entity.Student" alias="Student"/>
    13   </typeAliases>
    14   
    15   --><!-- 3.事务和数据源的配置 -->
    16   <environments default="development">
    17     <environment id="development">
    18       <transactionManager type="jdbc"/>
    19       <dataSource type="POOLED">
    20         <property name="driver" value="${driver}"/>
    21         <property name="url" value="${url}"/>
    22         <property name="username" value="${username}"/>
    23         <property name="password" value="${password}"/>
    24       </dataSource>
    25     </environment>
    26   </environments>
    27   <!-- 4.注册映射文件 -->
    28   <mappers>
    29   <!-- 有映射文件时resource属性=映射文件的路径 -->
    30   <!-- 使用注解没有映射文件时class属性=映射文件的路径 -->
    31   <mapper resource="com/mapper/PUserMapper.xml"/>
    32    
    33   </mappers>
    34   
    35 </configuration>
    configuration.xml

    5.在src下com.entity创建PUser.java

     1 package com.entity;
     2 
     3 public class PUser {
     4     private int id;
     5     private String name;
     6     private String sex;
     7     
     8     public PUser() {
     9     }
    10     public PUser(int id, String name, String sex) {
    11         this.id = id;
    12         this.name = name;
    13         this.sex = sex;
    14     }
    15     public int getId() {
    16         return id;
    17     }
    18     public void setId(int id) {
    19         this.id = id;
    20     }
    21     public String getName() {
    22         return name;
    23     }
    24     public void setName(String name) {
    25         this.name = name;
    26     }
    27     public String getSex() {
    28         return sex;
    29     }
    30     public void setSex(String sex) {
    31         this.sex = sex;
    32     }
    33     @Override
    34     public String toString() {
    35         return "PUser [id=" + id + ", name=" + name + ", sex=" + sex + "]";
    36     }
    37     
    38     
    39 
    40 }
    PUser.java

    6.在src下com.util包下创建MyBaitsUtil.java

     1 package com.util;
     2 
     3 import java.io.IOException;
     4 import java.io.Reader;
     5 
     6 import org.apache.ibatis.io.Resources;
     7 import org.apache.ibatis.session.SqlSession;
     8 import org.apache.ibatis.session.SqlSessionFactory;
     9 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
    10 
    11 public class MyBaitsUtil {
    12     private static SqlSessionFactory sqlSessionFactory;
    13     
    14     static{
    15         try {
    16             Reader reader=Resources.getResourceAsReader("configuration.xml");
    17             sqlSessionFactory=new SqlSessionFactoryBuilder().build(reader); 
    18         } catch (IOException e) {
    19             e.printStackTrace();
    20         }
    21         
    22     }
    23     /**
    24      * 
    25      * @param isCommit true表示执行完操作提交事务,false表示执行完不自动提交事务
    26      * @return
    27      */
    28     public static SqlSession getSqlSession(boolean isCommit){
    29         return sqlSessionFactory.openSession(isCommit);
    30     }
    31 
    32 }
    MyBaitsUtil.java

    7.在src下com.mapper包下创建PUserMapper.java

     1 package com.mapper;
     2 
     3 import java.util.Map;
     4 
     5 public interface PUserMapper {
     6     /**
     7      * 统计某个性别的数据
     8      * @param paramMap
     9      * @return
    10      */
    11     int getCountBySex(Map<String, Integer> paramMap);
    12 
    13 }
    PUserMapper.java

    8.在src下com.mapper包下创建PUserMapper.xml

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
     3 <mapper namespace="com.mapper.PUserMapper">
     4    <!--parameterMap表示参数是一个集合,
     5    statementType="CALLABLE" 表示该节点下写的是调用存储过程的语句 -->
     6    <select id="getCountBySex" parameterMap="paramMap" 
     7      statementType="CALLABLE">
     8       CALL holly.get_user_count(?,?)
     9    </select>
    10    <!-- 配置参数集合 -->
    11    <parameterMap id="paramMap" type="java.util.Map" >
    12      <!-- 配置集合的输入参数 -->
    13     <parameter property="sexid" mode="IN" jdbcType="INTEGER"/>
    14     <!-- 配置集合的输出参数 -->
    15     <parameter property="usercount" mode="OUT" jdbcType="INTEGER"/>
    16    </parameterMap>
    17    
    18 </mapper>
    PUserMapper.xml

    9.在src下com.mapper.impl包下创建PUserMapperImpl.java

     1 package com.mapper.impl;
     2 
     3 import java.util.Map;
     4 
     5 import org.apache.ibatis.session.SqlSession;
     6 
     7 import com.mapper.PUserMapper;
     8 import com.util.MyBaitsUtil;
     9 
    10 public class PUserMapperImpl implements PUserMapper {
    11    
    12     public int getCountBySex(Map<String, Integer> paramMap) {
    13         SqlSession sqlSession=MyBaitsUtil.getSqlSession(false);
    14         String statement="com.mapper.PUserMapper.getCountBySex";
    15         //调用存储过程,输出参数参数给usercount
    16         sqlSession.selectOne( statement, paramMap);
    17         //通过key获取value
    18         int count=paramMap.get("usercount");
    19         sqlSession.close();
    20         return count;
    21     }
    22 
    23 }
    PUserMapperImpl.java

    10.在src下com.test包下创建Test.java

     1 package com.test;
     2 
     3 import java.util.HashMap;
     4 import java.util.Map;
     5 
     6 import com.mapper.PUserMapper;
     7 import com.mapper.impl.PUserMapperImpl;
     8 
     9 public class Test {
    10 
    11     /**
    12      * @param args
    13      */
    14     public static void main(String[] args) {
    15         PUserMapper mapper=new PUserMapperImpl();
    16         
    17         Map<String, Integer> paramMap=new HashMap<String, Integer>();
    18         paramMap.put("sexid", 0);//0女 1男
    19         paramMap.put("usercount", -1);
    20         
    21         int count=mapper.getCountBySex(paramMap);
    22         
    23         if(count>0){
    24             System.out.println("男员工的数目为:"+count);
    25         }
    26 
    27     }
    28 
    29 }
    Test.java
  • 相关阅读:
    libevent的问题
    mysql homedir迁移
    mysql海量数据的优化
    Spark(Hive) SQL中UDF的使用(Python)【转】
    Spark SQL inferSchema实现原理探微(Python)【转】
    Spark SQL利器:cacheTable/uncacheTable【转】
    Spark使用CombineTextInputFormat缓解小文件过多导致Task数目过多的问题【转】
    ExecutorService-10个要诀和技巧【转】
    漫游Kafka之过期数据清理【转】
    kafka多线程消费及处理和手动提交处理方案设计[转]
  • 原文地址:https://www.cnblogs.com/holly8/p/5797739.html
Copyright © 2020-2023  润新知