Java Code Examples for org.mybatis.spring.SqlSessionFactoryBean#setDataSource()
The following examples show how to use
org.mybatis.spring.SqlSessionFactoryBean#setDataSource() .
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 |
@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: MyBatisConfig.java From cc-s with MIT License | 6 votes |
@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 3
Source File: DataSourceConfiguration.java From seata-samples with Apache License 2.0 | 6 votes |
@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: DataSourceConfiguration.java From seata-samples with Apache License 2.0 | 6 votes |
@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 5
Source File: DatabaseConfig.java From SO with BSD 2-Clause "Simplified" License | 6 votes |
@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: MasterDruidDataSourceConfig.java From SpringBoot-Study with Apache License 2.0 | 6 votes |
/** * 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 7
Source File: DatabaseConfig.java From SO with BSD 2-Clause "Simplified" License | 6 votes |
@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: GjpaymentDatabaseConfig.java From maintain with MIT License | 6 votes |
@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 9
Source File: DbTestsApplication.java From java-persistence-frameworks-comparison with MIT License | 6 votes |
@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 10
Source File: DataSourceConfig.java From springboot-learning-experience with Apache License 2.0 | 5 votes |
@Bean public SqlSessionFactory sqlSessionFactory(@Qualifier("masterDataSource") DataSource myTestDbDataSource, @Qualifier("slaveDataSource") DataSource myTestDb2DataSource) throws Exception { SqlSessionFactoryBean fb = new SqlSessionFactoryBean(); fb.setDataSource(this.dataSource(myTestDbDataSource, myTestDb2DataSource)); fb.setTypeAliasesPackage(env.getProperty("mybatis.type-aliases-package")); fb.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(env.getProperty("mybatis.mapper-locations"))); return fb.getObject(); }
Example 11
Source File: MybatisConfig.java From kitty with GNU Lesser General Public License v3.0 | 5 votes |
@Bean public SqlSessionFactory sqlSessionFactory() throws Exception { SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean(); sessionFactory.setDataSource(dataSource); sessionFactory.setTypeAliasesPackage("com.louis.kitty.*.model"); // 扫描Model PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); sessionFactory.setMapperLocations(resolver.getResources("classpath*:**/sqlmap/*.xml")); // 扫描映射文件 return sessionFactory.getObject(); }
Example 12
Source File: DatabaseConfig.java From java-master with Apache License 2.0 | 5 votes |
/** * 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 13
Source File: DataSourceConfig.java From seata-samples with Apache License 2.0 | 5 votes |
@Bean public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception { SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean(); factoryBean.setDataSource(dataSource); factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver() .getResources("classpath*:/mapper/*.xml")); return factoryBean.getObject(); }
Example 14
Source File: MyBatisOAuthConfig.java From mini-platform with MIT License | 5 votes |
@Bean @Primary public SqlSessionFactory sqlSessionFactoryBeanForOAuth(@Qualifier("dataSourceOAuth") DataSource dataSource) throws Exception { SqlSessionFactoryBean factory = new SqlSessionFactoryBean(); factory.setDataSource(dataSource); factory.setTypeAliasesPackage("com.github.hiling.auth.model"); ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); factory.setMapperLocations(resolver.getResources("classpath:mapper/*.xml")); return factory.getObject(); }
Example 15
Source File: TestApplication.java From hsweb-framework with Apache License 2.0 | 5 votes |
@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 16
Source File: MasterDataSourceConfig.java From springBoot-study with Apache License 2.0 | 5 votes |
@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)); return sessionFactory.getObject(); }
Example 17
Source File: TestDbConfig.java From springboot-course with GNU General Public License v3.0 | 5 votes |
@Bean(name = "testSqlSessionFactory") public SqlSessionFactory testSqlSessionFactory(@Qualifier("testDataSource") DataSource dataSource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:conf/mybatis/mapper/test/*.xml")); return bean.getObject(); }
Example 18
Source File: SeataAutoConfig.java From demo-seata-springcloud with Apache License 2.0 | 5 votes |
/** * 初始化mybatis sqlSessionFactory * * @param dataSourceProxy * @return * @throws Exception * @author sly * @time 2019年6月11日 */ @Bean public SqlSessionFactory sqlSessionFactory(DataSourceProxy dataSourceProxy) throws Exception { SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean(); factoryBean.setDataSource(dataSourceProxy); factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml")); factoryBean.setTypeAliasesPackage("com.sly.seata.common.model"); factoryBean.setTransactionFactory(new JdbcTransactionFactory()); return factoryBean.getObject(); }
Example 19
Source File: MybatisConfiguration.java From spring-boot-demo with MIT License | 5 votes |
/** * 创建会话工厂。 * * @param dataSource 数据源 * @return 会话工厂 */ @Bean(name = "sqlSessionFactory") @SneakyThrows public SqlSessionFactory getSqlSessionFactory(@Qualifier("dataSource") DataSource dataSource) { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); return bean.getObject(); }
Example 20
Source File: SpringAnnotationTest.java From Mapper with MIT License | 4 votes |
@Bean public SqlSessionFactory sqlSessionFactory() throws Exception { SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean(); sessionFactory.setDataSource(dataSource()); return sessionFactory.getObject(); }