欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

springboot多模塊化整合mybatis,mapper自動注入失敗問題及解決

 更新時間:2022年01月14日 10:03:46   作者:Kevinten10  
這篇文章主要介紹了springboot多模塊化整合mybatis,mapper自動注入失敗問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

springboot多模塊化整合mybatis,mapper自動注入失敗

問題

啟動類添加@MapperScan或@ComponentScan,mapper類添加@Mapper或@Repository

==> Consider defining a bean of type 'com.ten.mapper.UserMapper' in your configuration.

Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required in spring mock mvc test for spring boot with mybatis

解決

手動裝配dataSource

啟動類:

package com.ten; 
import com.alibaba.druid.pool.DruidDataSource;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component; 
import javax.sql.DataSource;
 
@SpringBootApplication
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class})
@MapperScan("com.ten.mapper")
class LcWebApplication {
    public static void main(String[] args) {
        SpringApplication.run(LcWebApplication.class, args);
    }
 
    @Autowired
    private Environment env;
 
    //destroy-method="close"的作用是當數(shù)據(jù)庫連接不使用的時候,就把該連接重新放到數(shù)據(jù)池中,方便下次使用調(diào)用.
    @Bean
    public DataSource dataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUrl(env.getProperty("spring.datasource.url"));
        dataSource.setUsername(env.getProperty("spring.datasource.username"));//用戶名
        dataSource.setPassword(env.getProperty("spring.datasource.password"));//密碼
        dataSource.setDriverClassName(env.getProperty("spring.datasource.driver-class-name"));
        dataSource.setInitialSize(2);//初始化時建立物理連接的個數(shù)
        dataSource.setMaxActive(20);//最大連接池數(shù)量
        dataSource.setMinIdle(0);//最小連接池數(shù)量
        dataSource.setMaxWait(60000);//獲取連接時最大等待時間,單位毫秒。
        dataSource.setValidationQuery("SELECT 1");//用來檢測連接是否有效的sql
        dataSource.setTestOnBorrow(false);//申請連接時執(zhí)行validationQuery檢測連接是否有效
        dataSource.setTestWhileIdle(true);//建議配置為true,不影響性能,并且保證安全性。
        dataSource.setPoolPreparedStatements(false);//是否緩存preparedStatement,也就是PSCache
        return dataSource;
    } 
}

啟動類配置文件:

spring:
    datasource:
        name: test
        url: jdbc:mysql://localhost:3306/db
        username: root
        password: root
        # 使用druid數(shù)據(jù)源
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.jdbc.Driver
        filters: stat
        maxActive: 20
        initialSize: 1
        maxWait: 60000
        minIdle: 1
        timeBetweenEvictionRunsMillis: 60000
        minEvictableIdleTimeMillis: 300000
        validationQuery: select 'x'
        testWhileIdle: true
        testOnBorrow: false
        testOnReturn: false
        poolPreparedStatements: true
        maxOpenPreparedStatements: 20 
 
mybatis:
  mapperLocations: classpath*:mapper/*.xml
  typeAliasesPackage: com.ten.entity

啟動類依賴

 <!--web-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--rest-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
        </dependency>
        <!-- mybatis -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
        </dependency>

DAO類

@Repository
public interface UserMapper {
    String getTest(String test);
}

DAO類依賴

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

目錄結(jié)構(gòu):

springboot mapper注入失敗的一種原因

今天啟動項目報錯----mapper注入失敗。細細查找一番發(fā)現(xiàn)是時間類型的問題。

具體情況是

數(shù)據(jù)庫有個字段的類型是datetime,但是實體類里的類型我寫成了LocalDateTime,結(jié)果當然是jdbctype對不上,導致mapper注入不進去。

解決辦法

實體類型定義成Date。

LocalDateTime其實是一種時間轉(zhuǎn)換工具,不要定義為實體的類型。 實體類是時間的話,類型一般是Date或者timestamp。 

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 詳解Java線程同步器CountDownLatch

    詳解Java線程同步器CountDownLatch

    這篇文章主要介紹了Java線程同步器CountDownLatch的相關(guān)資料,幫助大家更好的理解和學習Java,感興趣的朋友可以了解下
    2020-09-09
  • Mybatis中BindingException異常的產(chǎn)生原因及解決過程

    Mybatis中BindingException異常的產(chǎn)生原因及解決過程

    BindingException異常是MyBatis框架中自定義的異常,顧名思義指的是綁定出現(xiàn)問題,下面這篇文章主要給大家介紹了關(guān)于MyBatis報錯BindingException異常的產(chǎn)生原因及解決過程,需要的朋友可以參考下
    2023-06-06
  • java?數(shù)組實現(xiàn)學生成績統(tǒng)計教程

    java?數(shù)組實現(xiàn)學生成績統(tǒng)計教程

    這篇文章主要介紹了java?數(shù)組實現(xiàn)學生成績統(tǒng)計教程,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • Java????????HashMap遍歷方法匯總

    Java????????HashMap遍歷方法匯總

    這篇文章主要介紹了Java????????HashMap遍歷方法匯總,HashMap?的遍歷方法有很多種,不同的?JDK?版本有不同的寫法,下文關(guān)于其遍歷方法總結(jié)需要的小伙伴可以參考一下
    2022-05-05
  • Java中自然排序和比較器排序詳解

    Java中自然排序和比較器排序詳解

    這篇文章給大家介紹Java中的排序并不是指插入排序、希爾排序、歸并排序等具體的排序算法。而是自然排序和比較器排序,文中通過實例代碼介紹的很詳細,有需要的朋友們可以參考借鑒。
    2016-09-09
  • spring boot封裝HttpClient的示例代碼

    spring boot封裝HttpClient的示例代碼

    這篇文章主要介紹了spring boot封裝HttpClient的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-01-01
  • 閱讀EnumSet抽象類源碼

    閱讀EnumSet抽象類源碼

    這篇文章主要介紹了閱讀EnumSet抽象類源碼,具有一定參考價值,需要的朋友可以了解下。
    2017-12-12
  • 修改maven本地倉庫路徑的方法

    修改maven本地倉庫路徑的方法

    本篇文章主要介紹了修改maven本地倉庫路徑的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-12-12
  • SpringBoot的@RestControllerAdvice作用詳解

    SpringBoot的@RestControllerAdvice作用詳解

    這篇文章主要介紹了SpringBoot的@RestControllerAdvice作用詳解,@RestContrllerAdvice是一種組合注解,由@ControllerAdvice,@ResponseBody組成,本質(zhì)上就是@Component,需要的朋友可以參考下
    2024-01-01
  • Java的ThreadPoolExecutor業(yè)務(wù)線程池詳細解析

    Java的ThreadPoolExecutor業(yè)務(wù)線程池詳細解析

    這篇文章主要介紹了Java線程池ThreadPoolExecutor詳細解析,任務(wù)剛開始進來的時候就創(chuàng)建核心線程,核心線程滿了會把任務(wù)放到阻塞隊列,阻塞隊列滿了之后才會創(chuàng)建空閑線程,達到最大線程數(shù)之后,再有任務(wù)進來,就只能執(zhí)行拒絕策略了,需要的朋友可以參考下
    2024-01-01

最新評論