org.apache.ibatis.annotations.Mapper Java Examples

The following examples show how to use org.apache.ibatis.annotations.Mapper. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MapperAutoConfiguration.java    From mapper-boot-starter with MIT License 6 votes vote down vote up
@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {

    logger.debug("Searching for mappers annotated with @Mapper");

    ClassPathMapperScanner scanner = new ClassPathMapperScanner(registry);
    scanner.setMapperProperties(environment);
    try {
        if (this.resourceLoader != null) {
            scanner.setResourceLoader(this.resourceLoader);
        }

        List<String> packages = AutoConfigurationPackages.get(this.beanFactory);
        if (logger.isDebugEnabled()) {
            for (String pkg : packages) {
                logger.debug("Using auto-configuration base package '{}'", pkg);
            }
        }

        scanner.setAnnotationClass(Mapper.class);
        scanner.registerFilters();
        scanner.doScan(StringUtils.toStringArray(packages));
    } catch (IllegalStateException ex) {
        logger.debug("Could not determine auto-configuration package, automatic mapper scanning disabled.", ex);
    }
}
 
Example #2
Source File: LocMybatisAutoConfiguration.java    From loc-framework with MIT License 6 votes vote down vote up
private void createClassPathMapperScanner(BeanDefinitionRegistry registry, String prefixName) {
  ClassPathMapperScanner scanner = new ClassPathMapperScanner(registry);

  try {
    if (this.resourceLoader != null) {
      scanner.setResourceLoader(this.resourceLoader);
    }

    List<String> packages = AutoConfigurationPackages.get(beanFactory);
    packages.forEach(pkg -> log.info("Using auto-configuration base package '{}'", pkg));

    scanner.setAnnotationClass(Mapper.class);
    scanner.setSqlSessionFactoryBeanName(prefixName + "SessionFactory");
    scanner.registerFilters();
    scanner.doScan(StringUtils.toStringArray(packages));
  } catch (IllegalStateException ex) {
    log.info("Could not determine auto-configuration package", ex);
  }
}
 
Example #3
Source File: MyBatisPlusAutoConfiguration.java    From mybatis-plus-sharding-jdbc-spring-boot-starter with Apache License 2.0 6 votes vote down vote up
@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {

    logger.debug("Searching for mappers annotated with @Mapper");

    ClassPathMapperScanner scanner = new ClassPathMapperScanner(registry);

    try {
        if (this.resourceLoader != null) {
            scanner.setResourceLoader(this.resourceLoader);
        }

        List<String> packages = AutoConfigurationPackages.get(this.beanFactory);
        if (logger.isDebugEnabled()) {
            for (String pkg : packages) {
                logger.debug("Using auto-configuration base package '" + pkg + "'");
            }
        }

        scanner.setAnnotationClass(Mapper.class);
        scanner.registerFilters();
        scanner.doScan(StringUtils.toStringArray(packages));
    } catch (IllegalStateException ex) {
        logger.debug("Could not determine auto-configuration package, automatic mapper scanning disabled." + ex);
    }
}
 
Example #4
Source File: MapperAutoConfiguration.java    From mapper-boot-starter with MIT License 6 votes vote down vote up
@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {

    logger.debug("Searching for mappers annotated with @Mapper");

    ClassPathMapperScanner scanner = new ClassPathMapperScanner(registry);
    scanner.setMapperProperties(environment);
    try {
        if (this.resourceLoader != null) {
            scanner.setResourceLoader(this.resourceLoader);
        }

        List<String> packages = AutoConfigurationPackages.get(this.beanFactory);
        if (logger.isDebugEnabled()) {
            for (String pkg : packages) {
                logger.debug("Using auto-configuration base package '{}'", pkg);
            }
        }

        scanner.setAnnotationClass(Mapper.class);
        scanner.registerFilters();
        scanner.doScan(StringUtils.toStringArray(packages));
    } catch (IllegalStateException ex) {
        logger.debug("Could not determine auto-configuration package, automatic mapper scanning disabled.", ex);
    }
}
 
Example #5
Source File: AnnotationStatementScanner.java    From mybatis-jpa with Apache License 2.0 6 votes vote down vote up
public void scan() {
  for (String basePackage : basePackages) {
    Reflections reflections = new Reflections(basePackage, new TypeAnnotationsScanner(),
        new SubTypesScanner(), new MethodAnnotationsScanner());
    Set<Class<?>> mappers = reflections.getTypesAnnotatedWith(Mapper.class);
    for (Class<?> mapperClass : mappers) {
      Method[] methods = mapperClass.getMethods();
      for (Method method : methods) {
        Annotation[] annotations = method.getDeclaredAnnotations();
        for (Annotation annotation : annotations) {
          StatementFactory statementFactory = annotationStatementRegistry
              .findFactory(annotation.annotationType());
          if (statementFactory != null) {
            MappedStatement statement = statementFactory.parseStatement(configuration, method, mapperClass);
            configuration.addMappedStatement(statement);
          }
        }

      }
    }
  }
  parsePendingMethods();
}
 
