MyBaits:如何接收Mysql存储过程多个返回集

我测试所用的表分别为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,谢谢大家!!



  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值