MyBatis SQL execution has data but the returned result is null

Source: Internet
Author: User

When querying a database using MyBatis recently, it was found that the result was a value when executed separately in PL/SQL, but Null was obtained in the program, with the following configuration:

(1)

<resultmap type= "Monitortaskstatus" id= "monitortaskstatusmap" >
<result column= "Batch_num" property= "Batchnum"/>
<result column= "Task_status" property= "TaskStatus"/>
<result column= "Called_phonenum" property= "Calledphonenum"/>
</resultMap>

(2)

<select id= "Querystatusbyprojectid" parametertype= "java.lang.String"Resulttype="Monitortaskstatusmap">
Select B.batch_num, C.called_phonenum,c.task_status
From Cc_call_task C
Left JOIN Cc_batch_calledperson b
On c.batch_id = b.batch_id
where B.batch_type in (2, 3)
and C.task_status! = 5
<if test= "VALUE! = null and Value! =" ">
and c.project_id = #{value}
</if>
</select>

(3)

Java:

list<taskstatus> calltasks = Service.querystatusbyprojectid (ProjectID);

Debug found calltasks.size () = 1, but the value is NUL, finally engaged in a half-day only to find the original (1) in the <select> tag Resulttype attribute use error, changed to Resultmap can

The reason is that, in the MyBatis in the query for the select mapping, the return type can be used Resulttype, or can be used to directly represent the return type Resultmap,resulttype, The Resultmap is a reference to the external resultmap tag ( as in this example (1)), but Resulttype and Resultmap cannot exist at the same time . In the MyBatis query mapping, in fact, each of the properties of the query is placed in a corresponding map inside, where the key is the property name, the value is its corresponding value. When the provided return type property is Resulttype, MyBatis will remove the key value pair from the map to the property assigned to the object specified by Resulttype. So in fact, each of the MyBatis query map return type is Resultmap, but when we provide the return type property is Resulttype, MyBatis to automatically give us the corresponding value assigned to resulttype the property of the specified object, And when we provide the return type is RESULTMAP, because map does not represent the domain model well, we need to further convert it to the corresponding object, which is often useful in complex queries . (including tables associated with queries in this case, etc.)

Finally, the reason for the error in this example is that the result set with ID monitortaskstatusmap in <resultMap> has been customized, and the requirement is to refer to this, and the correct one should be specified with Resultmap. I used the resulttype ...  

finally , we can briefly summarize two points:

1. When a single-table simple query and the return value type is the basic type, generally try to use resulttype;

2. When a table is associated with a query, or if the associated RESULTMAP tag is defined in XML, the RESULTMAP is generally used as much as possible;

3.resultType and Resultmap cannot be used simultaneously.

This article is referenced from: http://zhuyuehua.iteye.com/blog/1721715

MyBatis SQL execution has data but the returned result is null

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.