Bladeren bron

原生态jdbc批量插入未拦截bug

huangzhiyong 8 jaren geleden
bovenliggende
commit
acb1c266e4

+ 4 - 27
hos-web-framework/src/main/java/com/yihu/hos/web/framework/dao/SQLGeneralDAO.java

@ -3,7 +3,6 @@ package com.yihu.hos.web.framework.dao;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.JsonNodeType;
import com.yihu.hos.core.datatype.CollectionUtil;
import com.yihu.hos.core.datatype.NumberUtil;
import com.yihu.hos.core.datatype.StringUtil;
import com.yihu.hos.core.log.Logger;
@ -16,7 +15,6 @@ import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.hibernate.jdbc.Work;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
@ -28,9 +26,7 @@ import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.io.IOException;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Collection;
import java.util.Iterator;
@ -421,30 +417,11 @@ public class SQLGeneralDAO implements XSQLGeneralDAO {
    }
    public void insertBatch(final List<String> insertSqlList) {
        Session session = this.getHibernateTemplate().getSessionFactory().getCurrentSession();
        session.doWork(new Work() {
            public void execute(Connection connection) throws SQLException {
                if (!CollectionUtil.isEmpty(insertSqlList)) {
                    Statement stmt = connection.createStatement();
                    connection.setAutoCommit(false);
                    Iterator ex = insertSqlList.iterator();
                    while (ex.hasNext()) {
                        String sql = (String) ex.next();
                        stmt.addBatch(sql);
                    }
                    try {
                        stmt.executeBatch();
                    } catch (Exception var5) {
                        logger.error("insertBatch-ERROR: ", var5);
                    }
//        Session session = this.getHibernateTemplate().getSessionFactory().getCurrentSession();
        String[] sqlStr = new String[insertSqlList.size()];
        String[] sqlArr = insertSqlList.toArray(sqlStr);
        this.jdbcTemplate.batchUpdate(sqlArr);
                    stmt.close();
                }
            }
        });
    }
    public Session openSession() {

+ 10 - 1
src/main/java/com/yihu/hos/interceptor/JdbcTemplateAdvice.java

@ -17,7 +17,7 @@ import org.springframework.stereotype.Component;
@Component
public class JdbcTemplateAdvice {
    @Around("execution(* org.springframework.jdbc.core.JdbcTemplate.query*(..)) || execution(* org.springframework.jdbc.core.JdbcTemplate.execute*(..))")
    @Around("execution(* org.springframework.jdbc.core.JdbcTemplate.batchUpdate*(..)) || execution(* org.springframework.jdbc.core.JdbcTemplate.query*(..)) || execution(* org.springframework.jdbc.core.JdbcTemplate.execute*(..))")
    public Object process(ProceedingJoinPoint point) throws Throwable {
        String schemaName = getSchema();
        //访问目标方法的参数:
@ -29,6 +29,15 @@ public class JdbcTemplateAdvice {
                completeSql = myCatAnnotation + completeSql;
            }
            args[0]=completeSql;
        }else  if (args != null && args.length > 0 && args[0].getClass() == String[].class) {
            String[] sqls = (String[]) args[0];
            if (StringUtils.isNotEmpty(schemaName)) {
                for (int i=0;i<sqls.length;i++){
                    String myCatAnnotation = "/*#mycat:schema=" + schemaName + "*/ ";
                    sqls[i] = myCatAnnotation + sqls[i];
                }
            }
            args[0]=sqls;
        }
        //用改变后的参数执行目标方法
        Object returnValue = point.proceed(args);