在maven中找不到要转到的声明? 当我运行测试类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> 我尝试了一些对其他人有用的解决方案: 文件|使缓存无效/重新启动 选择目录|将目录设为资源根目录等。。 在pom.xml中添加相关代码段: 相对码 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

在maven中找不到要转到的声明? 当我运行测试类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> 我尝试了一些对其他人有用的解决方案: 文件|使缓存无效/重新启动 选择目录|将目录设为资源根目录等。。 在pom.xml中添加相关代码段: 相对码 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,maven,intellij-idea,mybatis,Maven,Intellij Idea,Mybatis,映射器资源路径应以斜杠分隔 除了ave的解决方案之外,我们还需要检查包的命名和路径。 因为IntelliJ IDEA中的光学包命名相同,所以两者都是tk.mybatis.simple.mapper 实际上,正确的路径是tk/mybatis/simple/mapper,错误路径是tk.mybatis.simple.mapper 请按照以下方法进行检查: 文件|项目结构。。。 单击模块|您的项目名称|来源 如果使用斜杠“/”而不是点“.”,两者也可能导致相同的错误。@Sky能否将项目上载到GitHu

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

除了ave的解决方案之外,我们还需要检查包的命名和路径。 因为IntelliJ IDEA中的光学包命名相同,所以两者都是tk.mybatis.simple.mapper

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

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

文件|项目结构。。。 单击模块|您的项目名称|来源
如果使用斜杠“/”而不是点“.”,两者也可能导致相同的错误。@Sky能否将项目上载到GitHub或类似的服务?图片中的目录结构不清楚。谢谢!我已将修复程序作为PR发送。您将CountryMapper.xml放在名为tk.mybatis.simple.mapper的文件夹中。它应该位于嵌套文件夹中:src/main/resources/tk/mybatis/simple/mapper。
<?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>