Java Code Examples for org.mybatis.spring.SqlSessionFactoryBean

The following examples show how to use org.mybatis.spring.SqlSessionFactoryBean. These examples are extracted from open source projects. 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 Project: SuperBoot   Source File: ReadDataSourcesConfig.java    License: 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 Project: erp-framework   Source File: FramedbConfig.java    License: MIT License 6 votes vote down vote up
@Bean(name = "framedbSqlSessionFactory")
public SqlSessionFactory testSqlSessionFactory(@Qualifier("framedbDataSource") DataSource dataSource) throws Exception {
    SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
    bean.setPlugins(new Interceptor[]{ sqlInterceptor});

    bean.setDataSource(dataSource);

    PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();

    Resource[] res = resolver.getResources("classpath:mapper/**");
    for (int i =0;i< res.length;i++) {
        if(res[i].getFilename().indexOf("xml")==-1){
            res[i] = null;
        }
    }
    bean.setMapperLocations(res);

    return bean.getObject();
}
 
Example 3
/**
 * SqlSessionFactory配置
 *
 * @return
 * @throws Exception
 */
@Bean(name = "cluster1SqlSessionFactory")
public SqlSessionFactory cluster1SqlSessionFactory(
        @Qualifier("cluster1DataSource") DataSource dataSource
) throws Exception {
    SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
    sqlSessionFactoryBean.setDataSource(dataSource);

    PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
    //配置mapper文件位置
    sqlSessionFactoryBean.setMapperLocations(resolver.getResources(cluster1MapperLocations));

    //配置分页插件
    PageHelper pageHelper = new PageHelper();
    Properties properties = new Properties();
    properties.setProperty("reasonable", "true");
    properties.setProperty("supportMethodsArguments", "true");
    properties.setProperty("returnPageInfo", "check");
    properties.setProperty("params", "count=countSql");
    pageHelper.setProperties(properties);

    //设置插件
    sqlSessionFactoryBean.setPlugins(new Interceptor[]{pageHelper});
    return sqlSessionFactoryBean.getObject();
}
 
Example 4
Source Project: ly-security   Source File: MybatisConfig.java    License: Apache License 2.0 6 votes vote down vote up
@Bean
public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource) throws IOException {
    SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();

    //mybatis配置
    Properties prop = new Properties();
    prop.setProperty("mapUnderscoreToCamelCase", "true");

    sqlSessionFactoryBean.setConfigurationProperties(prop);
    sqlSessionFactoryBean.setTypeAliasesPackage("com.tc.ly.bean");

    PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
    Resource[] resources = resolver.getResources("classpath:mapper/*.xml");

    sqlSessionFactoryBean.setMapperLocations(resources);
    sqlSessionFactoryBean.setDataSource(dataSource);

    return sqlSessionFactoryBean;
}
 
Example 5
Source Project: maintain   Source File: MhoutDatabaseConfig.java    License: MIT License 6 votes vote down vote up
@Bean(name = "mhoutSqlSessionFactory")
public SqlSessionFactory mhoutSqlSessionFactory() throws Exception {
	SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
	logger.info("mhout sqlsession--" + this.mhoutDataSource().hashCode());
	sqlSessionFactoryBean.setDataSource(this.mhoutDataSource());
	PageHelper pageHelper = new PageHelper();
	Properties properties = new Properties();
	properties.setProperty("dialect", "oracle");
	properties.setProperty("pageSizeZero", "true");
	properties.setProperty("reasonable", "false");
	properties.setProperty("params", "pageNum=pageHelperStart;pageSize=pageHelperRows;");
	properties.setProperty("supportMethodsArguments", "true");
	properties.setProperty("returnPageInfo", "none");
	pageHelper.setProperties(properties);
	Interceptor[] interceptors = new Interceptor[] { pageHelper };
	sqlSessionFactoryBean.setPlugins(interceptors);
	return sqlSessionFactoryBean.getObject();
}
 
Example 6
Source Project: maintain   Source File: ParaDatabaseConfig.java    License: MIT License 6 votes vote down vote up
@Bean(name = "paraSqlSessionFactory")
public SqlSessionFactory paraSqlSessionFactory() throws Exception {
	SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
	logger.info("para sqlsession--" + this.paraDataSource().hashCode());
	sqlSessionFactoryBean.setDataSource(this.paraDataSource());
	PageHelper pageHelper = new PageHelper();
	Properties properties = new Properties();
	properties.setProperty("dialect", "oracle");
	properties.setProperty("pageSizeZero", "true");
	properties.setProperty("reasonable", "false");
	properties.setProperty("params", "pageNum=pageHelperStart;pageSize=pageHelperRows;");
	properties.setProperty("supportMethodsArguments", "true");
	properties.setProperty("returnPageInfo", "none");
	pageHelper.setProperties(properties);
	Interceptor[] interceptors = new Interceptor[] { pageHelper };
	sqlSessionFactoryBean.setPlugins(interceptors);
	return sqlSessionFactoryBean.getObject();
}
 
Example 7
Source Project: seata-samples   Source File: DataSourceConfiguration.java    License: 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 8
/**
 * SqlSessionFactory配置
 *
 * @return
 * @throws Exception
 */
@Bean(name = "read1SqlSessionFactory")
public SqlSessionFactory read1SqlSessionFactory(
        @Qualifier("read1DataSource") DataSource dataSource
) throws Exception {
    SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
    sqlSessionFactoryBean.setDataSource(dataSource);

    PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
    // 配置mapper文件位置
    sqlSessionFactoryBean.setMapperLocations(resolver.getResources(read1MapperLocations));

    //配置分页插件
    PageHelper pageHelper = new PageHelper();
    Properties properties = new Properties();
    properties.setProperty("reasonable", "true");
    properties.setProperty("supportMethodsArguments", "true");
    properties.setProperty("returnPageInfo", "check");
    properties.setProperty("params", "count=countSql");
    pageHelper.setProperties(properties);

    //设置插件
    sqlSessionFactoryBean.setPlugins(new Interceptor[]{pageHelper});
    return sqlSessionFactoryBean.getObject();
}
 
Example 9
Source Project: Shop-for-JavaWeb   Source File: Runnable.java    License: MIT License 6 votes vote down vote up
/**
 * 执行刷新
 * 
 * @param filePath
 *            刷新目录
 * @param beforeTime
 *            上次刷新时间
 * @throws NestedIOException
 *             解析异常
 * @throws FileNotFoundException
 *             文件未找到
 */
public void refresh(String filePath, Long beforeTime) throws Exception {

	// 本次刷新时间
	Long refrehTime = System.currentTimeMillis();

	List<File> refreshs = this.getRefreshFile(new File(filePath),
			beforeTime);
	if (refreshs.size() > 0) {
		log.debug("refresh files:" + refreshs.size());
	}
	for (int i = 0; i < refreshs.size(); i++) {
		System.out.println("Refresh file: "
				+ mappingPath
				+ StringUtils.substringAfterLast(refreshs.get(i)
						.getAbsolutePath(), mappingPath));
		log.debug("refresh file:" + refreshs.get(i).getAbsolutePath());
		log.debug("refresh filename:" + refreshs.get(i).getName());
		SqlSessionFactoryBean.refresh(new FileInputStream(refreshs.get(i)),
				refreshs.get(i).getAbsolutePath(), configuration);
	}
	// 如果刷新了文件,则修改刷新时间,否则不修改
	if (refreshs.size() > 0) {
		this.beforeTime = refrehTime;
	}
}
 
Example 10
Source Project: SO   Source File: DatabaseConfig.java    License: 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 11
Source Project: SO   Source File: DatabaseConfig.java    License: 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 12
/**
 * SqlSessionFactory配置
 *
 * @return
 * @throws Exception
 */
@Bean(name = "clusterSqlSessionFactory")
public SqlSessionFactory clusterSqlSessionFactory(
        @Qualifier("clusterDataSource") DataSource dataSource
) throws Exception {
    SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
    sqlSessionFactoryBean.setDataSource(dataSource);

    PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
    //配置mapper文件位置
    sqlSessionFactoryBean.setMapperLocations(resolver.getResources(clusterMapperLocations));

    //配置分页插件
    PageHelper pageHelper = new PageHelper();
    Properties properties = new Properties();
    properties.setProperty("reasonable", "true");
    properties.setProperty("supportMethodsArguments", "true");
    properties.setProperty("returnPageInfo", "check");
    properties.setProperty("params", "count=countSql");
    pageHelper.setProperties(properties);

    //设置插件
    sqlSessionFactoryBean.setPlugins(new Interceptor[]{pageHelper});
    return sqlSessionFactoryBean.getObject();
}
 
