Mybatis-plus依賴及配置文件方式
Mybatis-plus依賴及配置文件
依賴
<dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.5.1</version> </dependency>
配置文件
package com.zhang.demo.config; import com.baomidou.mybatisplus.annotation.DbType; import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor; import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor; import org.apache.ibatis.annotations.Mapper; import org.mybatis.spring.annotation.MapperScan; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration //springboot里面提供配置的一個(gè)注解 ,這里面所有的配置想要注入springboot容器的話必須需要這個(gè)注解 @MapperScan("com.zhang.demo.mapper") //@MapperScan最好是放到MybatisPlusConfig配置里面,我們把所有跟Mybatis有關(guān)的配置都放在這里 //通過(guò)這種方式,把mapper接口注入到容器里里面去 == @Mapper //最好是放到這里,配置就要放到配置類里面,不要放到啟動(dòng)類上面 public class MybatisPlusConfig { // 最新版 @Bean //把mybatisPlusInterceptor注入到springboot容器里面 public MybatisPlusInterceptor mybatisPlusInterceptor() { MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));//數(shù)據(jù)庫(kù)類型是mysql return interceptor; } }
只需要添加兩個(gè)地方
- @MapperScan("包的地址")
- Dbtype.數(shù)據(jù)庫(kù)名字
當(dāng)引入mybatisplus的配置文件之后
可以直接通過(guò)@MapperScan("包的地址")這種方式把所有的mapper接口全部注入到spring 容器里面
mybatis-plus依賴錯(cuò)誤: org.springframework.beans.factory.UnsatisfiedDependencyException
mybatis-plus報(bào)錯(cuò)
今天試了試springboot3
UnsatisfiedDependencyException: 不滿足依賴關(guān)系異常
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userServiceImpl': Unsatisfied dependency expressed through field 'baseMapper': No qualifying bean of type 'com.example.demo.mapper.UserMapper' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:716) ~[spring-beans-6.0.11.jar:6.0.11]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:696) ~[spring-beans-6.0.11.jar:6.0.11]
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:145) ~[spring-beans-6.0.11.jar:6.0.11]
at '此處省略一大堆'
我是第一次碰到, 然后去搜了幾篇文章, 說(shuō)什么的都有. 但是報(bào)錯(cuò)信息是一樣的, 解決辦法很簡(jiǎn)單:
只需要讓mybatis-plus和mybatis版本保持一致
問(wèn)題原因
mybatis-plus和mybatis依賴關(guān)系異常
解決辦法
版本號(hào)保持一致!!!
我個(gè)人非常喜歡的一個(gè)主題
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring延遲Bean初始化的實(shí)現(xiàn)示例
延遲初始化也叫做惰性初始化,指不提前初始化Bean,而是只有在真正使用時(shí)才創(chuàng)建及初始化Bean,本文主要介紹了Spring延遲Bean初始化的實(shí)現(xiàn)示例,感興趣的可以了解一下2024-06-06mybatis動(dòng)態(tài)插入list傳入List參數(shù)的實(shí)例代碼
本文通過(guò)實(shí)例代碼給大家介紹了mybatis動(dòng)態(tài)插入list,Mybatis 傳入List參數(shù)的方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧2018-04-04Spring使用ThreadPoolTaskExecutor自定義線程池及異步調(diào)用方式
這篇文章主要介紹了Spring使用ThreadPoolTaskExecutor自定義線程池及異步調(diào)用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02Java中多線程的ABA場(chǎng)景問(wèn)題分析
這篇文章主要為大家介紹了Java中多線程的ABA場(chǎng)景問(wèn)題分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12Spring Security獲取用戶認(rèn)證信息的實(shí)現(xiàn)流程
Spring Security是一個(gè)能夠?yàn)榛赟pring的企業(yè)應(yīng)用系統(tǒng)提供聲明式的安全訪問(wèn)控制解決方案的安全框架。它提供了一組可以在Spring應(yīng)用上下文中配置的Bean,充分利用了Spring IoC,DI和AOP功能,為應(yīng)用系統(tǒng)提供聲明式的安全訪問(wèn)控制功能2022-12-12JSP服務(wù)器端和前端出現(xiàn)亂碼問(wèn)題解決方案
這篇文章主要介紹了JSP服務(wù)器端和前端出現(xiàn)亂碼問(wèn)題解決方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02Java模擬多線程實(shí)現(xiàn)搶票代碼實(shí)例
這篇文章主要介紹了Java模擬多線程實(shí)現(xiàn)搶票,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-01-01