use of com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean in project poseidon by muggle0.
the class MybatisPlusConfig method createSqlSessionFactoryBean.
@Bean(name = "sqlSessionFactory")
public SqlSessionFactory createSqlSessionFactoryBean(@Qualifier(value = "globalConfig") GlobalConfig configuration) throws Exception {
log.info("初始化SqlSessionFactory");
MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource);
Interceptor[] interceptor = { new PaginationInterceptor() };
sqlSessionFactoryBean.setPlugins(interceptor);
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
// sqlSessionFactoryBean.setTypeHandlers(new TypeHandler[]{new MybatisDataHandler()});
try {
sqlSessionFactoryBean.setGlobalConfig(configuration);
sqlSessionFactoryBean.setMapperLocations(resolver.getResources(mapperLocations));
sqlSessionFactoryBean.setTypeAliasesPackage(typeAliasesPackage);
return sqlSessionFactoryBean.getObject();
} catch (Exception e) {
e.printStackTrace();
}
return sqlSessionFactoryBean.getObject();
}
use of com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean in project scaleph by flowerfine.
the class MasterDataSourceConfig method masterSqlSessionFactory.
@Primary
@Bean(MASTER_SQL_SESSION_FACTORY)
public SqlSessionFactory masterSqlSessionFactory() throws Exception {
MybatisSqlSessionFactoryBean factoryBean = new MybatisSqlSessionFactoryBean();
MybatisPlusProperties props = new MybatisPlusProperties();
props.setMapperLocations(new String[] { MASTER_MAPPER_XML_PATH });
factoryBean.setMapperLocations(props.resolveMapperLocations());
MybatisConfiguration configuration = new MybatisConfiguration();
configuration.setMapUnderscoreToCamelCase(true);
configuration.setLogImpl(Slf4jImpl.class);
factoryBean.setConfiguration(configuration);
factoryBean.setDataSource(masterDataSource());
factoryBean.setTypeAliasesPackage(MASTER_ENTITY_PACKAGE);
factoryBean.setPlugins(mybatisPlusInterceptor);
return factoryBean.getObject();
}
use of com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean in project loc-framework by lord-of-code.
the class LocMybatisAutoConfiguration method createSqlSessionFactory.
@Nullable
private SqlSessionFactory createSqlSessionFactory(ConfigurableListableBeanFactory configurableListableBeanFactory, String prefixName, MybatisProperties mybatisProperties) {
DataSource dataSource = configurableListableBeanFactory.getBean(prefixName + "Ds", DataSource.class);
MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource);
sqlSessionFactoryBean.setVfs(LocSpringBootVFS.class);
Optional.ofNullable(mybatisProperties.getConfigLocation()).map(this.resourceLoader::getResource).ifPresent(sqlSessionFactoryBean::setConfigLocation);
org.apache.ibatis.session.Configuration configuration = mybatisProperties.getConfiguration();
if (configuration == null && !StringUtils.hasText(mybatisProperties.getConfigLocation())) {
configuration = new org.apache.ibatis.session.Configuration();
}
sqlSessionFactoryBean.setConfiguration(configuration);
Optional.ofNullable(mybatisProperties.getConfigurationProperties()).ifPresent(sqlSessionFactoryBean::setConfigurationProperties);
Optional.ofNullable(mybatisProperties.getTypeAliasesPackage()).ifPresent(sqlSessionFactoryBean::setTypeAliasesPackage);
Optional.ofNullable(mybatisProperties.getTypeHandlersPackage()).ifPresent(sqlSessionFactoryBean::setTypeHandlersPackage);
if (!ObjectUtils.isEmpty(mybatisProperties.resolveMapperLocations())) {
sqlSessionFactoryBean.setMapperLocations(mybatisProperties.resolveMapperLocations());
}
PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
sqlSessionFactoryBean.setPlugins(new Interceptor[] { paginationInterceptor });
try {
SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBean.getObject();
if (sqlSessionFactory == null) {
log.error("sqlSessionFactoryBean get object is null");
return null;
}
register(configurableListableBeanFactory, sqlSessionFactory, prefixName + "SessionFactory", prefixName + "Sf");
if (!Strings.isNullOrEmpty(mybatisProperties.getBasePackage())) {
createBasePackageScanner((BeanDefinitionRegistry) configurableListableBeanFactory, mybatisProperties.getBasePackage(), prefixName);
} else {
createClassPathMapperScanner((BeanDefinitionRegistry) configurableListableBeanFactory, prefixName);
}
return sqlSessionFactory;
} catch (Exception e) {
log.error(e.getMessage(), e);
}
return null;
}
use of com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean in project muses by acgist.
the class MssqlConfiguration method mssqlSqlSessionFactory.
@Bean(name = "mssqlSqlSessionFactory")
public SqlSessionFactory mssqlSqlSessionFactory(@Qualifier("mssqlDataSource") DataSource mssqlDataSource) throws Exception {
final MybatisSqlSessionFactoryBean sessionFactory = new MybatisSqlSessionFactoryBean();
sessionFactory.setDataSource(mssqlDataSource);
sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:/mybatis/mssql/mapper/*.xml"));
return sessionFactory.getObject();
}
use of com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean in project citrus by Yiuman.
the class DynamicDataSourceAutoConfiguration method createSqlSessionFactory.
/**
* 用于动态数据源
* 根据数据源创建SqlSessionFactory
*
* @param dataSource 数据源
* @return SqlSessionFactory
* @throws Exception in case of creation errors
*/
private SqlSessionFactory createSqlSessionFactory(DataSource dataSource) throws Exception {
// 使用 MybatisSqlSessionFactoryBean 而不是 SqlSessionFactoryBean
MybatisSqlSessionFactoryBean factory = new MybatisSqlSessionFactoryBean();
factory.setDataSource(dataSource);
factory.setVfs(SpringBootVFS.class);
if (StringUtils.hasText(this.mybatisPlusProperties.getConfigLocation())) {
factory.setConfigLocation(this.resourceLoader.getResource(this.mybatisPlusProperties.getConfigLocation()));
}
applyConfiguration(factory);
if (this.mybatisPlusProperties.getConfigurationProperties() != null) {
factory.setConfigurationProperties(this.mybatisPlusProperties.getConfigurationProperties());
}
if (this.databaseIdProvider != null) {
factory.setDatabaseIdProvider(this.databaseIdProvider);
}
if (StringUtils.hasLength(this.mybatisPlusProperties.getTypeAliasesPackage())) {
factory.setTypeAliasesPackage(this.mybatisPlusProperties.getTypeAliasesPackage());
}
if (this.mybatisPlusProperties.getTypeAliasesSuperType() != null) {
factory.setTypeAliasesSuperType(this.mybatisPlusProperties.getTypeAliasesSuperType());
}
if (StringUtils.hasLength(this.mybatisPlusProperties.getTypeHandlersPackage())) {
factory.setTypeHandlersPackage(this.mybatisPlusProperties.getTypeHandlersPackage());
}
if (!ObjectUtils.isEmpty(this.typeHandlers)) {
factory.setTypeHandlers(this.typeHandlers);
}
if (!ObjectUtils.isEmpty(this.mybatisPlusProperties.resolveMapperLocations())) {
factory.setMapperLocations(this.mybatisPlusProperties.resolveMapperLocations());
}
Class<? extends LanguageDriver> defaultLanguageDriver = this.mybatisPlusProperties.getDefaultScriptingLanguageDriver();
if (!ObjectUtils.isEmpty(this.languageDrivers)) {
factory.setScriptingLanguageDrivers(this.languageDrivers);
}
Optional.ofNullable(defaultLanguageDriver).ifPresent(factory::setDefaultScriptingLanguageDriver);
if (!ObjectUtils.isEmpty(this.interceptors)) {
factory.setPlugins(this.interceptors);
}
// 自定义枚举包
if (StringUtils.hasLength(this.mybatisPlusProperties.getTypeEnumsPackage())) {
factory.setTypeEnumsPackage(this.mybatisPlusProperties.getTypeEnumsPackage());
}
// 这里每个MybatisSqlSessionFactoryBean对应的都是独立的一个GlobalConfig,不然会出现问题
GlobalConfig globalConfig = GlobalConfigUtils.defaults();
// 去除打印
globalConfig.setBanner(false);
// 注入填充器
this.getBeanThen(MetaObjectHandler.class, globalConfig::setMetaObjectHandler);
// 注入主键生成器
this.getBeansThen(IKeyGenerator.class, i -> globalConfig.getDbConfig().setKeyGenerators(i));
// 注入sql注入器
this.getBeanThen(ISqlInjector.class, globalConfig::setSqlInjector);
// 注入ID生成器
this.getBeanThen(IdentifierGenerator.class, globalConfig::setIdentifierGenerator);
// 设置 GlobalConfig 到 MybatisSqlSessionFactoryBean
factory.setGlobalConfig(globalConfig);
return factory.getObject();
}
Aggregations