您的位置:首页 > 数据库 > MySQL

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,谢谢大家!!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: