com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean Java Examples

The following examples show how to use com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean. 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: MyBatisConfig.java    From seata-samples with Apache License 2.0 6 votes vote down vote up
@Bean(name = "sqlSessionFactory")
public SqlSessionFactory sqlSessionFactoryBean(DataSourceProxy dataSourceProxy) throws Exception {
    MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
    bean.setDataSource(dataSourceProxy);
    ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
    // bean.setConfigLocation(resolver.getResource("classpath:mybatis-config.xml"));
    bean.setMapperLocations(resolver.getResources("classpath*:mybatis/**/*-mapper.xml"));

    SqlSessionFactory factory = null;
    try {
        factory = bean.getObject();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return factory;
}
 
Example #2
Source File: SeataDataSourceProxyConfig.java    From lion with Apache License 2.0 6 votes vote down vote up
@Bean(name = "sqlSessionFactory")
public SqlSessionFactory sqlSessionFactoryBean(DataSource dataSource) throws Exception {
    MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
    bean.setDataSource(dataSource);
    ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
    //bean.setConfigLocation(resolver.getResource("classpath:mybatis-config.xml"));
    bean.setMapperLocations(resolver.getResources(mapperLocations));

    SqlSessionFactory factory;
    try {
        factory = bean.getObject();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return factory;
}
 
Example #3
Source File: SeataDataSourceProxyConfig.java    From lion with Apache License 2.0 6 votes vote down vote up
@Bean(name = "sqlSessionFactory")
public SqlSessionFactory sqlSessionFactoryBean(DataSource dataSource) throws Exception {
    MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
    bean.setDataSource(dataSource);
    ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
    //bean.setConfigLocation(resolver.getResource("classpath:mybatis-config.xml"));
    bean.setMapperLocations(resolver.getResources(mapperLocations));

    SqlSessionFactory factory;
    try {
        factory = bean.getObject();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return factory;
}
 
Example #4
Source File: DataSourceProxyConfig.java    From seata-samples with Apache License 2.0 5 votes vote down vote up
@Bean
@ConfigurationProperties(prefix = "mybatis")
public MybatisSqlSessionFactoryBean sqlSessionFactoryBean(@Qualifier("dynamicDataSource") DataSource dataSource) {
    // 这里用 MybatisSqlSessionFactoryBean 代替了 SqlSessionFactoryBean,否则 MyBatisPlus 不会生效
    MybatisSqlSessionFactoryBean mybatisSqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
    mybatisSqlSessionFactoryBean.setDataSource(dataSource);
    return mybatisSqlSessionFactoryBean;
}
 
Example #5
Source File: LocMybatisAutoConfiguration.java    From loc-framework with MIT License 4 votes vote down vote up
private @Nullable
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;
}