Commit 0190c3be authored by Java-范 志勇's avatar Java-范 志勇

增加存入本地数据库操作

parent aaca331f
package cn.gq.financial.db.local.dao;
import cn.gq.financial.model.db.RepayDetailFinancial;
import org.springframework.stereotype.Repository;
import java.util.List;
/**
*
* @author zhyong.fan
*
* @date 2016/9/21 16:43 <br/>
*
*/
@Repository
public interface FinancialRepayDetailMapper {
/**
*
* @param repayDetails 待插入的记录条数
* @return 成功插入的记录数
*/
int insert(List<RepayDetailFinancial> repayDetails);
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="cn.gq.financial.db.local.dao.FinancialRepayDetailMapper">
<resultMap id="BaseResultMap" type="cn.gq.financial.model.db.RepayDetailFinancial">
<result column="id" property="id" jdbcType="BIGINT"/>
<result column="biz_type" property="bizType" jdbcType="VARCHAR"/>
<result column="pay_account" property="payAccount" jdbcType="VARCHAR"/>
<result column="month" property="month" jdbcType="TINYINT"/>
<result column="repayment_plan_id" property="repaymentPlanId" jdbcType="BIGINT"/>
<result column="order_id" property="orderId" jdbcType="INTEGER"/>
<result column="lhp_seria_no" property="lhpSeriaNo" jdbcType="VARCHAR"/>
<result column="customer_from" property="customerFrom" jdbcType="VARCHAR"/>
<result column="funding_corp_name" property="fundingCorpName" jdbcType="VARCHAR"/>
<result column="contract_no" property="contractNo" jdbcType="VARCHAR"/>
<result column="user_name" property="userName" jdbcType="VARCHAR"/>
<result column="user_id_no" property="userIdNo" jdbcType="VARCHAR"/>
<result column="user_phone" property="userPhone" jdbcType="VARCHAR"/>
<result column="loan_paid_time" property="loanPaidAt" jdbcType="TIMESTAMP"/>
<result column="should_loan_amount" property="shouldLoanAmount" jdbcType="DECIMAL"/>
<result column="real_loan_amount" property="realLoanAmount" jdbcType="DECIMAL"/>
<result column="contract_term" property="contractTerm" jdbcType="TINYINT"/>
<result column="term_no" property="termNo" jdbcType="TINYINT"/>
<result column="deadline" property="deadline" jdbcType="TIMESTAMP"/>
<result column="required_repayment" property="requiredRepayment" jdbcType="DECIMAL"/>
<result column="repaid_time" property="repaidAt" jdbcType="TIMESTAMP"/>
<result column="principal_amount" property="principalAmount" jdbcType="DECIMAL"/>
<result column="interest" property="interest" jdbcType="DECIMAL"/>
<result column="overdue_fee" property="overdueFee" jdbcType="DECIMAL"/>
<result column="self_mangemet_fee" property="selfManexpense" jdbcType="DECIMAL"/>
<result column="activity_relief" property="activityRelief" jdbcType="DECIMAL"/>
<result column="collection_relief" property="collectionRelief" jdbcType="DECIMAL"/>
<result column="current_repayment" property="currentRepayment" jdbcType="DECIMAL"/>
<result column="pay_approach_poundage" property="payApproachPoundage" jdbcType="DECIMAL"/>
<result column="financial_merchant" property="financialMerchant" jdbcType="VARCHAR"/>
<result column="actual_funding_corp" property="actuallyFundingCorp" jdbcType="VARCHAR"/>
<result column="pay_center_orderNo" property="payCenterOrderNo" jdbcType="VARCHAR"/>
<result column="loan_history_id" property="loanApplicationHistoryId" jdbcType="BIGINT"/>
<result column="contract_create_time" property="contractCreateAt" jdbcType="TIMESTAMP"/>
<result column="remark" property="remark" jdbcType="VARCHAR"/>
<result column="phone_merchant" property="phoneMerchant" jdbcType="VARCHAR"/>
<result column="product_name3C" property="productName3C" jdbcType="VARCHAR"/>
</resultMap>
<sql id="BaseColumns">
biz_type, pay_account, month, repayment_plan_id,order_id, lhp_seria_no,
customer_from, funding_corp_name, contract_no, user_name, user_id_no, user_phone, loan_paid_time,
should_loan_amount, real_loan_amount, contract_term, term_no, deadline, required_repayment,
repaid_time, principal_amount, interest, overdue_fee, self_mangemet_fee, activity_relief, collection_relief,
current_repayment, pay_approach_poundage, financial_merchant, actual_funding_corp, pay_center_orderno,
loan_history_id, contract_create_time, remark, phone_merchant, product_name3c
</sql>
<insert id="insert" useGeneratedKeys="true" parameterType="java.util.List">
INSERT INTO repay_detail_financial(<include refid="BaseColumns"/>)
VALUES
<foreach collection="list" item="record" separator=",">
(#{record.bizType, jdbcType=VARCHAR},
#{record.payAccount,jdbcType=VARCHAR},
#{record.month,jdbcType=TINYINT},
#{record.repaymentPlanId,jdbcType=BIGINT},
#{record.orderId,jdbcType=INTEGER},
#{record.lhpSeriaNo,jdbcType=VARCHAR},
#{record.customerFrom,jdbcType=VARCHAR},
#{record.fundingCorpName,jdbcType=VARCHAR},
#{record.contractNo,jdbcType=VARCHAR},
#{record.userName,jdbcType=VARCHAR},
#{record.userIdNo,jdbcType=VARCHAR},
#{record.userPhone,jdbcType=VARCHAR},
#{record.loanPaidAt,jdbcType=TIMESTAMP},
#{record.shouldLoanAmount,jdbcType=DECIMAL},
#{record.realLoanAmount,jdbcType=DECIMAL},
#{record.contractTerm,jdbcType=TINYINT},
#{record.termNo,jdbcType=TINYINT},
#{record.deadline,jdbcType=TIMESTAMP},
#{record.requiredRepayment,jdbcType=DECIMAL},
#{record.repaidAt,jdbcType=TIMESTAMP},
#{record.principalAmount,jdbcType=DECIMAL},
#{record.interest,jdbcType=DECIMAL},
#{record.overdueFee,jdbcType=DECIMAL},
#{record.selfManexpense,jdbcType=DECIMAL},
#{record.activityRelief,jdbcType=DECIMAL},
#{record.collectionRelief,jdbcType=DECIMAL},
#{record.currentRepayment,jdbcType=DECIMAL},
#{record.payApproachPoundage,jdbcType=DECIMAL},
#{record.financialMerchant,jdbcType=VARCHAR},
#{record.actuallyFundingCorp,jdbcType=VARCHAR},
#{record.payCenterOrderNo,jdbcType=VARCHAR},
#{record.loanApplicationHistoryId,jdbcType=BIGINT},
#{record.contractCreateAt,jdbcType=TIMESTAMP},
#{record.remark,jdbcType=VARCHAR},
#{record.phoneMerchant,jdbcType=VARCHAR},
#{record.productName3C,jdbcType=VARCHAR})
</foreach>
</insert>
</mapper>
\ No newline at end of file
package cn.gq.financial.service;
import cn.gq.financial.app.handler.RepayHandlerAsync;
import cn.gq.financial.db.local.dao.FinancialRepayDetailMapper;
import cn.gq.financial.model.db.RepayDetailFinancial;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Base64;
import java.util.List;
import java.util.concurrent.*;
import java.util.concurrent.locks.Condition;
/**
* @author zhyong.fan
* @date 2016/9/22 11:25 <br/>
*/
@Service
public class FinancialRepayDetailService {
private static final Logger LOGGER = LoggerFactory.getLogger(FinancialRepayDetailService.class);
private static final int BATCH_COUNT = 500;
@Autowired
private FinancialRepayDetailMapper repayDetailMapper;
@Autowired
private ThreadPoolTaskExecutor taskExecutor;
private CountDownLatch latch;
public void saveDetails(List<RepayDetailFinancial> records) throws InterruptedException {
int succCount = 0;
int totalCount = records.size();
if (totalCount <= BATCH_COUNT) {
latch = new CountDownLatch(1);
succCount = repayDetailMapper.insert(records);
} else {
int loopCount = totalCount / BATCH_COUNT == 0 ? (totalCount / BATCH_COUNT) : (totalCount / BATCH_COUNT + 1);
latch = new CountDownLatch(loopCount);
for (int i = 0; i < loopCount; i++) {
int startIndex = i * BATCH_COUNT;
int endIndex = ((i + 1) * BATCH_COUNT < totalCount) ? (i + 1) * BATCH_COUNT : totalCount;
succCount += repayDetailMapper.insert(records);
// taskExecutor.execute(new BatchInsertThread(repayDetailMapper, records.subList(startIndex, endIndex), latch));
}
}
//taskExecutor.execute(new BatchInsertThread(repayDetailMapper, records, latch));
//latch.await();
LOGGER.info("待插入记录数为{}条, 成功插入{}条", records.size(), succCount);
}
class BatchInsertThread implements Runnable {
private FinancialRepayDetailMapper repayDetailMapper;
private CountDownLatch latch;
private List<RepayDetailFinancial> records;
public BatchInsertThread(FinancialRepayDetailMapper repayDetailMapper, List<RepayDetailFinancial> records, CountDownLatch latch) {
this.repayDetailMapper = repayDetailMapper;
this.records = records;
this.latch = latch;
}
@Override
public void run() {
int succCount = repayDetailMapper.insert(records);
latch.countDown();
LOGGER.info("待插入记录数为{}条, 成功插入{}条", records.size(), succCount);
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC
"-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<mappers>
<mapper resource="cn/gq/financial/db/local/mapping/MallDataSourceMapper.xml"/>
</mappers>
</configuration>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment