關於java.lang.NoClassDefFoundError: org/springframework/util/unit/DataSize

關於java.lang.NoClassDefFoundError: org/springframework/util/unit/DataSize的問題 spring boot 開啓錯誤

今天在使用mybatis的基於springboot的框架的基礎功能包抽離出來的時候,在增加spring yml自定義設置後,在業務項目啓動時會報出以下異常。

***************************
APPLICATION FAILED TO START
***************************

Description:

Cannot determine embedded database driver class for database type NONE

Action:

If you want an embedded database please put a supported one on the classpath. If you have database settings to be loaded from a particular profile you may need to active it (the profiles "dev,redis,config,mongodb,activemq,socketio" are currently active).

原因是需要在啓動類的@EnableAutoConfiguration或@SpringBootApplication中添加exclude = {DataSourceAutoConfiguration.class},排除此類的autoconfig啓動以後就可以正常運行。

並且需要使用到

<dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot</artifactId>
            <version>4.3.8.RELEASE</version>
        </dependency>

上面兩個依賴,更新spring boot的版本
並且mybatis在某個版本後取消了sqlSessionFactory的自動注入,需要顯示注入。

然而設置這玩意兒是需要設置dataSource的,於是dataSource也得顯示聲明

單獨配置一個:

@Configuration
public class MybatisConfig {


    @Autowired
    private DataSourceProperties dataSourceProperties;


    @Bean(name = "dataSource")
    public DataSource dataSource() {

        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUrl(dataSourceProperties.getUrl());
        System.out.println(dataSourceProperties.getUrl());
        dataSource.setDriverClassName(dataSourceProperties.getDriverClassName());
        dataSource.setUsername(dataSourceProperties.getUsername());
        dataSource.setPassword(dataSourceProperties.getPassword());

        return dataSource;

    }

    public SqlSessionFactory sqlSessionFactory() throws Exception {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource());
        return sqlSessionFactoryBean.getObject();
    }
}

tips:
註釋:@MapperScan(basePackages = {“xxx.mapper.front”,“xxx,mapper.admin”,“xxx.mapper”})

意思爲:改註解內聲明對應包下的所有class都是Mapper類
通過使用@MapperScan可以指定要掃描的Mapper類的包的路徑

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章