首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在maven中找不到声明吗?

在maven中找不到声明吗?
EN

Stack Overflow用户
提问于 2019-05-07 04:58:32
回答 2查看 761关注 0票数 0

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

代码语言:javascript
复制
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

项目目录

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

代码语言:javascript
复制
<mappers>
    <mapper resource="tk.mybatis.simple.mapper.CountryMapper.xml"/>
</mappers>

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

  1. 文件_使缓存无效/重新启动
  2. 选择目录,让目录作为资源根,等等。
  3. 在pom.xml中添加相关代码片段:
代码语言:javascript
复制
<resource>
  <directory>src/main/java</directory>
    <includes>
      <include>**/*.xml</include>
    </includes>
  </resource>
</resources>

相对码

CountryMapperTest.java

代码语言:javascript
复制
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

代码语言:javascript
复制
<?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

代码语言:javascript
复制
<?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>

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

更多细节

  • 艾德: IntelliJ IDEA,2019.1
  • 操作系统: macOS Mojave,10.14.3
EN

回答 2

Stack Overflow用户

发布于 2019-05-07 06:14:12

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

代码语言:javascript
复制
<mapper resource="tk/mybatis/simple/mapper/CountryMapper.xml"/>

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

票数 1
EN

Stack Overflow用户

发布于 2019-05-08 13:59:33

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

因为光学包命名是--在IntelliJ IDEA中是相同的,所以两者都是tk.mybatis.simple.mapper

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

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

  1. 文件\项目结构。
  2. 单击“模块”,“您的项目名称”,“源”
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56015793

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档