一:概述
Mybatis的使用简化了普通JDBC代码频繁和繁琐的操作,但在使用过程中难免会出现错误,在这里我将说明我看视频学习遇到的一个问题,对它进行分析和解决。
二:具体说明
<1>问题的详细说明
控制台出现的错误
2024-08-18 09:44:55.381 [DEBUG] org.apache.ibatis.logging.LogFactory [main] : Logging initialized using 'class org.apache.ibatis.logging.slf4j.Slf4jImpl' adapter.
2024-08-18 09:44:55.384 [DEBUG] org.apache.ibatis.io.VFS [main] : Class not found: org.jboss.vfs.VFS
2024-08-18 09:44:55.384 [DEBUG] org.apache.ibatis.io.JBoss6VFS [main] : JBoss 6 VFS API is not available in this environment.
2024-08-18 09:44:55.384 [DEBUG] org.apache.ibatis.io.VFS [main] : Class not found: org.jboss.vfs.VirtualFile
2024-08-18 09:44:55.385 [DEBUG] org.apache.ibatis.io.VFS [main] : VFS implementation org.apache.ibatis.io.JBoss6VFS is not valid in this environment.
2024-08-18 09:44:55.385 [DEBUG] org.apache.ibatis.io.VFS [main] : Using VFS adapter org.apache.ibatis.io.DefaultVFS
2024-08-18 09:44:55.397 [DEBUG] org.apache.ibatis.datasource.pooled.PooledDataSource [main] : PooledDataSource forcefully closed/removed all connections.
2024-08-18 09:44:55.398 [DEBUG] org.apache.ibatis.datasource.pooled.PooledDataSource [main] : PooledDataSource forcefully closed/removed all connections.
2024-08-18 09:44:55.398 [DEBUG] org.apache.ibatis.datasource.pooled.PooledDataSource [main] : PooledDataSource forcefully closed/removed all connections.
2024-08-18 09:44:55.398 [DEBUG] org.apache.ibatis.datasource.pooled.PooledDataSource [main] : PooledDataSource forcefully closed/removed all connections.
2024-08-18 09:44:55.400 [DEBUG] org.apache.ibatis.io.DefaultVFS [main] : Find JAR URL: file:/E:/Code/Java/mybatis2/target/classes/club/zhuyue/dao
2024-08-18 09:44:55.400 [DEBUG] org.apache.ibatis.io.DefaultVFS [main] : Not a JAR: file:/E:/Code/Java/mybatis2/target/classes/club/zhuyue/dao
2024-08-18 09:44:55.421 [DEBUG] org.apache.ibatis.io.DefaultVFS [main] : Reader entry: OrderMapper.class
2024-08-18 09:44:55.421 [DEBUG] org.apache.ibatis.io.DefaultVFS [main] : Reader entry: OrderMapper.xml
2024-08-18 09:44:55.421 [DEBUG] org.apache.ibatis.io.DefaultVFS [main] : Reader entry: UserMapper.class
2024-08-18 09:44:55.422 [DEBUG] org.apache.ibatis.io.DefaultVFS [main] : Reader entry: UserMapper.xml
2024-08-18 09:44:55.422 [DEBUG] org.apache.ibatis.io.DefaultVFS [main] : Listing file:/E:/Code/Java/mybatis2/target/classes/club/zhuyue/dao
2024-08-18 09:44:55.422 [DEBUG] org.apache.ibatis.io.DefaultVFS [main] : Find JAR URL: file:/E:/Code/Java/mybatis2/target/classes/club/zhuyue/dao/OrderMapper.class
2024-08-18 09:44:55.422 [DEBUG] org.apache.ibatis.io.DefaultVFS [main] : Not a JAR: file:/E:/Code/Java/mybatis2/target/classes/club/zhuyue/dao/OrderMapper.class
2024-08-18 09:44:55.422 [DEBUG] org.apache.ibatis.io.DefaultVFS [main] : Reader entry: ���� 4 findOrderByNumber ,(Ljava/lang/String;)Lclub/zhuyue/pojo/Order;
2024-08-18 09:44:55.423 [DEBUG] org.apache.ibatis.io.DefaultVFS [main] : Find JAR URL: file:/E:/Code/Java/mybatis2/target/classes/club/zhuyue/dao/OrderMapper.xml
2024-08-18 09:44:55.423 [DEBUG] org.apache.ibatis.io.DefaultVFS [main] : Not a JAR: file:/E:/Code/Java/mybatis2/target/classes/club/zhuyue/dao/OrderMapper.xml
2024-08-18 09:44:55.423 [DEBUG] org.apache.ibatis.io.DefaultVFS [main] : Reader entry: <?xml version="1.0" encoding="UTF-8" ?>
2024-08-18 09:44:55.423 [DEBUG] org.apache.ibatis.io.DefaultVFS [main] : Find JAR URL: file:/E:/Code/Java/mybatis2/target/classes/club/zhuyue/dao/UserMapper.class
2024-08-18 09:44:55.423 [DEBUG] org.apache.ibatis.io.DefaultVFS [main] : Not a JAR: file:/E:/Code/Java/mybatis2/target/classes/club/zhuyue/dao/UserMapper.class
2024-08-18 09:44:55.424 [DEBUG] org.apache.ibatis.io.DefaultVFS [main] : Reader entry: ���� 4 findUserById ,(Ljava/lang/Integer;)Lclub/zhuyue/pojo/User;
2024-08-18 09:44:55.424 [DEBUG] org.apache.ibatis.io.DefaultVFS [main] : Find JAR URL: file:/E:/Code/Java/mybatis2/target/classes/club/zhuyue/dao/UserMapper.xml
2024-08-18 09:44:55.424 [DEBUG] org.apache.ibatis.io.DefaultVFS [main] : Not a JAR: file:/E:/Code/Java/mybatis2/target/classes/club/zhuyue/dao/UserMapper.xml
2024-08-18 09:44:55.424 [DEBUG] org.apache.ibatis.io.DefaultVFS [main] : Reader entry: <?xml version="1.0" encoding="UTF-8" ?>
2024-08-18 09:44:55.425 [DEBUG] org.apache.ibatis.io.ResolverUtil [main] : Checking to see if class club.zhuyue.dao.OrderMapper matches criteria [is assignable to Object]
2024-08-18 09:44:55.425 [DEBUG] org.apache.ibatis.io.ResolverUtil [main] : Checking to see if class club.zhuyue.dao.UserMapper matches criteria [is assignable to Object]
java.lang.ExceptionInInitializerError
at club.zhuyue.test.OrderMapperTest.testFindOrderByNumber(OrderMapperTest.java:20)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
at com.intellij.rt.junit.IdeaTestRunner$Repeater$1.execute(IdeaTestRunner.java:38)
at com.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeater.java:11)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:232)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:55)
Caused by: org.apache.ibatis.exceptions.PersistenceException:
### Error building SqlSession.
### The error may exist in club/zhuyue/dao/OrderMapper.xml
### The error occurred while processing mapper_resultMap[orderMap]
### Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. The XML location is 'club/zhuyue/dao/OrderMapper.xml'. Cause: org.apache.ibatis.builder.BuilderException: Error resolving class. Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias 'Order'. Cause: java.lang.ClassNotFoundException: Cannot find class: Order
at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:82)
at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:66)
at club.zhuyue.utils.MybatisUtil.<clinit>(MybatisUtil.java:31)
... 25 more
Caused by: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. The XML location is 'club/zhuyue/dao/OrderMapper.xml'. Cause: org.apache.ibatis.builder.BuilderException: Error resolving class. Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias 'Order'. Cause: java.lang.ClassNotFoundException: Cannot find class: Order
at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:133)
at org.apache.ibatis.builder.xml.XMLConfigBuilder.parse(XMLConfigBuilder.java:110)
at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:80)
... 27 more
Caused by: org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. The XML location is 'club/zhuyue/dao/OrderMapper.xml'. Cause: org.apache.ibatis.builder.BuilderException: Error resolving class. Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias 'Order'. Cause: java.lang.ClassNotFoundException: Cannot find class: Order
at org.apache.ibatis.builder.xml.XMLMapperBuilder.configurationElement(XMLMapperBuilder.java:125)
at org.apache.ibatis.builder.xml.XMLMapperBuilder.parse(XMLMapperBuilder.java:98)
at org.apache.ibatis.builder.annotation.MapperAnnotationBuilder.loadXmlResource(MapperAnnotationBuilder.java:164)
at org.apache.ibatis.builder.annotation.MapperAnnotationBuilder.parse(MapperAnnotationBuilder.java:117)
at org.apache.ibatis.binding.MapperRegistry.addMapper(MapperRegistry.java:72)
at org.apache.ibatis.binding.MapperRegistry.addMappers(MapperRegistry.java:108)
at org.apache.ibatis.binding.MapperRegistry.addMappers(MapperRegistry.java:121)
at org.apache.ibatis.session.Configuration.addMappers(Configuration.java:932)
at org.apache.ibatis.builder.xml.XMLConfigBuilder.mappersElement(XMLConfigBuilder.java:396)
at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:131)
... 29 more
Caused by: org.apache.ibatis.builder.BuilderException: Error resolving class. Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias 'Order'. Cause: java.lang.ClassNotFoundException: Cannot find class: Order
at org.apache.ibatis.builder.BaseBuilder.resolveClass(BaseBuilder.java:103)
at org.apache.ibatis.builder.xml.XMLMapperBuilder.resultMapElement(XMLMapperBuilder.java:222)
at org.apache.ibatis.builder.xml.XMLMapperBuilder.resultMapElement(XMLMapperBuilder.java:214)
at org.apache.ibatis.builder.xml.XMLMapperBuilder.resultMapElements(XMLMapperBuilder.java:206)
at org.apache.ibatis.builder.xml.XMLMapperBuilder.configurationElement(XMLMapperBuilder.java:121)
... 38 more
Caused by: org.apache.ibatis.type.TypeException: Could not resolve type alias 'Order'. Cause: java.lang.ClassNotFoundException: Cannot find class: Order
at org.apache.ibatis.type.TypeAliasRegistry.resolveAlias(TypeAliasRegistry.java:128)
at org.apache.ibatis.builder.BaseBuilder.resolveAlias(BaseBuilder.java:132)
at org.apache.ibatis.builder.BaseBuilder.resolveClass(BaseBuilder.java:101)
... 42 more
Caused by: java.lang.ClassNotFoundException: Cannot find class: Order
at org.apache.ibatis.io.ClassLoaderWrapper.classForName(ClassLoaderWrapper.java:226)
at org.apache.ibatis.io.ClassLoaderWrapper.classForName(ClassLoaderWrapper.java:103)
at org.apache.ibatis.io.Resources.classForName(Resources.java:322)
at org.apache.ibatis.type.TypeAliasRegistry.resolveAlias(TypeAliasRegistry.java:124)
... 44 more
<2>主要存在的问题
java.lang.ClassNotFoundException: Cannot find class: Order`。这表示 MyBatis 在尝试解析类型别名 `'Order'` 时未能找到相应的类。
<3>详细的解决步骤
这里有几个可能的解决方案:
- 确保类路径正确:
- 确保你的
Order
类已经编译并且在类路径中。检查是否在正确的包中。
- 检查 MyBatis 配置:
- 在 MyBatis 的配置文件或 XML 映射文件中,确保类型别名的定义是正确的。例如,通常需要在 MyBatis 配置文件 (如
mybatis-config.xml
中定义(order)类的别名
Order
类的别名:
<typeAliases>
<typeAlias alias="Order" type="com.yourpackage.Order"/>
</typeAliases>
- 如果已经定义,检查类的全名是否正确。
- 检查 XML 映射文件:
- 在
OrderMapper.xml
中,确认使用的类型别名与类的实际路径一致。例如,如果你在 XML 中使用了<resultMap>
,确保type
属性正确指向Order
类。
- 编译和构建问题:
- 如果你在使用 IDE(如 IntelliJ IDEA 或 Eclipse),确保项目已经成功构建并且没有编译错误。如果需要,尝试清理并重新构建项目。
- 检查依赖:
- 如果
Order
类在某个外部依赖中,确保该依赖已被正确添加并且已下载。
- IDE 的模块设置:
- 如果你在使用模块化项目,确保
Order
类所在的模块已正确引用。
如果采取以上的步骤还未解决问题,就应该一步步进行排除。