论坛首页 Web前端技术论坛

mybatis 调用mysql存储过程 带输出输入参数

浏览 10571 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2013-10-17  

首先感谢网络上以为朋友。其实我不认识。看到他的博客才做出来。

http://lohasle.iteye.com/blog/1669879 他的地址。下面的是我一些简单拓展的

 

 

存储过程都是一样的,只是根据自己的喜好,可以用MAP或者JAVABEAN传递参数。

-- --------------------------------------------------------------------------------
-- Routine DDL
-- Note: comments before and after the routine body will not be stored by the server
-- --------------------------------------------------------------------------------
DELIMITER $$

CREATE DEFINER=`root`@`localhost` PROCEDURE `selectCount`(
	IN pcsId int,
	IN drId int,
	IN partnerId int,
	IN customerId int,
	OUT pcsCount int,
	OUT drCount int
	
)
BEGIN
	
	select count(md.id) into @pcsC from mdm_device md 
left join mdm_device_security mds on mds.device_id = md.id
where mds.device_rooted = pcsId
and md.partner_id = partnerId and md.customer_id = customerId;
set pcsCount = @pcsC;


select count(md.id) into @drC from mdm_device md
where md.managed_status = drId and  DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(md.un_manage_date)
and md.partner_id = partnerId and md.customer_id = customerId;
set drCount = @drC;



END

 

 

1.java调用传入MAP。获取通过MAP获取。

1.1 mapper文件写法

<parameterMap type="map" id="homeVO">  
   <parameter property="pcsId" jdbcType="INTEGER" mode="IN"/>  
   <parameter property="drId" jdbcType="INTEGER" mode="IN"/>  
   <parameter property="partnerId" jdbcType="INTEGER" mode="IN"/>  
   <parameter property="customerId" jdbcType="INTEGER" mode="IN"/>  
   <parameter property="pcsCount" jdbcType="INTEGER" mode="OUT"/>  
   <parameter property="drCount" jdbcType="INTEGER" mode="OUT"/>  
 </parameterMap>  
<select id="selectForHome" parameterMap="homeVO" 
      statementType="CALLABLE">
      {call selectCount(
      	?,?,?,?,?,?
      )}
  </select>

 1.2 java调用写法

@Override
	public StringselectHomeCount(HomeVO home) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("pscId", 0);  
		map.put("drId", 1);
		map.put("partnerId", 25);
		map.put("customerId", 50);
		map.put("isolation", 1);
		selectOne("Mapper.selectForHome", map);
		System.out.println(map.get("pcsCount"));
		
	        return map.get("drCount");
	}

 

 

2.java调用传入javaBean。返回值通过javaBean属性获取

2.1 通过javabean传递参数  mapper写法

 <parameterMap type="com.polysaas.mdm.device.entity.HomeVO" id="home">
		 <parameter property="pcsId" jdbcType="INTEGER" mode="IN"/>  
	   <parameter property="drId" jdbcType="INTEGER" mode="IN"/>  
	   <parameter property="partnerId" jdbcType="INTEGER" mode="IN"/>  
	   <parameter property="customerId" jdbcType="INTEGER" mode="IN"/>  
	   <parameter property="pcsCount" jdbcType="INTEGER" mode="OUT"/>  
	   <parameter property="drCount" jdbcType="INTEGER" mode="OUT"/>
  </parameterMap>

 javaBean可以通过两种来进行映射

<resultMap type="com.polysaas.mdm.device.entity.HomeVO" id="home">
		<result column="partnerId" property="partnerId" jdbcType="INTEGER"/>
		<result column="customerId" property="customerId" jdbcType="INTEGER" />
		<result column="pcsId" property="pcsId" jdbcType="INTEGER" />
		<result column="drId" property="drId" jdbcType="INTEGER" />
		<result column="pcsCount" property="pcsCount" jdbcType="INTEGER" />
		<result column="drCount" property="drCount" jdbcType="INTEGER" />
  </resultMap>
  <select id="selectForHome2" parameterType="com.polysaas.mdm.device.entity.HomeVO" 
      statementType="CALLABLE">
      {call selectCount(
      	#{pcsId,jdbcType=INTEGER, mode=IN},
      	#{drId, jdbcType=INTEGER,mode=IN},
      	#{partnerId, jdbcType=INTEGER,mode=IN},
      	#{customerId, jdbcType=INTEGER,mode=IN},
      	#{pcsCount, jdbcType=INTEGER,mode=OUT},
      	#{drCount, jdbcType=INTEGER,mode=OUT}
      )}
  </select>

 2.2 通过javabean传递参数  java调用写法

@Override

public HomeVO selectHomeCount(HomeVO home) throws Exception {

HomeVO vo = new HomeVO();

vo.setPcsId(0);

vo.setDrId(0);

vo.setPartnerId(25);

vo.setCustomerId(50);

vo.setPcsCount(0);

vo.setDrCount(0);

selectOne("com.polysaas.mdm.device.mapper.MdmDeviceMapper.selectForHome2", vo);

return vo;

}

 

遇到异常很多。。记得的写下

1.通过javaBean的时候,#{pcsId, mode=IN, javaType=INTEGER},javaType这个属性是必须的。

3.map可以用占位符问号,javaBean不可以。  会有异常 No value specified for parameter 1

论坛首页 Web前端技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics