记录一次通用Mapper+自定义mapper出现的问题分析以及排查

首先看配置

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:JavaProjectstate_onestate_one-mapper	argetclassescomliaoyuanpingmapperCarouselMapper.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:JavaProjectstate_onestate_one-mapper	argetclassesmappersUsersMapper.xml]'; nested exception is org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. The XML location is 'file [F:JavaProjectstate_onestate_one-mapper	argetclassesmappersUsersMapper.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测试)

 

原文地址:https://www.cnblogs.com/liaoyuanping-24/p/14071494.html