1

我正在尝试使用 spring 3 配置 mybatis。我收到以下错误

 Error creating bean with name 'loginController': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'userMapper' is defined

对于 UserMapper 接口,我已经这样做了。

@Service("userMapper")
@Transactional
public interface UserMapper {

 // methods here

}

我的控制器类是

@Controller
@RequestMapping("/")
 public class LoginController
 {
static final Logger logger = Logger.getLogger(LoginController.class);

@Resource(name = "userMapper")
private UserMapper userMapper;

@RequestMapping("/login")
public ModelAndView login(@ModelAttribute User userBean){


    return new ModelAndView("login", "userBean", userBean); 

}

}

我的 spring-servelet.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:p="http://www.springframework.org/schema/p"
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-3.1.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.1.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">


<!-- Enable annotation driven controllers, validation etc... -->
<mvc:annotation-driven />


<context:component-scan
    base-package="com.mycom.myproject" />

<mvc:resources mapping="/resources/**" location="/resources/" />


<bean id="datasource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost/mydatabase"/>
    <property name="username" value="root"/>
    <property name="password" value="root"/>
</bean>

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="com.mycom.mydatabase.db.mybatis.sqlmap" />
</bean> 

<bean id="viewResolver"
    class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    <property name="viewClass"
        value="org.springframework.web.servlet.view.JstlView" />
    <property name="prefix" value="/WEB-INF/jsp/" />
    <property name="suffix" value=".jsp" />
</bean>

那么,我如何在此处注入依赖项并可以访问我的数据库方法。如果有任何不清楚的地方,请现在告诉我。

4

2 回答 2

1

在您的 UserMapper 界面中,写下如下 -

  public interface UserMapper {

//for example, saveUser will save data in your table taking User as bean
public void saveUser(User user);
}

然后维护一个 UserMapper.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.puneet.persistance.UserMapper">  
<insert id="saveUser" parameterType="com.puneet.spring3.beans.User">
        INSERT INTO USER (username,password) 
        VALUES (#{username},#{password})
    </insert>
</mapper>

在上面的 xml 文件中,我们提到了查询和您作为参考的接口。

在您的 LoginController 中,调用您的方法,如下所示 -

@RequestMapping(method=RequestMethod.POST)
    public ModelAndView add(@ModelAttribute(value="user")User user,BindingResult result){

        ModelAndView mv = new ModelAndView("yourjpspage");
        if(!result.hasErrors()){
            userMapper.saveUser(user);
            user = new User();

        }
        return mv;
    }

最后维护 spring-servelet.xml 文件中的所有配置。

希望最重要的是会帮助你。

于 2013-09-19T14:32:12.270 回答
-1

UserMapper是一个接口,因此不能注入。您需要在 XML 配置中配置实现。

于 2012-08-02T15:54:21.120 回答