org.mybatis.spring.SqlSessionFactoryBean Java Examples

The following examples show how to use org.mybatis.spring.SqlSessionFactoryBean. 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: ClusterDruidDataSourceConfig.java    From SpringBoot-Study with Apache License 2.0 6 votes vote down vote up
/**
 * 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 #3
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 #4
Source File: DruidDataSourceConfig.java    From xxpay-master with 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 #5
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 #6
Source File: Runnable.java    From Shop-for-JavaWeb with 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 #7
Source File: Read1DruidDataSourceConfig.java    From SpringBoot-Study with Apache License 2.0 6 votes vote down vote up
/**
 * 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 #8
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 #9
Source File: DataSourceConfig.java    From springbootexamples with 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 #10
Source File: SqlSessionMapperHotspotLoader.java    From super-cloudops with Apache License 2.0 6 votes vote down vote up
/**
 * 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 #11
Source File: ParaDatabaseConfig.java    From maintain with 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 #12
Source File: MasterDruidDataSourceConfig.java    From SpringBoot-Study with Apache License 2.0 6 votes vote down vote up
/**
 * 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 #13
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 #14
Source File: MhoutDatabaseConfig.java    From maintain with 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 #15
Source File: SqlSessionFactory2Config.java    From mybatis.flying with 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 #16
Source File: GjentDatabaseConfig.java    From maintain with 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 #17
Source File: MybatisAutoConfiguration.java    From FS-Blog with 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 #18
Source File: DatabaseConfiguration.java    From flowable-engine with 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 #19
Source File: EcssentDatabaseConfig.java    From maintain with 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 #20
Source File: ClusterDataSourceConfig.java    From springBoot-study with 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 #21
Source File: Cluster1DruidDataSourceConfig.java    From SpringBoot-Study with Apache License 2.0 6 votes vote down vote up
/**
 * 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 #22
Source File: MybatisConfig.java    From ly-security with 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 #23
Source File: FramedbConfig.java    From erp-framework with 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 #24
Source File: MybatisConfigurer.java    From mySpringBoot with Apache License 2.0 5 votes vote down vote up
@Bean
public SqlSessionFactory sqlSessionFactoryBean(DataSource dataSource) throws Exception {
	SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
	factory.setDataSource(dataSource);
	factory.setTypeAliasesPackage(ProjectConstant.MODEL_PACKAGE);
	// 添加XML目录
	ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
	factory.setMapperLocations(resolver.getResources("classpath:mapper/*.xml"));
	return factory.getObject();
}
 
Example #25
Source File: ConfigDataSourceConfig.java    From c2mon with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Bean
public static SqlSessionFactoryBean configSqlSessionFactory(DataSource configurationDataSource,
                                                            VendorDatabaseIdProvider databaseIdProvider) throws Exception {
  SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
  sessionFactory.setDataSource(configurationDataSource);
  sessionFactory.setTypeHandlersPackage("cern.c2mon.server.configuration.mybatis");
  sessionFactory.setDatabaseIdProvider(databaseIdProvider);
  return sessionFactory;
}
 
Example #26
Source File: DataConfig.java    From playground with Apache License 2.0 5 votes vote down vote up
@Bean
public SqlSessionFactoryBean sqlSessionFactory() throws Exception {
	SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
	sessionFactory.setDataSource(dataSource());
	sessionFactory.setTypeAliasesPackage("org.lanyonm.playground.domain");
	return sessionFactory;
}
 
Example #27
Source File: DatasourceConfig.java    From libevent with Apache License 2.0 5 votes vote down vote up
@Bean
@Primary
public SqlSessionFactory sqlSessionFactory(final DataSource dataSource) throws Exception {
    SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
    bean.setDataSource(dataSource);
    bean.setMapperLocations(new PathMatchingResourcePatternResolver()
            .getResources("classpath*:sqlMap/**/*.xml"));
    return bean.getObject();
}
 
Example #28
Source File: MasterDataSourceConfig.java    From springBoot-study with Apache License 2.0 5 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);
    return sessionFactory.getObject();
}
 
Example #29
Source File: MyBatisConfig.java    From springboot-multiple-dataSources with Apache License 2.0 5 votes vote down vote up
/**
 * 根据数据源创建SqlSessionFactory
 */
@Bean
public SqlSessionFactory sqlSessionFactory(DynamicDataSource ds) throws Exception {
    PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();

    SqlSessionFactoryBean fb = new SqlSessionFactoryBean();
    fb.setDataSource(ds);// 指定数据源(这个必须有,否则报错)
    // 下边两句仅仅用于*.xml文件,如果整个持久层操作不需要使用到xml文件的话(只用注解就可以搞定),则不加
    fb.setTypeAliasesPackage("com.huan.model");// 指定基包
    fb.setMapperLocations(resolver.getResources("classpath:mapper/**/*.xml"));//

    return fb.getObject();
}
 
Example #30
Source File: CacheDataSourceConfig.java    From c2mon with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Bean
public static SqlSessionFactoryBean cacheSqlSessionFactory(DataSource cacheDataSource) {
  SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
  sessionFactory.setDataSource(cacheDataSource);
  sessionFactory.setDatabaseIdProvider(databaseIdProvider());
  sessionFactory.setTypeHandlersPackage("cern.c2mon.server.cache.dbaccess.type");
  return sessionFactory;
}