时间:2021-07-01 10:21:17 帮助过:4人阅读
step2:编写StudentMapper.xml文件
- <!-- 通过mybatis调用存储过程procedure,来实现查询功能 ,statementType=<span style="color: #800000;">"</span><span style="color: #800000;">CALLABLE</span><span style="color: #800000;">"</span> -->
- <<span style="color: #0000ff;">select</span> id=<span style="color: #800000;">"</span><span style="color: #800000;">queryCountByGradeWithProcedure</span><span style="color: #800000;">"</span> statementType=<span style="color: #800000;">"</span><span style="color: #800000;">CALLABLE</span><span style="color: #800000;">"</span> parameterType=<span style="color: #800000;">"</span><span style="color: #800000;">HashMap</span><span style="color: #800000;">"</span>><span style="color: #000000;">
- CALL queryCountByGrade( #{gradenameinput,mode</span>=IN,jdbcType=VARCHAR}, #{counts,mode=OUT,jdbcType=<span style="color: #000000;">INTEGER} </span>
- <span> //queryCountByGrade:就是mysql存储过程名称</span>
- <span style="color: #000000;">) </span></<span style="color: #0000ff;">select</span>>
step3:编写StudentManager.java 接口文件
- <span style="color: #008000;">//</span><span style="color: #008000;">使用存储过程实现查询的操作</span>
- <span style="color: #0000ff;">void</span> queryCountByGradeWithProcedure(Map<String,Object> 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);
- Map<String ,Object>mapp = new HashMap();//通过Map给存储过程,指定输入参数gradenameinput<br> //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()
mybatis调用mysql的存储过程(procedure),实现查询操作(student表中的某个年级中的总人数 select (1) 或者 select (*))
标签:编写 exception 就是 param into sql rop nts sele