我的Spring MyBatis项目出了问题。 这里是4个包层的类: 调节器 映射器 模型 服务
com.mb.alf.controller
ServizioController.java
package com.mb.alf.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;
import com.mb.alf.model.Servizio;
import com.mb.alf.service.ServizioService;
@Controller
@SessionAttributes("servizio")
public class ServizioController {
@Autowired
private ServizioService servizioService;
@RequestMapping("/returnServizio")
public ModelAndView returnServizio() {
Servizio servizio = servizioService.getServizio();
return new ModelAndView("returnServizio", "servizio", servizio); }}
com.mb.alf.mapper
ServizioMapper.java
package com.mb.alf.mapper;
import org.apache.ibatis.annotations.Select;
import com.mb.alf.model.Servizio;
public interface ServizioMapper {
@Select("select s.ser_puntopresa, s.ser_oldcodser from serviz s where s.ser_ute = '01' and S.SER_PUNTOPRESA = 101")
public Servizio getServizio();
}
com.mb.alf.model
Servizio.java
package com.mb.alf.model;
public class Servizio {
Integer serv_puntopresa;
Integer ser_oldcodser;
public Integer getSer_oldcodser() {
return ser_oldcodser; }
public void setSer_oldcodser(Integer ser_oldcodser) {
this.ser_oldcodser = ser_oldcodser; }
public Integer getServ_puntopresa() {
return serv_puntopresa; }
public void setServ_puntopresa(Integer num) {
this.serv_puntopresa = num; }
}
com.mb.alf.service
ServizioService.java
package com.mb.alf.service;
import com.mb.alf.model.Servizio;
public interface ServizioService {
Servizio getServizio();
}
ServizioServiceImpl.java
package com.mb.alf.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.mb.alf.mapper.ServizioMapper;
import com.mb.alf.model.Servizio;
@Component @Service("servizioService")
public class ServizioServiceImpl implements ServizioService {
@Autowired
private ServizioMapper servizioMapper;
@Transactional
public Servizio getServizio() {
return servizioMapper.getServizio(); } }
这是我的XML文件:
的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>myBatisServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/springConfig.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>myBatisServlet</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<display-name>Archetype Created Web Application</display-name>
</web-app>
springConfig.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"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
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-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<mvc:annotation-driven />
<context:component-scan base-package="com.mb.alf" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@MYHOST:orcl"/>
<property name="username" value="USER"/>
<property name="password" value="PASSWORD"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="typeAliasesPackage" value="com.mb.alf.model"/>
<property name="mapperLocations" value="classpath*:com/mb/alf/mapper/*.xml" />
</bean>
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory" />
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.mb.alf.mappers" />
</bean>
</beans>
它得到了这个例外:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'servizioController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.mb.alf.service.ServizioService com.mb.alf.controller.ServizioController.servizioService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'servizioService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.mb.alf.mapper.ServizioMapper com.mb.alf.service.ServizioServiceImpl.servizioMapper; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.mb.alf.mapper.ServizioMapper] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
请问,我如何以及在哪里实施正确的bean?
提前致谢
答案 0 :(得分:0)
也许您应该在另一个xml中分离mybatis设置,而不是将其放在当前的springConfig.xml中
例如:
web.xml 添加以下内容:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:conf/spring.xml;
classpath:conf/spring-mybatis.xml
</param-value>
</context-param>
...
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
在 spring-mybatis.xml * 中,添加dataSource,transactionManager,sqlSessionFactory,sqlSession和org.mybatis.spring.mapper.MapperScannerConfigurer。
可在此处找到一个示例https://github.com/liratanak/SpdSample-Spring-MVC-3-MyBatis-3-Tiles-3