Search in sources :

Example 26 with MybatisSqlSessionFactoryBean

use of com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean in project incubator-linkis by apache.

the class MybatisConfigurationFactory method sqlSessionFactory.

// Provide SqlSeesion(提供SqlSeesion)
@Bean(name = "sqlSessionFactory")
@Primary
public MybatisSqlSessionFactoryBean sqlSessionFactory() {
    String typeAliasesPackage = MybatisConfiguration.BDP_SERVER_MYBATIS_TYPEALIASESPACKAGE.getValue();
    // Configure the mapper scan to find all mapper.xml mapping
    // files(配置mapper的扫描,找到所有的mapper.xml映射文件)
    String mapperLocations = MybatisConfiguration.BDP_SERVER_MYBATIS_MAPPER_LOCATIONS.getValue();
    // Load the global configuration file(加载全局的配置文件)
    String configLocation = MybatisConfiguration.BDP_SERVER_MYBATIS_CONFIGLOCATION.getValue();
    try {
        MybatisSqlSessionFactoryBean sessionFactoryBean = new MybatisSqlSessionFactoryBean();
        sessionFactoryBean.setDataSource(dataSource);
        info("Mybatis typeAliasesPackage=" + typeAliasesPackage);
        info("Mybatis mapperLocations=" + mapperLocations);
        info("Mybatis configLocation=" + configLocation);
        // Read configuration(读取配置)
        sessionFactoryBean.setTypeAliasesPackage(typeAliasesPackage);
        // Set the location of the mapper.xml file(设置mapper.xml文件所在位置)
        if (StringUtils.isNotBlank(mapperLocations)) {
            String[] mapperArray = mapperLocations.split(",");
            List<Resource> resources = new ArrayList<>();
            for (String mapperLocation : mapperArray) {
                CollectionUtils.addAll(resources, new PathMatchingResourcePatternResolver().getResources(mapperLocation));
            }
            sessionFactoryBean.setMapperLocations(resources.toArray(new Resource[0]));
        }
        /* Resource[] resources = new PathMatchingResourcePatternResolver().getResources(mapperLocations);
            sessionFactoryBean.setMapperLocations(resources);*/
        // Set the location of the mybatis-config.xml configuration
        // file(设置mybatis-config.xml配置文件位置)
        sessionFactoryBean.setConfigLocation(new DefaultResourceLoader().getResource(configLocation));
        // Add paging plugin, print sql plugin(添加分页插件、打印sql插件)
        Interceptor[] plugins = new Interceptor[] { pageInterceptor() };
        sessionFactoryBean.setPlugins(plugins);
        return sessionFactoryBean;
    } catch (IOException e) {
        error("mybatis resolver mapper*xml is error", e);
        return null;
    } catch (Exception e) {
        error("mybatis sqlSessionFactoryBean create error", e);
        return null;
    }
}
Also used : MybatisSqlSessionFactoryBean(com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean) Resource(org.springframework.core.io.Resource) ArrayList(java.util.ArrayList) IOException(java.io.IOException) PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver) PageInterceptor(com.github.pagehelper.PageInterceptor) Interceptor(org.apache.ibatis.plugin.Interceptor) IOException(java.io.IOException) DefaultResourceLoader(org.springframework.core.io.DefaultResourceLoader) Primary(org.springframework.context.annotation.Primary) MybatisSqlSessionFactoryBean(com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean) Bean(org.springframework.context.annotation.Bean)

Example 27 with MybatisSqlSessionFactoryBean

use of com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean in project FCLProject by FlowingCloudL.

the class DataSourceProxyConfig method sqlSessionFactoryBean.

@Bean
public SqlSessionFactory sqlSessionFactoryBean(DataSource dataSource) throws Exception {
    // 订单服务中引入了mybatis-plus,所以要使用特殊的SqlSessionFactoryBean
    MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
    // 代理数据源
    sqlSessionFactoryBean.setDataSource(new DataSourceProxy(dataSource));
    // 生成SqlSessionFactory
    return sqlSessionFactoryBean.getObject();
}
Also used : MybatisSqlSessionFactoryBean(com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean) DataSourceProxy(io.seata.rm.datasource.DataSourceProxy) MybatisSqlSessionFactoryBean(com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean) Bean(org.springframework.context.annotation.Bean)

Example 28 with MybatisSqlSessionFactoryBean

use of com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean in project dolphinscheduler by apache.

the class SpringConnectionFactory method sqlSessionFactory.

/**
 * * get sql session factory
 * @return sqlSessionFactory
 * @throws Exception sqlSessionFactory exception
 */