Example 13
Source Project: maintain   Source File: EcssentDatabaseConfig.java    License: MIT License 6 votes vote down vote up
@Primary
@Bean(name = "ecssentSqlSessionFactory")
public SqlSessionFactory ecssentSqlSessionFactory() throws Exception {
	SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
	logger.info("ecssent sqlsession--" + this.ecssentDataSource().hashCode());
	sqlSessionFactoryBean.setDataSource(this.ecssentDataSource());
	PageHelper pageHelper = new PageHelper();
	Properties properties = new Properties();
	properties.setProperty("dialect", "oracle");
	properties.setProperty("pageSizeZero", "true");
	properties.setProperty("reasonable", "false");
	properties.setProperty("params", "pageNum=pageHelperStart;pageSize=pageHelperRows;");
	properties.setProperty("supportMethodsArguments", "true");
	properties.setProperty("returnPageInfo", "none");
	pageHelper.setProperties(properties);
	Interceptor[] interceptors = new Interceptor[] { pageHelper };
	sqlSessionFactoryBean.setPlugins(interceptors);
	return sqlSessionFactoryBean.getObject();
}
 
Example 14
Source Project: mybatis.flying   Source File: SqlSessionFactoryConfig.java    License: 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 15
Source Project: springBoot-study   Source File: ClusterDataSourceConfig.java    License: Apache License 2.0 6 votes vote down vote up
@Bean(name = "clusterSqlSessionFactory")
public SqlSessionFactory clusterSqlSessionFactory(@Qualifier("clusterDataSource") DataSource clusterDataSource)
        throws Exception {
    final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
    sessionFactory.setDataSource(clusterDataSource);
    //分页插件
    Interceptor interceptor = new PageInterceptor();
    Properties properties = new Properties();
    //数据库
    properties.setProperty("helperDialect", "mysql");
    //是否将参数offset作为PageNum使用
    properties.setProperty("offsetAsPageNum", "true");
    //是否进行count查询
    properties.setProperty("rowBoundsWithCount", "true");
    //是否分页合理化
    properties.setProperty("reasonable", "false");
    interceptor.setProperties(properties);
    sessionFactory.setPlugins(new Interceptor[] {interceptor});
    
    
    sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(ClusterDataSourceConfig.MAPPER_LOCATION));
    return sessionFactory.getObject();
}
 
Example 16
Source Project: springBoot-study   Source File: MasterDataSourceConfig.java    License: Apache License 2.0 6 votes vote down vote up
@Bean(name = "masterSqlSessionFactory")
@Primary
public SqlSessionFactory masterSqlSessionFactory(@Qualifier("masterDataSource") DataSource masterDataSource)
        throws Exception {
    final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
    sessionFactory.setDataSource(masterDataSource);
    sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
            .getResources(MasterDataSourceConfig.MAPPER_LOCATION));
    //分页插件
    Interceptor interceptor = new PageInterceptor();
    Properties properties = new Properties();
    //数据库
    properties.setProperty("helperDialect", "mysql");
    //是否将参数offset作为PageNum使用
    properties.setProperty("offsetAsPageNum", "true");
    //是否进行count查询
    properties.setProperty("rowBoundsWithCount", "true");
    //是否分页合理化
    properties.setProperty("reasonable", "false");
    interceptor.setProperties(properties);
    sessionFactory.setPlugins(new Interceptor[] {interceptor});
    
    return sessionFactory.getObject();
}
 
Example 17
Source Project: maintain   Source File: GjentDatabaseConfig.java    License: MIT License 6 votes vote down vote up
@Bean(name = "gjentSqlSessionFactory")
public SqlSessionFactory gjentSqlSessionFactory() throws Exception {
	SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
	logger.info("gjent sqlsession--" + this.gjentDataSource().hashCode());
	sqlSessionFactoryBean.setDataSource(this.gjentDataSource());
	PageHelper pageHelper = new PageHelper();
	Properties properties = new Properties();
	properties.setProperty("dialect", "oracle");
	properties.setProperty("pageSizeZero", "true");
	properties.setProperty("reasonable", "false");
	properties.setProperty("params", "pageNum=pageHelperStart;pageSize=pageHelperRows;");
	properties.setProperty("supportMethodsArguments", "true");
	properties.setProperty("returnPageInfo", "none");
	pageHelper.setProperties(properties);
	Interceptor[] interceptors = new Interceptor[] { pageHelper };
	sqlSessionFactoryBean.setPlugins(interceptors);
	return sqlSessionFactoryBean.getObject();
}
 
