use of org.sagacity.sqltoy.callback.ReflectPropertyHandler in project sagacity-sqltoy by chenrenfei.
the class DialectUtils method saveAllIgnoreExist.
/**
* @todo 执行批量保存或修改操作
* @param sqlToyContext
* @param sql
* @param entities
* @param entityMeta
* @param reflectPropertyHandler
* @param conn
* @param autoCommit
* @throws Exception
*/
public static Long saveAllIgnoreExist(SqlToyContext sqlToyContext, List<?> entities, final int batchSize, EntityMeta entityMeta, GenerateSqlHandler generateSqlHandler, ReflectPropertyHandler reflectPropertyHandler, Connection conn, Boolean autoCommit) throws Exception {
// 构造全新的新增记录参数赋值反射(覆盖之前的)
ReflectPropertyHandler handler = getAddReflectHandler(sqlToyContext, reflectPropertyHandler);
List<Object[]> paramValues = BeanUtil.reflectBeansToInnerAry(entities, entityMeta.getFieldsArray(), null, handler, false, 0);
int pkIndex = entityMeta.getIdIndex();
// 是否存在业务ID
boolean hasBizId = (entityMeta.getBusinessIdGenerator() == null) ? false : true;
int bizIdColIndex = hasBizId ? entityMeta.getFieldIndex(entityMeta.getBusinessIdField()) : 0;
// 标识符
String signature = entityMeta.getBizIdSignature();
Integer relatedColumn = entityMeta.getBizIdRelatedColIndex();
// 无主键以及多主键以及assign或通过generator方式产生主键策略
if (null != entityMeta.getIdStrategy() && null != entityMeta.getIdGenerator()) {
int bizIdLength = entityMeta.getBizIdLength();
int idLength = entityMeta.getIdLength();
Object[] rowData;
Object relatedColValue = null;
int idJdbcType = entityMeta.getIdType();
int businessIdType = hasBizId ? entityMeta.getColumnType(entityMeta.getBusinessIdField()) : 0;
for (int i = 0; i < paramValues.size(); i++) {
rowData = (Object[]) paramValues.get(i);
// 关联字段赋值
if (relatedColumn != null) {
relatedColValue = rowData[relatedColumn];
}
if (StringUtil.isBlank(rowData[pkIndex])) {
rowData[pkIndex] = entityMeta.getIdGenerator().getId(entityMeta.getTableName(), signature, relatedColValue, null, idJdbcType, idLength);
// 回写主键值
BeanUtils.setProperty(entities.get(i), entityMeta.getIdArray()[0], rowData[pkIndex]);
}
if (hasBizId && StringUtil.isBlank(rowData[bizIdColIndex])) {
rowData[bizIdColIndex] = entityMeta.getBusinessIdGenerator().getId(entityMeta.getTableName(), signature, relatedColValue, null, businessIdType, bizIdLength);
// 回写业务主键值
BeanUtils.setProperty(entities.get(i), entityMeta.getBusinessIdField(), rowData[bizIdColIndex]);
}
}
}
String saveAllNotExistSql = generateSqlHandler.generateSql(entityMeta, null);
if (sqlToyContext.isDebug())
out.println("saveAllNotExistSql=" + saveAllNotExistSql);
return SqlUtil.batchUpdateByJdbc(saveAllNotExistSql, paramValues, batchSize, null, entityMeta.getFieldsTypeArray(), autoCommit, conn);
}
Aggregations