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
- -- --------------------------------------------------------------------------------
- --Routine DDL
- --note:comments before and after the routine body is not being stored by the server
- -- --------------------------------------------------------------------------------
- DELIMITER $$
- CREATE definer= ' root ' @ ' localhost ' PROCEDURE ' SelectCount ' (
- in Pcsid int,
- in Drid int,
- in Partnerid int,
- in customerId int,
- Out pcscount int,
- Out drcount int
- )
- BEGIN
- Select count (md.id) to @pcsC from mdm_device md
- Left join Mdm_device_security mds on mds.device_id = md.id
- where mds.device_rooted = Pcsid
- and md.partner_id = Partnerid and md.customer_id = customerId;
- Set pcscount = @pcsC;
- Select count (md.id) to @drC from mdm_device MD
- where md.managed_status = Drid and Date_sub (Curdate (), INTERVAL 7 day ) <= DATE (md.un_manage_date)
- and md.partner_id = Partnerid and md.customer_id = customerId;
- Set drcount = @drC;
- END
1.java call incoming map. Get via map.
1.1 Mapper File notation
XML code
- <parametermap type="map" id="Homevo">
- <parameter property="Pcsid" jdbctype="INTEGER" mode="in"/>
- <parameter property="Drid" jdbctype="INTEGER" mode="in"/>
- <parameter property="Partnerid" jdbctype="INTEGER" mode="in"/>
- <parameter property="customerId" jdbctype="INTEGER" mode="in"/>
- <parameter property="Pcscount" jdbctype="INTEGER" mode="out"/>
- <parameter property="Drcount" jdbctype="INTEGER" mode="out"/>
- </parametermap>
- <select id= "selectforhome" parametermap="Homevo "
- statementtype="callable">
- {Call SelectCount (
- ?,?,?,?,?,?
- )}
- </Select>
1.2 Java call notation
Java code
- @Override
- Public Stringselecthomecount (Homevo Home) throws Exception {
- map<string, object> map = new hashmap<string, object> ();
- Map.put ("Pscid", 0);
- Map.put ("Drid", 1);
- Map.put ("Partnerid", 25);
- Map.put ("CustomerId", 50);
- Map.put ("Isolation", 1);
- SelectOne ("Mapper.selectforhome", map);
- System.out.println (Map.get ("Pcscount"));
- return Map.get ("Drcount");
- }
2.java calls incoming JavaBean. The return value is obtained through the JavaBean property
2.1 Passing Parameters mapper notation by JavaBean
XML code
- <parametermap type="Com.polysaas.mdm.device.entity.HomeVO" id="Home">
- <parameter property="Pcsid" jdbctype="INTEGER" mode="in"/>
- <parameter property="Drid" jdbctype="INTEGER" mode="in"/>
- <parameter property="Partnerid" jdbctype="INTEGER" mode="in"/>
- <parameter property="customerId" jdbctype="INTEGER" mode="in"/>
- <parameter property="Pcscount" jdbctype="INTEGER" mode="out"/>
- <parameter property="Drcount" jdbctype="INTEGER" mode="out"/>
- </parametermap>
JavaBean can be mapped in two ways
XML code
- <resultmap type="Com.polysaas.mdm.device.entity.HomeVO" id="Home">
- <result column="Partnerid" property="Partnerid" jdbctype="INTEGER"/ >
- <result column="customerId" property="customerId" jdbctype="INTEGER" />
- <result column="Pcsid" property="Pcsid" jdbctype="INTEGER" />
- <result column="Drid" property="Drid" jdbctype="INTEGER" />
- <result column="Pcscount" property="Pcscount" jdbctype="INTEGER" / >
- <result column="Drcount" property="Drcount" jdbctype="INTEGER" />
- </resultmap>
- <Select id= "selectForHome2" parametertype=" Com.polysaas.mdm.device.entity.HomeVO "
- statementtype="callable">
- {Call SelectCount (
- #{pcsid,jdbctype=INTEGER, mode= in},
- #{drid, jdbctype=INTEGER,mode= in},
- #{partnerid, jdbctype=INTEGER,mode= in},
- #{customerid, jdbctype=INTEGER,mode= in},
- #{pcscount, jdbctype=INTEGER,mode= out},
- #{drcount, jdbctype=INTEGER,mode= out}
- )}
- </Select>
2.2 Using JavaBean to pass parameter Java call notation
Java code
- @Override
- Public Homevo Selecthomecount (Homevo Home) throws Exception {
- Homevo vo = new Homevo ();
- Vo.setpcsid (0);
- Vo.setdrid (0);
- Vo.setpartnerid (25);
- Vo.setcustomerid (50);
- Vo.setpcscount (0);
- Vo.setdrcount (0);
- SelectOne ("com.polysaas.mdm.device.mapper.MdmDeviceMapper.selectForHome2", VO);
- return VO;
- }
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