MyBatis calls the MySQL stored procedure and returns the result set.

Source: Internet
Author: User

MyBatis calls the MySQL stored procedure and returns the result set.

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! = ''Then
Select id, name, date_format (create_time, '% Y-% m-% D') as repDate from ad_place where
Name like concat ('%', sear_name, '% ');
ELSE
Select 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 //" 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: we can see that the select statement can be directly used in the mysql stored procedure to return the result set, and mybatis can directly use list to receive the result set (no cursor required ).

MyBatis getting started

Practical application of Java: Mybatis implements addition, deletion, and modification of a single table

[Java] [Mybatis] physical paging implementation

Mybatis quick start tutorial

Test batis's batch data operations

Batch insert operation for List <Object> Object List in Mybatis

For more information about MyBatis, click here.
MyBatis: click here

This article permanently updates the link address:

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.