springMVC + mybatis 调用存储过程二
存储过程返回单个结果集
<resultMap type="user" id="restReturnResultMap"> <result column="USER_ID" property="id" javaType="long" jdbcType="BIGINT"/> <result column="NAME" property="name" javaType="string" jdbcType="VARCHAR"/> <result column="PASSWORD" property="password" javaType="string" jdbcType="VARCHAR"/> <result column="EMAIL" property="email" javaType="string" jdbcType="VARCHAR"/> <result column="LOCKED" property="locked" javaType="boolean" jdbcType="BIT"/> <result column="ENABLED" property="enabled" javaType="boolean" jdbcType="BIT"/> <!-- java.sql.Timestamp不是常规类型所以要写全 --> <result column="EXPIRATION" property="expiration" javaType="java.sql.Timestamp" jdbcType="DATE"/> <result column="CREATEDATE" property="createDate" javaType="java.sql.Timestamp" jdbcType="DATE"/> </resultMap>
sql
BEGIN SELECT * FROM `USER`; END
mapper.xml
<resultMap type="user" id="restReturnResultMap"> <result column="USER_ID" property="id" javaType="long" jdbcType="BIGINT"/> <result column="NAME" property="name" javaType="string" jdbcType="VARCHAR"/> <result column="PASSWORD" property="password" javaType="string" jdbcType="VARCHAR"/> <result column="EMAIL" property="email" javaType="string" jdbcType="VARCHAR"/> <result column="LOCKED" property="locked" javaType="boolean" jdbcType="BIT"/> <result column="ENABLED" property="enabled" javaType="boolean" jdbcType="BIT"/> <!-- java.sql.Timestamp不是常规类型所以要写全 --> <result column="EXPIRATION" property="expiration" javaType="java.sql.Timestamp" jdbcType="DATE"/> <result column="CREATEDATE" property="createDate" javaType="java.sql.Timestamp" jdbcType="DATE"/> </resultMap> <!-- 调用存储过程返回单个结果集 --> <select id="restReturnResult" statementType="CALLABLE" resultMap="restReturnResultMap"> {CALL restReturnResult()} </select>
java
public List<User> restReturnResult() { return userMapper.restReturnResult(); }
返回多个结果集
sql
BEGIN SELECT * FROM `user`; SELECT * FROM `authority`; END
mapper.xml
<!-- 调用存储过程返回多个结果集 mapper中方法定义public List<List<?>> testReturnMoreResult(); 返回的结果集中按照resultMap="restReturnResultMap,com.study.sm.mapper.AuthorityMapper.authorityBaseResultMap" 配置的顺序存放 即List<List<?>> 中第一个 List<?>为User类型,第二个为Authority类型 --> <select id="testReturnMoreResult" statementType="CALLABLE" resultMap="restReturnResultMap,com.study.sm.mapper.AuthorityMapper.authorityBaseResultMap"> {CALL testReturnMoreResult()} </select>
java
public List<List<?>> testReturnMoreResult() { return userMapper.testReturnMoreResult(); } @RequestMapping(value = "/testReturnMoreResult.html") public void testReturnMoreResult() { List<List<?>> lists = userService.testReturnMoreResult(); for (int i = 0; i < lists.size(); i++) { List<?> list = lists.get(i); for (int j = 0; j < list.size(); j++) { System.out.println(list.get(j)); } } }