zoukankan      html  css  js  c++  java
  • 如何使用mybatis?(包括存储过程,函数,事务)

    sql语句:
    ------函数--------
    delimiter //
    drop function if exists totalStu;
    
    create function totalStu(pageSize int) returns int
    begin
        declare total int;
        select ceil(count(1)/pagesize) from studentinfo into total;
        return total;
    end;
    //
    ------存储过程---------
    drop procedure if exists proPageStu;
    //
    
    create procedure proPageStu(out total int,in pageNo int,in pageSize int)
    begin
        declare _total int default 0;
        declare _begin int default (pageNo-1)*pageSize;
        select ceil(count(1)/pageSize) from studentinfo into _total;
        set total=_total;
        select * from studentinfo limit _begin,pageSize;
    end;
    //

    public interface StudentInfoMapper 中:
        // 1.函数不能返回结果集
        @Select("select totalStu(#{pageSize})")
        int function(int pageSize);
    
        // 2.存储过程
        List<StudentInfo> proPageStu(HashMap map);
      
      // 3. 存储过程-2:增加PageParam类,包含total,pagesize,pageno入参和出参,其中出参有构造方法

      List<StudentInfo> proPageStu2(PageParam pageParam);
    StudentInfoMapper.xml 
        <select id="proPageStu" resultType="StudentInfo" statementType="CALLABLE" parameterType="map">
            {
              call proPageStu(#{total,mode=OUT,jdbcType=INTEGER},#{pageNo},#{pageSize})
            }
        </select>

    // 自动识别pageparam中的total,pageno和pagesize
      <select id="proPageStu2" resultType="StudentInfo" statementType="CALLABLE" parameterType="PageParam">
    {
    call proPageStu(#{total,mode=OUT,jdbcType=INTEGER},#{pageNo},#{pageSize})
    }
      </select>
    Test
        @Test
        public void func() throws IOException{
            int function = mapper.function(1);
            System.out.println(function);
        }
    
        @Test
        public void prod() throws IOException{
            HashMap map = new HashMap();
            map.put("pageNo",2);
            map.put("pageSize",8);
            List<StudentInfo> list = mapper.proPageStu(map);
    // 存储过程中in和out的值全部放在map中 System.out.println(map.get(
    "total")); for (StudentInfo info : list) { System.out.println(info); } }
    @Test
    public void prod2() throws IOException{
    PageParam param = new PageParam(8,2);
    List<StudentInfo> list = mapper.proPageStu2(param);
    System.out.println(param.getTotal());
    for (StudentInfo info : list) {
    System.out.println(info);
    }
    }
     
    
    
  • 相关阅读:
    软工网络15团队作业4——Alpha阶段敏捷冲刺
    (转) linux目录结构详细介绍
    ActiveMQ使用记录
    .NET4.5中WCF中默认生成的basicHttpsBinding的研究
    StackExchange.Redis的使用
    微信/QQ机器人的实现
    EntityFramework中的datetime2异常的解决
    在Web API中使用Swagger-UI开源组件(一个深坑的解决)
    (转)使用Migrations更新数据库结构(Code First )
    WebApi中帮助页Description的中文显示
  • 原文地址:https://www.cnblogs.com/sabertobih/p/13408267.html
Copyright © 2011-2022 走看看