本文整理汇总了Java中org.mybatis.spring.SqlSessionFactoryBean.setDataSource方法的典型用法代码示例。如果您正苦于以下问题:Java SqlSessionFactoryBean.setDataSource方法的具体用法?Java SqlSessionFactoryBean.setDataSource怎么用?Java SqlSessionFactoryBean.setDataSource使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.mybatis.spring.SqlSessionFactoryBean
的用法示例。
在下文中一共展示了SqlSessionFactoryBean.setDataSource方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: businessSqlSessionFactory
import org.mybatis.spring.SqlSessionFactoryBean; //导入方法依赖的package包/类
@Bean
@Primary
public SqlSessionFactory businessSqlSessionFactory(@Qualifier("businessDataSource") DruidDataSource businessDataSource) throws Exception {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(businessDataSource);
//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(MAPPERXML_LOCATION));
return sqlSessionFactoryBean.getObject();
}
示例2: sqlSessionFactory
import org.mybatis.spring.SqlSessionFactoryBean; //导入方法依赖的package包/类
@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;
}
示例3: SqlSessionFactory
import org.mybatis.spring.SqlSessionFactoryBean; //导入方法依赖的package包/类
@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")
);
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();
}
示例4: sqlSessionFactoryBean
import org.mybatis.spring.SqlSessionFactoryBean; //导入方法依赖的package包/类
@Bean
public SqlSessionFactoryBean sqlSessionFactoryBean(
DataSource dataSource,
ApplicationContext applicationContext) throws IOException {
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
// 마이바티스가 사용한 DataSource를 등록
factoryBean.setDataSource(dataSource);
// 마이바티스 설정파일 위치 설정
factoryBean.setConfigLocation(applicationContext.getResource("classpath:mybatis-config.xml"));
factoryBean.setMapperLocations(applicationContext.getResources("classpath:net/andromedarabbit/persistence/mybatis/**/*.xml"));
factoryBean.setPlugins(new Interceptor[]{
new PaginationInterceptor(),
new PaginationResultSetHandlerInterceptor()
});
return factoryBean;
}
示例5: sqlSessionFactoryBean
import org.mybatis.spring.SqlSessionFactoryBean; //导入方法依赖的package包/类
@Bean
public static SqlSessionFactory sqlSessionFactoryBean(DataSource dataSource) throws Exception {
SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
factory.setDataSource(dataSource);
factory.setTypeAliasesPackage(MODEL_PACKAGE);
//配置分页插件,详情请查阅官方文档
PageHelper pageHelper = new PageHelper();
Properties properties = new Properties();
properties.setProperty("pageSizeZero", "true");//分页尺寸为0时查询所有纪录不再执行分页
properties.setProperty("reasonable", "true");//页码<=0 查询第一页,页码>=总页数查询最后一页
properties.setProperty("supportMethodsArguments", "true");//支持通过 Mapper 接口参数来传递分页参数
pageHelper.setProperties(properties);
//添加插件
factory.setPlugins(new Interceptor[]{pageHelper});
//添加XML目录
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
factory.setMapperLocations(resolver.getResources("classpath:mapper/*.xml"));
return factory.getObject();
}
示例6: clusterSqlSessionFactory
import org.mybatis.spring.SqlSessionFactoryBean; //导入方法依赖的package包/类
/**
* 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();
}
示例7: sqlSessionFactory
import org.mybatis.spring.SqlSessionFactoryBean; //导入方法依赖的package包/类
@Bean("sqlSessionFactory")
public SqlSessionFactory sqlSessionFactory() throws Exception {
SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(dataSource());
sessionFactory.setConfiguration(configuration());
sessionFactory.setTypeAliasesPackage(entityBasePackage);
sessionFactory.setTypeAliasesSuperType(AbstractEntity.class);
sessionFactory.setMapperLocations(getResources(mapperResources));
OffsetLimitInterceptor offserInterceptor = new OffsetLimitInterceptor();
offserInterceptor.setDialect(new MySQLDialect());
MapperInterceptor mapperInterceptor = new MapperInterceptor();
Properties properties = new Properties();
properties.setProperty("mappers", mappers);
properties.setProperty("IDENTITY", dialect);
mapperInterceptor.setProperties(properties);
sessionFactory.setPlugins(new Interceptor[] { offserInterceptor ,mapperInterceptor});
return sessionFactory.getObject();
}
示例8: businessSqlSessionFactory
import org.mybatis.spring.SqlSessionFactoryBean; //导入方法依赖的package包/类
@Bean
@Primary
public SqlSessionFactory businessSqlSessionFactory(@Qualifier("businessDataSource") DruidDataSource businessDataSource) throws Exception {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(businessDataSource);
//mybatis分页
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 pageHelper = new PageHelper();
pageHelper.setProperties(props);
//添加插件
sqlSessionFactoryBean.setPlugins(new Interceptor[]{pageHelper});
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
sqlSessionFactoryBean.setMapperLocations(resolver.getResources(MAPPERXML_LOCATION));
return sqlSessionFactoryBean.getObject();
}
示例9: sqlSessionFactory
import org.mybatis.spring.SqlSessionFactoryBean; //导入方法依赖的package包/类
@Bean(name="sqlSessionFactory")
@ConditionalOnMissingBean
public SqlSessionFactory sqlSessionFactory() {
try {
SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(dataSource);
sessionFactory.setTypeAliasesPackage(typeAliasesPackage);
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
sessionFactory.setMapperLocations(resolver.getResources(mapperLocations));
PageInterceptor pageInterceptor = new MysqlPageInterceptor();
sessionFactory.setPlugins(new Interceptor[]{pageInterceptor});
return sessionFactory.getObject();
} catch (Exception e) {
logger.warn("Could not confiure mybatis session factory", e);
return null;
}
}
示例10: myBatisBatchOperationsSession
import org.mybatis.spring.SqlSessionFactoryBean; //导入方法依赖的package包/类
@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);
}
示例11: sqlSessionFactory
import org.mybatis.spring.SqlSessionFactoryBean; //导入方法依赖的package包/类
@Bean
public SqlSessionFactory sqlSessionFactory(DataSource ds) throws Exception {
/*
* 配置SqlSessionFactory的Bean
*/
SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(ds);
SqlSessionFactory factory = sessionFactory.getObject();
/*
* 注册Mapper。
* 如果不注册Mapper,则会在Spring-Boot启动时报
* "Type interface XXXMapper is not known to the MapperRegistry"
*/
factory.getConfiguration().addMapper(PgDatabaseMapper.class);
return factory;
}
示例12: sqlSessionFactory
import org.mybatis.spring.SqlSessionFactoryBean; //导入方法依赖的package包/类
@Bean
public SqlSessionFactory sqlSessionFactory( @Autowired DynamicDataSource dynamicDataSource ) {
try {
SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(dynamicDataSource);
sessionFactory.setTypeAliasesPackage(this.typeAliasesPackage);
sessionFactory.setMapperLocations( new PathMatchingResourcePatternResolver().getResources(mapperLocations) );
sessionFactory.setConfigLocation(new PathMatchingResourcePatternResolver().getResource(configLocation));
PageHelper pageHelper = new PageHelper();
Properties props = new Properties();
props.setProperty("reasonable", "false");
props.setProperty("supportMethodsArguments", "true");
props.setProperty("returnPageInfo", "check");
props.setProperty("params", "count=countSql");
pageHelper.setProperties(props);
sessionFactory.setPlugins(new Interceptor[] { pageHelper });
return sessionFactory.getObject();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
示例13: testAddToConfigTrue
import org.mybatis.spring.SqlSessionFactoryBean; //导入方法依赖的package包/类
@Test
public void testAddToConfigTrue() throws Exception {
// the default SqlSessionFactory in AbstractMyBatisSpringTest is created with an explicitly set
// MapperLocations list, so create a new factory here that tests auto-loading the config
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDatabaseIdProvider(null);
// mapperLocations properties defaults to null
factoryBean.setDataSource(dataSource);
factoryBean.setPlugins(new Interceptor[] { executorInterceptor });
SqlSessionFactory sqlSessionFactory = factoryBean.getObject();
find(new SqlSessionTemplate(sqlSessionFactory), true);
assertCommit(); // SqlSesssionTemplate autocommits
assertSingleConnection();
assertExecuteCount(1);
}
示例14: testAddToConfigFalse
import org.mybatis.spring.SqlSessionFactoryBean; //导入方法依赖的package包/类
@Test(expected = org.apache.ibatis.binding.BindingException.class)
public void testAddToConfigFalse() throws Throwable {
try {
// the default SqlSessionFactory in AbstractMyBatisSpringTest is created with an explicitly
// set MapperLocations list, so create a new factory here that tests auto-loading the
// config
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
// mapperLocations properties defaults to null
factoryBean.setDataSource(dataSource);
SqlSessionFactory sqlSessionFactory = factoryBean.getObject();
find(new SqlSessionTemplate(sqlSessionFactory), false);
fail("TestDao's mapper xml should not be loaded");
} catch (MyBatisSystemException mbse) {
// unwrap exception so the exact MyBatis exception can be tested
throw mbse.getCause();
} finally {
// connection not used; force close to avoid failing in validateConnectionClosed()
connection.close();
}
}
示例15: getSessionFactoryBean
import org.mybatis.spring.SqlSessionFactoryBean; //导入方法依赖的package包/类
@Bean
public SqlSessionFactoryBean getSessionFactoryBean(DataSource dataSource) {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource);
sqlSessionFactoryBean.setTypeAliasesPackage("com.oneops.cms.dj.domain");
return sqlSessionFactoryBean;
}