ssm项目的问题 Servlet.init() for servlet [spring-dispatcher] threw exception

跟着网上的ssm项目做,可以说代码基本都是照着敲的。但是还是会有各种各样的bug。这个问题困扰我两天了,网上常见的我都试过,包括spring和jdk版本的适配,jar包的调整等等。请各位大佬帮忙看一下,感激不尽!下面是我警告和错误的代码:

WARN org.springframework.web.context.support.XmlWebApplicationContext - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in file [D:\Program Files\Apache Software Foundation\Tomcat 9.0\webapps\o2o\WEB-INF\classes\spring\spring-dao.xml]: Invocation of init method failed; nested exception is org.springframework.core.NestedIOException: Failed to parse mapping resource: 'file [D:\Program Files\Apache Software Foundation\Tomcat 9.0\webapps\o2o\WEB-INF\classes\mapper\AreaDao.xml]'; nested exception is org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. The XML location is 'file [D:\Program Files\Apache Software Foundation\Tomcat 9.0\webapps\o2o\WEB-INF\classes\mapper\AreaDao.xml]'. Cause: org.apache.ibatis.builder.BuilderException: Error resolving class. Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias 'com.immoc.o2o.entity.Area'.  Cause: java.lang.ClassNotFoundException: Cannot find class: com.immoc.o2o.entity.Area

19:32:17.316 [http-nio-8080-exec-3] ERRORorg.springframework.web.servlet.DispatcherServlet - Context initialization failed
org.springframework.beans.factory.BeanCreationException:


Error creating bean with name 'sqlSessionFactory' defined in file [D:\Program Files\Apache Software Foundation\Tomcat 9.0\webapps\o2o\WEB-INF\classes\spring\spring-dao.xml]: Invocation of init method failed;


下面是错误中提到的AreaDao.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">
<mapper namespace="com.immoc.o2o.dao.AreaDao">
<select id="queryArea" resultType="com.immoc.o2o.entity.Area">

SELECT area_id,area_name,priority,create_time,last_edit_time
FROM tb_area 
ORDER BY priority DESC
</select>
</mapper>

Controller:

package com.immoc.o2o.web.superadmin;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.immoc.o2o.entity.Area;
import com.immoc.o2o.service.AreaService;


@Controller
@RequestMapping("/superadmin")
public class AreaController {

    @Autowired
     private AreaService areaService ;
    @RequestMapping(value="/listarea",method=RequestMethod.GET)//涉及前端的全小写
    //pose的传值是相对安全的,而get的传值会反馈到URL上面

    @ResponseBody
     private Map <String,Object> listArea(){
        Map <String,Object> modelMap= new HashMap<String,Object>();//map's key不能重复
        List<Area> list=new ArrayList<Area>();
        try {
         list=areaService.getAreaList();

         modelMap.put("rows", list);
         modelMap.put("total", list.size());
            }catch(Exception e) {
         e.printStackTrace();
         modelMap.put("success", false);
         modelMap.put("errMsg", e.toString());
         }
        return modelMap;
}
}


pom的配置,都是一些基本的配置,spring版本我用的都是最新的5.1.8

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.immoc</groupId>
  <artifactId>o2o</artifactId>
 <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>o2o Maven Webapp</name>
  <url>http://maven.apache.org</url>



  <dependencies>
  <!-- maven-dependency-plugin是来自csdn的博客 -->
  <!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-dependency-plugin -->
<dependency>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>3.1.1</version>
</dependency>
<!-- 原作是junit,这里已经升级 -->
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-api</artifactId>
    <version>5.5.1</version>
    <scope>test</scope>
</dependency>

    <!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-classic -->
<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
    <version>1.2.3</version>
</dependency>
    <!-- 这里用的都是5.1.8,而在原作里是设置了spring version的 -->
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>5.1.8.RELEASE</version>
</dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans这个其实就是备注 -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-beans</artifactId>
    <version>5.1.8.RELEASE</version>
</dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>5.1.8.RELEASE</version>
</dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>5.1.8.RELEASE</version>
</dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-tx -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-tx</artifactId>
    <version>5.1.8.RELEASE</version>
</dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>5.1.8.RELEASE</version>
</dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc   核心-->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.1.8.RELEASE</version>
</dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>5.1.8.RELEASE</version>
    <scope>test</scope>
</dependency>

<!--  除了spring之外还用到其他的架包,比如servlet -->
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>4.0.1</version>
    <scope>provided</scope>
</dependency>
<!-- 根据csdn加的jackson-core -->
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.9.9</version>
</dependency>




<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.9.9</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-collections4 -->
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-collections4</artifactId>
    <!-- commons collection has been moved to commons collections4 -->
    <version>4.1</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.5.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis-spring</artifactId>
    <version>2.0.2</version>
</dependency>

<!-- 下面适合数据库配合的架包 -->
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.17</version>
</dependency>
<!-- 连接池 -->
<!-- https://mvnrepository.com/artifact/com.mchange/c3p0 -->
<dependency>
    <groupId>com.mchange</groupId>
    <artifactId>c3p0</artifactId>
    <version>0.9.5.4</version>
</dependency>

  </dependencies>
  <build>
    <finalName>o2o</finalName>
    <plugins>
    <plugin>
    <!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-compiler-plugin -->

    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.8.1</version>
    <configuration>
    <source>1.8</source>
    <target>1.8</target>
    <encoding>UTF8</encoding>
    </configuration>

    </plugin>
    </plugins>
  </build>
 </project>

下面加个springDao吧:

<?xml version="1.0" encoding="UTF-8"?>
<!-- 引入schema约束 -->
<beans 
    xmlns="http://www.springframework.org/schema/beans"   
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation=
    "http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans.xsd  
            http://www.springframework.org/schema/context 
            http://www.springframework.org/schema/context/spring-context.xsd  
            http://www.springframework.org/schema/tx 
            http://www.springframework.org/schema/tx/spring-tx.xsd
            http://www.springframework.org/schema/mvc 
            http://www.springframework.org/schema/mvc/spring-mvc.xsd
            ">

            <!-- mvc是新加入的 -->
    <!--  加载jdbc.properties配置文件-->

    <context:property-placeholder location="classpath:jdbc.properties"/>

    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}"></property>
        <property name="jdbcUrl" value="${jdbc.url}"></property>
        <property name="user" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>

        <!-- c3p0的私有属性 -->
        <property name="maxPoolSize" value="30"/>
        <property name="minPoolSize" value="10"/>
        <!-- 关闭连接后不自动commit -->
        <property name="autoCommitOnClose" value="false"/>
        <!-- 获取超时链接的时间-->
        <property name="checkoutTimeout" value="10000"/>
        <!-- 连接失败的重试次数 -->
        <property name="acquireRetryAttempts" value="2"/>


</bean>


    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
     <property name="dataSource" ref="dataSource" />
     <property name="configLocation" value="classpath:mybatis-config.xml" />
     <property name="typeAliasesPackage" value="com.immoc.entity" />
     <!-- 上面一行中原代码没有o2o -->
     <property name="mapperLocations" value="classpath:mapper/*.xml" />
</bean>
<!-- 配置dao接口包,动态实现dao接口 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
    <property name="basePackage" value="com.immoc.o2o.dao" />
     <!-- 上面一行中原代码没有o2o,后来又加上了 -->
</bean>
 </beans>



顺便说一下,debug真的好难呀,随便一下一天就过去了

3个回答

图片说明
改成你的路径com.immoc.o2o

toomanyhair
toomanyhair 这个我倒是试过,改了之后就直接404了。不过还是谢谢你呀
大约一年之前 回复

提示 com.immoc.o2o.entity.Area 类没找到,确定下如果真的定义了这个类,那么执行下 clean 重新编译后再试试。

toomanyhair
toomanyhair clean了一下还是老样子,这个类确实是定义了的。谢谢啦
大约一年之前 回复

建议你把错误信息帖全,因为很多错误看着是那个地方报错,实际上有用的提示核能都在后面

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