- Common ways to obtain MybatisSqlSessionFactoryBean
private void myMethod () {}
@Bean(name = "basisSqlSessionFactory") public SqlSessionFactory sqlSessionFactory(@Qualifier(value = "basisGlobalConfig")GlobalConfiguration globalConfig, @Qualifier(value = "basisDataSource")DruidDataSource dataSource) throws Exception { log.info("初始化SqlSessionFactory"); String mapperLocations = "classpath:db-ason/sql/**/*.xml"; String configLocation = "classpath:db-ason/mybatis/mybatis-sqlconfig.xml"; String typeAliasesPackage = "com.ason.entity.**"; MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean(); // 数据源 sqlSessionFactory.setDataSource(dataSource); // 全局配置 sqlSessionFactory.setGlobalConfig(globalConfig); Interceptor[] interceptor = {new PaginationInterceptor()}; // 分页插件 sqlSessionFactory.setPlugins(interceptor); ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); try { // 自动扫描Mapping.xml文件 sqlSessionFactory.setMapperLocations(resolver.getResources(mapperLocations)); sqlSessionFactory.setConfigLocation(resolver.getResource(configLocation)); sqlSessionFactory.setTypeAliasesPackage(typeAliasesPackage); return sqlSessionFactory.getObject(); } catch (Exception e) { e.printStackTrace(); throw e; } }
config.setSqlInjector(new AutoSqlInjector()); config.setMetaObjectHandler(new BaseMetaObjectHandler()); sessionFactory.setGlobalConfig(config);
@Bean("mybatisSqlSession") public SqlSessionFactory sqlSessionFactory(DataSource dataSource, ResourceLoader resourceLoader, GlobalConfiguration globalConfiguration) throws Exception { MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean(); sqlSessionFactory.setDataSource(dataSource); MybatisConfiguration configuration = new MybatisConfiguration(); configuration.setDefaultScriptingLanguage(MybatisXMLLanguageDriver.class); configuration.setJdbcTypeForNull(JdbcType.NULL); sqlSessionFactory.setConfiguration(configuration); sqlSessionFactory.setPlugins(new Interceptor[]{ new PaginationInterceptor(), new PerformanceInterceptor(), new OptimisticLockerInterceptor() }); sqlSessionFactory.setGlobalConfig(globalConfiguration); return sqlSessionFactory.getObject(); }
factory.setGlobalConfig(this.properties.getGlobalConfig().convertGlobalConfiguration());
factory.setGlobalConfig(this.properties.getGlobalConfig().convertGlobalConfiguration());
mybatisPlus.setGlobalConfig(globalConfig); MybatisConfiguration mc = new MybatisConfiguration(); mc.setDefaultScriptingLanguage(MybatisXMLLanguageDriver.class);
mybatisPlus.setGlobalConfig(globalConfig); MybatisConfiguration mc = new MybatisConfiguration();