最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解mybatis-plus的 mapper.xml 路徑配置的坑

 更新時間:2020年08月25日 14:23:21   作者:一只倔強的碼蟻  
這篇文章主要介紹了詳解mybatis-plus的 mapper.xml 路徑配置的坑,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

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

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

org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.husy.mapper.SystemUserMapper.findUserByName

	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)

錯誤代碼如下:

mapper.xml 目錄


代碼如下:

單元測試

@Test
public void findUser(){
	SystemUser systemUser= systemUserService.findUserByName("admin");
	System.out.println(systemUser.toString());
}

mybatis-puls 配置

@EnableTransactionManagement
@Configuration
@MapperScan("com.husy.mapper")
public class MybatisConfig {

  /**
   * mybatis-plus 分頁插件
   */
  @Bean
  public PaginationInterceptor paginationInterceptor() {
    return new PaginationInterceptor();
  }
}

service實現(xiàn)

@Service
public class SystemUserServiceImpl implements SystemUserService {
	@Autowired
	private SystemUserMapper userMapper;

	@Override
	public SystemUser findUserByName(String name) {
		return userMapper.findUserByName(name);
	}
}

mapper 接口

@Component
public interface SystemUserMapper {
	SystemUser findUserByName(@Param("userAccount") String name);
}

mapper.xml

<?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接口中的方法和映射文件中的方法名稱是一樣的。不存在名稱錯誤導致的情況,返回值,參數(shù)類型等你都正確。如果找不到方法,那一定是映射文件配置問題,只有沒有讀取到,才會出現(xiàn)找不到的情況。
我的配置如下:

問題出錯的關鍵位置

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

<dependency>
  <groupId>com.baomidou</groupId>
   <artifactId>mybatis-plus-boot-starter</artifactId>
   <version>3.1.0</version>
 </dependency>

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

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

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

經(jīng)過查閱:

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

如下:

引用 mybatis-plus 包

<dependency>
  <groupId>com.baomidou</groupId>
  <artifactId>mybatis-plus</artifactId>
  <version>3.1.0</version>
</dependency>
mybatis.mapper-locations=classpath:mapper/*.xml

引用 mybatis-plus-boot-starter 包

<dependency>
   <groupId>com.baomidou</groupId>
   <artifactId>mybatis-plus-boot-starter</artifactId>
   <version>3.1.0</version>
 </dependency>
mybatis-plus.mapper-locations=classpath:mapper/*.xml

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

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

POM

<?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

# 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

@EnableTransactionManagement
@Configuration
@MapperScan("com.example.demo.mapper")
public class MybatisConfig {
	/**mybatis-plus 分頁插件*/
	@Bean
	public PaginationInterceptor paginationInterceptor() {
		return new PaginationInterceptor();
	}
}

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

單元測試

@SpringBootTest
class DemoApplicationTests {
	@Autowired
	SystemUserService systemUserService;
	@Test
	public void findUser(){
		SystemUser systemUser= systemUserService.findUserByName("admin");
		System.out.println(systemUser.toString());
	}

}

到此這篇關于詳解mybatis-plus的 mapper.xml 路徑配置的坑的文章就介紹到這了,更多相關mybatis-plus mapper.xml路徑配置內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • SpringBoot基于redis自定義注解實現(xiàn)后端接口防重復提交校驗

    SpringBoot基于redis自定義注解實現(xiàn)后端接口防重復提交校驗

    本文主要介紹了SpringBoot基于redis自定義注解實現(xiàn)后端接口防重復提交校驗,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • Spring的@Autowired加到接口上但獲取的是實現(xiàn)類的問題

    Spring的@Autowired加到接口上但獲取的是實現(xiàn)類的問題

    這篇文章主要介紹了Spring的@Autowired加到接口上但獲取的是實現(xiàn)類的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-10-10
  • 詳解java封裝繼承多態(tài)

    詳解java封裝繼承多態(tài)

    這篇文章主要介紹了java封裝繼承多態(tài),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-03-03
  • 教你一步解決java.io.FileNotFoundException:找不到文件異常

    教你一步解決java.io.FileNotFoundException:找不到文件異常

    這篇文章主要給大家介紹了關于如何一步解決java.io.FileNotFoundException:找不到文件異常的相關資料,文中通過圖文以及代碼介紹的非常詳細,需要的朋友可以參考下
    2024-01-01
  • Java基礎之數(shù)組的初始化過程

    Java基礎之數(shù)組的初始化過程

    Java數(shù)組分為靜態(tài)和動態(tài)初始化,靜態(tài)初始化中,程序員設定元素初始值,系統(tǒng)決定長度;動態(tài)初始化中,程序員設定長度,系統(tǒng)提供初始值,數(shù)組初始化后長度固定,存儲在堆內(nèi)存中,數(shù)組變量在棧內(nèi)存指向堆內(nèi)存數(shù)組對象,基本類型數(shù)組存儲數(shù)據(jù)值,引用類型數(shù)組存儲對象引用
    2024-10-10
  • 深入解析反編譯字節(jié)碼文件中的代碼邏輯JVM中的String操作

    深入解析反編譯字節(jié)碼文件中的代碼邏輯JVM中的String操作

    這篇文章主要介紹了深入解析反編譯字節(jié)碼文件中的代碼邏輯JVM中的String操作,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-10-10
  • springSecurity之AuthenticationProvider用法解析

    springSecurity之AuthenticationProvider用法解析

    這篇文章主要介紹了springSecurity之AuthenticationProvider用法解析,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • springboot整合logback打印日志,分文件

    springboot整合logback打印日志,分文件

    本文主要介紹了springboot整合logback打印日志,分文件,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-11-11
  • springboot中@ConfigurationProperties無效果的解決方法

    springboot中@ConfigurationProperties無效果的解決方法

    本文主要介紹了springboot中@ConfigurationProperties無效果,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2024-06-06
  • Java 如何同時返回多個不同類型

    Java 如何同時返回多個不同類型

    這篇文章主要介紹了Java 同時返回多個不同類型的方法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-08-08

最新評論

正阳县| 克拉玛依市| 古浪县| 调兵山市| 龙游县| 德格县| 蚌埠市| 宜兰县| 乌什县| 古浪县| 景宁| 阳城县| 宝清县| 齐河县| 新巴尔虎左旗| 彝良县| 成安县| 本溪| 肥城市| 会宁县| 图片| 宁武县| 铁力市| 阿巴嘎旗| 平和县| 晋州市| 大庆市| 中江县| 南康市| 乌鲁木齐县| 武城县| 奎屯市| 凌海市| 洱源县| 成都市| 株洲县| 河西区| 襄汾县| 连云港市| 兴安县| 大理市|