session应用二

从session中获取mapper对象,利用mapper对象进行增删改查

        Date now = new Date();
        SqlSession session = this.yangchebaoDbManagerImpl.getSqlSessionFactory().openSession(false);
        InsureQueryInfoMapper insureQueryInfoMapper = session.getMapper(InsureQueryInfoMapper.class);
        InsureQuoteDetailMapper insureQuoteDetailMapper = session.getMapper(InsureQuoteDetailMapper.class);
        
        try {
            boolean semSign = SemaphoreControllerUtil.getInstance().acquireInsureHandleSemaphore(sqId);
            if(!semSign){
                throw new BadRequestException("common", "请求被阻止,请稍后再试");
            }
            
            InsureQueryInfo insureQueryInfo = this.queryInsureInfoBySqId(sqId, session);
            // 只有保单报价查询信息不为空,而且报价状态允许进行报价处理时才处理,防止重复处理“报价成功”
            if(insureQueryInfo!=null){
                if(QunabaoNotifyStatus.canQuoteHandle(insureQueryInfo.getStatus())){
                    JSONObject dealBack = params.optJSONObject("dealBack");
                    //由于去哪保目前的是吧,只能靠此属性判断,所以临时处理
                    Boolean isSimilar = MapUtils.getBoolean(dealBack, "isSimilar", false);
                    if(isSimilar) {
                        notifyStatus = QunabaoNotifyStatus.QUOTE_FAILED;
                    }
                    if(notifyStatus.getCode()==QunabaoNotifyStatus.QUOTE_FAILED.getCode()){
                        // 报价失败
                    }else if(notifyStatus.getCode()==QunabaoNotifyStatus.QUOTE_SUCCESSED.getCode()){
                        // 报价成功
                        // 更新车船税价格、总价格、商业险价格、交强险价格
                        // 增加各险种的具体报价信息
                        JSONObject dealOffer = params.optJSONObject("dealOffer");
                        if(dealBack!=null && !dealBack.isEmpty()){
                            insureQueryInfo.setTaxCharge(dealBack.optDouble("taxCharge", PayConstants.DEFAULT_NULL_ID));// 车船税价格
                            insureQueryInfo.setTotalCharge(dealBack.optDouble("totalCharge", PayConstants.DEFAULT_NULL_ID));// 总价格
                            insureQueryInfo.setBizCharge(dealBack.optDouble("bizCharge", PayConstants.DEFAULT_NULL_ID));// 商业险价格
                            insureQueryInfo.setEfcCharge(dealBack.optDouble("efcCharge", PayConstants.DEFAULT_NULL_ID));// 交强险价格
                        }
                        
                        if(dealOffer!=null && !dealOffer.isEmpty()){
                            JSONObject suite = dealOffer.optJSONObject("suite");
                            if(suite!=null && !suite.isEmpty()){
                                JSONObject items = suite.optJSONObject("items");
                                if(items!=null && !items.isEmpty()){
                                    Set entrySet = items.keySet();
                                    Iterator<String> iterator = entrySet.iterator();
                                    while(iterator.hasNext()){
                                        String key = iterator.next();
                                        JSONObject quoteInfo = items.optJSONObject(key);
                                        if(quoteInfo!=null && !quoteInfo.isEmpty()){
                                            InsureQuoteDetail insureQuoteDetail = new InsureQuoteDetail();
                                            insureQuoteDetail.setSqid(sqId);// 单方号----例如““6010306151601232422”
                                            insureQuoteDetail.setEcode(quoteInfo.optString("ecode"));// 保障项代码----例如:"VehicleDemageIns"
                                            insureQuoteDetail.setSelIdx(quoteInfo.optString("selIdx"));// 选择项----例如:1
                                            insureQuoteDetail.setSelName(quoteInfo.optString("caption"));// 选择项名称
                                            insureQuoteDetail.setAmount(quoteInfo.optDouble("amount", PayConstants.DEFAULT_NULL_ID));// 保障金额
                                            insureQuoteDetail.setCharge(quoteInfo.optDouble("charge", PayConstants.DEFAULT_NULL_ID));// 实际保费
                                            insureQuoteDetail.setListPrice(quoteInfo.optDouble("listPrice", PayConstants.DEFAULT_NULL_ID));// 原价
                                            insureQuoteDetail.setDiscountRate(quoteInfo.optDouble("discountRate", PayConstants.DEFAULT_NULL_ID));// 折扣率
                                            insureQuoteDetail.setCreateDate(now);// 记录创建时间
                                            
                                            insureQuoteDetailMapper.insert(insureQuoteDetail);// 增加险种报价信息
                                        }
                                    }
                                }
                            }
                        }
                    }
                    insureQueryInfo.setReceiveDate(now);// 报价回执时间
                    
                    insureQueryInfo.setStatus(String.valueOf(notifyStatus.getCode()));// 报价状态----报价成功/失败
                    insureQueryInfo.setRemarks(notifyMsg);// 报价成功/失败描述信息
                    
                    insureQueryInfoMapper.updateByPrimaryKey(insureQueryInfo);// 更新数据库记录
                    
                }else{
                    LOGGER.warn("去哪保回调----"+notifyStatus.getDesc()+"----单方号:"+sqId+"的状态为:"+insureQueryInfo.getStatus()+"忽略此回调");
                }
            }else{
                throw new BadRequestException("common", "没有找到单方号"+sqId+"的信息");
            }
            
            session.commit();
        } catch(CarsmartException e) {
            session.rollback();
            throw new BadRequestException("common", e.getMessage());
        } catch(Exception e) {
            session.rollback();
            LOGGER.error(e.getMessage(), e);
            throw new InternalErrorException(e);
        } finally {
            session.close();
            SemaphoreControllerUtil.getInstance().releaseInsureHandleSemaphore(sqId);
        }

利用session  获取mapper对象进行更新的第二种方法利用 map存储变量,用session的update方法

    Map<String, Object> paramMap=new HashMap<String, Object>();
        paramMap.put("modelQaId", modelQaId);
        SqlSession session=null;
        try {
            session=this.yangchebaoDbManagerImpl.getSqlSessionFactory().openReplicableSqlSession(false);
            session.update("cn.com.carsmart.ws.ibatis.mapper.ComplexQueryNewMapper.updateModelQaViewCnt", paramMap);
            session.commit();
        } catch(Exception e) {
            if(null != session) {
                session.rollback();
            }
            logger.error("method detail fail " + ExceptionUtils.getFullStackTrace(e));
        } finally {
            if(null != session) {
                session.close();
            }
        }
原文地址:https://www.cnblogs.com/tian830937/p/5520342.html