springboot多模塊化整合mybatis,mapper自動(dòng)注入失敗問(wèn)題及解決
springboot多模塊化整合mybatis,mapper自動(dòng)注入失敗
問(wèn)題
啟動(dòng)類(lèi)添加@MapperScan或@ComponentScan,mapper類(lèi)添加@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
解決
手動(dòng)裝配dataSource
啟動(dòng)類(lèi):
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"的作用是當(dāng)數(shù)據(jù)庫(kù)連接不使用的時(shí)候,就把該連接重新放到數(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"));//用戶(hù)名
dataSource.setPassword(env.getProperty("spring.datasource.password"));//密碼
dataSource.setDriverClassName(env.getProperty("spring.datasource.driver-class-name"));
dataSource.setInitialSize(2);//初始化時(shí)建立物理連接的個(gè)數(shù)
dataSource.setMaxActive(20);//最大連接池?cái)?shù)量
dataSource.setMinIdle(0);//最小連接池?cái)?shù)量
dataSource.setMaxWait(60000);//獲取連接時(shí)最大等待時(shí)間,單位毫秒。
dataSource.setValidationQuery("SELECT 1");//用來(lái)檢測(cè)連接是否有效的sql
dataSource.setTestOnBorrow(false);//申請(qǐng)連接時(shí)執(zhí)行validationQuery檢測(cè)連接是否有效
dataSource.setTestWhileIdle(true);//建議配置為true,不影響性能,并且保證安全性。
dataSource.setPoolPreparedStatements(false);//是否緩存preparedStatement,也就是PSCache
return dataSource;
}
}啟動(dòng)類(lèi)配置文件:
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啟動(dòng)類(lèi)依賴(lài)
<!--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類(lèi)
@Repository
public interface UserMapper {
String getTest(String test);
}DAO類(lèi)依賴(lài)
<!-- mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>目錄結(jié)構(gòu):

springboot mapper注入失敗的一種原因
今天啟動(dòng)項(xiàng)目報(bào)錯(cuò)----mapper注入失敗。細(xì)細(xì)查找一番發(fā)現(xiàn)是時(shí)間類(lèi)型的問(wèn)題。
具體情況是
數(shù)據(jù)庫(kù)有個(gè)字段的類(lèi)型是datetime,但是實(shí)體類(lèi)里的類(lèi)型我寫(xiě)成了LocalDateTime,結(jié)果當(dāng)然是jdbctype對(duì)不上,導(dǎo)致mapper注入不進(jìn)去。
解決辦法
實(shí)體類(lèi)型定義成Date。
LocalDateTime其實(shí)是一種時(shí)間轉(zhuǎn)換工具,不要定義為實(shí)體的類(lèi)型。 實(shí)體類(lèi)是時(shí)間的話(huà),類(lèi)型一般是Date或者timestamp。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java中關(guān)于字典樹(shù)的算法實(shí)現(xiàn)
字典樹(shù),又稱(chēng)單詞查找樹(shù),Trie樹(shù),是一種樹(shù)形結(jié)構(gòu),哈希表的一個(gè)變種。用于統(tǒng)計(jì),排序和保存大量的字符串,本文針對(duì)字典樹(shù)給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值2021-09-09
詳解Spring Boot配置文件application.properties
在本文中我們給大家整理了關(guān)于Spring Boot 的配置文件 application.properties的相關(guān)知識(shí)點(diǎn)內(nèi)容,需要的朋友們參考學(xué)習(xí)下。2019-06-06
Java編譯錯(cuò)誤信息提示java.lang.ExceptionInInitializer解決
這篇文章主要介紹了Java編譯錯(cuò)誤信息提示java.lang.ExceptionInInitializer的分析講解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07
spring基于注解配置實(shí)現(xiàn)事務(wù)控制操作
這篇文章主要介紹了spring基于注解配置實(shí)現(xiàn)事務(wù)控制操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09
Java設(shè)計(jì)模式的策略模式簡(jiǎn)析
這篇文章主要介紹了Java設(shè)計(jì)模式的策略模式簡(jiǎn)析,策略模式中定義了一系列的算法族,算法族指的是類(lèi)似于一系列的行為、策略,策略模式將一系列的行為封裝成類(lèi),既可以說(shuō)是將每一種相類(lèi)似的行為都封裝成一個(gè)類(lèi),也有可能存在特殊的不進(jìn)行封裝的行為,需要的朋友可以參考下2023-12-12
Java?方法(方法的定義,可變參數(shù),參數(shù)的傳遞問(wèn)題,方法重載,方法簽名)
這篇文章主要介紹了Java?方法(方法的定義,可變參數(shù),參數(shù)的傳遞問(wèn)題,方法重載,方法簽名),文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,感興趣的小伙伴可以參考一下2022-09-09

