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"的作用是當數據庫連接不使用的時候,就把該連接重新放到數據池中,方便下次使用調用.
    @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);//初始化時建立物理連接的個數
        dataSource.setMaxActive(20);//最大連接池數量
        dataSource.setMinIdle(0);//最小連接池數量
        dataSource.setMaxWait(60000);//獲取連接時最大等待時間,單位毫秒。
        dataSource.setValidationQuery("SELECT 1");//用來檢測連接是否有效的sql
        dataSource.setTestOnBorrow(false);//申請連接時執行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數據源
        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>

目錄結構:

springboot mapper註入失敗的一種原因

今天啟動項目報錯—-mapper註入失敗。細細查找一番發現是時間類型的問題。

具體情況是

數據庫有個字段的類型是datetime,但是實體類裡的類型我寫成瞭LocalDateTime,結果當然是jdbctype對不上,導致mapper註入不進去。

解決辦法

實體類型定義成Date。

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

以上為個人經驗,希望能給大傢一個參考,也希望大傢多多支持WalkonNet。

推薦閱讀: