org.mybatis.spring.boot.autoconfigure.SpringBootVFS Java Examples

The following examples show how to use org.mybatis.spring.boot.autoconfigure.SpringBootVFS. 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 RuoYi-Vue with MIT License 6 votes vote down vote up
@Bean
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception
{
    String typeAliasesPackage = env.getProperty("mybatis.typeAliasesPackage");
    String mapperLocations = env.getProperty("mybatis.mapperLocations");
    String configLocation = env.getProperty("mybatis.configLocation");
    typeAliasesPackage = setTypeAliasesPackage(typeAliasesPackage);
    VFS.addImplClass(SpringBootVFS.class);

    final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
    sessionFactory.setDataSource(dataSource);
    sessionFactory.setTypeAliasesPackage(typeAliasesPackage);
    sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(mapperLocations));
    sessionFactory.setConfigLocation(new DefaultResourceLoader().getResource(configLocation));
    return sessionFactory.getObject();
}
 
Example #2
Source File: DatabaseConfig.java    From SO with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@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 #3
Source File: DatabaseConfig.java    From SO with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@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 #4
Source File: MyBatisConfiguration.java    From Aooms with Apache License 2.0 5 votes vote down vote up
@Bean
ConfigurationCustomizer mybatisConfigurationCustomizer() {
    return new ConfigurationCustomizer() {

        @Override
        public void customize(org.apache.ibatis.session.Configuration configuration) {
            // HashSet<MappedStatement> mappedStatements = new HashSet<MappedStatement>(configuration.getMappedStatements());

            /*SqlSource sqlSource = new SqlSource() {
                @Override
                public BoundSql getBoundSql(Object parameterObject) {
                    return new BoundSql(configuration,"nosqlscript",null,null);
                }
            };

            MappedStatement.Builder statementBuilder = new MappedStatement.Builder(configuration, MyBatisConst.MS_RECORD_INSERT, sqlSource, SqlCommandType.INSERT);
            configuration.addMappedStatement(statementBuilder.build());
            */
            // 修改默认vfs, 解决jar包 mapper.xml 不加载问题
            VFS.addImplClass(SpringBootVFS.class);
            //configuration.setVfsImpl(SpringBootVFS.class);

            // 设置map属性空值时仍返回数据
            configuration.setCallSettersOnNulls(true);

            RecordMappedStatmentFactory recordMappedStatmentFactory = new RecordMappedStatmentFactory(configuration);
            configuration.addMappedStatement(recordMappedStatmentFactory.getRecordInsertMappedStatment());
            configuration.addMappedStatement(recordMappedStatmentFactory.getRecordUpdateMappedStatment());
            configuration.addMappedStatement(recordMappedStatmentFactory.getRecordDeleteMappedStatment());
            configuration.addMappedStatement(recordMappedStatmentFactory.getRecordFindByPkMappedStatment());

        }
    };
}
 
Example #5
Source File: TestApplication.java    From hsweb-framework with Apache License 2.0 5 votes vote down vote up
@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 #6
Source File: MybatisPlusConfig.java    From watchdog-framework with MIT License 4 votes vote down vote up
/**
 * 这里全部使用mybatis-autoconfigure 已经自动加载的资源。不手动指定
 * 配置文件和mybatis-boot的配置文件同步
 */
@Bean
public MybatisSqlSessionFactoryBean mybatisSqlSessionFactoryBean() throws IOException {
    MybatisSqlSessionFactoryBean mybatisPlus = new MybatisSqlSessionFactoryBean();
    mybatisPlus.setDataSource(dataSource);
    mybatisPlus.setVfs(SpringBootVFS.class);
    PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
    mybatisPlus.setMapperLocations(resolver.getResources("classpath:mapper/*.xml"));
    if (StringUtils.hasText(this.properties.getConfigLocation())) {
        mybatisPlus.setConfigLocation(this.resourceLoader.getResource(this.properties.getConfigLocation()));
    }
    mybatisPlus.setConfiguration(properties.getConfiguration());
    if (!ObjectUtils.isEmpty(this.interceptors)) {
        mybatisPlus.setPlugins(this.interceptors);
    }
    // MP 全局配置,更多内容进入类看注释
    GlobalConfiguration globalConfig = new GlobalConfiguration();
    globalConfig.setDbType(DBType.MYSQL.name());//数据库类型
    // ID 策略 AUTO->`0`("数据库ID自增") INPUT->`1`(用户输入ID") ID_WORKER->`2`("全局唯一ID") UUID->`3`("全局唯一ID")
    //使用ID_WORKER_STR,因为前后端分离使用整形,前端JS会有精度丢失
    globalConfig.setIdType(IdType.ID_WORKER_STR.getKey());
    globalConfig.setSqlInjector(new AutoSqlInjector());
    //MP 属性下划线 转 驼峰 , 如果原生配置 mc.setMapUnderscoreToCamelCase(true) 开启,该配置可以无。
    //globalConfig.setDbColumnUnderline(true);
    mybatisPlus.setGlobalConfig(globalConfig);
    MybatisConfiguration mc = new MybatisConfiguration();
    // 对于完全自定义的mapper需要加此项配置,才能实现下划线转驼峰
    mc.setMapUnderscoreToCamelCase(true);
    mc.setDefaultScriptingLanguage(MybatisXMLLanguageDriver.class);
    mybatisPlus.setConfiguration(mc);
    if (this.databaseIdProvider != null) {
        mybatisPlus.setDatabaseIdProvider(this.databaseIdProvider);
    }
    if (StringUtils.hasLength(this.properties.getTypeAliasesPackage())) {
        mybatisPlus.setTypeAliasesPackage(this.properties.getTypeAliasesPackage());
    }
    if (StringUtils.hasLength(this.properties.getTypeHandlersPackage())) {
        mybatisPlus.setTypeHandlersPackage(this.properties.getTypeHandlersPackage());
    }
    if (!ObjectUtils.isEmpty(this.properties.resolveMapperLocations())) {
        mybatisPlus.setMapperLocations(this.properties.resolveMapperLocations());
    }
    return mybatisPlus;
}
 
Example #7
Source File: MybatisPlusConfig.java    From MI-S with MIT License 4 votes vote down vote up
/**
 * 这里全部使用mybatis-autoconfigure 已经自动加载的资源。不手动指定
 * 配置文件和mybatis-boot的配置文件同步
 * @return
 */
@Bean
@ConditionalOnMissingBean
public MybatisSqlSessionFactoryBean mybatisSqlSessionFactoryBean() {
    MybatisSqlSessionFactoryBean mybatisPlus = new MybatisSqlSessionFactoryBean();
    mybatisPlus.setDataSource(dataSource);
    mybatisPlus.setVfs(SpringBootVFS.class);
    if (StringUtils.hasText(this.properties.getConfigLocation())) {
        mybatisPlus.setConfigLocation(this.resourceLoader.getResource(this.properties.getConfigLocation()));
    }
    mybatisPlus.setConfiguration(properties.getConfiguration());
    if (!ObjectUtils.isEmpty(this.interceptors)) {
        mybatisPlus.setPlugins(this.interceptors);
    }
    // MP 全局配置,更多内容进入类看注释
    GlobalConfiguration globalConfig = new GlobalConfiguration();
    //驼峰下划线规则
    globalConfig.setDbColumnUnderline(true);
    globalConfig.setDbType(DBType.MYSQL.name());
    // ID 策略
    // AUTO->`0`("数据库ID自增")
    // INPUT->`1`(用户输入ID")
    // ID_WORKER->`2`("全局唯一ID")
    // UUID->`3`("全局唯一ID")
    globalConfig.setIdType(3);
    mybatisPlus.setGlobalConfig(globalConfig);
    MybatisConfiguration mc = new MybatisConfiguration();
    mc.setDefaultScriptingLanguage(MybatisXMLLanguageDriver.class);
    mybatisPlus.setConfiguration(mc);
    if (this.databaseIdProvider != null) {
        mybatisPlus.setDatabaseIdProvider(this.databaseIdProvider);
    }
    if (StringUtils.hasLength(this.properties.getTypeAliasesPackage())) {
        mybatisPlus.setTypeAliasesPackage(this.properties.getTypeAliasesPackage());
    }
    if (StringUtils.hasLength(this.properties.getTypeHandlersPackage())) {
        mybatisPlus.setTypeHandlersPackage(this.properties.getTypeHandlersPackage());
    }
    if (!ObjectUtils.isEmpty(this.properties.resolveMapperLocations())) {
        mybatisPlus.setMapperLocations(this.properties.resolveMapperLocations());
    }
    return mybatisPlus;
}
 
Example #8
Source File: MybatisPlusConfig.java    From MI-S with MIT License 4 votes vote down vote up
/**
 * 这里全部使用mybatis-autoconfigure 已经自动加载的资源。不手动指定
 * 配置文件和mybatis-boot的配置文件同步
 * @return
 */
@Bean
@ConditionalOnMissingBean
public MybatisSqlSessionFactoryBean mybatisSqlSessionFactoryBean() {
    MybatisSqlSessionFactoryBean mybatisPlus = new MybatisSqlSessionFactoryBean();
    mybatisPlus.setDataSource(roundRobinDataSouceProxy());
    mybatisPlus.setVfs(SpringBootVFS.class);
    if (StringUtils.hasText(this.properties.getConfigLocation())) {
        mybatisPlus.setConfigLocation(this.resourceLoader.getResource(this.properties.getConfigLocation()));
    }
    mybatisPlus.setConfiguration(properties.getConfiguration());
    if (!ObjectUtils.isEmpty(this.interceptors)) {
        mybatisPlus.setPlugins(this.interceptors);
    }
    // MP 全局配置,更多内容进入类看注释
    GlobalConfiguration globalConfig = new GlobalConfiguration();
    //驼峰下划线规则
    globalConfig.setDbColumnUnderline(true);
    globalConfig.setDbType(DBType.MYSQL.name());
    // ID 策略
    // AUTO->`0`("数据库ID自增")
    // INPUT->`1`(用户输入ID")
    // ID_WORKER->`2`("全局唯一ID")
    // UUID->`3`("全局唯一ID")
    globalConfig.setIdType(3);
    mybatisPlus.setGlobalConfig(globalConfig);
    MybatisConfiguration mc = new MybatisConfiguration();
    mc.setDefaultScriptingLanguage(MybatisXMLLanguageDriver.class);
    mybatisPlus.setConfiguration(mc);
    if (this.databaseIdProvider != null) {
        mybatisPlus.setDatabaseIdProvider(this.databaseIdProvider);
    }
    if (StringUtils.hasLength(this.properties.getTypeAliasesPackage())) {
        mybatisPlus.setTypeAliasesPackage(this.properties.getTypeAliasesPackage());
    }
    if (StringUtils.hasLength(this.properties.getTypeHandlersPackage())) {
        mybatisPlus.setTypeHandlersPackage(this.properties.getTypeHandlersPackage());
    }
    if (!ObjectUtils.isEmpty(this.properties.resolveMapperLocations())) {
        mybatisPlus.setMapperLocations(this.properties.resolveMapperLocations());
    }
    return mybatisPlus;
}