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: