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);
}
}