当前位置:编程学习 > JAVA >>

新手myibatis配置报错 求解答

org.apache.ibatis.exceptions.PersistenceException: 
### Error building SqlSession.
### The error may exist in SQL Mapper Configuration
### Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.builder.BuilderException: Error registering typeAlias for 'UserDaoMapper.xml'. Cause: java.lang.ClassNotFoundException: Cannot find class: com.icreate.dao/UserDaoMapper.xml
at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:23)
at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:51)
at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:35)
at com.mybatis.config.UserDaoTest.userDaoTest(UserDaoTest.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:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.builder.BuilderException: Error registering typeAlias for 'UserDaoMapper.xml'. Cause: java.lang.ClassNotFoundException: Cannot find class: com.icreate.dao/UserDaoMapper.xml
at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:103)
at org.apache.ibatis.builder.xml.XMLConfigBuilder.parse(XMLConfigBuilder.java:86)
at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:49)
... 25 more
Caused by: org.apache.ibatis.builder.BuilderException: Error registering typeAlias for 'UserDaoMapper.xml'. Cause: java.lang.ClassNotFoundException: Cannot find class: com.icreate.dao/UserDaoMapper.xml
at org.apache.ibatis.builder.xml.XMLConfigBuilder.typeAliasesElement(XMLConfigBuilder.java:124)
at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:93)
... 27 more
Caused by: java.lang.ClassNotFoundException: Cannot find class: com.icreate.dao/UserDaoMapper.xml
at org.apache.ibatis.io.ClassLoaderWrapper.classForName(ClassLoaderWrapper.java:188)
at org.apache.ibatis.io.ClassLoaderWrapper.classForName(ClassLoaderWrapper.java:87)
at org.apache.ibatis.io.Resources.classForName(Resources.java:250)
at org.apache.ibatis.builder.xml.XMLConfigBuilder.typeAliasesElement(XMLConfigBuilder.java:117)
... 28 more

------------------------------
代码:
package com.mybatis.config;

import java.io.Reader;
import java.util.Iterator;
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.Test;

public class UserDaoTest {
    @Test
    public void userDaoTest() throws Exception
    {
        String resource = "MyBatis-Configuration.xml";
        Reader reader = Resources.getResourceAsReader(resource);
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        SqlSessionFactory factory = builder.build(reader);
        
        SqlSession session = factory.openSession();
        UserDao userDao = session.getMapper(UserDao.class);
        
        User user = new User();
        user.setUserName("hongye");
        user.setPassword("123456");
        user.setComment("备注");
        
        userDao.insert(user);
        System.out.println("记录条数:"+userDao.countAll());
        
        List<User> users = userDao.selectAll();
        Iterator<User> iter = users.iterator();
        while(iter.hasNext()){
            User u = iter.next();
            System.out.println("用户名:"+u.getUserName()+"密码:"+u.getPassword());
        }
        
        user.setComment("comment");
        userDao.update(user);
        User u = userDao.findByUserName("hongye");
        System.out.println(u.getComment());
        
        userDao.delete("hongye");
        System.out.println("记录条数:"+userDao.countAll());
        
        session.commit();
        session.close();
    }
}
------------------
<?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>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC" />
            <dataSource type="POOLED">
                <property name="driver" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
                <property name="url" value="jdbc:sqlserver://localhost:1433;databaseName=zzgj_sbgl" />
                <property name="username" value="sa" />
                <property name="password" value="Wlzx123456" />
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="com/mybatis/config/UserDaoMapper.xml"/>
    </mappers>
</configuration>
------------------------------------
<?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="com.mybatis.config.UserDao">
    <select id="countAll" resultType="int">
        select count(*) c from user;
    </select>
    
    <select id="selectAll" resultType="com.mybatis.config.User">
        select * from user order by userName asc
    </select>
    
    <insert id="insert" parameterType="com.mybatis.config.User">
        insert into user(userName,password,comment) values(#{userName},#{password},#{comment})
    </insert>
    
    <update id="update" parameterType="com.mybatis.config.User">
        update user set userName=#{userName},password=#{password},comment=#{comment} where userName=#{userName}
    </update>
    
    <delete id="delete" parameterType="int">
        delete from user where userName=#{userName}
    </delete>
    
    <select id="findByUserName" parameterType="String" resultType="com.mybatis.config.User">
        select * from user where userName=#{userName}
    </select>
</mapper>
--------------------编程问答-------------------- 除 --------------------编程问答-------------------- icreate  啥情况? --------------------编程问答-------------------- java.lang.ClassNotFoundException: Cannot find class: com.icreate.dao/UserDaoMapper.xml 这报错的类在是在com.icreate.dao包下而你配置的dao接口在这个包下com.mybatis.config<mapper namespace="com.mybatis.config.UserDao">两个接口不在同一个包下
补充:Java ,  Java相关
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,