Mybatis calls methods for views and stored procedures

  • 2020-05-10 18:14:24
  • OfStack

The current project USES Mybatis as the O/R mapping framework, which is really useful and very convenient for the development of the project. MyBatis supports normal sql queries, view queries, stored procedure calls, and is an excellent persistence layer framework. It maps the interface and POJO in java to records in the database using a simple XML or annotation configuration and raw mapping.

1. Call the view

The following is the call view to query the revenue details. The sql section is as follows:


<!--  For detail  -->
<select id ="getContactEarnsDetail" resultType= "java.util.Map" parameterType ="java.lang.Integer">
select
title,trade_time,trade_amount
from v_contacts_earn where user_id = #{userId}
</select >

The data type returned by this view is map.

The mapper section is as follows:


List<Map<String, Object>> getContactEarnsDetail(Integer userId);

The interface part is as follows:


List<Map<String, Object>> getContactEarnsDetail(Integer userId);

The implementation is as follows:


@Override
public List<Map<String, Object>> getContactEarnsDetail(Integer userId) {
Assert. notNull(userId);
return contactEarnsMapper.getContactEarnsDetail(userId);
}

As shown in the example above, calling the view is like calling normal sql query statement 1.

2. Call the stored procedure

Calling a stored procedure may also have a return result set, which I'll focus on here.

(1) contains the returned result set

For example, the structure of the stored procedure is as follows:


p_my_wallet(IN var_user_id INT) ; 
 The parameter is the user id
revenue_today  Today's earnings 
revenue_contacts  Network benefits 
balance  Available balance 

sql is as follows:


<!--  Access to wallet information  -->
<select id="getMyWallet" parameterType="java.lang.Integer" resultType="java.util.Map" statementType="CALLABLE">
{
  call p_my_wallet(
    #{userId,jdbcType=INTEGER,mode=IN}
  )
}
</select>

Then, part mapper is:


Map<String, Object> getMyWallet(@Param("userId")Integer userId);

The interface part is:


Map<String, Object> getMyWallet(Integer userId);

(2) no result set is returned

The sql section is as follows:


< select id= "cardBuild" statementType ="CALLABLE">
<![CDATA[
{call p_insert_card_build_info (#{is_customized_,mode=IN,jdbcType=INTEGER},#{face_value_,mode=IN,jdbcType=INTEGER},#{number_,mode=IN,jdbcType=INTEGER})}
]]>
</ select>

Related articles: