IDEA做SSM框架整合时,出现无法加载mapper,导致无法注入。

今日学习SSM框架整合,整合以后,单独用测试类可以连接成功连接数据库,可是一加入MVC用TOMCAT启动,项目就会报错,错误内容为:Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
以下为项目各部分截图,有没有大佬帮我分析下,问题到底出在哪?

项目报错详细情况

项目报错详细情况

spring配置文件已经配置了mapper.xml路径和齐对应的接口扫描包

spring配置文件已经配置了mapper.xml路径和齐对应的接口扫描包

编译文件夹target里已经编译出了mapper相关配置文件及接口类

编译文件夹target里已经编译出了mapper相关配置文件及接口类

mapper.xml里的namespace已经指明了接口路径

mapper.xml里的namespace已经指明了接口路径

pom文件里也配置了resource源

pom文件里也配置了resource源

详细代码已经贴在下方,只选了几个配置文件及mapper接口类,service及controller应该没关系就没传!

mybatis-config.xml

<?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>
    <!--添加缓存  引入第三方二级缓存 -->
    <settings>
        <setting name="mapUnderscoreToCamelCase" value="true"/>
        <setting name="cacheEnabled" value="false"/>
    </settings>

    <!-- 配置数据源  default默认使用的数据源 -->
    <environments default="mysql">
        <environment id="mysql">
            <transactionManager type="JDBC" />
            <dataSource type="POOLED">
                <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql:///orderuser"/>
                <property name="username" value="zhijia3278"/>
                <property name="password" value="0528"/>
            </dataSource>
        </environment>
    </environments>


    <!-- 映射文件 -->
    <mappers>
        <mapper resource="OrderUserMapper.xml"/>
    </mappers>

</configuration>

OrderUserMapper.java

package cn.tedu.mapper;

import cn.tedu.domain.Order;
import cn.tedu.domain.User;

public interface OrderUserMapper {

    public User queryUser(String userId);

    public Order queryOrder(String orderId);

    public void updateUserPoints(User user);

}

OrderUserMapper.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="cn.tedu.mapper.OrderUserMapper">
    <select id="queryUser" parameterType="String"
            resultType="User">
        select * from t_user where user_id=#{userId}
    </select>
    <select id="queryOrder" parameterType="String"
            resultType="Order">
        select * from t_order where order_id=#{orderId}
    </select>
    <update id="updateUserPoints" parameterType="User">
        update t_user set points=#{points}
        where user_id=#{userId}
    </update>
</mapper>

spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:util="http://www.springframework.org/schema/util"
       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/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/util
        http://www.springframework.org/schema/util/spring-util.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <!-- 配置内容 -->
            <context:component-scan base-package="cn.tedu">
            </context:component-scan>
    <context:annotation-config></context:annotation-config>

    <!-- 持久层数据源 -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <!-- 4个属性 -->
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql:///orderuser"/>
        <property name="username" value="zhijia3278"/>
        <property name="password" value="0528"/>
    </bean>
    <!-- sqlSession -->
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 数据源注入 -->
        <property name="dataSource" ref="dataSource"/>
        <!-- 扫描mybatis的独立配置文件 关闭二级缓存,驼峰命名 -->
        <property name="configLocation" value="classpath:mybatis/mybatis-config.xml"/>
        <!-- 映射文件扫描 -->
        <property name="mapperLocations" value="classpath:mapper/OrderUserMapper.xml" />
        <!-- 别名包 -->
        <property name="typeAliasesPackage" value="cn.tedu.domain"></property>
    </bean>
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 扫描的接口包 cn.tedu.mapper -->
        <property name="basePackage" value="cn.tedu.mapper"/>
    </bean>


    <!-- 注解驱动开启,@RequestMapping @RequestParam等 -->
    <mvc:annotation-driven/>
    <!-- 防止静态文件js css html访问被dispacherServlet拦截 -->
    <!-- localhost静态文件根目录 /static /js /css -->
    <!-- mapping 满足什么样子的访问地址 http://localhost:8080/js//1.js -->
    <mvc:resources location="/" mapping="/**"></mvc:resources>

</beans>

springmvc-servlet

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:util="http://www.springframework.org/schema/util"
       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/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/util
        http://www.springframework.org/schema/util/spring-util.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <!-- 配置内容 -->
    <context:component-scan base-package="cn.tedu"/>

    <!-- 注解驱动开启,@RequestMapping @RequestParam等 -->
    <mvc:annotation-driven/>
    <!-- 防止静态文件js css html访问被dispacherServlet拦截 -->
    <!-- localhost静态文件根目录 /static /js /css -->
    <!-- mapping 满足什么样子的访问地址 http://localhost:8080/js//1.js -->
    <mvc:resources location="/" mapping="/**"></mvc:resources>



