mysql 查询某个年级_mybatis调用mysql的存储过程(procedure),实现查询操作(student表中的某个年级中的总人数 select (1) 或者 select (*))...

step1:在mysql cmd中新建存储过程:

drop procedure ifexists queryCountByGrade ;

delimiter//-- 定义存储过程结束符号为//

create procedure queryCountByGrade(IN gradenameinput INT(11),OUT counts int(11)

beginselect count(*) into counts from student where grade = gradenameinput;end //delimiter ;--重新定义存储过程的结束符号是分号

step2:编写StudentMapper.xml文件

CALL queryCountByGrade( #{gradenameinput,mode=IN,jdbcType=VARCHAR}, #{counts,mode=OUT,jdbcType=INTEGER}

//queryCountByGrade:就是mysql存储过程名称

)

step3:编写StudentManager.java 接口文件

//使用存储过程实现查询的操作

void queryCountByGradeWithProcedure(Map mapp);

step4:测试函数

/**

* 使用存储过程实现数量的查询,某个年级的人数

* @param args

* @throws IOException

*/

/**

* 带转换器

* @throws IOException

*/

public static void throughProcedureToQueryCountByGradeName() throws IOException

{

Reader reader = Resources.getResourceAsReader("conf.xml");

SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);

SqlSession session = sessionFactory.openSession();//就是session名字即可

StudentManager studentManager = session.getMapper(StudentManager.class);

Mapmapp = new HashMap();//通过Map给存储过程,指定输入参数gradenameinput

//gradenameinput和counts都是mysql中存储过程定义时候的,名称,跟CALL queryCountByGrade(#{},#{})里的是一致的

mapp.put("gradenameinput", 5);//指定存储过程的出入参数gradenameinput,是5

studentManager.queryCountByGradeWithProcedure(mapp);//调用存储过程

Object obj = mapp.get("counts");//获取存储过程的输出参数:counts

System.out.println(obj);

session.close();

}//queryonestudentwithconverser()

原文:https://www.cnblogs.com/guofen3399/p/11830305.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值