Search in sources :

Example 1 with MapperLabel

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);
}
Also used : Method(java.lang.reflect.Method) MapperLabel(com.github.dreamroute.mybatis.pro.core.consts.MapperLabel)

Example 2 with MapperLabel

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());
}
Also used : CollectionUtils.isEmpty(org.springframework.util.CollectionUtils.isEmpty) ClassUtil.getTypeArgument(cn.hutool.core.util.ClassUtil.getTypeArgument) ID(com.github.dreamroute.mybatis.pro.core.consts.MapperLabel.ID) Arrays(java.util.Arrays) DocumentUtil.fillSqlNode(com.github.dreamroute.mybatis.pro.core.util.DocumentUtil.fillSqlNode) MyBatisProException(com.github.dreamroute.mybatis.pro.core.exception.MyBatisProException) HashMap(java.util.HashMap) Table(com.github.dreamroute.mybatis.pro.core.annotations.Table) AnnotationUtil.hasAnnotation(cn.hutool.core.annotation.AnnotationUtil.hasAnnotation) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) DocumentUtil.createResourceFromDocument(com.github.dreamroute.mybatis.pro.core.util.DocumentUtil.createResourceFromDocument) JsonUtil.toJsonStr(com.github.dreamroute.mybatis.pro.base.codec.enums.JsonUtil.toJsonStr) Sets.difference(com.google.common.collect.Sets.difference) Mapper(com.github.dreamroute.mybatis.pro.sdk.Mapper) Collectors.toMap(java.util.stream.Collectors.toMap) DocumentUtil.createDocumentFromResource(com.github.dreamroute.mybatis.pro.core.util.DocumentUtil.createDocumentFromResource) Document(org.w3c.dom.Document) Arrays.asList(java.util.Arrays.asList) Map(java.util.Map) AnnotationUtil.getAnnotationValue(cn.hutool.core.annotation.AnnotationUtil.getAnnotationValue) DELETE(com.github.dreamroute.mybatis.pro.core.consts.MapperLabel.DELETE) Collectors.toSet(java.util.stream.Collectors.toSet) Resource(org.springframework.core.io.Resource) XmlUtil.getNamespaceFromXmlResource(com.github.dreamroute.mybatis.pro.core.util.XmlUtil.getNamespaceFromXmlResource) ClassUtil.getSpecialMethods(com.github.dreamroute.mybatis.pro.core.util.ClassUtil.getSpecialMethods) Optional.ofNullable(java.util.Optional.ofNullable) SetView(com.google.common.collect.Sets.SetView) Set(java.util.Set) Field(java.lang.reflect.Field) ClassUtil.loadClass(cn.hutool.core.util.ClassUtil.loadClass) MapperLabel(com.github.dreamroute.mybatis.pro.core.consts.MapperLabel) Sets(com.google.common.collect.Sets) ClassUtil.getMethodName2ReturnType(com.github.dreamroute.mybatis.pro.core.util.ClassUtil.getMethodName2ReturnType) SqlUtil.toLine(com.github.dreamroute.mybatis.pro.core.util.SqlUtil.toLine) List(java.util.List) ClassUtil.scanPackageBySuper(cn.hutool.core.util.ClassUtil.scanPackageBySuper) XPathParser(org.apache.ibatis.parsing.XPathParser) Column(com.github.dreamroute.mybatis.pro.core.annotations.Column) ClassUtil.getBaseMethodNames(com.github.dreamroute.mybatis.pro.core.util.ClassUtil.getBaseMethodNames) SELECT(com.github.dreamroute.mybatis.pro.core.consts.MapperLabel.SELECT) XNode(org.apache.ibatis.parsing.XNode) ClassPathUtil(com.github.dreamroute.mybatis.pro.base.util.ClassPathUtil) XMLMapperEntityResolver(org.apache.ibatis.builder.xml.XMLMapperEntityResolver) Arrays.stream(java.util.Arrays.stream) StringUtils(org.springframework.util.StringUtils) ClassUtil.loadClass(cn.hutool.core.util.ClassUtil.loadClass) ArrayList(java.util.ArrayList) Arrays.asList(java.util.Arrays.asList) List(java.util.List) DocumentUtil.createResourceFromDocument(com.github.dreamroute.mybatis.pro.core.util.DocumentUtil.createResourceFromDocument) Document(org.w3c.dom.Document) MyBatisProException(com.github.dreamroute.mybatis.pro.core.exception.MyBatisProException) HashMap(java.util.HashMap) Collectors.toMap(java.util.stream.Collectors.toMap) Map(java.util.Map) MapperLabel(com.github.dreamroute.mybatis.pro.core.consts.MapperLabel)

Aggregations

MapperLabel (com.github.dreamroute.mybatis.pro.core.consts.MapperLabel)2 AnnotationUtil.getAnnotationValue (cn.hutool.core.annotation.AnnotationUtil.getAnnotationValue)1 AnnotationUtil.hasAnnotation (cn.hutool.core.annotation.AnnotationUtil.hasAnnotation)1 ClassUtil.getTypeArgument (cn.hutool.core.util.ClassUtil.getTypeArgument)1 ClassUtil.loadClass (cn.hutool.core.util.ClassUtil.loadClass)1 ClassUtil.scanPackageBySuper (cn.hutool.core.util.ClassUtil.scanPackageBySuper)1 JsonUtil.toJsonStr (com.github.dreamroute.mybatis.pro.base.codec.enums.JsonUtil.toJsonStr)1 ClassPathUtil (com.github.dreamroute.mybatis.pro.base.util.ClassPathUtil)1 Column (com.github.dreamroute.mybatis.pro.core.annotations.Column)1 Table (com.github.dreamroute.mybatis.pro.core.annotations.Table)1 DELETE (com.github.dreamroute.mybatis.pro.core.consts.MapperLabel.DELETE)1 ID (com.github.dreamroute.mybatis.pro.core.consts.MapperLabel.ID)1 SELECT (com.github.dreamroute.mybatis.pro.core.consts.MapperLabel.SELECT)1 MyBatisProException (com.github.dreamroute.mybatis.pro.core.exception.MyBatisProException)1 ClassUtil.getBaseMethodNames (com.github.dreamroute.mybatis.pro.core.util.ClassUtil.getBaseMethodNames)1 ClassUtil.getMethodName2ReturnType (com.github.dreamroute.mybatis.pro.core.util.ClassUtil.getMethodName2ReturnType)1 ClassUtil.getSpecialMethods (com.github.dreamroute.mybatis.pro.core.util.ClassUtil.getSpecialMethods)1 DocumentUtil.createDocumentFromResource (com.github.dreamroute.mybatis.pro.core.util.DocumentUtil.createDocumentFromResource)1 DocumentUtil.createResourceFromDocument (com.github.dreamroute.mybatis.pro.core.util.DocumentUtil.createResourceFromDocument)1 DocumentUtil.fillSqlNode (com.github.dreamroute.mybatis.pro.core.util.DocumentUtil.fillSqlNode)1