Mybatis calls the mysql stored procedure and returns the result set, mybatismysql

Source: Internet
Author: User

Mybatis calls the mysql stored procedure and returns the result set, mybatismysql

A result set is often returned in a stored procedure. Mysql directly uses select to return the result set. Oracle uses a cursor to return the result set. This is relatively convenient for Mysql. The following code can implement the output result set:

Stored Procedure Definition:

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; 
Execution result:


Call the stored procedure in mybatis and obtain the result set:

1. xml configuration file

<?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 code

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();}


Note:As we can see above, mysql can directly use the select statement to return the result set in the stored procedure, and mybatis can directly use list to receive the result set (no cursor is required ).


Reference: http://yhjhappy234.blog.163.com/blog/static/316328322012455714892/



Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.