@Bean
public SqlSessionFactory sqlSessionFactory() throws Exception {
    MybatisConfiguration configuration = new MybatisConfiguration();
    configuration.setMapUnderscoreToCamelCase(true);
    configuration.setCacheEnabled(false);
    configuration.setCallSettersOnNulls(true);
    configuration.setJdbcTypeForNull(JdbcType.NULL);
    configuration.addInterceptor(paginationInterceptor());
    MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
    sqlSessionFactoryBean.setConfiguration(configuration);
    sqlSessionFactoryBean.setDataSource(dataSource());
    GlobalConfig.DbConfig dbConfig = new GlobalConfig.DbConfig();
    dbConfig.setIdType(IdType.AUTO);
    GlobalConfig globalConfig = new GlobalConfig();
    globalConfig.setDbConfig(dbConfig);
    sqlSessionFactoryBean.setGlobalConfig(globalConfig);
    sqlSessionFactoryBean.setTypeAliasesPackage("org.apache.dolphinscheduler.dao.entity");
    ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
    sqlSessionFactoryBean.setMapperLocations(resolver.getResources("org/apache/dolphinscheduler/dao/mapper/*Mapper.xml"));
    sqlSessionFactoryBean.setTypeEnumsPackage("org.apache.dolphinscheduler.*.enums");
    return sqlSessionFactoryBean.getObject();
}
Also used : MybatisSqlSessionFactoryBean(com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean) PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver) ResourcePatternResolver(org.springframework.core.io.support.ResourcePatternResolver) MybatisConfiguration(com.baomidou.mybatisplus.core.MybatisConfiguration) GlobalConfig(com.baomidou.mybatisplus.core.config.GlobalConfig) PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver) MybatisSqlSessionFactoryBean(com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean) Bean(org.springframework.context.annotation.Bean)

Example 29 with MybatisSqlSessionFactoryBean

use of com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean in project dolphinscheduler by apache.

the class ConnectionFactory method getSqlSessionFactory.

/**
 * * get sql session factory
 *
 * @return sqlSessionFactory
 * @throws Exception sqlSessionFactory exception
 */
private SqlSessionFactory getSqlSessionFactory() throws Exception {
    TransactionFactory transactionFactory = new JdbcTransactionFactory();
    Environment environment = new Environment("development", transactionFactory, getDataSource());
    MybatisConfiguration configuration = new MybatisConfiguration();
    configuration.setEnvironment(environment);
    configuration.setLazyLoadingEnabled(true);
    configuration.addMappers("org.apache.dolphinscheduler.dao.mapper");
    configuration.addInterceptor(new PaginationInterceptor());
    MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
    sqlSessionFactoryBean.setConfiguration(configuration);
    sqlSessionFactoryBean.setDataSource(getDataSource());
    sqlSessionFactoryBean.setTypeEnumsPackage("org.apache.dolphinscheduler.*.enums");
    sqlSessionFactory = sqlSessionFactoryBean.getObject();
    return sqlSessionFactory;
}
Also used : MybatisSqlSessionFactoryBean(com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean) MybatisConfiguration(com.baomidou.mybatisplus.core.MybatisConfiguration) TransactionFactory(org.apache.ibatis.transaction.TransactionFactory) JdbcTransactionFactory(org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory) Environment(org.apache.ibatis.mapping.Environment) JdbcTransactionFactory(org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory) PaginationInterceptor(com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor)

Aggregations

MybatisSqlSessionFactoryBean (com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean)29 Bean (org.springframework.context.annotation.Bean)23 PathMatchingResourcePatternResolver (org.springframework.core.io.support.PathMatchingResourcePatternResolver)18 MybatisConfiguration (com.baomidou.mybatisplus.core.MybatisConfiguration)12 GlobalConfig (com.baomidou.mybatisplus.core.config.GlobalConfig)11 ResourcePatternResolver (org.springframework.core.io.support.ResourcePatternResolver)8 SqlSessionFactory (org.apache.ibatis.session.SqlSessionFactory)7 Resource (org.springframework.core.io.Resource)5 PaginationInterceptor (com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor)4 Interceptor (org.apache.ibatis.plugin.Interceptor)3 SqlSessionFactoryBean (org.mybatis.spring.SqlSessionFactoryBean)3 Primary (org.springframework.context.annotation.Primary)3 MybatisPlusProperties (com.baomidou.mybatisplus.autoconfigure.MybatisPlusProperties)2 MybatisPlusInterceptor (com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor)2 PaginationInnerInterceptor (com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor)2 DataSourceProxy (io.seata.rm.datasource.DataSourceProxy)2 ConfigurationCustomizer (com.baomidou.mybatisplus.autoconfigure.ConfigurationCustomizer)1 MybatisMapWrapperFactory (com.baomidou.mybatisplus.extension.MybatisMapWrapperFactory)1 PerformanceInterceptor (com.baomidou.mybatisplus.extension.plugins.PerformanceInterceptor)1 OptimisticLockerInnerInterceptor (com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor)1