时间:2021-07-01 10:21:17 帮助过:28人阅读
call sp_name()
注意:存储过程名称后面必须加括号,哪怕该存储过程没有参数传递
drop procedure sp_name//
注意:不能在一个存储过程中删除另一个存储过程,只能调用另一个存储过程
show procedure status
显示数据库中所有存储的存储过程基本信息,包括所属数据库,存储过程名称,创建时间等
show create procedure sp_name
显示某一个MySQL存储过程的详细信息
案例主要以一个简单的统计某种名称设备的总数来实现。
DROP TABLE IF EXISTS `cus_device`;
CREATE TABLE `cus_device` (
`device_sn` varchar(20) NOT NULL COMMENT ‘设备编号‘,
`device_cat_id` int(1) DEFAULT NULL COMMENT ‘设备类型‘,
`device_name` varchar(64) DEFAULT NULL COMMENT ‘设备名称‘,
`device_type` varchar(64) DEFAULT NULL COMMENT ‘设备型号‘,
PRIMARY KEY (`device_sn`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
以设备的名称为输入参数,以统计到设备的总数为输出参数
DROP PROCEDURE IF EXISTS `countDevicesName`;
DELIMITER ;;
CREATE PROCEDURE `countDevicesName`(IN dName VARCHAR(12),OUT deviceCount INT)
BEGIN
SELECT COUNT(*) INTO deviceCount FROM cus_device WHERE device_name = dName;
END
;;
DELIMITER ;
<?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>
<settings>
<!-- 打印查询语句 -->
<setting name="logImpl" value="STDOUT_LOGGING" />
</settings>
<!-- 配置别名 -->
<typeAliases>
<typeAlias type="com.lidong.axis2demo.DevicePOJO" alias="DevicePOJO" />
</typeAliases>
<!-- 配置环境变量 -->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://127.0.0.1:3306/bms?characterEncoding=GBK" />
<property name="username" value="root" />
<property name="password" value="123456" />
</dataSource>
</environment>
</environments>
<!-- 配置mappers -->
<mappers>
<mapper resource="com/lidong/axis2demo/DeviceMapper.xml" />
</mappers>
</configuration>
public class DevicePOJO{
private String devoceName;//设备名称
private String deviceCount;//设备总数
public String getDevoceName() {
return devoceName;
}
public void setDevoceName(String devoceName) {
this.devoceName = devoceName;
}
public String getDeviceCount() {
return deviceCount;
}
public void setDeviceCount(String deviceCount) {
this.deviceCount = deviceCount;
}
}
package com.lidong.axis2demo;
public interface DeviceDAO {
/**
* 调用存储过程 获取设备的总数
* @param devicePOJO
*/
public void count(DevicePOJO devicePOJO);
}
<?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">
<mapper namespace="com.lidong.axis2demo.DeviceDAO">
<resultMap id="BaseResultMap" type="CusDevicePOJO">
<result column="device_sn" property="device_sn" jdbcType="VARCHAR" />
</resultMap>
<sql id="Base_Column_List">
device_sn, device_name,device_mac
</sql>
<select id="count" parameterType="DevicePOJO" useCache="false"
statementType="CALLABLE">
<![CDATA[
call countDevicesName(
#{devoceName,mode=IN,jdbcType=VARCHAR},
#{deviceCount,mode=OUT,jdbcType=INTEGER});
]]>
</select>
</mapper>
注意:statementType=”CALLABLE” 必须为CALLABLE,告诉MyBatis去执行存储过程, 否则会报错
Exception in thread “main” org.apache.ibatis.exceptions.PersistenceException
mode=IN 输入参数 mode=OUT输出参数 jdbcType为数据库定义的字段类型。
这样写 Mybatis会帮助我们自动回填输出的deviceCount的值。
package com.lidong.axis2demo;
import java.io.IOException;
import java.io.Reader;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
/**
* MyBatis 执行存储过程
* @author Administrator
*
*/
public class TestProduce {
private static SqlSessionFactoryBuilder sqlSessionFactoryBuilder;
private static SqlSessionFactory sqlSessionFactory;
private static void init() throws IOException {
String resource = "mybatis-config.xml";
Reader reader = Resources.getResourceAsReader(resource);
sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
sqlSessionFactory = sqlSessionFactoryBuilder.build(reader);
}
public static void main(String[] args) throws Exception {
testCallProduce();
}
/**
* @throws IOException
*/
private static void testCallProduce() throws IOException {
init();
SqlSession session= sqlSessionFactory.openSession();
DeviceDAO deviceDAO = session.getMapper(DeviceDAO.class);
DevicePOJO device = new DevicePOJO();
device.setDevoceName("设备名称");
deviceDAO.count(device);
System.out.println("获取"+device.getDevoceName()+"设备的总数="+device.getDeviceCount());
}
}
结果
Mybatis调用MySQL存储过程
标签:计划 utf8 网络流量 编号 lis inno pen status select