Example #6
Source File: PersistenceLayerTest.java    From ddd-architecture-samples with MIT License 5 votes vote down vote up
@Test
void mappers_be_annotated_with_Mapper() {
    classes()
            .that().resideInAPackage("..persistence..")
            .and().haveSimpleNameEndingWith("Mapper")
            .should().beAnnotatedWith(Mapper.class)
            .as("The mappers should be annotated with MyBatis 'Repository'.")
            .check(classes);
}
 
Example #7
Source File: MapperAutoConfiguration.java    From Mapper with MIT License 5 votes vote down vote up
@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {

    logger.debug("Searching for mappers annotated with @Mapper");

    ClassPathMapperScanner scanner = new ClassPathMapperScanner(registry);
    scanner.setMapperProperties(environment);
    try {
        if (this.resourceLoader != null) {
            scanner.setResourceLoader(this.resourceLoader);
        }
        List<String> packages = AutoConfigurationPackages.get(this.beanFactory);
        if (logger.isDebugEnabled()) {
            for (String pkg : packages) {
                logger.debug("Using auto-configuration base package '{}'", pkg);
            }
        }
        BaseProperties properties = SpringBootBindUtil.bind(environment, BaseProperties.class, BaseProperties.MYBATIS_PREFIX);
        if(properties != null && properties.getBasePackages() != null && properties.getBasePackages().length > 0){
            packages.addAll(Arrays.asList(properties.getBasePackages()));
        } else {
            //设置了包名的情况下,不需要指定该注解
            scanner.setAnnotationClass(Mapper.class);
        }
        scanner.registerFilters();
        scanner.doScan(StringUtils.toStringArray(packages));
    } catch (IllegalStateException ex) {
        logger.debug("Could not determine auto-configuration package, automatic mapper scanning disabled.", ex);
    }
}
 
Example #8
Source File: BaseProcessor.java    From utils with Apache License 2.0 5 votes vote down vote up
protected TypeSpec.Builder makeClass(TypeElement clazzElement, RedisMapperProperties properties) {
    TypeSpec.Builder clazzBuilder = TypeSpec.interfaceBuilder(clazzElement.getSimpleName().toString() + STAFF)
            .addModifiers(Modifier.PUBLIC);

    clazzBuilder.addAnnotation(Mapper.class);

    if (!checkXMLFileExist(clazzElement)) {
        AnnotationSpec cacheAnnotation = AnnotationSpec.builder(CacheNamespace.class)
                .addMember("flushInterval", "$L", properties.getFlushInterval())
                .addMember("implementation", "$L.class", properties.getImplementation())
                .addMember("eviction", "$L.class", properties.getEviction())
                .addMember("size", "$L", properties.getSize())
                .addMember("readWrite", "$L", properties.isReadWrite())
                .addMember("blocking", "$L", properties.isBlocking())
                .build();
        clazzBuilder.addAnnotation(cacheAnnotation);

        for (TypeMirror interfaceClass : clazzElement.getInterfaces()) {
            clazzBuilder.addSuperinterface(TypeName.get(interfaceClass));
        }
        clazzBuilder.addSuperinterface(TypeName.get(clazzElement.asType()));
    } else {
        updateXML(clazzElement, properties);
    }

    return clazzBuilder;
}
 
Example #9
Source File: MapperInstrument.java    From mybatis-boost with MIT License 5 votes vote down vote up
public static boolean modify(String className, boolean mapUnderscoreToCamelCase) {
    synchronized (className.intern()) {
        if (modifiedClassNames.contains(className)) return true;
        try {
            boolean modified = false;
            CtClass ctClass = ClassPool.getDefault().get(className);
            for (CtMethod ctMethod : ctClass.getDeclaredMethods()) {
                if (ctMethod.hasAnnotation(Mapper.class)) {
                    MethodNameParser parser =
                            new MethodNameParser(ctMethod.getName(), "#t", mapUnderscoreToCamelCase);
                    addQueryAnnotation(ctMethod, parser.toSql());
                    addRowBoundsParameter(ctMethod, parser.toRowBounds());
                    modified = true;
                }
            }
            if (modified) {
                ctClass.toClass(MapperInstrument.class.getClassLoader(),
                        MapperInstrument.class.getProtectionDomain());
                modifiedClassNames.add(className);
            }

            for (CtClass i : ctClass.getInterfaces()) {
                modified |= modify(i.getName(), mapUnderscoreToCamelCase);
            }
            return modified;
        } catch (CannotCompileException | NotFoundException e) {
            throw new RuntimeException(e);
        }
    }
}
 
