简体   繁体   English

在Maven中找不到要去的声明?

[英]Cannot find declaration to go to in maven?

When I ran the Test Class(CountryMapperTest.java), the error has been occurred. 当我运行测试类(CountryMapperTest.java)时,发生了错误。 The following is error information. 以下是错误信息。

org.apache.ibatis.exceptions.PersistenceException: 
### Error building SqlSession.
### The error may exist in tk.mybatis.simple.mapper.CountryMapper.xml
### Cause: org.apache.ibatis.builder.BuilderException:
    Error parsing SQL Mapper Configuration. 
    Cause: java.io.IOException: 
    Could not find resource tk.mybatis.simple.mapper.CountryMapper.xml

Project Directory 项目目录

项目目录

By analysing the error messages, I think that the bug comes from the following statement in the mybatis-config.xml file. 通过分析错误消息,我认为该错误来自mybatis-config.xml文件中的以下语句。

<mappers>
    <mapper resource="tk.mybatis.simple.mapper.CountryMapper.xml"/>
</mappers>

I have tried some solutions that work for others: 我尝试了一些对其他人有用的解决方案:

  1. File | 档案| Invalidate Caches / Restart 无效的缓存/重启
  2. Select the Directory | 选择目录| Make Directory as Resources Root, etc.. 将目录设为资源根等。
  3. Add the relative code snippet in the pom.xml: 在pom.xml中添加相对代码片段:
<resource>
  <directory>src/main/java</directory>
    <includes>
      <include>**/*.xml</include>
    </includes>
  </resource>
</resources>

Relative code 相对码

CountryMapperTest.java CountryMapperTest.java
<?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="tk.mybatis.simple.mapper.CountryMapper">
  <select id="selectAll" resultType="Country">
    select id,countryname,countrycode from country
  </select>
</mapper>
mybatis-config.xml MyBatis的-config.xml中
 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <settings> <setting name="logImpl" value="LOG4J"/> </settings> <typeAliases> <package name="tk.mybatis.simple.model"/> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"> <property name="" value=""/> </transactionManager> <dataSource type="UNPOOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/mybatis"/> <property name="username" value="root"/> <property name="password" value="12345"/> </dataSource> </environment> </environments> <mappers> <mapper resource="tk.mybatis.simple.mapper.CountryMapper.xml"/> </mappers> </configuration> 
CountryMapper.xml CountryMapper.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="tk.mybatis.simple.mapper.CountryMapper"> <select id="selectAll" resultType="Country"> select id,countryname,countrycode from country </select> </mapper> 

I expect to query the database and show data in console. 我希望查询数据库并在控制台中显示数据。

More Details 更多细节

  • IDE: IntelliJ IDEA, 2019.1 IDE:IntelliJ IDEA,2019.1
  • OS: macOS Mojave, 10.14.3 作业系统:macOS Mojave,10.14.3

The mapper resource path should be separated by slash. 映射器资源路径应以斜杠分隔。

<mapper resource="tk/mybatis/simple/mapper/CountryMapper.xml"/>

http://www.mybatis.org/mybatis-3/configuration.html#mappers http://www.mybatis.org/mybatis-3/configuration.html#mappers

In addition to the ave's solution, we also need to check the package naming and path. 除了ave的解决方案,我们还需要检查包的命名和路径。

Because the optical package naming is the same in IntelliJ IDEA, the both are the tk.mybatis.simple.mapper . 因为IntelliJ IDEA中的光学包装命名是相同的 ,所以两者都是tk.mybatis.simple.mapper

Actually, correct path is tk/mybatis/simple/mapper , error path is tk.mybatis.simple.mapper . 实际上,正确的路径是tk/mybatis/simple/mapper ,错误路径是tk.mybatis.simple.mapper

Please follow the below method to check it: 请按照以下方法进行检查:

  1. File | 档案| Project Structure... 项目结构...
  2. click Modules | 单击模块| Your Project Name | 您的项目名称| Sources 来源

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM