mysql 存儲過程能夠經過select 能夠直接返回結果集java
咱們寫兩個存儲過程一個有參數,另外一個沒有參數mysql
--無參數 DELIMITER $$ DROP PROCEDURE IF EXISTS `pro1`$$ CREATE DEFINER=`root`@`localhost` PROCEDURE `pro1`() BEGIN select * from course_info; END$$ DELIMITER ; -- 有參數 DELIMITER $$ DROP PROCEDURE IF EXISTS `pro2`$$ CREATE DEFINER=`root`@`localhost` PROCEDURE `pro2`(in idParam int) BEGIN select * from course_info where id = idParam; END$$ DELIMITER ;
course_info 表數據sql
CREATE TABLE `course_info` ( `id` int(11) NOT NULL AUTO_INCREMENT, `cname` varchar(255) DEFAULT '', `caddress` varchar(255) DEFAULT '', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8
mybatis代碼調用存儲過程mybatis
<resultMap id="BaseResultMap" type="com.dlh.enty.courseInfo" > <!-- WARNING - @mbggenerated This element is automatically generated by MyBatis Generator, do not modify. This element was generated on Sat Mar 25 23:15:07 CST 2017. --> <id column="id" property="id" jdbcType="INTEGER" /> <result column="cname" property="cname" jdbcType="VARCHAR" /> <result column="caddress" property="caddress" jdbcType="VARCHAR" /> </resultMap> --沒有參數傳入 <select id="queryByProcedure" resultMap="BaseResultMap" statementType="CALLABLE"> {call pro1()} </select> --有參數傳入 相對於上面調用沒有參數的存儲過程 多了個parameterType 接口層是把相關參數放到一個map裏面 <select id="queryByProcedureByMap" parameterType="java.util.Map" resultMap="BaseResultMap" statementType="CALLABLE"> {call pro2( #{idParam,jdbcType=VARCHAR,mode=IN})} </select>