@Autowired无法注入Mybatis的mapper界面

时间:2019-02-17 09:12:45

标签: java spring mybatis

我无法通过@Autowired注释获取Mybatis的映射器。我使用context-param标签和contextConfigLocation参数来设置与Spring框架相关的所有配置,请参考web.xml。我尝试了以下#1和#2的操作。我的访客web.xml没有配置我的Spring xml文件。如果有任何想法,请帮助我解决。谢谢〜

这是一个Maven项目,由eclipse创建,运行Tomcat 9.0.8。

  1. 我尝试使用ApplicationContext来获取EmpMapper,它可以工作。请参考我在Controller中标记的代码。

  2. 我还尝试使用@Repository添加EmpVO,并使用@Autowired在Controller中获取EmpVO实例,它可以正常工作。

但是,我无法通过EmpMapper注释获得@Autowired的实例。

控制器代码:

package com.emp.controller;
@Controller
@RequestMapping("/emp")
public class EmpController {
    @Autowired
    EmpMapper empMapper;
    @RequestMapping(value = "testInsert")
    public void insertEmp() {
        EmpVO empVO = new EmpVO();
        empVO.setEname("RAYSUN2");
        empVO.setComm(10000.0); 
        empVO.setHireDate(java.sql.Date.valueOf("2019-01-01"));
        empVO.setDeptNo(20);
        empVO.setSal(90000.0);
        empMapper.insert(empVO);
        //ApplicationContext context = new ClassPathXmlApplicationContext("spring/spring-mybatis.xml");
        //EmpMapper empMapper = context.getBean(EmpMapper.class);
        //empMapper.insert(empVO);
    }
}

Mybatis的映射器界面:

package com.emp.mapper;
public interface EmpMapper {
    void insert(EmpVO empVO);
    void update(EmpVO empVO);
    void delete(Integer empno);
    EmpVO findByPrimaryKey(Integer empno);
    List<EmpVO> getAll();
}

EmpMapper.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.emp.mapper.EmpMapper">
    <sql id="emp2_seq">emp2_seq.nextval</sql>
    <resultMap type="com.emp.model.EmpVO" id="EmpVO">
        <id property="empno" column="empno" jdbcType="DECIMAL" />
        <result property="ename" column="ename" jdbcType="VARCHAR" />
        <result property="job" column="job" jdbcType="VARCHAR" />
        <result property="hireDate" column="hiredate" jdbcType="DATE" />
        <result property="sal" column="sal" jdbcType="DOUBLE" />
        <result property="comm" column="comm" jdbcType="DOUBLE" />
        <result property="deptNo" column="deptno" jdbcType="DECIMAL" />
    </resultMap>
    <insert id="insert" parameterType="com.emp.model.EmpVO">
        insert into emp2 (empno, ename, job, hiredate, sal, comm, deptno)
        values
        (<include refid="emp2_seq" />, #{ename}, #{job}, #{hireDate}, #{sal}, #{comm}, #{deptNo})
    </insert>
</mapper>

spring-mybatis.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:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
    <import resource="classpath*:/spring/model-config2-JndiObjectFactoryBean.xml"/>

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation" value="classpath:/mybatis/mybatis-config.xml" />
        <property name="mapperLocations" value="classpath*:/mybatis/mapper/**/*.xml" />     
    </bean>

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
        <property name="basePackage" value="com.emp.mapper" />  
    </bean>
</beans>

spring-ssm_webapp-mvc.xml:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd"> 
    <context:component-scan base-package="com.emp">
    </context:component-scan>
    <mvc:annotation-driven />
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".jsp" />
    </bean>
</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>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath*:/spring/*.xml
        </param-value>
    </context-param>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath*:/springmvc/spring-ssm_webapp-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping> 
</web-app>

2 个答案:

答案 0 :(得分:0)

忘记添加

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

它在做完之后就起作用了。

答案 1 :(得分:0)

请注意mybatis的@Mapper批注,该批注允许与spring框架无缝集成,因此您可以使用@Autowired批注在任何地方注入mapper的依赖项。请也检查官方文档。 http://www.mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/