use of com.mendmix.mybatis.metadata.ColumnMetadata in project jeesuite-libs by vakinge.
the class AbstractSelectMethodBuilder method getResultMap.
/**
* 生成当前实体的resultMap对象
*
* @param configuration
* @return
*/
public static ResultMap getResultMap(Configuration configuration, Class<?> entityClass) {
List<ResultMapping> resultMappings = new ArrayList<ResultMapping>();
Set<ColumnMetadata> entityClassColumns = MetadataHelper.getEntityMapper(entityClass).getColumns();
for (ColumnMetadata entityColumn : entityClassColumns) {
ResultMapping.Builder builder = new ResultMapping.Builder(configuration, entityColumn.getProperty(), entityColumn.getColumn(), entityColumn.getJavaType());
if (entityColumn.getJdbcType() != null) {
builder.jdbcType(entityColumn.getJdbcType());
}
List<ResultFlag> flags = new ArrayList<ResultFlag>();
if (entityColumn.isId()) {
flags.add(ResultFlag.ID);
}
builder.flags(flags);
builder.lazy(false);
resultMappings.add(builder.build());
}
ResultMap.Builder builder = new ResultMap.Builder(configuration, "BaseResultMap", entityClass, resultMappings, true);
return builder.build();
}
use of com.mendmix.mybatis.metadata.ColumnMetadata in project jeesuite-libs by vakinge.
the class UpdateWithVersionProvider method updateByPrimaryKeyWithVersion.
public String updateByPrimaryKeyWithVersion(Object example) throws Exception {
EntityMetadata entityMapper = MetadataHelper.getEntityMapper(example.getClass());
Set<ColumnMetadata> columns = entityMapper.getColumns();
SQL sql = new SQL().UPDATE(entityMapper.getTable().getName());
StringBuilder setBuilder = new StringBuilder();
StringBuilder whereBuilder = new StringBuilder();
// 主键
ColumnMetadata idColumn = entityMapper.getIdColumn();
appendWhere(whereBuilder, idColumn);
Object value;
for (ColumnMetadata column : columns) {
if (column.isId() || !column.isUpdatable())
continue;
value = MetadataHelper.getEntityField(entityMapper.getTable().getName(), column.getProperty()).get(example);
if (value == null)
continue;
if (column.isVersionField()) {
appendWhere(whereBuilder, column);
// 乐观锁新值
value = getNewVersionVal(column.getJavaType(), value);
appendUpdateSet(setBuilder, column, value);
} else {
appendUpdateSet(setBuilder, column);
}
}
sql.SET(setBuilder.toString());
sql.WHERE(whereBuilder.toString());
return sql.toString();
}
use of com.mendmix.mybatis.metadata.ColumnMetadata in project jeesuite-libs by vakinge.
the class CountByExampleProvider method countByExample.
public String countByExample(Object example) throws Exception {
EntityMetadata entityMapper = MetadataHelper.getEntityMapper(example.getClass());
Set<ColumnMetadata> columns = entityMapper.getColumns();
SQL sql = new SQL().SELECT("COUNT(1)").FROM(entityMapper.getTable().getName());
Object value;
StringBuilder whereBuilder = new StringBuilder();
for (ColumnMetadata column : columns) {
value = MetadataHelper.getEntityField(entityMapper.getTable().getName(), column.getProperty()).get(example);
if (value == null)
continue;
appendWhere(whereBuilder, column);
}
if (whereBuilder.length() == 0)
throw new IllegalArgumentException("至少包含一个查询条件");
sql.WHERE(whereBuilder.toString());
return sql.toString();
}
use of com.mendmix.mybatis.metadata.ColumnMetadata in project jeesuite-libs by vakinge.
the class SqlRewriteHandler method start.
@Override
public void start(JeesuiteMybatisInterceptor context) {
isFieldSharddingTenant = MybatisConfigs.isFieldSharddingTenant(context.getGroupName());
softDeleteColumnName = MybatisConfigs.getSoftDeleteColumn(context.getGroupName());
softDeleteFalseValue = MybatisConfigs.getSoftDeletedFalseValue(context.getGroupName());
deptColumnName = MybatisConfigs.getDeptColumnName(context.getGroupName());
ownerColumnName = MybatisConfigs.getOwnerColumnName(context.getGroupName());
orgBasePermKey = MybatisConfigs.getCurrentOrgPermKey(context.getGroupName());
Properties properties = ResourceUtils.getAllProperties("mendmix.mybatis.permission.table-column-mappings");
properties.forEach((k, v) -> {
String tableName = k.toString().substring(k.toString().indexOf("[") + 1).replace("]", "").trim();
buildTableDataPermissionMapping(tableName, v.toString());
});
dynaDataPermEnabled = !dataPermMappings.isEmpty();
final List<MapperMetadata> mappers = MybatisMapperParser.getMapperMetadatas(context.getGroupName());
//
initColumnConfig(mappers, deptColumnName, deptMappedStatements);
// 软删除
initColumnConfig(mappers, softDeleteColumnName, softDeleteMappedStatements);
// 字段隔离租户模式
if (isFieldSharddingTenant) {
String tenantField = MybatisConfigs.getTenantSharddingField(context.getGroupName());
ColumnMetadata tenantColumn;
for (MapperMetadata mapper : mappers) {
tenantColumn = mapper.getEntityMetadata().getColumns().stream().filter(o -> {
return o.getColumn().equals(tenantField) || o.getProperty().equals(tenantField);
}).findFirst().orElse(null);
if (tenantColumn == null)
continue;
if (tenantColumnName == null)
tenantColumnName = tenantColumn.getColumn();
if (tenantPropName == null)
tenantPropName = tenantColumn.getProperty();
if (!dataPermMappings.containsKey(mapper.getTableName())) {
dataPermMappings.put(mapper.getTableName(), new LinkedHashMap<>());
}
dataPermMappings.get(mapper.getTableName()).put(tenantPropName, tenantColumnName);
}
}
logger.info("dataProfileMappings >> {}", dataPermMappings);
}
use of com.mendmix.mybatis.metadata.ColumnMetadata in project jeesuite-libs by vakinge.
the class SqlRewriteHandler method initColumnConfig.
private void initColumnConfig(List<MapperMetadata> mappers, String column, List<String> mapperNames) {
if (column == null)
return;
List<String> tmpTables = new ArrayList<>();
ColumnMetadata columnMetadata;
for (MapperMetadata mapper : mappers) {
columnMetadata = mapper.getEntityMetadata().getColumns().stream().filter(o -> o.getColumn().equals(column)).findFirst().orElse(null);
if (columnMetadata == null) {
continue;
}
if (column.equals(softDeleteColumnName)) {
softDeletePropName = columnMetadata.getProperty();
}
tmpTables.add(mapper.getTableName());
if (!dataPermMappings.containsKey(mapper.getTableName())) {
dataPermMappings.put(mapper.getTableName(), new LinkedHashMap<>());
}
dataPermMappings.get(mapper.getTableName()).put(columnMetadata.getProperty(), column);
}
//
for (MapperMetadata mapper : mappers) {
if (tmpTables.contains(mapper.getTableName())) {
mapperNames.add(mapper.getMapperClass().getName());
} else {
Set<String> querys = mapper.getQueryTableMappings().keySet();
List<String> tables;
for (String query : querys) {
tables = mapper.getQueryTableMappings().get(query);
for (String table : tables) {
if (tmpTables.contains(table)) {
mapperNames.add(query);
break;
}
}
}
}
}
}
Aggregations