注入豆我試圖用彈簧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配置MyBatis的interfcae我已經這樣做了。
@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);
}
}
我的彈簧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>
所以,我怎麼能在這裏注入的相關性和可訪問我的數據庫方法。如果有什麼不清楚的話,請讓我來。
他們對Spring和MyBatis有很好的指導。 http://www.mybatis.org/spring/sample.html – jasop 2012-08-10 06:03:48