<li id="jut4n"></li>
  • 系統城裝機大師 - 唯一官網:www.ship-models.net!

    當前位置:首頁 > 網絡編程 > JavaScript > 詳細頁面

    詳解mybatis-plus配置找不到Mapper接口路徑的坑

    時間:2020-10-19來源:www.ship-models.net作者:電腦系統城

    mybatis-plus今天遇到一個問題,就是mybatis 沒有讀取到mapper.xml 文件。

    特此記錄一下,問題如下:

    at com.baomidou.mybatisplus.core.override.MybatisMapperMethod$SqlCommand.<init>(MybatisMapperMethod.java:242)
    at com.baomidou.mybatisplus.core.override.MybatisMapperMethod.<init>(MybatisMapperMethod.java:54)
    at com.baomidou.mybatisplus.core.override.MybatisMapperProxy.lambda$cachedMapperMethod$0(MybatisMapperProxy.java:65)
    at java.util.concurrent.ConcurrentHashMap.computeIfAbsent(ConcurrentHashMap.java:1660)
    at com.baomidou.mybatisplus.core.override.MybatisMapperProxy.cachedMapperMethod(MybatisMapperProxy.java:65)
    at com.baomidou.mybatisplus.core.override.MybatisMapperProxy.invoke(MybatisMapperProxy.java:60)
    at com.sun.proxy.$Proxy72.findUserByName(Unknown Source)
    at com.husy.service.impl.SystemUserServiceImpl.findUserByName(SystemUserServiceImpl.java:23)org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.husy.mapper.SystemUserMapper.findUserByName

    錯誤代碼如下:

    mapper.xml 目錄

    在這里插入圖片描述

    代碼如下:

    單元測試

    ?
    1
    2
    3
    4
    5
    @Test
    public void findUser(){
        SystemUser systemUser= systemUserService.findUserByName("admin");
        System.out.println(systemUser.toString());
    }

    mybatis-puls 配置

    ?
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    @EnableTransactionManagement
    @Configuration
    @MapperScan("com.husy.mapper")
    public class MybatisConfig {
     
      /**
       * mybatis-plus 分頁插件
       */
      @Bean
      public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
      }
    }

    service實現

    ?
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    @Service
    public class SystemUserServiceImpl implements SystemUserService {
        @Autowired
        private SystemUserMapper userMapper;
     
        @Override
        public SystemUser findUserByName(String name) {
            return userMapper.findUserByName(name);
        }
    }

    mapper 接口

    ?
    1
    2
    3
    4
    @Component
    public interface SystemUserMapper {
        SystemUser findUserByName(@Param("userAccount") String name);
    }

    mapper.xml

    ?
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="com.husy.mapper.SystemUserMapper">
      <resultMap id="systemUserMap" type="com.husy.domain.SystemUser" >
        <id column="user_id" property="userId" />
        <result column="user_account" property="userAccount" />
        <result column="user_password" property="userPassword" />
        <result column="user_phone" property="userPhone" />
      </resultMap>
      <select id="findUserByName" resultMap="systemUserMap">
         SELECT
          user_id,
          user_account,
          user_password,
          user_phone
         FROM t_system_user
         where user_account = #{userAccount}
      </select>
    </mapper>

    通過上面的代碼可以看出。mapper接口中的方法和映射文件中的方法名稱是一樣的。不存在名稱錯誤導致的情況,返回值,參數類型等你都正確。如果找不到方法,那一定是映射文件配置問題,只有沒有讀取到,才會出現找不到的情況。
    我的配置如下:

    問題出錯的關鍵位置

    我這里引用的是 mybatis-plus-boot-starte 依賴

    ?
    1
    2
    3
    4
    5
    <dependency>
      <groupId>com.baomidou</groupId>
       <artifactId>mybatis-plus-boot-starter</artifactId>
       <version>3.1.0</version>
     </dependency>

    mapper.xml 的文件引用路徑配置如下:

    ?
    1 mybatis.mapper-locations=classpath:mapper/*.xml

    這就導致,mybatis 讀取不到 mapper映射文件。

    經過查閱:

    • 如果引用mybatis-plus-boot-starter 依賴,需要配置 mybatis-plus.mapper-locations
    • 如果引用mybatis-plus 依賴,需要配置 mybatis.mapper-locations

    如下:

    引用 mybatis-plus 包

    ?
    1
    2
    3
    4
    5
    <dependency>
      <groupId>com.baomidou</groupId>
      <artifactId>mybatis-plus</artifactId>
      <version>3.1.0</version>
    </dependency>


    ?
    1 mybatis.mapper-locations=classpath:mapper/*.xml

    引用 mybatis-plus-boot-starter 包

    ?
    1
    2
    3
    4
    5
    <dependency>
       <groupId>com.baomidou</groupId>
       <artifactId>mybatis-plus-boot-starter</artifactId>
       <version>3.1.0</version>
     </dependency>


    ?
    1 mybatis-plus.mapper-locations=classpath:mapper/*.xml

    只要選用其中一種方式,就沒有問題了。

    今天看到評論區有小伙伴說沒有作用,這里給了一份Demo ,有問題的小伙伴可以比對一下。

    POM

    ?
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
      </parent>
      <groupId>com.example</groupId>
      <artifactId>demo</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <name>demo</name>
      <description>Demo project for Spring Boot</description>
     
      <properties>
        <java.version>1.8</java.version>
      </properties>
     
      <dependencies>
        <dependency>
          <groupId>org.mybatis.spring.boot</groupId>
          <artifactId>mybatis-spring-boot-starter</artifactId>
          <version>2.1.1</version>
        </dependency>
     
        <dependency>
          <groupId>mysql</groupId>
          <artifactId>mysql-connector-java</artifactId>
          <scope>runtime</scope>
        </dependency>
        <dependency>
          <groupId>com.baomidou</groupId>
          <artifactId>mybatis-plus-boot-starter</artifactId>
          <version>3.3.0</version>
        </dependency>
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-test</artifactId>
          <scope>test</scope>
          <exclusions>
            <exclusion>
              <groupId>org.junit.vintage</groupId>
              <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
          </exclusions>
        </dependency>
      </dependencies>
     
      <build>
        <plugins>
          <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
          </plugin>
        </plugins>
      </build>
     
    </project>

    application.properties

    ?
    1
    2
    3
    4
    5
    6
    7
    # DataSource Config
    spring.datasource.url=jdbc:mysql://localhost:3306/db_husy?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=GMT%2B8&zeroDateTimeBehavior=CONVERT_TO_NULL
    spring.datasource.username=root
    spring.datasource.password=123456
    spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
     
    mybatis-plus.mapper-locations=classpath:/mapper/*.xml

    MybatisConfig

    ?
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    @EnableTransactionManagement
    @Configuration
    @MapperScan("com.example.demo.mapper")
    public class MybatisConfig {
        /**mybatis-plus 分頁插件*/
        @Bean
        public PaginationInterceptor paginationInterceptor() {
            return new PaginationInterceptor();
        }
    }

    其他文件基本沒變化。附上目錄

    在這里插入圖片描述

    單元測試

    ?
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    @SpringBootTest
    class DemoApplicationTests {
        @Autowired
        SystemUserService systemUserService;
        @Test
        public void findUser(){
            SystemUser systemUser= systemUserService.findUserByName("admin");
            System.out.println(systemUser.toString());
        }
     
    }

    在這里插入圖片描述

    到此這篇關于詳解mybatis-plus配置找不到Mapper接口路徑的坑的文章就介紹到這了

    分享到:

    相關信息

    系統教程欄目

    欄目熱門教程

    人氣教程排行

    站長推薦

    熱門系統下載

    淑芬两腿间又痒了