欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

SpringMVC与Mybatis集合实现调用存储过程、事务控制实例

程序员文章站 2024-03-11 17:45:43
在ssm框架中经常会用到调用数据库中的存储过程、以及事务控制,下面以保存某单据为例,介绍一下: 1、oracle中存储过程代码如下(主要逻辑将单据编码自动加1,并将该...

在ssm框架中经常会用到调用数据库中的存储过程、以及事务控制,下面以保存某单据为例,介绍一下:

1、oracle中存储过程代码如下(主要逻辑将单据编码自动加1,并将该单据编码返回):

create or replace procedure "update_djbhzt" (p_gsid in varchar2, p_tblname in varchar2, 
                  newrecno out number) as 
begin 
  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; 
end; 

2、mybatis中代码如下:

<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层代码如下:

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层代码如下:
接口:

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; 
   
} 

实现类:

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; 
import com.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 exception { 
    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 hashmap<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层代码如下:

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 logger 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() : "后台异常"); 
    } 
    return responsevo; 
  } 
   
} 

6、前端js层代码如下:

function save() { 
    $("#save").addclass("disabled"); 
     
    if ($("#selshck").val() == "") { 
      layer.msg('请填写收货仓库', { 
        icon : 5 
      }); 
      $("#save").removeclass("disabled"); 
      return; 
    } 
     
    if($("#selshck").val() == $("#selfhck").val()){ 
      layer.msg('发货仓库与收货仓库不能一样', { 
        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('请输入转仓数量', { 
          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('请输入转仓数量', { 
        icon : 5 
      }); 
      $("#save").removeclass("disabled"); 
      return; 
    } 
     
    if (zcditemary.length == 0) { 
      layer.msg('请输入转仓商品信息', { 
        icon : 5 
      }); 
      $("#save").removeclass("disabled"); 
      return; 
    } 
  /* else{ 
      for(var i=0;i<zcditemary;i++){ 
        if(zcditemary[i].zcdi03 == ""){ 
          layer.msg('请输入转仓数量', { 
            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('制定转仓单成功', { 
            icon : 6 
          }); 
        } else { 
          layer.msg('制定转仓单失败' + data.errormsg, { 
            icon : 5 
          }); 
          $("#save").removeclass("disabled"); 
        } 
      } 
    }); 
  } 

 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。