Example 18
/**
 * Initializing configuration、mapperLocations etc.
 * 
 * @throws Exception
 */
private synchronized void init() throws Exception {
	state(isNull(configuration) && isNull(mapperLocations),
			String.format("Already initialized mappers hotspot loader. configuration for: %s", configuration));
	// Obtain configuration.
	configuration = sessionFactory.getObject().getConfiguration();
	// Obtain mapperLocations.
	Field mapperLocaionsField = findField(SqlSessionFactoryBean.class, "mapperLocations", Resource[].class);
	makeAccessible(mapperLocaionsField);
	mapperLocations = (Resource[]) getField(mapperLocaionsField, sessionFactory);
	// Convert to origin resources.
	mapperLocations = getOriginResources(mapperLocations);

	notNull(configuration, "SqlSessionFactory configuration can't is null.");
	notNull(mapperLocations, "SqlSessionFactory mapperLocations can't is null.");
}
 
Example 19
Source Project: xxpay-master   Source File: DruidDataSourceConfig.java    License: MIT License 6 votes vote down vote up
@Bean
public SqlSessionFactory sqlSessionFactory() throws Exception {
    SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
    sqlSessionFactoryBean.setDataSource(dataSource());
    //mybatis分页
    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); //添加插件
    sqlSessionFactoryBean.setPlugins(new Interceptor[]{pageHelper});
    PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
    sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath:org/xxpay/dal/dao/mapper/*.xml"));
    return sqlSessionFactoryBean.getObject();
}
 
Example 20
Source Project: springbootexamples   Source File: DataSourceConfig.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 多数据源的 SqlSessionFactory
 * @param dynamicDataSource
 * @return
 * @throws Exception
 */
