use of com.mendmix.mybatis.metadata.MapperMetadata in project jeesuite-libs by vakinge.
the class JeesuiteMybatisEnhancer method handle.
public static void handle(String group, Configuration configuration) throws Exception {
if ("tkMapper".equals(MybatisConfigs.getCrudDriver())) {
Class<?> helperClazz = Class.forName("tk.mybatis.mapper.mapperhelper.MapperHelper");
Object helper = helperClazz.newInstance();
Class<?> configClazz = Class.forName("tk.mybatis.mapper.entity.Config");
Object config = configClazz.newInstance();
Method method = configClazz.getDeclaredMethod("setNotEmpty", boolean.class);
method.invoke(config, false);
method = helperClazz.getDeclaredMethod("setConfig", configClazz);
method.invoke(helper, config);
method = helperClazz.getDeclaredMethod("registerMapper", Class.class);
List<MapperMetadata> mappers = MybatisMapperParser.getMapperMetadatas(group);
for (MapperMetadata mapper : mappers) {
method.invoke(helper, mapper.getMapperClass());
}
method = helperClazz.getDeclaredMethod("processConfiguration", Configuration.class);
method.invoke(helper, configuration);
} else {
new GeneralSqlGenerator(group, configuration).generate();
}
// pageHelper
try {
Class<?> pageHelperClazz = Class.forName("com.github.pagehelper.PageInterceptor");
Interceptor pageInterceptor = (Interceptor) pageHelperClazz.newInstance();
configuration.addInterceptor(pageInterceptor);
} catch (Exception e) {
}
// 注册拦截器
String[] hanlderNames = MybatisConfigs.getHandlerNames(group);
JeesuiteMybatisInterceptor interceptor = new JeesuiteMybatisInterceptor(group, hanlderNames);
configuration.addInterceptor(interceptor);
interceptor.afterRegister();
logger.info(">> JeesuiteMybatisEnhancer finshed -> group:{},hanlderNames:{}", group, hanlderNames);
}
use of com.mendmix.mybatis.metadata.MapperMetadata in project jeesuite-libs by vakinge.
the class SqlRewriteHandler method onInterceptor.
@Override
public Object onInterceptor(InvocationVals invocation) throws Throwable {
if (!invocation.isSelect())
return null;
if (invocation.getMappedStatement().getId().endsWith(CrudMethods.selectByPrimaryKey.name())) {
return null;
}
Map<String, String[]> dataMappings = null;
if (dynaDataPermEnabled) {
dataMappings = MybatisRuntimeContext.getDataProfileMappings();
}
//
rewriteSql(invocation, dataMappings);
if (invocation.getPageParam() != null)
return null;
// 不查数据库直接返回
if (invocation.getSql() == null) {
List<Object> list = new ArrayList<>(1);
//
MapperMetadata entityInfo = MybatisMapperParser.getMapperMetadata(invocation.getMapperNameSpace());
String methodName = invocation.getMappedStatement().getId().replace(invocation.getMapperNameSpace(), StringUtils.EMPTY).substring(1);
Class<?> returnType = entityInfo.getMapperMethod(methodName).getMethod().getReturnType();
if (returnType == int.class || returnType == Integer.class || returnType == long.class || returnType == Long.class) {
list.add(0);
}
return list;
} else {
Executor executor = invocation.getExecutor();
MappedStatement mappedStatement = invocation.getMappedStatement();
ResultHandler<?> resultHandler = (ResultHandler<?>) invocation.getArgs()[3];
List<ParameterMapping> parameterMappings = invocation.getBoundSql().getParameterMappings();
BoundSql newBoundSql = new BoundSql(mappedStatement.getConfiguration(), invocation.getSql(), parameterMappings, invocation.getParameter());
//
copyForeachAdditionlParams(invocation.getBoundSql(), newBoundSql);
CacheKey cacheKey = executor.createCacheKey(mappedStatement, invocation.getParameter(), RowBounds.DEFAULT, newBoundSql);
List<?> resultList = executor.query(mappedStatement, invocation.getParameter(), RowBounds.DEFAULT, resultHandler, cacheKey, newBoundSql);
return resultList;
}
}
use of com.mendmix.mybatis.metadata.MapperMetadata 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;
}
}
}
}
}
}
use of com.mendmix.mybatis.metadata.MapperMetadata in project jeesuite-libs by vakinge.
the class SqlRewriteHandler method handleTableOrderBy.
private void handleTableOrderBy(PlainSelect selectBody, Table table, InvocationVals invocation) {
PageParams pageParam = invocation.getPageParam();
if (pageParam == null || pageParam.getOrderBys() == null || pageParam.getOrderBys().isEmpty()) {
return;
}
List<OrderByElement> orderByElements = new ArrayList<>(pageParam.getOrderBys().size());
OrderByElement orderByElement;
for (OrderBy orderBy : pageParam.getOrderBys()) {
if (orderBy == null)
continue;
MapperMetadata mapperMeta = MybatisMapperParser.getMapperMetadata(invocation.getMapperNameSpace());
String columnName = mapperMeta.getEntityMetadata().getProp2ColumnMappings().get(orderBy.getField());
if (columnName == null)
columnName = orderBy.getField();
orderByElement = new OrderByElement();
orderByElement.setAsc(OrderType.ASC.name().equals(orderBy.getSortType()));
orderByElement.setExpression(new Column(table, columnName));
orderByElements.add(orderByElement);
}
selectBody.setOrderByElements(orderByElements);
}
use of com.mendmix.mybatis.metadata.MapperMetadata in project jeesuite-libs by vakinge.
the class TableRouteHandler method start.
@Override
public void start(JeesuiteMybatisInterceptor context) {
List<MapperMetadata> mappers = MybatisMapperParser.getMapperMetadatas(context.getGroupName());
List<String> tmpTables = new ArrayList<>();
for (MapperMetadata mapper : mappers) {
if (!mapper.getTableName().contains(GlobalConstants.PLACEHOLDER_PREFIX))
continue;
tmpTables.add(mapper.getTableName());
}
for (MapperMetadata mapper : mappers) {
if (tmpTables.contains(mapper.getTableName())) {
tableRouteMappedStatements.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)) {
tableRouteMappedStatements.add(query);
break;
}
}
}
}
}
}
Aggregations