求教一下各位大牛Spring,Mybatis整合注解出现注入失败问题?

springmvc-servlet.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: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/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <context:component-scan base-package="controller" />
    <context:component-scan base-package="service" />
    <mvc:annotation-driven />

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value = ".jsp" />
    </bean>

</beans>

web.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" 
id="WebApp_ID" version="3.1">
    <servlet>
    <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
        </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <filter>
        <filter-name>characterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>characterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

service层代码

package service;

import java.util.ArrayList;

import org.springframework.stereotype.Service;

import pojo.User;
@Service
public class UserServiceImpl implements UserService{
    private static ArrayList<User> users = new ArrayList<>();

    @Override
    public boolean addUser(User u) {
        // TODO Auto-generated method stub
        if(!"IT民工".equals(u.getCarrer())) {
            users.add(u);
            return true;
        }
        return false;
    }
    public ArrayList<User> getUser() {
        // TODO Auto-generated method stub
        return users;
    }
}

控制层代码

package controller;

import java.util.HashMap;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import pojo.User;
import service.UserService;
@Controller
@RequestMapping("/user")
public class UserController {
    private static final Log logger = LogFactory.getLog(UserController.class);
    @Autowired
    private UserService userService;
    @RequestMapping(value = "/input")
    public String inputUser(Model model) {
        HashMap<String, String> hobbys = new HashMap<>();
        hobbys.put("篮球", "篮球");
        hobbys.put("点玩", "电玩");
        hobbys.put("游泳", "游泳");
        hobbys.put("乒乓球", "乒乓球");
        model.addAttribute("user", new User());
        model.addAttribute("hobbys", hobbys);
        model.addAttribute("carrers", new String[] {"教师","学生","coding搬运工","IT民工","其他"});
        model.addAttribute("houseRegisters", new String[] {"北京","上海","广州","深圳","其他"});
        return "useradd";
    }
    @RequestMapping("/save")
    public String addUser(@ModelAttribute User user,Model model) {
        if(userService.addUser(user)) {
            logger.info("成功");
            return "redirect:/user/list";
        }else {
            logger.info("失败");
            HashMap<String, String> hobbys = new HashMap<>();
            hobbys.put("篮球", "篮球");
            hobbys.put("点玩", "电玩");
            hobbys.put("游泳", "游泳");
            hobbys.put("乒乓球", "乒乓球");
            model.addAttribute("user", new User());
            model.addAttribute("hobbys", hobbys);
            model.addAttribute("carrers", new String[] {"教师","学生","coding搬运工","IT民工","其他"});
            model.addAttribute("houseRegisters", new String[] {"北京","上海","广州","深圳","其他"});
            return "useradd";
        }
    }
    @RequestMapping("/list")
    public String listUser(Model model) {
        List<User> users = userService.getUser();
        model.addAttribute("user", users);
        return "userList";
    }
}```


目录结构![图片说明](https://img-ask.csdn.net/upload/201904/14/1555240537_144458.png)

报错信息![图片说明](https://img-ask.csdn.net/upload/201904/14/1555240603_581132.png)

1个回答

检查下base-package包有没有包含DAO的包,其次你的给的图片链接访问结果为403权限有问题。

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