@Bean @Primary public MybatisSqlSessionFactoryBean mybatisSqlSessionFactoryBean ( DynamicMultipleDataSource dynamicMultipleDataSource ) { MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean(); sqlSessionFactoryBean.setDataSource( dynamicMultipleDataSource ); return sqlSessionFactoryBean; }
@Bean public MybatisMapperRefresh mybatisMapperRefresh(@Qualifier("sqlSessionFactory") MybatisSqlSessionFactoryBean sqlSessionFactory) throws Exception{ PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); return new MybatisMapperRefresh(resolver.getResources("classpath:/mappings/**/*.xml"),sqlSessionFactory.getObject(),true); }
@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(); }
@Bean @ConditionalOnMissingBean public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception { MybatisSqlSessionFactoryBean factory = new MybatisSqlSessionFactoryBean(); factory.setDataSource(dataSource); factory.setVfs(SpringBootVFS.class); if (StringUtils.hasText(this.properties.getConfigLocation())) { factory.setConfigLocation(this.resourceLoader.getResource(this.properties.getConfigLocation())); MybatisConfiguration configuration = this.properties.getConfiguration(); if (configuration == null && !StringUtils.hasText(this.properties.getConfigLocation())) { configuration = new MybatisConfiguration(); customizer.customize(configuration); factory.setConfiguration(configuration); if (this.properties.getConfigurationProperties() != null) { factory.setConfigurationProperties(this.properties.getConfigurationProperties()); factory.setPlugins(this.interceptors); factory.setDatabaseIdProvider(this.databaseIdProvider); if (StringUtils.hasLength(this.properties.getTypeAliasesPackage())) { factory.setTypeAliasesPackage(this.properties.getTypeAliasesPackage()); if (StringUtils.hasLength(this.properties.getTypeEnumsPackage())) { factory.setTypeEnumsPackage(this.properties.getTypeEnumsPackage()); if (StringUtils.hasLength(this.properties.getTypeHandlersPackage())) {
@ConditionalOnMissingBean public MybatisSqlSessionFactoryBean mybatisSqlSessionFactoryBean() { MybatisSqlSessionFactoryBean mybatisPlus = new MybatisSqlSessionFactoryBean(); mybatisPlus.setDataSource(dataSource); mybatisPlus.setVfs(SpringBootVFS.class); if (StringUtils.hasText(this.properties.getConfigLocation())) { mybatisPlus.setConfigLocation(this.resourceLoader.getResource(this.properties.getConfigLocation())); mybatisPlus.setConfiguration(properties.getConfiguration()); if (!ObjectUtils.isEmpty(this.interceptors)) { mybatisPlus.setPlugins(this.interceptors); mybatisPlus.setGlobalConfig(globalConfig); MybatisConfiguration mc = new MybatisConfiguration(); mc.setDefaultScriptingLanguage(MybatisXMLLanguageDriver.class); mybatisPlus.setConfiguration(mc); if (this.databaseIdProvider != null) { mybatisPlus.setDatabaseIdProvider(this.databaseIdProvider); mybatisPlus.setTypeAliasesPackage(this.properties.getTypeAliasesPackage()); mybatisPlus.setTypeHandlersPackage(this.properties.getTypeHandlersPackage()); mybatisPlus.setMapperLocations(this.properties.resolveMapperLocations());
@Bean @ConditionalOnMissingBean public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception { MybatisSqlSessionFactoryBean factory = new MybatisSqlSessionFactoryBean(); try { factory.setDataSource(dataSource); } catch (Exception e) { logger.error("DataSource:{}", e); factory.setVfs(SpringBootVFS.class); if (StringUtils.hasText(this.properties.getConfigLocation())) { factory.setConfigLocation(this.resourceLoader.getResource(this.properties.getConfigLocation())); factory.setConfiguration(configuration); if (this.properties.getConfigurationProperties() != null) { factory.setConfigurationProperties(this.properties.getConfigurationProperties()); factory.setPlugins(this.interceptors); factory.setDatabaseIdProvider(this.databaseIdProvider); factory.setTypeAliasesPackage(this.properties.getTypeAliasesPackage()); factory.setTypeEnumsPackage(this.properties.getTypeEnumsPackage()); factory.setTypeHandlersPackage(this.properties.getTypeHandlersPackage()); factory.setMapperLocations(this.properties.resolveMapperLocations()); factory.setGlobalConfig(this.properties.getGlobalConfig().convertGlobalConfiguration());
@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; } }
MybatisSqlSessionFactoryBean sessionFactory = new MybatisSqlSessionFactoryBean(); sessionFactory.setDataSource(dataSource); sessionFactory.setMapperLocations(resolver.getResources("classpath:/mappings/**/*.xml")); sessionFactory.setTypeAliasesPackage("cn.jeeweb.modules.*.entity"); sessionFactory.setConfigurationProperties(configurationProperties); config.setSqlInjector(new AutoSqlInjector()); config.setMetaObjectHandler(new BaseMetaObjectHandler()); sessionFactory.setGlobalConfig(config); sessionFactory.setConfigLocation(resolver.getResource("classpath:mybatis-config.xml")); return sessionFactory;
MybatisSqlSessionFactoryBean mybatisPlus = new MybatisSqlSessionFactoryBean(); mybatisPlus.setDataSource(dataSource); mybatisPlus.setVfs(SpringBootVFS.class); PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); mybatisPlus.setMapperLocations(resolver.getResources("classpath:mapper/*.xml")); if (StringUtils.hasText(this.properties.getConfigLocation())) { mybatisPlus.setConfigLocation(this.resourceLoader.getResource(this.properties.getConfigLocation())); mybatisPlus.setConfiguration(properties.getConfiguration()); if (!ObjectUtils.isEmpty(this.interceptors)) { mybatisPlus.setPlugins(this.interceptors); mybatisPlus.setGlobalConfig(globalConfig); MybatisConfiguration mc = new MybatisConfiguration(); mybatisPlus.setConfiguration(mc); if (this.databaseIdProvider != null) { mybatisPlus.setDatabaseIdProvider(this.databaseIdProvider); mybatisPlus.setTypeAliasesPackage(this.properties.getTypeAliasesPackage()); mybatisPlus.setTypeHandlersPackage(this.properties.getTypeHandlersPackage()); mybatisPlus.setMapperLocations(this.properties.resolveMapperLocations());