MyBatis call MySQL stored procedure with output input parameters

Source: Internet
Author: User

http://lohasle.iteye.com/blog/1669879

Stored procedures are the same, but according to their preferences, you can use map or JavaBean to pass parameters.

SQL code
  1. -- --------------------------------------------------------------------------------
  2. --Routine DDL
  3. --note:comments before and after the routine body is not being stored by the server
  4. -- --------------------------------------------------------------------------------
  5. DELIMITER $$
  6. CREATE definer= ' root ' @ ' localhost ' PROCEDURE ' SelectCount ' (
  7. in Pcsid int,
  8. in Drid int,
  9. in Partnerid int,
  10. in customerId int,
  11. Out pcscount int,
  12. Out drcount int
  13. )
  14. BEGIN
  15. Select count (md.id) to @pcsC from mdm_device md
  16. Left join Mdm_device_security mds on mds.device_id = md.id
  17. where mds.device_rooted = Pcsid
  18. and md.partner_id = Partnerid and md.customer_id = customerId;
  19. Set pcscount = @pcsC;
  20. Select count (md.id) to @drC from mdm_device MD
  21. where md.managed_status = Drid and Date_sub (Curdate (), INTERVAL 7 day ) <= DATE (md.un_manage_date)
  22. and md.partner_id = Partnerid and md.customer_id = customerId;
  23. Set drcount = @drC;
  24. END

1.java call incoming map. Get via map.

1.1 Mapper File notation

XML code
  1. <parametermap type="map" id="Homevo">
  2. <parameter property="Pcsid" jdbctype="INTEGER" mode="in"/>
  3. <parameter property="Drid" jdbctype="INTEGER" mode="in"/>
  4. <parameter property="Partnerid" jdbctype="INTEGER" mode="in"/>
  5. <parameter property="customerId" jdbctype="INTEGER" mode="in"/>
  6. <parameter property="Pcscount" jdbctype="INTEGER" mode="out"/>
  7. <parameter property="Drcount" jdbctype="INTEGER" mode="out"/>
  8. </parametermap>
  9. <select id= "selectforhome" parametermap="Homevo "
  10. statementtype="callable">
  11. {Call SelectCount (
  12. ?,?,?,?,?,?
  13. )}
  14. </Select>

1.2 Java call notation

Java code
  1. @Override
  2. Public Stringselecthomecount (Homevo Home) throws Exception {
  3. map<string, object> map = new hashmap<string, object> ();
  4. Map.put ("Pscid", 0);
  5. Map.put ("Drid", 1);
  6. Map.put ("Partnerid", 25);
  7. Map.put ("CustomerId", 50);
  8. Map.put ("Isolation", 1);
  9. SelectOne ("Mapper.selectforhome", map);
  10. System.out.println (Map.get ("Pcscount"));
  11. return Map.get ("Drcount");
  12. }

2.java calls incoming JavaBean. The return value is obtained through the JavaBean property

2.1 Passing Parameters mapper notation by JavaBean

XML code
  1. <parametermap type="Com.polysaas.mdm.device.entity.HomeVO" id="Home">
  2. <parameter property="Pcsid" jdbctype="INTEGER" mode="in"/>
  3. <parameter property="Drid" jdbctype="INTEGER" mode="in"/>
  4. <parameter property="Partnerid" jdbctype="INTEGER" mode="in"/>
  5. <parameter property="customerId" jdbctype="INTEGER" mode="in"/>
  6. <parameter property="Pcscount" jdbctype="INTEGER" mode="out"/>
  7. <parameter property="Drcount" jdbctype="INTEGER" mode="out"/>
  8. </parametermap>

JavaBean can be mapped in two ways

XML code
  1. <resultmap type="Com.polysaas.mdm.device.entity.HomeVO" id="Home">
  2. <result column="Partnerid" property="Partnerid" jdbctype="INTEGER"/ >
  3. <result column="customerId" property="customerId" jdbctype="INTEGER" />
  4. <result column="Pcsid" property="Pcsid" jdbctype="INTEGER" />
  5. <result column="Drid" property="Drid" jdbctype="INTEGER" />
  6. <result column="Pcscount" property="Pcscount" jdbctype="INTEGER" / >
  7. <result column="Drcount" property="Drcount" jdbctype="INTEGER" />
  8. </resultmap>
  9. <Select id= "selectForHome2" parametertype=" Com.polysaas.mdm.device.entity.HomeVO "
  10. statementtype="callable">
  11. {Call SelectCount (
  12. #{pcsid,jdbctype=INTEGER, mode= in},
  13. #{drid, jdbctype=INTEGER,mode= in},
  14. #{partnerid, jdbctype=INTEGER,mode= in},
  15. #{customerid, jdbctype=INTEGER,mode= in},
  16. #{pcscount, jdbctype=INTEGER,mode= out},
  17. #{drcount, jdbctype=INTEGER,mode= out}
  18. )}
  19. </Select>

2.2 Using JavaBean to pass parameter Java call notation

Java code
  1. @Override
  2. Public Homevo Selecthomecount (Homevo Home) throws Exception {
  3. Homevo vo = new Homevo ();
  4. Vo.setpcsid (0);
  5. Vo.setdrid (0);
  6. Vo.setpartnerid (25);
  7. Vo.setcustomerid (50);
  8. Vo.setpcscount (0);
  9. Vo.setdrcount (0);
  10. SelectOne ("com.polysaas.mdm.device.mapper.MdmDeviceMapper.selectForHome2", VO);
  11. return VO;
  12. }

Encountered a lot of exceptions: Remember to write down

1. By the time of JavaBean, #{pcsid, mode=in, Javatype=integer},javatype this attribute is required.

2. Transfer using JavaBean. The output parameter does not require an initial value. Personal preferences and recommended use of JavaBean, because the definition is clearer, encapsulation.

3.map can be used as a placeholder question mark, JavaBean. There will be an exception No value specified for parameter 1

4. Say you don't know a parameter. There may be no stored procedure created, this occurs in multiple libraries

5. Read only This may be a transaction control read-only: I am because the stored procedure has a set statement, and the method is called SELECTAAAA ().

MyBatis call MySQL stored procedure with output input parameters

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.