Java Code Examples for org.mybatis.spring.SqlSessionFactoryBean#setConfigLocation()

The following examples show how to use org.mybatis.spring.SqlSessionFactoryBean#setConfigLocation() . 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: ReadDataSourcesConfig.java    From SuperBoot with MIT License 7 votes vote down vote up
@Bean(name = "readSqlSessionFactory")
public SqlSessionFactory readSqlSessionFactory(@Qualifier("readDruidDataSource") DataSource dataSource) throws Exception {
    final SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
    sessionFactoryBean.setDataSource(dataSource);
    //加载配置文件
    sessionFactoryBean.setConfigLocation(new PathMatchingResourcePatternResolver().getResource(CONFIG_LOCAL));
    //加载映射文件
    sessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(MAPPER_LOCAL));
    return sessionFactoryBean.getObject();
}
 
Example 2
Source File: DataSourceConfiguration.java    From seata-samples with Apache License 2.0 6 votes vote down vote up
@Bean
public SqlSessionFactoryBean sqlSessionFactoryBean(DataSourceProxy dataSourceProxy,
                                                   MybatisProperties mybatisProperties) {
    SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
    bean.setDataSource(dataSourceProxy);

    ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
    try {
        Resource[] mapperLocaltions = resolver.getResources(mybatisProperties.getMapperLocations()[0]);
        bean.setMapperLocations(mapperLocaltions);

        if (StringUtils.isNotBlank(mybatisProperties.getConfigLocation())) {
            Resource[] resources = resolver.getResources(mybatisProperties.getConfigLocation());
            bean.setConfigLocation(resources[0]);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    return bean;
}
 
Example 3
Source File: DataSourceConfiguration.java    From seata-samples with Apache License 2.0 6 votes vote down vote up
@Bean
public SqlSessionFactoryBean sqlSessionFactoryBean(DataSourceProxy dataSourceProxy,
                                                   MybatisProperties mybatisProperties) {
    SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
    bean.setDataSource(dataSourceProxy);

    ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
    try {
        Resource[] mapperLocaltions = resolver.getResources(mybatisProperties.getMapperLocations()[0]);
        bean.setMapperLocations(mapperLocaltions);

        if (StringUtils.isNotBlank(mybatisProperties.getConfigLocation())) {
            Resource[] resources = resolver.getResources(mybatisProperties.getConfigLocation());
            bean.setConfigLocation(resources[0]);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return bean;
}
 
Example 4
Source File: DatabaseConfig.java    From SO with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Bean(name = "sqlSessionFactory")
    @Primary
    public SqlSessionFactory SqlSessionFactory(@Qualifier("datasource") DataSource dataSource
            , ApplicationContext applicationContext) throws Exception {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();

        // accur Could not resolve type alias in running jar
        sqlSessionFactoryBean.setVfs(SpringBootVFS.class);

        sqlSessionFactoryBean.setDataSource(dataSource);
        sqlSessionFactoryBean.setMapperLocations(
                applicationContext.getResources("classpath:META-INF/mappers/*.xml")
        );
        // configuration 은 xml 파일로 처리
        sqlSessionFactoryBean.setConfigLocation(
                applicationContext.getResource("classpath:META-INF/mybatis-config.xml")
        );
//        sqlSessionFactoryBean.setConfigurationProperties(mybatisProperties());
        sqlSessionFactoryBean.setTypeAliasesPackage("com.pineone.icbms.so.interfaces.database.model");

        return sqlSessionFactoryBean.getObject();
    }
 
Example 5
Source File: DbTestsApplication.java    From java-persistence-frameworks-comparison with MIT License 6 votes vote down vote up
@Bean
@Qualifier("batch-operations")
@SuppressWarnings("SpringJavaAutowiringInspection")
public SqlSession myBatisBatchOperationsSession(DataSource dataSource) throws Exception {
    /*
        NOTE: Unfortunately, in MyBatis it's not possible to execute batch and non-batch operations in single SqlSession.
        To support this scenario, we have to create completely new SqlSessionFactoryBean and completely new
        SqlSession. Surprisingly, this does not necessarily mean that the batch and non-batch operations will be
        executed in different transactions (as we would expect) - we tested this configuration using scenario 8.
        and it turned out that the bot non-batch and batch operations were run using same connection and in same transaction.
        I guess this has something to do with how connection is obtained by MyBatis from dataSource...
    */

    SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
    sqlSessionFactoryBean.setDataSource(dataSource);
    sqlSessionFactoryBean.setConfigLocation(new ClassPathResource("mybatis/mybatis-config.xml"));

    return new SqlSessionTemplate(sqlSessionFactoryBean.getObject(), ExecutorType.BATCH);
}
 
Example 6
Source File: DataSourceAutoConfiguration.java    From super-cloudops with Apache License 2.0 6 votes vote down vote up
@Bean
public SqlSessionFactoryBean SqlSessionFactoryBean(DataSource dataSource) throws Exception {
	// Define path matcher resolver.
	PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();

	// SqlSessionFactory
	SqlSessionFactoryBean factory = new MultipleSqlSessionFactoryBean();
	factory.setDataSource(dataSource);
	factory.setTypeAliases(getTypeAliases(resolver));
	factory.setConfigLocation(new ClassPathResource(configLocation));

	// Page.
	PageHelper pageHelper = new PageHelper();
	Properties props = new Properties();
	props.setProperty("dialect", "mysql");
	props.setProperty("reasonable", "true");
	props.setProperty("supportMethodsArguments", "true");
	props.setProperty("returnPageInfo", "check");
	props.setProperty("params", "count=countSql");
	pageHelper.setProperties(props); // 添加插件
	factory.setPlugins(new Interceptor[] { pageHelper });

	factory.setMapperLocations(resolver.getResources(mapperLocations));
	return factory;
}
 
Example 7
Source File: DatabaseConfig.java    From SO with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Bean(name = "sqlSessionFactory")
    @Primary
    public SqlSessionFactory SqlSessionFactory(@Qualifier("datasource") DataSource dataSource
            , ApplicationContext applicationContext) throws Exception {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();

        // accur Could not resolve type alias in running jar
        sqlSessionFactoryBean.setVfs(SpringBootVFS.class);

        sqlSessionFactoryBean.setDataSource(dataSource);
        sqlSessionFactoryBean.setMapperLocations(
                applicationContext.getResources("classpath:META-INF/mappers/*.xml")
        );
        // configuration 은 xml 파일로 처리
        sqlSessionFactoryBean.setConfigLocation(
                applicationContext.getResource("classpath:META-INF/mybatis-config.xml")
        );
//        sqlSessionFactoryBean.setConfigurationProperties(mybatisProperties());
        sqlSessionFactoryBean.setTypeAliasesPackage("com.pineone.icbms.so.interfaces.database.model");

        return sqlSessionFactoryBean.getObject();
    }
 
Example 8
Source File: MyBatisConfig.java    From cc-s with MIT License 6 votes vote down vote up
@Bean
public SqlSessionFactoryBean createSqlSessionFactoryBean() throws IOException {
    SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
    sqlSessionFactoryBean.setConfigLocation(new ClassPathResource(env.getProperty("mybatis.config-location")));
    PathMatchingResourcePatternResolver pathMatchingResourcePatternResolver = new PathMatchingResourcePatternResolver();
    String packageSearchPath = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + env.getProperty("mybatis.mapper-locations");
    sqlSessionFactoryBean.setMapperLocations(pathMatchingResourcePatternResolver.getResources(packageSearchPath));
    sqlSessionFactoryBean.setDataSource(dataSource());

    PageHelper pageHelper = new PageHelper();
    Properties properties = new Properties();
    properties.setProperty("reasonable", env.getProperty("pageHelper.reasonable"));
    properties.setProperty("supportMethodsArguments", env.getProperty("pageHelper.supportMethodsArguments"));
    properties.setProperty("returnPageInfo", env.getProperty("pageHelper.returnPageInfo"));
    properties.setProperty("params", env.getProperty("pageHelper.params"));
    pageHelper.setProperties(properties);
    sqlSessionFactoryBean.setPlugins(new Interceptor[]{pageHelper});

    return sqlSessionFactoryBean;
}
 
Example 9
Source File: SqlSessionFactoryConfig.java    From mybatis.flying with Apache License 2.0 6 votes vote down vote up
@Bean(name = "sqlSessionFactory")
@Primary
public SqlSessionFactoryBean createSqlSessionFactoryBean() throws IOException {
	SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
	/** 设置datasource */
	sqlSessionFactoryBean.setDataSource(dataSource1);
	VFS.addImplClass(SpringBootVFS.class);
	/** 设置mybatis configuration 扫描路径 */
	sqlSessionFactoryBean.setConfigLocation(new ClassPathResource("Configuration.xml"));
	/** 设置typeAlias 包扫描路径 */
	sqlSessionFactoryBean.setTypeAliasesPackage("indi.mybatis.flying");
	/** 添加mapper 扫描路径 */
	PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
	Resource[] ra1 = resolver.getResources("classpath*:indi/mybatis/flying/mapper*/*.xml");
	sqlSessionFactoryBean.setMapperLocations(ra1); // 扫描映射文件
	return sqlSessionFactoryBean;
}
 
Example 10
Source File: AbstractDataSourceConfig.java    From EasyReport with Apache License 2.0 5 votes vote down vote up
protected SqlSessionFactoryBean createSqlSessionFactoryBean(final DataSource dataSource,
                                                            final String mapperLocation) throws Exception {
    final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
    sessionFactory.setDataSource(dataSource);
    sessionFactory.setConfigLocation(new PathMatchingResourcePatternResolver().getResource(CONFIG_LOCATION));
    sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(mapperLocation));
    return sessionFactory;
}
 
Example 11
Source File: MybatisConfig.java    From genericdao with Artistic License 2.0 5 votes vote down vote up
@Bean
public SqlSessionFactoryBean sessionFactoryBean(DataSource dataSource , Environment environment) throws IOException{
	SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean() ;
	sqlSessionFactoryBean.setDataSource(dataSource);
	//别名包
	sqlSessionFactoryBean.setTypeAliasesPackage(environment.getRequiredProperty(PROPERTY_MYBATIS_ALIAS_PACKAGE));
	
	PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver() ;
	//获取mybatis映射文件路径,classpath:mybatis/mybatis-config.xml
	if(environment.containsProperty(PROPERTY_MYBATIS_CONFIG)){
		sqlSessionFactoryBean.setConfigLocation(resolver.getResource(environment.getRequiredProperty(PROPERTY_MYBATIS_CONFIG)));
	}
	//获取mybatis映射文件路径,例:classpath*:com/framework/sample/mapper/*Mapper.xml
	sqlSessionFactoryBean.setMapperLocations(resolver.getResources(environment.getRequiredProperty(PROPERTY_MYBATIS_MAPPER_LOCATION)));

	//开启后将在启动时检查设定的parameterMap,resultMap是否存在,是否合法
	sqlSessionFactoryBean.setFailFast(true) ;

	//存储mybatis的插件
	List<Interceptor> interceptorList = new ArrayList<Interceptor>() ;
	//初始化mybatis的插件
	initPlugins(interceptorList , environment) ;
	//设置mybatis插件
	sqlSessionFactoryBean.setPlugins(interceptorList.toArray(new Interceptor[]{}));

	//创建自定义的的Configuration
	Configuration configuration = new Configuration() ;
	//设置ObjectWrapperFactory,在其中映射mybatis查询结果和jpa实体
	configuration.setObjectWrapperFactory(new JPAObjectWrapperFactory());
	sqlSessionFactoryBean.setConfiguration(configuration);

	return sqlSessionFactoryBean ;
}
 
Example 12
Source File: MapperAutoConfiguration.java    From Mapper with MIT License 5 votes vote down vote up
@Bean
@ConditionalOnMissingBean
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
    SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
    factory.setDataSource(dataSource);
    factory.setVfs(SpringBootVFS.class);
    if (StringUtils.hasText(this.properties.getConfigLocation())) {
        factory.setConfigLocation(this.resourceLoader.getResource(this.properties.getConfigLocation()));
    }
    applyConfiguration(factory);
    if (this.properties.getConfigurationProperties() != null) {
        factory.setConfigurationProperties(this.properties.getConfigurationProperties());
    }
    if (!ObjectUtils.isEmpty(this.interceptors)) {
        factory.setPlugins(this.interceptors);
    }
    if (this.databaseIdProvider != null) {
        factory.setDatabaseIdProvider(this.databaseIdProvider);
    }
    if (StringUtils.hasLength(this.properties.getTypeAliasesPackage())) {
        factory.setTypeAliasesPackage(this.properties.getTypeAliasesPackage());
    }
    if (this.properties.getTypeAliasesSuperType() != null) {
        factory.setTypeAliasesSuperType(this.properties.getTypeAliasesSuperType());
    }
    if (StringUtils.hasLength(this.properties.getTypeHandlersPackage())) {
        factory.setTypeHandlersPackage(this.properties.getTypeHandlersPackage());
    }
    if (!ObjectUtils.isEmpty(this.properties.resolveMapperLocations())) {
        factory.setMapperLocations(this.properties.resolveMapperLocations());
    }

    return factory.getObject();
}
 
Example 13
Source File: AbstractDataSourceConfig.java    From EasyReport with Apache License 2.0 5 votes vote down vote up
protected SqlSessionFactoryBean createSqlSessionFactoryBean(final DataSource dataSource,
                                                            final String mapperLocation) throws Exception {
    final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
    sessionFactory.setDataSource(dataSource);
    sessionFactory.setConfigLocation(new PathMatchingResourcePatternResolver().getResource(CONFIG_LOCATION));
    sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(mapperLocation));
    return sessionFactory;
}
 
Example 14
Source File: MybatisDataSourceConfig.java    From molicode with Apache License 2.0 5 votes vote down vote up
@Bean("sqlSessionFactory")
public SqlSessionFactory sqlSessionFactory(DataSource dataSource)
        throws Exception {
    final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
    sessionFactory.setDataSource(dataSource);
    //sessionFactory.setTypeAliasesPackage("mybatis.model");
    sessionFactory.setConfigLocation(new PathMatchingResourcePatternResolver().getResource("classpath:mybatis-config.xml"));
    sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml"));
    return sessionFactory.getObject();
}
 
Example 15
Source File: WriteOrReadDatabaseConfig.java    From demo-project with MIT License 5 votes vote down vote up
/**
 * 多数据源需要自己设置sqlSessionFactory
 */
@Bean
public SqlSessionFactory sqlSessionFactory() throws Exception {
    SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
    bean.setDataSource(routingDataSource());
    ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
    // 实体类对应的位置
    bean.setTypeAliasesPackage(typeAliasesPackage);
    // mybatis的XML的配置
    bean.setMapperLocations(resolver.getResources(mapperLocation));
    bean.setConfigLocation(resolver.getResource(configLocation));
    return bean.getObject();
}
 
Example 16
Source File: RootConfig.java    From PedestrianDetectionSystem with Apache License 2.0 5 votes vote down vote up
@Bean(name = "sqlSessionFactory")
public SqlSessionFactoryBean initSqlSessionFactory(){
    SqlSessionFactoryBean sqlSessionFactory = new SqlSessionFactoryBean();
    sqlSessionFactory.setDataSource(initDataSource());
    Resource resource = new ClassPathResource("mybatis-config.xml");
    sqlSessionFactory.setConfigLocation(resource);
    return sqlSessionFactory;
}
 
Example 17
Source File: MyBatisConfig.java    From java-tutorial with MIT License 5 votes vote down vote up
@Bean
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
    SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
    sqlSessionFactoryBean.setDataSource(dataSource);

    ///sqlSessionFactoryBean.setConfigLocation(new ClassPathResource("mybatis-config.xml"));
    //加载全局mybatis的配置文件
    sqlSessionFactoryBean.setConfigLocation(new DefaultResourceLoader()
            .getResource("classpath:mybatis/mybatis-config.xml"));
    // 扫描mapper配置文件
    Resource[] mapperResources = new PathMatchingResourcePatternResolver()
            .getResources("classpath*:/mybatis/mapper/*.xml");

    sqlSessionFactoryBean.setMapperLocations(mapperResources);
    sqlSessionFactoryBean.setTypeAliasesPackage("classpath*:/com/softmax/marvel/entity/*");

    ResolverUtil<Class<?>> resolverUtil = new ResolverUtil<>();
    resolverUtil.find(new ResolverUtil.IsA(BaseEnum.class), "com.softmax.marvel.entity");

    Set<Class<? extends Class<?>>> handlerSet = resolverUtil.getClasses();
    for (Class<?> clazz : handlerSet) {
        if (BaseEnum.class.isAssignableFrom(clazz) && !BaseEnum.class.equals(clazz)) {
            sqlSessionFactoryBean.getObject()
                    .getConfiguration()
                    .getTypeHandlerRegistry()
                    .register(clazz, EnumValueTypeHandler.class);
        }
    }

    return sqlSessionFactoryBean.getObject();

}
 
