mybatis調用mysql預存程序返回結果集,mybatismysql

來源:互聯網
上載者:User

mybatis調用mysql預存程序返回結果集,mybatismysql

預存程序中經常需要返回結果集。 Mysql 中直接用 select 即可返回結果集。而 oracle 則需要使用遊標來返回結果集。這一點 Mysql 相對比較方便,如下代碼即可實現輸出結果集:

預存程序定義:

DELIMITER $$ DROP procedure IF EXISTS pro_sql_data1 $$  CREATE procedure pro_sql_data1(in sear_name  varchar(2000))  BEGIN   if sear_name is not null and sear_name!='' thenselect id,name,date_format(create_time,'%Y-%m-%d') as repDate from ad_place where name like concat('%',sear_name,'%');ELSEselect id,name,date_format(create_time,'%Y-%m-%d') as repDate from ad_place;end if;END$$DELIMITER; 
執行結果:


在mybatis中調用預存程序,然後擷取該結果集:

1、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.ifeng.iis.bean.iis.Report" ><resultMap type="java.util.HashMap" id="resultMap">       <result column="id" property="id" javaType="java.lang.Integer" jdbcType="INTEGER"/>       <result column="name" property="name" javaType="java.lang.String" jdbcType="VARCHAR"/>       <result column="repDate" property="repDate" javaType="java.lang.String" jdbcType="VARCHAR"/>       <result column="summ" property="summ" javaType="java.lang.Long" jdbcType="BIGINT"/>    </resultMap>     <select id="test123" parameterType="java.util.Map"  resultMap="resultMap" statementType="CALLABLE" >        {call pro_sql_data(      #{obj,jdbcType=VARCHAR,mode=IN}   )      }    </select>  </mapper>
java代碼

public String query(String param) throws Exception {logger.info(param);Map queryMap = new HashMap();queryMap.put("obj", param);//List<Map> listIis1 = reportDao.select4MapParam(queryMap, "currentSql");List<Map> listIis2 =reportDao.select4MapParam(queryMap,"test123");return JSONArray.fromObject(listIis2).toString();}


註:有上面可知,mysql預存程序中可以直接使用select語句返回結果集,而且mybatis可以直接使用list接收這個結果集(無需遊標)。


參考文章:http://yhjhappy234.blog.163.com/blog/static/316328322012455714892/



著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.