首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我使用Spring和MyBatis作为学习MyBatis框架的演示项目。我正在反复上课,没有发现异常。

我使用Spring和MyBatis作为学习MyBatis框架的演示项目。我正在反复上课,没有发现异常。
EN

Stack Overflow用户
提问于 2014-06-17 05:14:32
回答 1查看 2.7K关注 0票数 0

我得到的类没有发现异常反复。

代码语言:javascript
复制
Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.builder.BuilderException: Error registering typeAlias for 'Emp'. Cause: java.lang.ClassNotFoundException: Cannot find class: classpath*:com.attinad.model.Employee

下面给出了我的myBati-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>
  <typeAliases><typeAlias type="classpath*:com.attinad.model.Employee" alias="Emp"/></typeAliases>

 <mappers>
        <mapper resource="classpath:com/attinad/mappers/EmployeeMapper.xml" />
 </mappers>

</configuration>

和ds-config.xml:

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
 <beans >
    <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiName" value="java:comp/env/jdbc/corpds"/>
    </bean>
    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
     </bean>

            <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dataSource" />             
            <property name="typeAliasesPackage" value="classpath*:com/attinad/model/*"/>        
            <property name="configLocation" value="classpath:myBatis-config.xml" />
            <property name="mapperLocations" value="classpath*:com/attinad/mappers/*.xml" />
     </bean>

</beans>

我的映射文件EmployeeMapper.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="com.attinad.dao.EmployeeMapperInterface">
<resultMap type="Emp" id="EmpResult">
    <id property="id" column="id"/>
    <result property="createdAt" column="createdAt"/>
    <result property="department" column="department"/>
    <result property="displayName" column="displayName"/>
    <result property="emailId" column="emailId"/>
    <result property="isEnable" column="isEnable"/>
    <result property="name" column="name"/>
    <result property="updatedAt" column="updatedAt"/>
</resultMap>

<select id="getEmployeeWithId" parameterType="Long" resultMap="EmpResult"> 
        select *  from
        employees where
        id=#{empId}

    </select>
</mapper>

这是我的刀课:

代码语言:javascript
复制
package com.attinad.dao;

import java.util.*;
import com.attinad.model.*;
public interface EmployeeMapperInterface extends BaseMapperInterface{
    public List<Employee> getEmployeeWithId(long id);
    public int insertEmployee(Employee e);
    public int updateEmployee(Employee e);
    public void deleteEmployee(int id);

}

我的服务班:

代码语言:javascript
复制
package com.attinad.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.attinad.dao.EmployeeMapperInterface;
import com.attinad.model.Employee;


    @Service
    public class EmployeeService implements EmployeeBaseService{

        @Autowired
        EmployeeMapperInterface employeeMapper;

         @Override    
         public Employee getEmployeeById(long empId){

            List<Employee> empList = employeeMapper.getEmployeeWithId(empId);
            if(empList != null && empList.size()>0){
                //System.out.println("List accumulated");
                System.out.println(empList);
                return (Employee) empList.get(0);

            }
            return null;

        }
}

Eclipse构建路径:src/main/ Java包含在Java构建路径中,我的包在这个类路径下。

请帮我解决这个错误。我不知道真正的原因。当我移除resultMap并使它成为ResultType="hashMap“时,它就起作用了。但是现在,如果我尝试这样做,我将为其他类获得ClassNotFound。

EN

回答 1

Stack Overflow用户

发布于 2014-06-18 05:01:38

错误消息非常明确地表明类加载程序试图加载名为classpath*:com.attinad.model.Employee的类。

typeAliases配置应该如下所示:

代码语言:javascript
复制
<typeAliases><typeAlias type="com.attinad.model.Employee" alias="Emp"/></typeAliases>

注意事项classpath*:被移除。

sqlSessionFactory bean配置也存在同样的问题。typeAliasPackage应该如下所示:

代码语言:javascript
复制
<property name="typeAliasesPackage" value="com.attinad.model"/>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24256165

复制
相关文章

相似问题

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