use of com.github.dreamroute.mybatis.pro.core.consts.MapperLabel in project mybatis-pro by Dreamroute.
the class MapperUtil method parse.
/**
* 将通用crud方法填充到resource里
*/
public Resource parse() {
Method[] methods = Mapper.class.getMethods();
Stream.of(methods).map(Method::getName).forEach(methodName -> {
MapperLabel ml;
String returnType = null;
if (methodName.startsWith(MapperLabel.SELECT.getCode())) {
ml = MapperLabel.SELECT;
returnType = entityCls.getName();
} else if (methodName.startsWith(MapperLabel.INSERT.getCode())) {
ml = MapperLabel.INSERT;
} else if (methodName.startsWith(MapperLabel.UPDATE.getCode())) {
ml = MapperLabel.UPDATE;
} else {
ml = MapperLabel.DELETE;
}
DocumentUtil.fillSqlNode(this.document, ml, methodName, returnType, methodName2Sql.get(methodName), type, idName);
});
return DocumentUtil.createResourceFromDocument(this.document);
}
use of com.github.dreamroute.mybatis.pro.core.consts.MapperLabel in project mybatis-pro by Dreamroute.
the class MyBatisProUtil method processSpecialMethods.
public static Set<Resource> processSpecialMethods(Set<Resource> resources) {
return resources.stream().map(resource -> {
Class<?> mapperCls = getNamespaceFromXmlResource(resource);
validateDuplicateMethods(mapperCls, resource);
Document doc = createDocumentFromResource(resource);
List<String> specialMethods = getSpecialMethods(mapperCls);
if (!isEmpty(specialMethods)) {
Class<?> entityCls = getTypeArgument(mapperCls);
if (!hasAnnotation(entityCls, Table.class)) {
throw new MyBatisProException("实体" + entityCls.getName() + "必须包含@" + Table.class.getName() + "注解");
}
// 将findBy方法的返回值的别名进行缓存,这里将缓存里面没有的返回值进行缓存,实体部分已经在前面进行了缓存,这里缓存非实体返回类型
Map<String, String> name2Type = getMethodName2ReturnType(mapperCls);
cacheAlias(name2Type);
specialMethods.forEach(specialMethodName -> {
String conditions = null;
String sql = null;
MapperLabel mapperLabel = SELECT;
if (specialMethodName.startsWith("findBy")) {
conditions = specialMethodName.substring(6);
sql = "select * from ";
} else if (specialMethodName.startsWith("deleteBy")) {
conditions = specialMethodName.substring(8);
sql = "delete from ";
mapperLabel = DELETE;
} else if (specialMethodName.startsWith("countBy")) {
conditions = specialMethodName.substring(7);
sql = "select count(*) c from ";
} else if (specialMethodName.startsWith("existBy")) {
conditions = specialMethodName.substring(7);
sql = "select (case when count(*)=0 then 'false' ELSE 'true' end) from ";
}
sql += getAnnotationValue(entityCls, Table.class) + " <where> " + createCondition(conditions, FIELDS_ALIAS_CACHE.get(entityCls));
// 对于delete需要特殊处理,delete不需要设置resultType
String resultType = mapperLabel == DELETE ? null : name2Type.get(specialMethodName);
fillSqlNode(doc, mapperLabel, specialMethodName, resultType, sql, null, null);
});
}
return createResourceFromDocument(doc);
}).collect(toSet());
}
Aggregations