http://lohasle.iteye.com/blog/1669879
存储过程都是一样的,只是根据自己的喜好,可以用MAP或者JAVABEAN传递参数。
Sql代码
- -- --------------------------------------------------------------------------------
- -- 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文件写法
Xml代码
- <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调用写法
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写法
Xml代码
- <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可以通过两种来进行映射
Xml代码
- <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调用写法
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这个属性是必须的。
2.使用javaBean传递。输出参数不需要初始值。个人喜好并建议使用javaBean,因为定义更清晰,封装性。
3.map可以用占位符问号,javaBean不可以。 会有异常 No value specified for parameter 1
4. 说不认识某个参数。。可能是没有创建存储过程,这个发生在多个库的时候
5. read Only 这个可能是事务控制只读。。我是因为存储过程有set 语句,而方法叫做selectAaaa()。
时间: 2024-10-10 00:37:36