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

The following examples show how to use org.mybatis.spring.SqlSessionFactoryBean#setPlugins() . 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: 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 2
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 3
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 4
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 5
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 6
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 7
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 8
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 9
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 10
Source File: GjpaymentDatabaseConfig.java    From maintain with MIT License 6 votes vote down vote up
@Bean(name = "gjpaymentSqlSessionFactory")
public SqlSessionFactory gjpaymentSqlSessionFactory() throws Exception {
	SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
	logger.info("gjpayment sqlsession--" + this.gjpaymentDataSource().hashCode());
	sqlSessionFactoryBean.setDataSource(this.gjpaymentDataSource());
	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 11
Source File: MhinDatabaseConfig.java    From maintain with MIT License 6 votes vote down vote up
@Bean(name = "mhinSqlSessionFactory")
public SqlSessionFactory mhinSqlSessionFactory() throws Exception {
	SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
	logger.info("mhin sqlsession--" + this.mhinDataSource().hashCode());
	sqlSessionFactoryBean.setDataSource(this.mhinDataSource());
	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: 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 13
Source File: DataSourceConfig.java    From momo-cloud-permission with Apache License 2.0 6 votes vote down vote up
@Bean(name = "primarySqlSessionFactory")
@Primary
public SqlSessionFactory sqlSessionFactory(@Qualifier("primaryDataSource") DataSource dataSource) throws Exception {
    final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
    sessionFactory.setTypeAliasesPackage(TYPE_ALIASES_Package);
    PathMatchingResourcePatternResolver pathMatchingResourcePatternResolver = new PathMatchingResourcePatternResolver();

    String packageSearchPath = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + MAPPER_PATH;

    //添加插件
    sessionFactory.setPlugins(pageInterceptor());
    sessionFactory.setDataSource(dataSource);
    sessionFactory.setMapperLocations(pathMatchingResourcePatternResolver.getResources(packageSearchPath));
    sessionFactory.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);
    return sessionFactory.getObject();
}
 
Example 14
Source File: DataSource2Config.java    From Resource with GNU General Public License v3.0 6 votes vote down vote up
@Bean(name = "db2SqlSessionFactory")
public SqlSessionFactory setSqlSessionFactory(@Qualifier("db2DataSource") DataSource dataSource,
                                              @Qualifier("db2Configuration") org.apache.ibatis.session.Configuration configuration)
    throws Exception
{
    String url = "classpath:com/zsm/mapper/db2/*.xml";
    SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
    bean.setDataSource(dataSource);
    bean.setConfiguration(configuration);
    bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(url));
    org.apache.ibatis.session.Configuration config = new org.apache.ibatis.session.Configuration();
    config.setMapUnderscoreToCamelCase(true); // 开启驼峰命名支持
    bean.setConfiguration(config);
    //格式化sql语句打印
    PerformanceInterceptor performanceInterceptor = new PerformanceInterceptor();
    Properties properties = new Properties();
    properties.setProperty("format", "true");
    performanceInterceptor.setProperties(properties);
    bean.setPlugins(new Interceptor[] {performanceInterceptor});
    return bean.getObject();
}
 
Example 15
Source File: MysqlConfig.java    From jee-universal-bms with Apache License 2.0 5 votes vote down vote up
@Bean
  public SqlSessionFactoryBean sqlSessionFactory() throws Exception {
  	SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
  	sessionFactory.setDataSource(dataSource());

      //String[] paths = {"classpath*:com/yuzhi/bms/dao/**/*.class"};
      //String aliasesPackage = PathUtil.getMultiResources(paths);
      //sessionFactory.setTypeAliasesPackage(aliasesPackage);

Interceptor[] plugins = new Interceptor[] {pageHelper()};
sessionFactory.setPlugins(plugins);

  	return sessionFactory;
  }
 
Example 16
Source File: MybatisConfig.java    From light-reading-cloud with MIT License 5 votes vote down vote up
/** 工厂配置 */
@Bean
public SqlSessionFactory sqlSessionFactoryBean(@Qualifier("bookCenterDataSource") DataSource dataSource) throws Exception {
    // 设置数据源
    SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
    factory.setDataSource(dataSource);

    // 添加XML映射
    ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
    factory.setMapperLocations(resolver.getResources(MAPPER_LOCATIONS));

    //添加插件
    factory.setPlugins(new Interceptor[]{ this.getPageHelper() });
    return factory.getObject();
}
 
