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();
  • 相关阅读:
    WPF 实现窗体拖动
    CAP带你轻松玩转ASP.NETCore消息队列
    ASP.NET Core开发-获取所有注入(DI)服务
    k8s实战为aspnetcore.webapi微服务注入配置信息
    详解docker实战之搭建私有镜像仓库
    自己动手破解Z.EntityFramework.Extensions 4.0.11.0的方法
    可以获取随机图片的API收集 必应 等
    一键发布部署vs插件[AntDeploy],让net开发者更幸福
    比ngx_http_substitutions_filter_module 更强大的替换模块sregex的replace-filter-nginx-module
    编译nginx的源码安装subs_filter模块
  • 原文地址:https://www.cnblogs.com/mayouyou/p/13218882.html
Copyright © 2011-2022 走看看