Search in sources :

Example 1 with ClassFilter

use of org.jessma.util.PackageHelper.ClassFilter in project JessMA by ldcsaa.

the class MyBatisSessionMgr method buildSessionFactory.

/** 创建 {@link SqlSessionFactory} */
private void buildSessionFactory() {
    synchronized (this) {
        if (sessionFactory == null) {
            try {
                Reader reader = Resources.getResourceAsReader(configFile);
                sessionFactory = new SqlSessionFactoryBuilder().build(reader, environment);
                if (GeneralHelper.isStrNotEmpty(pattern)) {
                    Set<String> packages = PackageHelper.getPackages(pattern);
                    for (String pkg : packages) {
                        Set<Class<?>> entities = PackageHelper.getClasses(pkg, false, new ClassFilter() {

                            @Override
                            public boolean accept(Class<?> clazz) {
                                if (!BeanHelper.isPublicInterface(clazz))
                                    return false;
                                return true;
                            }
                        });
                        Configuration cfg = sessionFactory.getConfiguration();
                        for (Class<?> clazz : entities) {
                            if (!cfg.hasMapper(clazz))
                                cfg.addMapper(clazz);
                        }
                    }
                }
            } catch (IOException e) {
                throw new SqlSessionException(e);
            }
        }
    }
}
Also used : Configuration(org.apache.ibatis.session.Configuration) ClassFilter(org.jessma.util.PackageHelper.ClassFilter) Reader(java.io.Reader) SqlSessionFactoryBuilder(org.apache.ibatis.session.SqlSessionFactoryBuilder) IOException(java.io.IOException) SqlSessionException(org.apache.ibatis.session.SqlSessionException)

Aggregations

IOException (java.io.IOException)1 Reader (java.io.Reader)1 Configuration (org.apache.ibatis.session.Configuration)1 SqlSessionException (org.apache.ibatis.session.SqlSessionException)1 SqlSessionFactoryBuilder (org.apache.ibatis.session.SqlSessionFactoryBuilder)1 ClassFilter (org.jessma.util.PackageHelper.ClassFilter)1