spring-MyBatis练习报错 求大神指导啊啊啊啊?

有的部分用直接用了老师的成品,刚开始练习一对一就报错了,在attach source里加上了mybatis的zip还是不对,UserMapper.xml里的sql语句下的username显示拼写而错误,但是可以运行,不知道怎么回事,刚开始学,求大神指导啊,非常感谢!!!

OrdersMapper

public interface OrdersMapper 
public List<OrdersExt> findOrdersAndUser();

}

OrdersMapper.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">
<!-- namespace:命名空间,对statement的信息进行分类管理 -->
<!-- 注意:在mapper代理时,它具有特殊及重要的作用 -->
<mapper namespace="com.zzr.mybatis.mapper.OrdersMapper">
    <!--一对一映射语句 -->
    <select id="findOrdersAndUser" resultType="OrdersExt">
       select
       orders.id,
       orders.user_id,
       orders.number,
       user.username,
       user.sex
       from
       orders,user
       where orders.user_id = user.id;
    </select>
</mapper>

SqlSessionFactoryUtil

public class SqlSessionFactoryUtil {
    private static SqlSessionFactory sessionFactory;

    static {
        try {
            // 加载全局配置文件
            String resource = "mybatis-config.xml";
            InputStream is = Resources.getResourceAsStream(resource);
            // 创建sessionFactory
            sessionFactory = new SqlSessionFactoryBuilder().build(is);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    // 单例模式私有构造
    private SqlSessionFactoryUtil() {
    }
    // 单例模式获取sessionFactory
    public static SqlSessionFactory createSessionFactory() {
        return sessionFactory;
    }
}

Test

public class OrdersMapperTest {
    private SqlSessionFactory sessionFactory;

    @Before
    public void setup(){
        sessionFactory = SqlSessionFactoryUtil.createSessionFactory();
    }

    @Test
    public void testFindOrdersAndUser(){
        //创建session
        SqlSession session = this.sessionFactory.openSession();

        //使用mybatis创建mapper接口的代理对象
        OrdersMapper ordersMapper = session.getMapper(OrdersMapper.class);

        //查询订单信息,关联查询用户信息
        List<OrdersExt>ordersExts = ordersMapper.findOrdersAndUser();
        System.out.println(ordersExts);
    }

}

报错信息

DEBUG LogFactory:135 - Logging initialized using 'class org.apache.ibatis.logging.slf4j.Slf4jImpl' adapter.
DEBUG VFS:109 - Class not found: org.jboss.vfs.VFS
DEBUG JBoss6VFS:142 - JBoss 6 VFS API is not available in this environment.
DEBUG VFS:109 - Class not found: org.jboss.vfs.VirtualFile
DEBUG VFS:70 - VFS implementation org.apache.ibatis.io.JBoss6VFS is not valid in this environment.
DEBUG VFS:84 - Using VFS adapter org.apache.ibatis.io.DefaultVFS
DEBUG DefaultVFS:222 - Find JAR URL: file:/D:/java_se/zzr/MyBatis002/build/classes/com/zzr/mybatis/po
DEBUG DefaultVFS:248 - Not a JAR: file:/D:/java_se/zzr/MyBatis002/build/classes/com/zzr/mybatis/po
DEBUG DefaultVFS:102 - Reader entry: Items.class
DEBUG DefaultVFS:102 - Reader entry: Orderdetail.class
DEBUG DefaultVFS:102 - Reader entry: Orders.class
DEBUG DefaultVFS:102 - Reader entry: OrdersExt.class
DEBUG DefaultVFS:102 - Reader entry: User.class
DEBUG DefaultVFS:102 - Reader entry: UserQueryVO.class
DEBUG DefaultVFS:113 - Listing file:/D:/java_se/zzr/MyBatis002/build/classes/com/zzr/mybatis/po
DEBUG DefaultVFS:222 - Find JAR URL: file:/D:/java_se/zzr/MyBatis002/build/classes/com/zzr/mybatis/po/Items.class
DEBUG DefaultVFS:248 - Not a JAR: file:/D:/java_se/zzr/MyBatis002/build/classes/com/zzr/mybatis/po/Items.class
DEBUG DefaultVFS:102 - Reader entry: ����
DEBUG DefaultVFS:222 - Find JAR URL: file:/D:/java_se/zzr/MyBatis002/build/classes/com/zzr/mybatis/po/Orderdetail.class
DEBUG DefaultVFS:248 - Not a JAR: file:/D:/java_se/zzr/MyBatis002/build/classes/com/zzr/mybatis/po/Orderdetail.class
DEBUG DefaultVFS:102 - Reader entry: ����
DEBUG DefaultVFS:222 - Find JAR URL: file:/D:/java_se/zzr/MyBatis002/build/classes/com/zzr/mybatis/po/Orders.class
DEBUG DefaultVFS:248 - Not a JAR: file:/D:/java_se/zzr/MyBatis002/build/classes/com/zzr/mybatis/po/Orders.class
DEBUG DefaultVFS:102 - Reader entry: ����
DEBUG DefaultVFS:222 - Find JAR URL: file:/D:/java_se/zzr/MyBatis002/build/classes/com/zzr/mybatis/po/OrdersExt.class
DEBUG DefaultVFS:248 - Not a JAR: file:/D:/java_se/zzr/MyBatis002/build/classes/com/zzr/mybatis/po/OrdersExt.class
DEBUG DefaultVFS:102 - Reader entry: ����
DEBUG DefaultVFS:222 - Find JAR URL: file:/D:/java_se/zzr/MyBatis002/build/classes/com/zzr/mybatis/po/User.class
DEBUG DefaultVFS:248 - Not a JAR: file:/D:/java_se/zzr/MyBatis002/build/classes/com/zzr/mybatis/po/User.class
DEBUG DefaultVFS:102 - Reader entry: ����
DEBUG DefaultVFS:222 - Find JAR URL: file:/D:/java_se/zzr/MyBatis002/build/classes/com/zzr/mybatis/po/UserQueryVO.class
DEBUG DefaultVFS:248 - Not a JAR: file:/D:/java_se/zzr/MyBatis002/build/classes/com/zzr/mybatis/po/UserQueryVO.class
DEBUG DefaultVFS:102 - Reader entry: ����
DEBUG ResolverUtil:256 - Checking to see if class com.zzr.mybatis.po.Items matches criteria [is assignable to Object]
DEBUG ResolverUtil:256 - Checking to see if class com.zzr.mybatis.po.Orderdetail matches criteria [is assignable to Object]
DEBUG ResolverUtil:256 - Checking to see if class com.zzr.mybatis.po.Orders matches criteria [is assignable to Object]
DEBUG ResolverUtil:256 - Checking to see if class com.zzr.mybatis.po.OrdersExt matches criteria [is assignable to Object]
DEBUG ResolverUtil:256 - Checking to see if class com.zzr.mybatis.po.User matches criteria [is assignable to Object]
DEBUG ResolverUtil:256 - Checking to see if class com.zzr.mybatis.po.UserQueryVO matches criteria [is assignable to Object]
DEBUG LogFactory:135 - Logging initialized using 'class org.apache.ibatis.logging.log4j.Log4jImpl' adapter.
DEBUG PooledDataSource:335 - PooledDataSource forcefully closed/removed all connections.
DEBUG PooledDataSource:335 - PooledDataSource forcefully closed/removed all connections.
DEBUG PooledDataSource:335 - PooledDataSource forcefully closed/removed all connections.
DEBUG PooledDataSource:335 - PooledDataSource forcefully closed/removed all connections.
DEBUG DefaultVFS:222 - Find JAR URL: file:/D:/java_se/zzr/MyBatis002/build/classes/com/zzr/mybatis/mapper
DEBUG DefaultVFS:248 - Not a JAR: file:/D:/java_se/zzr/MyBatis002/build/classes/com/zzr/mybatis/mapper
DEBUG DefaultVFS:102 - Reader entry: OrdersMapper.class
DEBUG DefaultVFS:102 - Reader entry: OrdersMapper.xml
DEBUG DefaultVFS:102 - Reader entry: UserMapper.class
DEBUG DefaultVFS:102 - Reader entry: UserMapper.xml
DEBUG DefaultVFS:113 - Listing file:/D:/java_se/zzr/MyBatis002/build/classes/com/zzr/mybatis/mapper
DEBUG DefaultVFS:222 - Find JAR URL: file:/D:/java_se/zzr/MyBatis002/build/classes/com/zzr/mybatis/mapper/OrdersMapper.class
DEBUG DefaultVFS:248 - Not a JAR: file:/D:/java_se/zzr/MyBatis002/build/classes/com/zzr/mybatis/mapper/OrdersMapper.class
DEBUG DefaultVFS:102 - Reader entry: ����
DEBUG DefaultVFS:222 - Find JAR URL: file:/D:/java_se/zzr/MyBatis002/build/classes/com/zzr/mybatis/mapper/OrdersMapper.xml
DEBUG DefaultVFS:248 - Not a JAR: file:/D:/java_se/zzr/MyBatis002/build/classes/com/zzr/mybatis/mapper/OrdersMapper.xml
00:45:25,274 DEBUG DefaultVFS:102 - Reader entry: <?xml version="1.0" encoding="UTF-8"?>
DEBUG DefaultVFS:222 - Find JAR URL: file:/D:/java_se/zzr/MyBatis002/build/classes/com/zzr/mybatis/mapper/UserMapper.class
DEBUG DefaultVFS:248 - Not a JAR: file:/D:/java_se/zzr/MyBatis002/build/classes/com/zzr/mybatis/mapper/UserMapper.class
DEBUG DefaultVFS:102 - Reader entry: ����
DEBUG DefaultVFS:222 - Find JAR URL: file:/D:/java_se/zzr/MyBatis002/build/classes/com/zzr/mybatis/mapper/UserMapper.xml
DEBUG DefaultVFS:248 - Not a JAR: file:/D:/java_se/zzr/MyBatis002/build/classes/com/zzr/mybatis/mapper/UserMapper.xml
DEBUG DefaultVFS:102 - Reader entry: <?xml version="1.0" encoding="UTF-8"?>
DEBUG ResolverUtil:256 - Checking to see if class com.zzr.mybatis.mapper.OrdersMapper matches criteria [is assignable to Object]
DEBUG ResolverUtil:256 - Checking to see if class com.zzr.mybatis.mapper.UserMapper matches criteria [is assignable to Object]


java.lang.ExceptionInInitializerError
    at com.zzr.mybatis.test.OrdersMapperTest.setup(OrdersMapperTest.java:19)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    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.RunBefores.evaluate(RunBefores.java:24)
    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.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: org.apache.ibatis.exceptions.PersistenceException: 
### Error building SqlSession.
### The error may exist in com/zzr/mybatis/mapper/UserMapper.xml
### Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias 'org.mybatis.caches.ehcache.EhcacheCache'.  Cause: java.lang.ClassNotFoundException: Cannot find class: org.mybatis.caches.ehcache.EhcacheCache
    at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
    at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:80)
    at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:64)
    at com.zzr.mybatis.utils.SqlSessionFactoryUtil.<clinit>(SqlSessionFactoryUtil.java:19)
    ... 24 more
