ssm中报这个错误,该怎么解决!

Error creating bean with name 'sqlSessionFactory' defined in class path resource [spring-mybatis.xml]: Invocation of init method failed; nested exception is org.springframework.core.NestedIOException: Failed to parse mapping resource: 'file [G:通州学习java学习.metadata.pluginsorg.eclipse.wst.server.coretmp0wtpwebappsssmWEB-INFclassesmapperAdminDaoImpl.xml]'; nested exception is org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. Cause: org.apache.ibatis.builder.BuilderException: Error resolving class. Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias 'Admin'. Cause: java.lang.ClassNotFoundException: Cannot find class: Admin

阅读 6.6k
2 个回答
Error creating bean with name 'sqlSessionFactory' defined in class path resource [spring-mybatis.xml]: Invocation of init method failed; nested exception is org.springframework.core.NestedIOException: Failed to parse mapping resource: 'file [G:通州学习java学习.metadata.pluginsorg.eclipse.wst.server.coretmp0wtpwebappsssmWEB-INFclassesmapperAdminDaoImpl.xml]'; nested exception is org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. Cause: org.apache.ibatis.builder.BuilderException: Error resolving class. Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias 'Admin'. Cause: java.lang.ClassNotFoundException: Cannot find class: Admin

看异常栈需要抓重点,
那么你的错误重点在于最后一句

: Could not resolve type alias 'Admin'. Cause: java.lang.ClassNotFoundException: Cannot find class: Admin

应该是你的mapper使用了 Admin 而不是包名+Admin
解决方案 两种
1、
将AdminDaoImpl.xml 中所有的Admin改成包名+Admin
2、
新建mybatis-config.xml
配置别名

<configuration>  
  <typeAliases>  
      <typeAlias type="包名.Admin" alias="Admin"/>  
  </typeAliases>  
</configuration>  

在spring-mybatis.xml 引入mybatis配置

     <!-- 配置mybatis的sqlSessionFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!-- 自动扫描mapper.xml文件-->
        <property name="mapperLocations" value="classpath:mapper/*.xml"/>
        <!-- 载入mybatis全局配置文件-->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
    </bean>

最后,编码不易 望采纳。