SPRINGMVC and MyBatis set implementations invoke stored procedures, transaction control instances _java

Source: Internet
Author: User
Tags getmessage

In the SSM framework it is often used to invoke stored procedures in the database, as well as transaction control, to save a document as an example, to introduce the following:

1, Oracle stored procedure Code as follows (the main logic is to automatically add the document encoding 1 and return the document encoding):

CREATE OR REPLACE PROCEDURE "Update_djbhzt" (P_gsid in Varchar2, p_tblname in varchar2, newrecno out number) as is 
Gin 
  Update BHDJ Set BHDJ02 = bhdj02+1 where GSXX01 = P_gsid and BHDJ01 = P_tblname; 
  If Sql%rowcount = 0 then 
    insert into BHDJ (GSXX01, bhdj01,bhdj02) VALUES (P_gsid, p_tblname,1); 
  End If; 
  Select BHDJ02 into Newrecno from bhdj where GSXX01 = P_gsid and BHDJ01 = P_tblname; 

2, the MyBatis code is as follows:

<select id= "Update_djbhzt" parametertype= "Java.util.Map" statementtype= "callable" > 
<![ cdata[  
  {call UPDATE_DJBHZT (#{p_gsid,mode=in,jdbctype=varchar},#{p_tblname,mode=in,jdbctype=varchar},#{ Newrecno,mode=out,jdbctype=bigint})}  
]]> 
</select> 

3, DAO layer code is as follows:

Package com.pcmall.dao.sale.stock; 
 
Import java.util.List; 
Import Java.util.Map; 
 
Import Com.github.miemiedev.mybatis.paginator.domain.PageBounds; 
Import Com.pcmall.dao.common.BaseMapper; 
Import COM.PCMALL.DOMAIN.SALE.STOCK.ZCD; 
 
Public interface Zcdmapper extends basemapper<zcd> { 
   
  void Update_djbhzt (map<string,object> Map);  
 
} 

4, service layer code is as follows:
Interface:

Package com.pcmall.service.sale.stock; 
 
Import java.util.List; 
 
Import Com.github.miemiedev.mybatis.paginator.domain.PageBounds; 
Import com.pcmall.domain.sale.order.HssnCmmx; 
Import COM.PCMALL.DOMAIN.SALE.STOCK.ZCD; 
Import Com.pcmall.domain.sale.stock.Zcditem; 
Import Com.pcmall.domain.sale.user.User; 
Import Com.pcmall.domain.vo.ResponseVO; 
Import Com.pcmall.service.common.IBaseService; 
 
Public interface Izcdservice extends ibaseservice<zcd> { 
 
  Long getzcdno (String gsxx01, String tablename); 
 
  Responsevo SAVEZCD (zcd zcd, user user) throws Exception; 
   
} 

Implementation class:

Package Com.pcmall.service.sale.stock.impl; 
Import Java.text.SimpleDateFormat; 
Import java.util.ArrayList; 
Import Java.util.Date; 
Import Java.util.HashMap; 
Import java.util.List; 
 
Import Java.util.Map; 
 
Import Javax.annotation.Resource; 
Import Org.apache.axis.holders.SchemaHolder; 
Import Org.apache.commons.collections.CollectionUtils; 
Import Org.springframework.stereotype.Service; 
 
Import org.springframework.transaction.annotation.Transactional; 
Import Com.github.miemiedev.mybatis.paginator.domain.PageBounds; 
Import Com.google.common.collect.Collections2; 
Import Com.pcmall.common.utils.DateUtils; 
Import Com.pcmall.dao.sale.stock.ZcdMapper; 
Import Com.pcmall.dao.sale.stock.ZcditemMapper; 
Import com.pcmall.domain.sale.order.HssnCmmx; 
Import COM.PCMALL.DOMAIN.SALE.PROMOTION.HSCXLX; 
Import COM.PCMALL.DOMAIN.SALE.STOCK.ZCD; 
Import Com.pcmall.domain.sale.stock.Zcditem; 
Import Com.pcmall.domain.sale.stock.bo.CkspBO; 
Import Com.pcmall.domain.sale.user.User; ImportCom.pcmall.domain.vo.ResponseVO; 
Import Com.pcmall.service.common.AbstractServiceImpl; 
Import Com.pcmall.service.sale.order.IOrderService; 
Import Com.pcmall.service.sale.stock.IStockService; 
 
Import Com.pcmall.service.sale.stock.IZcdService; 
  @Service public class Zcdserviceimpl extends abstractserviceimpl<zcd> implements Izcdservice {@Resource 
   
  Private Zcdmapper Zcdmapper; 
   
  @Resource private Zcditemmapper Zcditemmapper; 
   
   
  @Resource private Istockservice Stockserviceimpl; @Transactional (rollbackfor = exception.class) @Override public Responsevo savezcd (zcd zcd, user user) throws Excepti 
    on {Responsevo Responsevo = new Responsevo (); 
    Long zcd01 = Getzcdno (zcd.getgsxx01 (), "ZCD"); 
    ZCD.SETZCD01 (ZCD01); 
    ZCD.SETZCD05 (User.getryxx (). getRyxx02 ()); 
     
    Zcd.setzcd06 (New Date ()); 
    Date Nowtime = new Date (); 
    SimpleDateFormat SDF = new SimpleDateFormat ("hhmmssms"); 
 ZCD.SETTIME01 (Sdf.format (nowtime));    
     
    For (Zcditem Zcditem:zcd.getZcditem ()) {zcditem.setzcd01 (ZCD01); 
    Zcditemmapper.insertselective (Zcditem); 
     
    } zcdmapper.insertselective (ZCD); 
    Responsevo.setdata (ZCD); 
  return Responsevo; @Override Public Long Getzcdno (string gsxx01, String tablename) {map<string, object> Map = new has 
    Hmap<string, object> (); 
    Map.put ("P_gsid", gsxx01); 
 
    Map.put ("P_tblname", TableName); 
    ZCDMAPPER.UPDATE_DJBHZT (map); 
    Long Newrecno = (long) map.get ("Newrecno"); 
  return newrecno; 
 } 
}

5, control layer code is as follows:

Package COM.PCMALL.CONTROLLER.STOCK.ZCD; 
 
Import java.util.List; 
Import Javax.annotation.Resource; 
 
Import Javax.servlet.http.HttpServletRequest; 
Import Org.apache.commons.collections.CollectionUtils; 
Import Org.slf4j.Logger; 
Import Org.slf4j.LoggerFactory; 
Import Org.springframework.stereotype.Controller; 
Import Org.springframework.web.bind.annotation.RequestBody; 
Import org.springframework.web.bind.annotation.RequestMapping; 
Import Org.springframework.web.bind.annotation.RequestParam; 
 
Import Org.springframework.web.bind.annotation.ResponseBody; 
Import Com.github.miemiedev.mybatis.paginator.domain.PageBounds; 
Import Com.pcmall.common.base.BaseController; 
Import COM.PCMALL.DOMAIN.SALE.STOCK.ZCD; 
Import Com.pcmall.domain.sale.stock.Zcditem; 
Import Com.pcmall.domain.sale.stock.bo.CkspDetailBO; 
Import Com.pcmall.domain.sale.stock.bo.ZcdBO; 
Import COM.PCMALL.DOMAIN.SALE.SYSTEM.GZZQX; 
Import Com.pcmall.domain.sale.system.GzzqxKey; Import Com.pcmall.domain.sale.sysTem. 
Ryxx; 
Import Com.pcmall.domain.sale.user.Czy; 
Import Com.pcmall.domain.sale.user.User; 
Import Com.pcmall.domain.vo.ResponseVO; 
Import Com.pcmall.service.sale.stock.IStockService; 
Import Com.pcmall.service.sale.stock.IZcdService; 
 
Import Com.pcmall.service.sale.system.IGzzqxService; @Controller @RequestMapping ("/STOCK/ZCD") public class Zcdcontroller extends Basecontroller {private static Logg 
   
  Er logger=loggerfactory.getlogger (zcdcontroller.class); 
   
   
  @Resource private Izcdservice Zcdserviceimpl; 
   
  @Resource private Istockservice Stockserviceimpl; 
   
   
   
  @Resource private Igzzqxservice Gzzqxserviceimpl; @RequestMapping ("/SAVEZCD") @ResponseBody public Responsevo SAVEZCD (httpservletrequest request, @RequestBody ZCD ZCD 
    ) {Responsevo Responsevo = new Responsevo (); 
      try{Responsevo = ZCDSERVICEIMPL.SAVEZCD (ZCD, Getloginuser ()); 
    Responsevo.setsuccess (TRUE); catch (Exception e) {logger.Error ("", e); 
      Responsevo.setsuccess (FALSE); Responsevo.seterrormsg (! "". Equals (E.getmessage ())? 
    E. GetMessage (): "Background anomalies"); 
  return Responsevo; 
 } 
   
}

6, the front-end JS layer code is as follows:

function Save () {$ ("#save"). AddClass ("Disabled"); 
      if ($ ("#selSHCK"). val () = "") {layer.msg (' please fill in the Receipt warehouse ', {icon:5}); 
      $ ("#save"). Removeclass ("Disabled"); 
    Return 
      } if ($ ("#selSHCK"). val () = = $ ("#selFHCK"). Val ()) {layer.msg (' shipping warehouse cannot be the same as receipt warehouse ', {Icon:5 
      }); 
      $ ("#save"). Removeclass ("Disabled"); 
    Return 
    var param = {}; 
    PARAM.BM01 = $ ("#selBm"). attr ("Valuea"); 
    PARAM.ZCD02 = $ ("#selFHCK"). attr ("Valuea"); 
    PARAM.ZCD03 = $ ("#selSHCK"). attr ("Valuea"); 
    PARAM.ZCD04 = $ ("#zcd04"). Val (); 
 
    PARAM.GSXX01 = $ ("#gsxx01"). Val (); 
    var zcditemary = []; 
    var flag = 0; 
      $ ("#tbody1"). Find ("tr"). each (function () {var zcditem = {}; 
 
      var ARRTD = $ (this). Children (); 
      zcditem.spxx01 = $.trim (Arrtd.eq (0). text ()); 
      zcditem.wldw01 = $.trim (Arrtd.eq (6). text ()); 
       
    zcditem.zcdi01 = $.trim (Arrtd.eq (7). text ());  if ($.trim (Arrtd.eq (2). Children (". zcdi03"). val ()) = = "") {/* LAYER.MSG (' Please enter the number of transfers ', {Icon:5 
        }); $ ("#save"). Removeclass ("Disabled"); 
        * * flag = 1; 
      Return 
      } zcditem.zcdi02 = $.trim (Arrtd.eq (2). Children (". zcdi03"). Val ()); 
      zcditem.zcdi03 = $.trim (Arrtd.eq (2). Children (". zcdi03"). Val ()); 
      zcditem.zcdi05 = $.trim (Arrtd.eq (8). text ()); 
      zcditem.zcdi06 = $.trim (Arrtd.eq (4). Children (". zcdi06"). Val ()); 
      ZCDITEM.GSXX01 = $ ("#gsxx01"). Val (); 
      zcditem.zcdi07 = $.trim (Arrtd.eq (9). text ()); 
    Zcditemary.push (Zcditem); 
 
    }) Param.zcditem = Zcditemary; 
      if (flag = = 1) {layer.msg (' Please enter the number of transfer positions ', {icon:5}); 
      $ ("#save"). Removeclass ("Disabled"); 
    Return 
      } if (zcditemary.length = = 0) {layer.msg (' Please enter the silo commodity information ', {icon:5}); 
      $ ("#save"). Removeclass ("Disabled"); 
    Return 
 }/* else{     for (Var i=0;i<zcditemary;i++) {if (zcditemary[i].zcdi03 = = "") {layer.msg (' Please enter the number of turns ') { 
          Icon:5}); 
          $ ("#save"). Removeclass ("Disabled"); 
        Return 
      }}/$.ajax ({url: "${ctx}/stock/zcd/savezcd", Data: $.json.decode (param), 
        ContentType: "Application/json", type: "POST", DataType: "JSON", success:function (data) { 
          if (data.success) {$ ("#zcd01"). Val (DATA.DATA.ZCD01); 
          $ ("#zcd05"). Val (data.data.zcd05); 
          $ ("#zcd06"). Val (data.data.zcd06); 
        Layer.msg (' Make transfer order success ', {icon:6}); 
          else {layer.msg (' Make a transfer slip failure ' + data.errormsg, {icon:5}); 
        $ ("#save"). Removeclass ("Disabled"); 
  } 
      } 
    }); 
 }

  above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud-dwelling community.

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.