Caused by: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias 'org.mybatis.caches.ehcache.EhcacheCache'.  Cause: java.lang.ClassNotFoundException: Cannot find class: org.mybatis.caches.ehcache.EhcacheCache
    at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:121)
    at org.apache.ibatis.builder.xml.XMLConfigBuilder.parse(XMLConfigBuilder.java:99)
    at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:78)
    ... 26 more
Caused by: org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias 'org.mybatis.caches.ehcache.EhcacheCache'.  Cause: java.lang.ClassNotFoundException: Cannot find class: org.mybatis.caches.ehcache.EhcacheCache
    at org.apache.ibatis.builder.xml.XMLMapperBuilder.configurationElement(XMLMapperBuilder.java:120)
    at org.apache.ibatis.builder.xml.XMLMapperBuilder.parse(XMLMapperBuilder.java:92)
    at org.apache.ibatis.builder.annotation.MapperAnnotationBuilder.loadXmlResource(MapperAnnotationBuilder.java:173)
    at org.apache.ibatis.builder.annotation.MapperAnnotationBuilder.parse(MapperAnnotationBuilder.java:124)
    at org.apache.ibatis.binding.MapperRegistry.addMapper(MapperRegistry.java:72)
    at org.apache.ibatis.binding.MapperRegistry.addMappers(MapperRegistry.java:97)
    at org.apache.ibatis.binding.MapperRegistry.addMappers(MapperRegistry.java:105)
    at org.apache.ibatis.session.Configuration.addMappers(Configuration.java:737)
    at org.apache.ibatis.builder.xml.XMLConfigBuilder.mapperElement(XMLConfigBuilder.java:364)
    at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:119)
    ... 28 more
