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);
    }
    }
     
    
    
  • 相关阅读:
    Tomcat example 应用信息泄漏漏洞及修复
    Tomcat远程代码执行漏洞(CVE-2017-12615)修复
    Oracle安装和使用说明
    关于乱码的问题
    js 判断字符串中是否包含某个字符串
    com.alibaba.fastjson.JSONPathException: expect '], but 'y'
    java.util.UnknownFormatConversionException: Conversion = ''';
    nested exception is java.lang.NoClassDefFoundError: org/codehaus/jettison/json/JSONObject异常的解决办法
    复选框做成单选的效果
    sFlow-rt安装部署
  • 原文地址:https://www.cnblogs.com/sabertobih/p/13408267.html
Copyright © 2011-2022 走看看