</beans>

web.xml

<!DOCTYPE web-app PUBLIC
        "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
        "http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
    <display-name>Archetype Created Web Application</display-name>
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- tomcat启动加载spring.xml -->
<!--        <init-param>-->
<!--            <param-name>contextConfigLocation</param-name>-->
<!--            <param-value>classpath:springmvc/springmvc-servlet.xml</param-value>-->
<!--        </init-param>-->
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

pom.xml

<?xml version="1.0" encoding="UTF-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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>cn.tedu</groupId>
    <artifactId>SSMDemo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>SSM-DEMO01 Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <dependencies>
<!--        <dependency>-->
<!--            <groupId>junit</groupId>-->
<!--            <artifactId>junit</artifactId>-->
<!--            <version>4.12</version>-->
<!--            <scope>compile</scope>-->
<!--        </dependency>-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.3.13.RELEASE</version>
        </dependency>
        <!-- datasource -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.0.14</version>
        </dependency>
        <!-- spring mysql -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>4.3.13.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.0.8</version>
        </dependency>
        <!-- mybatis -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.5</version>
        </dependency>
        <!-- spring mybatis  -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.3.1</version>
        </dependency>
        <!-- springmvc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.3.13.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.3.13.RELEASE</version>
        </dependency>
        <!-- jackson -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.8.8</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.8.8</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.8.8</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
    <build>
        <finalName>SSM-DEMO01</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>
                <!-- 插件属性配置 -->
                <configuration>
                    <!-- 端口 -->
                    <port>8080</port>
                    <!-- 项目访问路径 -->
                    <path>/</path>
                    <!-- 编解码 -->
                    <uriEncoding>utf-8</uriEncoding>
                    <useBodyEncodingForURI>true</useBodyEncodingForURI>
                </configuration>
            </plugin>
        </plugins>
        <resources>
            <resource>
                <directory>resource</directory>
                <includes>
                    <include>**/*.xml</include>
                    <include>mapper/*.xml</include>
                    <include>mybatis/*.xml</include>
                    <include>spring/*.xml</include>
                </includes>
<!--                <filtering>true</filtering>-->
            </resource>
        </resources>
    </build>
</project>

Locaslove
小华华丫 @Autowired注入mapper失败 在spring的applicationContext.xml文件加<context:annotation-config/>就行
9 个月之前 回复
qq_45431107
酒叁仙 我到现在还没解决呢 ...
10 个月之前 回复
m0_43396955
落染 请问你是怎样解决这个问题的,我也遇到了在网上找了半天也没找到合适的方法
10 个月之前 回复

3个回答

持久层OrderUserMapper添加注解@Repository

Locaslove
小华华丫 @Autowired注入mapper失败 在spring的applicationContext.xml文件加<context:annotation-config/>就行
9 个月之前 回复
qq_45431107
酒叁仙 controller和service里都加了,问题主要出在service里的@autowire,提示的不能强注入,如果默认为true,一启动就会报错,显示这个mapper接口无效,如果把@autowire的值改成false,这个错误就会换成一个空指针的错误,实际还是mapper注入失败!
12 个月之前 回复
tiankongzhichenglyf
tkzc_shark 回复qq_45431107: 使用的时候还得@Autowire注解
12 个月之前 回复
qq_45431107
酒叁仙 添加了,没用!不管@repository还是@comment,甚至连@mapperscan都加了,就是不管用,提示注入的mapper无效!
12 个月之前 回复
 <!-- 映射文件扫描 -->
<property name="mapperLocations" value="classpath:/mapper/**Mapper.xml" />

启动的时候先看下编译后的工程结构是否正确

qq_45431107
酒叁仙 target里面的mapper接口类,配置文件都有
12 个月之前 回复
qq_42280739
SimpleWu。 回复qq_45431107: 看下target里面的接口,idea没配置可能配置文件不会打包
12 个月之前 回复
qq_45431107
酒叁仙 我不太了解工程结构,但是该有的文件都有,还是不行,你说的这个地方也该了,还是不行,一直提示注入的mapper无效!
12 个月之前 回复

或者有句话,扫描项目的注解你配置没?

qq_45431107
酒叁仙 你是说mapper的接口类么?@Repository这个注解加了
12 个月之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