MyBaits:如何接收Mysql存储过程多个返回集
2018-01-11 15:43
393 查看
我测试所用的表分别为app_action、app_action_type、integral_sign_source内容随便,大家可以随便建立;
存储过程名为:mytest 创建语句如下:
DROP PROCEDURE IF EXISTS `mytest`$$
CREATE DEFINER=`wsrp`@`%` PROCEDURE `mytest`()
BEGIN
SELECT COUNT(1) AS test1 FROM app_action;
SELECT COUNT(1) AS test2 FROM app_action_type;
SELECT COUNT(1) AS test3 FROM integral_sign_source;
END$$
DELIMITER ;Service层代码
package com.hori.jfms.service;
import java.util.List;
public interface TestService {
List<?> test ();
}
Service层实现类代码
package com.hori.jfms.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hori.jfms.mapper.TestMapper;
import com.hori.jfms.service.TestService;
/**
* app用户行为service实现
* @author laizs
* @time 2018年1月2日上午11:17:11
*/
@Service("testService")
public class TestServiceImpl implements TestService{
@Autowired
private TestMapper testMapper;
@Override
public List<?> test() {
return testMapper.test();
}
}
Mapper接口代码
package com.hori.jfms.mapper;
import java.util.List;
public interface TestMapper {
List<?> test();
}Mapper.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" >
<mapper namespace="com.hori.jfms.mapper.TestMapper" >
<resultMap id="test1" type="Integer" >
<result column="test1" jdbcType="INTEGER" javaType="Integer" />
</resultMap>
<resultMap id="test2" type="Integer" >
<result column="test2" jdbcType="INTEGER" javaType="Integer" />
</resultMap>
<resultMap id="test3" type="Integer" >
<result column="test3" jdbcType="INTEGER" javaType="Integer" />
</resultMap>
<select id="test" statementType="CALLABLE" resultMap="test1,test2,test3" >
CALL mytest()
</select>
</mapper>测试类
package com.hori.jfms;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.hori.jfms.model.IntegralSource;
import com.hori.jfms.service.TestService;
@RunWith(SpringJUnit4ClassRunner.class)
@org.springframework.boot.test.context.SpringBootTest(classes=JfmsSrvApplication.class)// 指定spring-boot的启动类
public class ConsumerTest {
@Autowired
private TestService testService;
@Test
public void test(){
List<?> integralSource = testService.test();
System.out.println("test1:"+((List<Integer>)integralSource.get(0)).get(0));//注意返回集是List对象
System.out.println("test2:"+((List<Integer>)integralSource.get(1)).get(0));
System.out.println("test3:"+((List<Integer>)integralSource.get(2)).get(0));
}
}
测试结果:
test1:18
test2:3
test3:7作者用框架为SpringBoot,谢谢大家!!
存储过程名为:mytest 创建语句如下:
DROP PROCEDURE IF EXISTS `mytest`$$
CREATE DEFINER=`wsrp`@`%` PROCEDURE `mytest`()
BEGIN
SELECT COUNT(1) AS test1 FROM app_action;
SELECT COUNT(1) AS test2 FROM app_action_type;
SELECT COUNT(1) AS test3 FROM integral_sign_source;
END$$
DELIMITER ;Service层代码
package com.hori.jfms.service;
import java.util.List;
public interface TestService {
List<?> test ();
}
Service层实现类代码
package com.hori.jfms.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hori.jfms.mapper.TestMapper;
import com.hori.jfms.service.TestService;
/**
* app用户行为service实现
* @author laizs
* @time 2018年1月2日上午11:17:11
*/
@Service("testService")
public class TestServiceImpl implements TestService{
@Autowired
private TestMapper testMapper;
@Override
public List<?> test() {
return testMapper.test();
}
}
Mapper接口代码
package com.hori.jfms.mapper;
import java.util.List;
public interface TestMapper {
List<?> test();
}Mapper.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" >
<mapper namespace="com.hori.jfms.mapper.TestMapper" >
<resultMap id="test1" type="Integer" >
<result column="test1" jdbcType="INTEGER" javaType="Integer" />
</resultMap>
<resultMap id="test2" type="Integer" >
<result column="test2" jdbcType="INTEGER" javaType="Integer" />
</resultMap>
<resultMap id="test3" type="Integer" >
<result column="test3" jdbcType="INTEGER" javaType="Integer" />
</resultMap>
<select id="test" statementType="CALLABLE" resultMap="test1,test2,test3" >
CALL mytest()
</select>
</mapper>测试类
package com.hori.jfms;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.hori.jfms.model.IntegralSource;
import com.hori.jfms.service.TestService;
@RunWith(SpringJUnit4ClassRunner.class)
@org.springframework.boot.test.context.SpringBootTest(classes=JfmsSrvApplication.class)// 指定spring-boot的启动类
public class ConsumerTest {
@Autowired
private TestService testService;
@Test
public void test(){
List<?> integralSource = testService.test();
System.out.println("test1:"+((List<Integer>)integralSource.get(0)).get(0));//注意返回集是List对象
System.out.println("test2:"+((List<Integer>)integralSource.get(1)).get(0));
System.out.println("test3:"+((List<Integer>)integralSource.get(2)).get(0));
}
}
测试结果:
test1:18
test2:3
test3:7作者用框架为SpringBoot,谢谢大家!!
相关文章推荐
- 如何接收存储过程的返回值
- 如何在mysql存储过程中处理select语句返回的多行结果(用游标)
- 关于mysql存储过程,返回多个表数据的接收问题。
- 如何用 MySQL 进行存储过程编写,以用户登陆为例,结构清晰,涵盖内容很多
- 如何在Delphi中调用oracle的存储过程返回数据集
- 【转】ibatis中mysql存储过程返回结果集
- 关于使用表接收存储过程返回结果集的问题
- ASP.NET如何调用MySQL的存储过程
- Mysql存储过程内中文乱码,如何处理
- 如何在MySQL存储过程中保留注释
- 如何在Delphi中调用oracle的存储过程返回数据集
- mysql如何在一个存储过程中使用另外一个存储过程的结果集
- 如何在MySQL存储过程中保留注释
- MySQL存储过程里字符串拼接返回参数的方法
- Oracle在存储过程中如何返回结果集
- 在.net中接收存储过程返回的值
- 怎样接收存储过程的返回值
- MySQL:存储过程详解和如何获得 MySQL 毫秒、微秒
- Java调用MySQL存储过程并获得返回值的方法
- 如何实现对多台mysql实例的存储过程definer进行批量修改