Example #10
Source File: MybatisPlusAutoConfig.java    From seata-samples with Apache License 2.0 5 votes vote down vote up
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
    MybatisPlusAutoConfig.logger.debug("Searching for mappers annotated with @Mapper");
    ClassPathMapperScanner scanner = new ClassPathMapperScanner(registry);

    try {
        if (this.resourceLoader != null) {
            scanner.setResourceLoader(this.resourceLoader);
        }

        List<String> packages = AutoConfigurationPackages.get(this.beanFactory);
        if (MybatisPlusAutoConfig.logger.isDebugEnabled()) {
            Iterator iterator = packages.iterator();

            while(iterator.hasNext()) {
                String pkg = (String)iterator.next();
                MybatisPlusAutoConfig.logger.debug("Using auto-configuration base package '" + pkg + "'");
            }
        }

        scanner.setAnnotationClass(Mapper.class);
        scanner.registerFilters();
        scanner.doScan(StringUtils.toStringArray(packages));
    } catch (IllegalStateException var7) {
        MybatisPlusAutoConfig.logger.debug("Could not determine auto-configuration package, automatic mapper scanning disabled." + var7);
    }

}
 
Example #11
Source File: MybatisPlusAutoConfig.java    From seata-samples with Apache License 2.0 5 votes vote down vote up
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
    MybatisPlusAutoConfig.logger.debug("Searching for mappers annotated with @Mapper");
    ClassPathMapperScanner scanner = new ClassPathMapperScanner(registry);

    try {
        if (this.resourceLoader != null) {
            scanner.setResourceLoader(this.resourceLoader);
        }

        List<String> packages = AutoConfigurationPackages.get(this.beanFactory);
        if (MybatisPlusAutoConfig.logger.isDebugEnabled()) {
            Iterator iterator = packages.iterator();

            while(iterator.hasNext()) {
                String pkg = (String)iterator.next();
                MybatisPlusAutoConfig.logger.debug("Using auto-configuration base package '" + pkg + "'");
            }
        }

        scanner.setAnnotationClass(Mapper.class);
        scanner.registerFilters();
        scanner.doScan(StringUtils.toStringArray(packages));
    } catch (IllegalStateException var7) {
        MybatisPlusAutoConfig.logger.debug("Could not determine auto-configuration package, automatic mapper scanning disabled." + var7);
    }

}
 
Example #12
Source File: PersistenceLayerTest.java    From ddd-architecture-samples with MIT License 5 votes vote down vote up
@Test
void mappers_should_be_named_ending_with_Mapper() {
    classes()
            .that().resideInAPackage("..persistence..")
            .and().areAnnotatedWith(Mapper.class)
            .should().haveSimpleNameEndingWith("Mapper")
            .as("The mappers should be named ending with 'Mapper'.")
            .check(classes);
}
 
Example #13
Source File: PersistenceLayerTest.java    From ddd-architecture-samples with MIT License 5 votes vote down vote up
@Test
void mappers_be_interface() {
    classes()
            .that().resideInAPackage("..persistence..")
            .and().areAnnotatedWith(Mapper.class)
            .should().beInterfaces()
            .as("The mappers should be interface.")
            .check(classes);
}
 
Example #14
Source File: ProjectNosqlMapper.java    From mybatis-boost with MIT License 4 votes vote down vote up
@Mapper
Project selectAllOffset1Limit1();
 
Example #15
Source File: ProjectNosqlMapper.java    From mybatis-boost with MIT License 4 votes vote down vote up
@Mapper
List<Project> selectByGroupIdAndArtifactId(String groupId, String artifactId);
 
Example #16
Source File: ProjectNosqlMapper.java    From mybatis-boost with MIT License 4 votes vote down vote up
@Mapper
List<Project> selectByGroupIdOrArtifactId(String groupId, String artifactId);
 
Example #17
Source File: ProjectNosqlMapper.java    From mybatis-boost with MIT License 4 votes vote down vote up
@Mapper
List<Project> selectByArtifactIdNot(String artifactId);
 
Example #18
Source File: ProjectNosqlMapper.java    From mybatis-boost with MIT License 4 votes vote down vote up
@Mapper
List<Project> selectAllOrderByGroupIdDesc();
 
Example #19
Source File: ProjectNosqlMapper.java    From mybatis-boost with MIT License 4 votes vote down vote up
@Mapper
List<Project> selectTop2();
 
Example #20
Source File: ProjectNosqlMapper.java    From mybatis-boost with MIT License 4 votes vote down vote up
@Mapper
Project selectFirst();
 
Example #21
Source File: ProjectNosqlMapper.java    From mybatis-boost with MIT License 4 votes vote down vote up
@Mapper
int deleteAll();