Example 18
Source File: PhoenixDataSourceConfig.java    From BigData-In-Practice with Apache License 2.0 5 votes vote down vote up
@Bean(name = "PhoenixSqlSessionFactory")
@Primary
public SqlSessionFactory phoenixSqlSessionFactory(
        @Qualifier("PhoenixDataSource") DataSource dataSource) throws Exception {
    SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
    factoryBean.setDataSource(dataSource);
    ResourceLoader loader = new DefaultResourceLoader();
    String resource = "classpath:mybatis-config.xml";
    factoryBean.setConfigLocation(loader.getResource(resource));
    factoryBean.setSqlSessionFactoryBuilder(new SqlSessionFactoryBuilder());
    return factoryBean.getObject();
}
 
Example 19
Source File: DatabaseConfig.java    From java-master with Apache License 2.0 5 votes vote down vote up
/**
 * mybatis配置的核心bean
 */
@Bean
public SqlSessionFactory mysqlSqlSessionFactory(DataSource dataSource)
        throws Exception {
    SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
    sqlSessionFactoryBean.setDataSource(dataSource);
    final String mapperLocation = "classpath:mapper/**/*.xml";
    sqlSessionFactoryBean.setMapperLocations(resolver.getResources(mapperLocation));
    String configLocation = "classpath:mybatis-config.xml";
    sqlSessionFactoryBean.setConfigLocation(resolver.getResources(configLocation)[0]);
    return sqlSessionFactoryBean.getObject();
}
 
Example 20
Source File: AbstractDataSourceConfig.java    From EasyReport with Apache License 2.0 5 votes vote down vote up
protected SqlSessionFactoryBean createSqlSessionFactoryBean(final DataSource dataSource,
                                                            final String mapperLocation) throws Exception {
    final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
    sessionFactory.setDataSource(dataSource);
    sessionFactory.setConfigLocation(new PathMatchingResourcePatternResolver().getResource(CONFIG_LOCATION));
    sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(mapperLocation));
    return sessionFactory;
}