MyBatis (iv) annotations and stored procedures

Source: Internet
Author: User
1. SQL Common Fragment
<sql id= "Publicfindemp" >
        select E.*,depname from Employee E, Department d
        where D.depid=e.depid
< /sql>

Using SQL Common fragments:

<include refid= "Publicfindemp"/>
<!--3.1 paged query and query all-
    <select id= "Findemloyee" parametertype= "Pageutil" resultmap= "Empmap" >
        <choose>
            <!--paging query-
            <when test= "startrow!=0 and pagesize>0" >
                <include refid= " Publicfindemp "/>
                limit #{startrow},#{pagesize}
            </when>
            <!--query All--
            < otherwise>
                <include refid= "publicfindemp"/>
            </otherwise>
        </choose>
    </select>
2. How the stored procedure works
Delimiter//
CREATE PROCEDURE pro_emp (in StartRow int,in pagesize int.)
begin
   Select E.*,d.depname from Employee E,department D
    where d.depid=e.depid limit startrow,pagesize;
End 
//
delimiter;
Delimiter//
CREATE PROCEDURE Proc_emptotalcount (out Empcount int)
begin
    Select COUNT (*) to Empcount from Employee E,department d
    where d.depid = E.depid;
End
//
delimiter;

In Mapper.xml, write this:
1) in

<select id= "Findbyprimarykey" 
parametermap= "Parammap" >>>> Note to write Parametermap
Statementtype= "Callable" >>>> represents a stored procedure
resulttype= "Com.pojo.Employee" >
        call Employeesys.proc_empbyid (?)
</select>
<parametermap id= "Parammap" type= "Employee" >
  <parameter property= "empId" mode= " In "jdbctype=" INTEGER "/>
 </parameterMap>

Type: Encapsulates the types of Java objects
Jdbctype: The data type of the parameter, must be a reference data type, must be uppercase;
Mode: The type of the parameter of the stored procedure, if the input parameter is in, the output parameter is out;
2) out

<select id= "Gettotalcount" 
 parametermap= "Totalcountmap" 
 statementtype= "callable" >
      call Employeesys.proc_emptotalcount (?)
  </select>

<parametermap id= "Totalcountmap" type= "Pageutil" >
     <parameter property= " TotalCount "mode=" Out "jdbctype=" INTEGER "/>
</parameterMap>

3) test on out

        SYSTEM.OUT.PRINTLN ("---gettotalcount (pageutil pageutil)---");
        Pageutil pageUtil1 = new Pageutil (n);
        Mapper.gettotalcount (PAGEUTIL1);
        System.out.println (Pageutil1.gettotalcount ());

Understanding: The stored procedure out is: Out property= "TotalCount", and is encapsulated in type= "Pageutil" inside, so with Pageutil1.gettotalcount () to pick up. Select COUNT (*) into Empcount, the method does not need to return the value, directly into the property inside the fishing. 3) Annotations

To write SQL statements directly inside the Mapper interface, you do not need to use an XML file:

@Select ("SELECT * From Project")
 list<workorder> findName ();
 Paged Query
 @Select ("Select W.*,p.projectname from WorkOrder w,project p where   p.id=w.projectid limit #{startrow},# {pagesize} ")
 list<workorder> findpage (Pageutil pageutil);
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.