SpringMVC와 Mybatis 집합은 호출 저장 프로세스, 사무 제어 실례를 실현한다

11877 단어 SpringMVCMybatis사무
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. 서비스 레이어 코드는 다음과 같습니다.
인터페이스:

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"); 
        } 
      } 
    }); 
  } 
이상은 본문의 전체 내용입니다. 여러분의 학습에 도움이 되고 저희를 많이 응원해 주십시오.

좋은 웹페이지 즐겨찾기