简体   繁体   中英

Injecting bean in spring and mybatis configuration

I am trying to configure mybatis with spring 3. I am getting the following error

 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

For UserMapper interfcae I have done like this.

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

 // methods here

}

My Controller class is

@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); 

}

}

My spring-servelet.xml is

 <?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>

So, How can I Inject the Dependency here and can access my database methods. Please let me now if any thing is not clear.

In your UserMapper interface, write down as below-

  public interface UserMapper {

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

then maintain a UserMapper.xml file like below-

    <?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>

In above xml file we mention queries and the interface that you are taking as reference.

in your LoginController, call your method like below-

@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;
    }

In last maintain all configurations in your spring-servelet.xml file.

Hope above all will help you.

UserMapper is an interface and as such, can't be injected. You need to configure an implementation in your XML config.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM