首先看配置
1、這是pom文件依賴
<!-- mybatis配置 --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.0</version> </dependency> <!-- 通用mapper逆向工具 --> <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper-spring-boot-starter</artifactId> <version>2.1.5</version> </dependency>
2、這是yml文件的配置
##########
# mybatis 配置
##########
mybatis:
type-aliases-package: com.liaoyuanping.pojo # 所有POJO類所在包路徑
mapper-locations: classpath:mappers/*.xml # mapper映射文件
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
################
# mybatis mapper 配置
################
# 通用 Mapper 配置
mapper:
mappers: com.liaoyuanping.my.mapper.MyMapper
not-empty: false # 在進行數據庫操作的的時候,判斷表達式 username != null, 是否追加 username != ''
identity: MYSQL
3、啟動類添加相關掃描
@SpringBootApplication //掃描 mybatis 通用mapper 所在的包 @MapperScan(basePackages = "com.liaoyuanping.mapper") //掃描所有包以及相關組件包 @ComponentScan(basePackages = {"com.liaoyuanping", "org.n3r.idworker"}) public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
4、檢查先根據下面的問題一一排查
前提,寫完后端方法后,在自己測試接口方法的時候,提示Integer跟String轉換異常,經過排查發現是VO類的fatherId用錯了類型,改動以后,重新install,又Build以后,出現了下面無法獲取這個類下的方法,如下問題1,說明是配置出了問題,因為連方法測試都不正常
問題1:測試接口獲取父級下的二三級分類,出現異常 nvalid bound statement (not found)
提示找不到該方法而出現的BindException,同時在啟動日志看到了 Property'MapperLocations'was not specified
分析:排除上面的那些常見出現問題后,還是報該錯誤,一開始以為是Resources下的資源文件有問題,然后查看一些對應的配置文件,發現也沒有錯誤,就進行build以及
重新把這個Mapper接口以及對應的XML文件刪除重寫,然后發現還是不行,在target目錄下也發現有打包到對應的XML文件,然后查了查資料,分析會不會是XML打
包了,但是並沒有映射到對應的Maper接口呢,就修改為classpath:resources/mapper/*.xml,然后發現還是不行,又添加<Build>指引resources目錄下的文件添加進
install,發現還是報該錯誤,然后經過重復的打包,以及build,可能出現了新的一些變動,導致出現了新的錯誤(這個問題不知道如何出現的),如下問題2
問題2:install以后,運行的時候提示這個Controller類下的對應的一系列嵌套方法有問題,提示target目錄下的UsersMapper.xml是出現了
IllegalArgumentException錯誤,無法解析這個UsersMapper.xml文件
Error creating bean with name 'indexontroller': Unsatisfied dependency expressed through field 'carouselService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'carouselServiceImpl': Unsatisfied dependency expressed through field 'carouselMapper'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'carouselMapper' defined in file [F:\JavaProject\state_one\state_one-mapper\target\classes\com\liaoyuanping\mapper\CarouselMapper.class]: Unsatisfied dependency expressed through bean property 'sqlSessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [tk/mybatis/mapper/autoconfigure/MapperAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.ibatis.session.SqlSessionFactory]: Factory method 'sqlSessionFactory' threw exception; nested exception is org.springframework.core.NestedIOException: Failed to parse mapping resource: 'file [F:\JavaProject\state_one\state_one-mapper\target\classes\mappers\UsersMapper.xml]'; nested exception is org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. The XML location is 'file [F:\JavaProject\state_one\state_one-mapper\target\classes\mappers\UsersMapper.xml]'. Cause: java.lang.IllegalArgumentException: Result Maps collection already contains value for com.liaoyuanping.mapper.UsersMapper.BaseResultMap
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:596)
分析:因為是通用Mapper,而且直接使用的是逆向工程生成的POJO類跟mapper接口以及對應的Mapper.xml,而且在生成完以后的方法也是可以實現的,是沒有錯誤的。
首先排除生成的過程以及最終生成的數據類有錯誤這個方向。打開一看發現不知道什么時候多了一段ResultMap的配置,因為生成的時候是只有上面這一個ResultMap的
並且id 都是為 "BaseResultMap",所以我推測可能是之前的install或者build project的時候,通用mapper二次生成配置了一些東西,因為我根本沒改過這個XML的配置
解決:最后把下面這個不知道什么時候多出來的ResultMap刪除掉,重新install,然后重新運行,通用Mapper下的方法都可以正常執行,自定義Mapper接口的方法又重復出現了問題1那樣無法獲取該方法的類型,在很詫異之余,重新檢查相關配置,然后發現yml文件的路徑指定沒改回來,改回來以后執行正常
最后啟動正常,控制台也正常輸出,沒有報異常或者警告,測試自定義接口方法也正常返回數據(為了方便,直接使用swagger測試)