• 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();
  • 相关阅读:
    Jenkins安装
    Python操作yaml文件
    class 中构造函数与析构函数
    python发送邮件(yagmail模块)
    filter、map函数的区别
    python redis操作
    多个 python的pip版本选择
    python Excel操作
    python MD5操作
    缓存淘汰算法之LRU实现
  • 原文地址:https://www.cnblogs.com/mayouyou/p/13218882.html
Copyright © 2020-2023  润新知