• 如何使用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);
    }
    }
     
    
    
  • 相关阅读:
    CF263E Rhombus
    AtCoder Grand Contest 034
    Docker C/S 架构逻辑图
    使用filledeat modules配置
    filebeat分别收集多个类型日志
    ELK部署收集日志
    ES界面
    Django下的post请求访问出现403错误
    Django配置(urls.py)
    Day-1 python
  • 原文地址:https://www.cnblogs.com/sabertobih/p/13408267.html
Copyright © 2020-2023  润新知