@Bean(name = "sqlSessionFactory")
public SqlSessionFactory sqlSessionFactory(@Qualifier("dynamicDataSource") DataSource dynamicDataSource)
        throws Exception {
    SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
    bean.setDataSource(dynamicDataSource);
    //设置数据数据源的Mapper.xml路径
    bean.setMapperLocations(
            new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/*.xml"));
    //设置Mybaties查询数据自动以驼峰式命名进行设值
    org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session
            .Configuration();
    configuration.setMapUnderscoreToCamelCase(true);
    bean.setConfiguration(configuration);

    return bean.getObject();
}
 
Example 21
/**
 * SqlSessionFactory配置
 *
 * @return
 * @throws Exception
 */
@Bean(name = "masterSqlSessionFactory")
@Primary
public SqlSessionFactory masterSqlSessionFactory(
        @Qualifier("masterDataSource") DataSource dataSource
) throws Exception {
    SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
    sqlSessionFactoryBean.setDataSource(dataSource);

    PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
    // 配置mapper文件位置
    sqlSessionFactoryBean.setMapperLocations(resolver.getResources(masterMapperLocations));

    //配置分页插件
    PageHelper pageHelper = new PageHelper();
    Properties properties = new Properties();
    properties.setProperty("reasonable", "true");
    properties.setProperty("supportMethodsArguments", "true");
    properties.setProperty("returnPageInfo", "check");
    properties.setProperty("params", "count=countSql");
    pageHelper.setProperties(properties);

    //设置插件
    sqlSessionFactoryBean.setPlugins(new Interceptor[]{pageHelper});
    return sqlSessionFactoryBean.getObject();
}
 
Example 22
Source Project: flowable-engine   Source File: DatabaseConfiguration.java    License: Apache License 2.0 6 votes vote down vote up
@Bean
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) {
    SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
    sqlSessionFactoryBean.setDataSource(dataSource);

    try {
        Properties properties = new Properties();
        properties.put("prefix", env.getDataSourcePrefix());
        sqlSessionFactoryBean.setConfigurationProperties(properties);
        sqlSessionFactoryBean
                .setMapperLocations(ResourcePatternUtils.getResourcePatternResolver(resourceLoader).getResources("classpath:/META-INF/admin-mybatis-mappings/*.xml"));
        sqlSessionFactoryBean.afterPropertiesSet();
        return sqlSessionFactoryBean.getObject();
    } catch (Exception e) {
        throw new RuntimeException("Could not create sqlSessionFactory", e);
    }

}
 
Example 23
Source Project: FS-Blog   Source File: MybatisAutoConfiguration.java    License: Apache License 2.0 6 votes vote down vote up
@Bean(name = "sqlSessionFactory")
@ConditionalOnMissingBean
public SqlSessionFactory sqlSessionFactory(HikariDataSource dataSource) throws Exception {
    SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
    factory.setDataSource(dataSource);
    if (StringUtils.hasText(this.properties.getConfig())) {
        factory.setConfigLocation(
                this.resourceLoader.getResource(this.properties.getConfig()));
    } else {
        if (this.interceptors != null && this.interceptors.length > 0) {
            factory.setPlugins(this.interceptors);
        }
        factory.setTypeAliasesPackage(this.properties.getTypeAliasesPackage());
        factory.setTypeHandlersPackage(this.properties.getTypeHandlersPackage());
        factory.setMapperLocations(this.properties.getMapperLocations());
    }
    return factory.getObject();
}
 
Example 24
Source Project: mybatis.flying   Source File: SqlSessionFactory2Config.java    License: Apache License 2.0 6 votes vote down vote up
@Bean(name = "sqlSessionFactory2")
@Primary
public SqlSessionFactoryBean createSqlSessionFactory2Bean() throws IOException {
	SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
	/** 设置datasource */
	sqlSessionFactoryBean.setDataSource(dataSource2);
	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 25
Source Project: spring-boot-demo   Source File: MyBatisConfig.java    License: MIT License 5 votes vote down vote up
@Bean
public SqlSessionFactory sqlSessionFactory() throws Exception {
    SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
    sqlSessionFactoryBean.setDataSource(myRoutingDataSource);
    sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml"));
    return sqlSessionFactoryBean.getObject();
}
 
Example 26
Source Project: easyweb   Source File: DruidDataSourceConfig.java    License: Apache License 2.0 5 votes vote down vote up
@Bean(name = "masterSqlSessionFactory")
@Primary
public SqlSessionFactory sqlSessionFactory(@Qualifier("mysqlDataSource") DataSource dataSource) throws Exception {
    SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
    sessionFactoryBean.setDataSource(dataSource);
    sessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
            .getResources("classpath*:mapper/*.xml"));
    return sessionFactoryBean.getObject();
}
 
Example 27
Source Project: hsweb-framework   Source File: TestApplication.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
public SqlSessionFactory sqlSessionFactory2(@Qualifier("dataSource") DataSource dataSource) throws Exception {
    SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
    factory.setVfs(SpringBootVFS.class);
    factory.setDataSource(dataSource);
    String typeHandlers = "org.hswebframework.web.dao.mybatis.handler";
    factory.setTypeHandlersPackage(typeHandlers);
    factory.setMapperLocations(new Resource[]{new ClassPathResource("org/hswebframework/web/dao/test/TestDao.xml")});

    SqlSessionFactory sqlSessionFactory = factory.getObject();
    return sqlSessionFactory;
}
 
Example 28
Source Project: pmq   Source File: MySqlDatasourceConfiguration.java    License: Apache License 2.0 5 votes vote down vote up
@Bean(name = "mysqlSessionFactory")
@Primary
public SqlSessionFactory sqlSessionFactory(@Qualifier("mysqlDataSource") DataSource dataSource,SoaConfig soaConfig) throws Exception {
    SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
    bean.setPlugins(new Interceptor[] {new CatMybatisPlugin(soaConfig)});
    bean.setDataSource(dataSource);
    bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(MAPPER_XML_PATH));
    bean.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);
    return bean.getObject();
}
 
Example 29
Source Project: pmq   Source File: InMemoryDbDatasourceConfiguration.java    License: Apache License 2.0 5 votes vote down vote up
@Bean(name = "inMemoryDbSessionFactory")
@Primary
public SqlSessionFactory sqlSessionFactory(@Qualifier("inMemoryDbDataSource") DataSource dataSource,SoaConfig soaConfig) throws Exception {
    SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
    bean.setPlugins(new Interceptor[] {new CatMybatisPlugin(soaConfig)});
    bean.setDataSource(dataSource);
    bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(MAPPER_XML_PATH));
    bean.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);
    return bean.getObject();
}
 
Example 30
Source Project: EasyReport   Source File: AbstractDataSourceConfig.java    License: 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;
}