public class BaseDao {
@Autowired
private SqlSessionTemplate sqlSessionTemplate;
private final Logger logger = LoggerFactory.getLogger(this.getClass());
void batchInsert(String statement, List parameterList, int pageSize) {
if (parameterList == null || parameterList.size() == 0) {
return;
;
}
int size = parameterList.size();
if (size <= pageSize) {
batchInsert(statement, parameterList);
}
}
void batchInsert(String statement, List parameterList) {
if (parameterList == null || parameterList.size() == 0) {
return;
}
SqlSession batchSqlSession = null;
try {
batchSqlSession = sqlSessionTemplate.getSqlSessionFactory().openSession(ExecutorType.BATCH, false);
for (Object obj : parameterList) {
batchSqlSession.insert(statement, obj);
}
batchSqlSession.commit();
//清理緩存防止溢出
batchSqlSession.clearCache();
} catch (Exception e) {
logger.error("batch insert error");
if (batchSqlSession != null) {
batchSqlSession.rollback();
}
} finally {
if (batchSqlSession != null) {
batchSqlSession.close();
}
}
}
}
用法:
1.寫個接口繼承這個類,在接口實現類上加入@component註解
2. 填寫SQL ID