Example 17
Source File: MybatisAutoConfiguration.java    From spring-boot-mybatis-rw with Apache License 2.0 5 votes vote down vote up
@Bean
public SqlSessionFactory sqlSessionFactory(DataSourceProxy dataSource)
		throws Exception {

	SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
	Interceptor rwplugin = new RWPlugin();
	if (StringUtils.hasText(this.properties.getConfigLocation())) {
		factory.setConfigLocation(this.resourceLoader.getResource(this.properties.getConfigLocation()));
	}
	factory.setConfiguration(properties.getConfiguration());

	if (ObjectUtils.isEmpty(this.interceptors)) {
		Interceptor[] plugins = { rwplugin };
		factory.setPlugins(plugins);
	} else {
		List<Interceptor> interceptorList = new ArrayList<Interceptor>(Arrays.asList(interceptors));
		interceptorList.add(rwplugin);
		factory.setPlugins(interceptorList.toArray(new Interceptor[interceptorList.size()]));
	}
	if (this.databaseIdProvider != null) {
		factory.setDatabaseIdProvider(this.databaseIdProvider);
	}
	if (StringUtils.hasLength(this.properties.getTypeAliasesPackage())) {
		factory.setTypeAliasesPackage(this.properties.getTypeAliasesPackage());
	}
	if (StringUtils.hasLength(this.properties.getTypeHandlersPackage())) {
		factory.setTypeHandlersPackage(this.properties.getTypeHandlersPackage());
	}
	if (!ObjectUtils.isEmpty(this.properties.resolveMapperLocations())) {
		factory.setMapperLocations(this.properties.resolveMapperLocations());
	}
	factory.setDataSource(dataSource);
	return factory.getObject();
}
 
Example 18
Source File: ReadWriteDataSourceConfig.java    From EasyReport with Apache License 2.0 5 votes vote down vote up
@Bean(name = "readwriteSqlSessionFactory")
public SqlSessionFactory sqlSessionFactory(@Qualifier("readwriteDataSource") final DataSource dataSource)
    throws Exception {
    final SqlSessionFactoryBean sessionFactory = this.createSqlSessionFactoryBean(dataSource, MAPPER_LOCATION);
    sessionFactory.setPlugins(new Interceptor[] {new DynamicDataSourcePlugin()});
    return sessionFactory.getObject();
}
 
Example 19
Source File: MyBatisConfig.java    From springBoot-swagger-mybatis-shardbatis with Apache License 2.0 5 votes vote down vote up
@Bean(name = "sqlSessionFactory")
public SqlSessionFactory sqlSessionFactoryBean() {
	SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
	bean.setDataSource(dataSource);
	bean.setTypeAliasesPackage("com.zhushoumao.springboot.pojo");

	/** 分页插件begin **/
	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);
	/** 分页插件end **/

	/** 分表插件begin **/
	ShardPlugin shardPlugin = new ShardPlugin();
	Properties shardProperties = new Properties();
	shardProperties.setProperty("shardingConfig", "sharding_config.xml");
	shardPlugin.setProperties(shardProperties);
	/** 分表插件end **/
	// 添加插件
	bean.setPlugins(new Interceptor[] { shardPlugin, pageHelper });

	// 添加XML目录
	ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
	try {
		bean.setMapperLocations(resolver.getResources("classpath:com/zhushoumao/springboot/mapper/*.xml"));
		return bean.getObject();
	} catch (Exception e) {
		e.printStackTrace();
		throw new RuntimeException(e);
	}
}
 
Example 20
Source File: DatasourceConfig.java    From micro-service with MIT License 5 votes vote down vote up
@Bean
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) {
    
	try {
        final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(dataSource);
        sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*Mapper.xml"));
        sessionFactory.setPlugins(new Interceptor[] { getShardPlugin() });
        
        return sessionFactory.getObject();
    } catch (Exception e) {
    	throw new RuntimeException("sqlSessionFactory configuration error", e);
    }
}