您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關mybatis-plus配置找不到Mapper接口路徑怎么解決,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
特此記錄一下,問題如下:
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 目錄
代碼如下:
單元測試
@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實現
@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接口中的方法和映射文件中的方法名稱是一樣的。不存在名稱錯誤導致的情況,返回值,參數類型等你都正確。如果找不到方法,那一定是映射文件配置問題,只有沒有讀取到,才會出現找不到的情況。
我的配置如下:
問題出錯的關鍵位置
我這里引用的是 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映射文件。
經過查閱:
如下:
引用 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
只要選用其中一種方式,就沒有問題了。
今天看到評論區有小伙伴說沒有作用,這里給了一份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接口路徑怎么解決,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。