Caused by: org.apache.ibatis.type.TypeException: Could not resolve type alias 'org.mybatis.caches.ehcache.EhcacheCache'.  Cause: java.lang.ClassNotFoundException: Cannot find class: org.mybatis.caches.ehcache.EhcacheCache
    at org.apache.ibatis.type.TypeAliasRegistry.resolveAlias(TypeAliasRegistry.java:120)
    at org.apache.ibatis.builder.xml.XMLMapperBuilder.cacheElement(XMLMapperBuilder.java:202)
    at org.apache.ibatis.builder.xml.XMLMapperBuilder.configurationElement(XMLMapperBuilder.java:114)
    ... 37 more
Caused by: java.lang.ClassNotFoundException: Cannot find class: org.mybatis.caches.ehcache.EhcacheCache
    at org.apache.ibatis.io.ClassLoaderWrapper.classForName(ClassLoaderWrapper.java:200)
    at org.apache.ibatis.io.ClassLoaderWrapper.classForName(ClassLoaderWrapper.java:89)
    at org.apache.ibatis.io.Resources.classForName(Resources.java:261)
    at org.apache.ibatis.type.TypeAliasRegistry.resolveAlias(TypeAliasRegistry.java:116)
    ... 39 more



2个回答

Caused by: java.lang.ClassNotFoundException: Cannot find class: org.mybatis.caches.ehcache.EhcacheCache
错误信息很明确,找不到类。搜搜这个类在哪个jar包里,把jar包加上就行。

你这个报错是开启了二级缓存Ehcache了,如果你不需要,在你的mybatis配置文件中找到这个cacheEnabled,去掉它或设置为false,在Mapper文件中找到这个类注释掉它。如果你要使用它,pom文件要加上依赖
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-ehcache</artifactId>
<version>1.0.0</version>
</dependency>

部署Ehcache (你要使用它的话,最好了解一下这一块的知识再使用,三两句话说不清楚,估计你是不小心开启了的可能性比较大)

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问