zoukankan      html  css  js  c++  java
  • MyBatis调用存储过程执行CRUD

    输入对象为HashMap:

    where stuage=#{stuAge}

    用map中的key的值匹配占位符#{stuAge},如果匹配成功就用map的value替换占位符

    Map<String, Object> studentMap=new HashMap<>();
            studentMap.put("stuAge", 23);
            studentMap.put("stuName","lss");
            
            List<Student> students = studentMapper.queryStudentByStuageOrStunameWtihHashMap(studentMap);
    
    
    <select id="queryStudentByStuageOrStunameWtihHashMap" parameterType="HashMap" resultType="student">
        select stuno,stuname,stuage from student1 where stuage=#{stuAge} or stuname like '%${stuName}%'
    </select>

    MyBatis调用存储过程

    mapper.xml->mapper接口->测试方法

    1.查询某个年级的学生总数

    输入:年级

    输出:该年级的学生总数

    create or replace procedure queryCountByGradeWithProcedure(gName in varchar,scount out number)
    as begin
    select count(1) into scount from student1 where graname=gname;
    end;
    /
    //创建存储过程
    <select id="queryCountByGradeWithProcedure" statementType="CALLABLE"  parameterType="HashMap">
            {
                CALL queryCountByGradeWithProcedure(
                #{gName,jdbcType=VARCHAR,mode=IN},
                #{sCount,jdbcType=INTEGER,mode=OUT}
                )
            }
    </select>
    
    //其中通过statementType="CALLABLE"设置Sql的执行方式是存储过程,存储过程的输出参数gName需要通过HashMap
    
    Map<String, Object> params=new HashMap<>();
                params.put("gName", "g1");//指定存储过程的输入参数的gName的值是g1
                
    studentMapper.queryCountByGradeWithProcedure(params);
    //获取存储过程的输出参数
    int count=(int) params.get("sCount");
    
    //在使用时,通过hashmap的put方法传入输入参数的值,通过hashmap的get方法获取输出参数的值

    No enum constant org.apache.ibatis.type.JdbcType.xxx,则说明mybatis不支持xx类型,需要查表

    2.根据学号删除学生

    create or replace procedure deleteBysnoWithProcedure(sno in number)
    as
    begin
    delete from student1 where stuno=sno;
    end;
    /

    存储过程无论输入参数是什么值,语法上都需要map来传递该值

    只要是<transactionManager type="JDBC"/>,则增删改都需要手工commit

    <!--通过存储过程删除  -->
    <delete id="deleteBysnoWithProcedure" statementType="CALLABLE" parameterType="HashMap">
        {
            CALL deleteBysnoWithProcedure(
                #{sno,jdbcType=INTEGER,mode=IN}
            )
        }
    </delete>
    
    
    Map<String, Object> map=new HashMap<>();
    map.put("sno", "3");
                    
    studentMapper.deleteBysnoWithProcedure(map);
    session.commit();
  • 相关阅读:
    0593. Valid Square (M)
    0832. Flipping an Image (E)
    1026. Maximum Difference Between Node and Ancestor (M)
    0563. Binary Tree Tilt (E)
    0445. Add Two Numbers II (M)
    1283. Find the Smallest Divisor Given a Threshold (M)
    C Primer Plus note9
    C Primer Plus note8
    C Primer Plus note7
    C Primer Plus note6
  • 原文地址:https://www.cnblogs.com/mayouyou/p/13218882.html
Copyright © 2011-2022 走看看