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

时间:2019-05-07 04:58:33

标签: maven intellij-idea mybatis

当我运行测试类(CountryMapperTest.java)时,发生了错误。以下是错误信息。

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

通过分析错误消息,我认为该错误来自mybatis-config.xml文件中的以下语句。

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

我尝试了一些对其他人有用的解决方案:

  1. 文件|使缓存无效/重新启动
  2. 选择目录|将目录设为资源根等。
  3. 在pom.xml中添加相对代码段:
<resource>
  <directory>src/main/java</directory>
    <includes>
      <include>**/*.xml</include>
    </includes>
  </resource>
</resources>

相对代码

CountryMapperTest.java
package tk.mybatis.simple.mapper;

import java.io.IOException;
import java.io.Reader;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.BeforeClass;
import org.junit.Test;

import tk.mybatis.simple.model.Country;

public class CountryMapperTest {

    private static SqlSessionFactory sqlSessionFactory;

    @BeforeClass
    public static void init() {
        try {
            Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
            System.out.println("Test1");
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
            System.out.println("Test2");

            reader.close();
        } catch (IOException ignore) {
            ignore.printStackTrace();
        }
    }

    @Test
    public void testSelectAll() {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        try {
            List<Country> countryList = sqlSession.selectList("selectAll");
            printCountryList(countryList);
        } finally {
            sqlSession.close();
        }
    }

    private void printCountryList(List<Country> countryList) {
        for (Country country : countryList) {
            System.out.printf("%-4d%4s%4s\n", country.getId(), country.getCountryname(), country.getCountrycode());
        }
    }
}
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
<?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>

我希望查询数据库并在控制台中显示数据。

更多详细信息

  • IDE:IntelliJ IDEA,2019.1
  • OS:macOS Mojave,10.14.3

2 个答案:

答案 0 :(得分:1)

映射器资源路径应以斜杠分隔。

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

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

答案 1 :(得分:0)

除了ave的解决方案外,我们还需要检查软件包的命名和路径。

由于IntelliJ IDEA中的 optical 包命名与相同相同,因此两者均为tk.mybatis.simple.mapper

实际上,正确的路径是tk/mybatis/simple/mapper,错误的路径是tk.mybatis.simple.mapper

请按照以下方法进行检查:

  1. 文件|项目结构...
  2. 单击模块|您的项目名称|来源