创建存储过程:根据输入班级查询该班级学生个数
存储过程知识点:
1.创建存储过程
在数据库中创建getStudentByStuClass存储过程(注意输入名和字段名不同)
DELIMITER $
CREATE PROCEDURE getStudentByStuClass(IN input INT, OUT stu_count INT)
BEGIN
SELECT COUNT(1) INTO stu_count FROM Student WHERE stuClass = input;
END
$
2.调用getStudentByStuClass存储过程
查询3班学生人数
DELIMITER ;
SET @stu_count = 0;
CALL getStudentByStuClass(3, @stu_count);
SELECT @stu_count;
3.查看存储过程
smbms:数据库名
SHOW PROCEDURE STATUS WHERE db='smbms';
4.删除存储过程
DROP PROCEDURE smbms.getStudentByStuClass;
Mybatis调用存储过程流程
所需要用到的jar包 mybatis-3.5.1.jar 和 mysql-connector-java-5.1.0-bin.jar
1.配置mybatis-config.xml
创建mybatis-config.xml
这里需要配置几个基本配置
- database.properties资源配置
- 类型别名配置
- mapper映射配置
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!--配置properties资源文件-->
<properties resource="database.properties"></properties>
<!--配置类型别名-->
<typeAliases>
<!--单个别名-->
<!--<typeAlias type="com.zy.Student" alias="student"></typeAlias>-->
<!--通过包名批量定义别名,使用直接用类名即别名-->
<package name="com.zy.pojo" />
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<!--配置mapper映射 StudentMapper.xml全路径 -->
<mappers>
<mapper resource="com/zy/mapper/StudentMapper.xml"/>
</mappers>
</configuration>
database.properties配置信息
创建database.properties
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/smbms
username=root
password=root
2.创建mapper接口
创建StudentMapper.java接口
package com.zy.mapper;
import java.util.Map;
public interface StudentMapper {
//这里通过Map取值和赋值,所以不需要返回值
void getStudentCount(Map<String, Object> stuClassMap);
}
4.创建mapper映射
创建StudentMapper.xml映射文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--
namespace:mapper的位置
id:接口的方法名
resultType:返回值类型
-->
<mapper namespace="com.zy.mapper.StudentMapper">
<!-- 通过存储过程根据班级查询学生个数
要实现动态传入表名、列名,需要做如下修改
添加属性statementType=”STATEMENT” statementType="CALLABLE"设置SQL的执行方式是存储过程,
stuClass,stu_count通过HashMap拿值和设置
-->
<!-- 第一种设置HashMap方式
mode=IN:输入
mode=OUT:输出
#{key,mode=IN,jdbcType=数据库字段类型},
-->
<select id="getStudentCount" parameterType="HashMap" statementType="CALLABLE">
{
<!--调用存储过程-->
call getStudentByStuClass
(
#{stuClass,mode=IN,jdbcType=VARCHAR},
#{stu_count,mode=OUT,jdbcType=INTEGER}
)
}
</select>
<!-- 第二种设置HashMap方式
<select id="getStudentCount" parameterMap="getStuCountMap" statementType="CALLABLE">
call getStudentByStuClass(?,?)
</select>
<parameterMap type="java.util.Map" id="getStuCountMap">
<parameter property="stuClass" mode="IN" jdbcType="VARCHAR"/>
<parameter property="stu_count" mode="OUT" jdbcType="INTEGER"/>
</parameterMap>
-->
</mapper>
5.测试类
创建StudentTest.java测试类
package com.zy.test;
import java.io.IOException;
import java.io.Reader;
import java.util.List;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import com.zy.mapper.StudentMapper;
import com.zy.pojo.Student;
public class StudentTest {
public static void main(String[] args){
getStudentCount();
}
// 通过存储过程根据班级查询学生个数
private static void getStudentCount() throws IOException {
Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader);
SqlSession sqlSession = factory.openSession();
// 通过HashMap传值
Map<String,Object> stuClassMap = new HashMap<>();
stuClassMap.put("stuClass", "3");
sqlSession.getMapper(StudentMapper.class).getStudentCount(stuClassMap);
// 通过HashMap取值
int count = (int) stuClassMap.get("stu_count");
System.out.println("学生人数"+count);
sqlSession.close();
}
}
6.运行结果
7.项目结构
解析一下:
-
存储过程通过HashMap赋值,所以没有返回值,通过key-value拿值赋值
-
在Mapper配置文件中调用存储过程