Example usage for org.apache.ibatis.binding MapperRegistry getMappers

List of usage examples for org.apache.ibatis.binding MapperRegistry getMappers

Introduction

In this page you can find the example usage for org.apache.ibatis.binding MapperRegistry getMappers.

Prototype

public Collection<Class<?>> getMappers() 

Source Link

Usage

From source file:org.nanoframework.orm.mybatis.MultiDataSourceModule.java

License:Apache License

@Override
protected void configure() {
    Reader reader = null;//from w  w w.j  a v  a 2s  .c  o m
    try {
        InputStream input;
        try {
            Resource resource = new ClassPathResource(mybatisConfigPath);
            input = resource.getInputStream();
            if (input == null)
                input = new FileInputStream(ResourceUtils.getFile(mybatisConfigPath));

        } catch (IOException e) {
            throw new LoaderException(": " + e.getMessage());
        }

        reader = new InputStreamReader(input);
        SqlSessionFactory sessionFactory;
        SqlSessionManager sessionManager = SqlSessionManager
                .newInstance(sessionFactory = new SqlSessionFactoryBuilder().build(reader, envId, jdbc));
        GlobalSqlSession.set(envId, sessionManager);

        Configuration configuration = sessionFactory.getConfiguration();

        MapperRegistry registry = configuration.getMapperRegistry();
        for (String pkg : mapperPackageName) {
            Set<Class<?>> classes = getClasses(pkg);
            if (!CollectionUtils.isEmpty(classes)) {
                for (Class<?> cls : classes) {
                    if (!registry.hasMapper(cls))
                        registry.addMapper(cls);
                }
            }
        }

        // bind mappers
        Collection<Class<?>> mapperClasses = registry.getMappers();
        for (Class<?> mapperType : mapperClasses) {
            bindMapper(mapperType, sessionManager);
        }
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
            }
        }
    }
}