spring4通用mapper报错

J2EE 码拜 4年前 (2016-09-26) 1593次浏览
实体类:package com.ssm.po;
import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Table;
@Table(name=”user_info”)
public class UserInfo {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name=”user_id”)
private Integer userId;
@Column(name=”user_name”)
private String userName;
@Column(name=”user_sex”)
private String userSex;
public UserInfo() {
super();
}
public UserInfo(Integer userId, String userName, String userSex) {
super();
this.userId = userId;
this.userName = userName;
this.userSex = userSex;
}
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserSex() {
return userSex;
}
public void setUserSex(String userSex) {
this.userSex = userSex;
}
@Override
public String toString() {
return “UserInfo [userId=” + userId + “, userName=” + userName
+ “, userSex=” + userSex + “]”;
}
}
Mapper:package com.ssm.mapper;
import java.util.List;
import tk.mybatis.mapper.common.Mapper;
import com.ssm.po.UserInfo;
public interface UserInfoMapper extends Mapper<UserInfo> {
/*
* 添加用户信息
*/
//public int addUser(UserInfo userInfo);
public List<UserInfo> getUserList(UserInfo user);
public int updateUser(UserInfo userInfo);
public UserInfo getUser(Integer userId);
public int deleteUser(Integer userId);
}
Mapper.xml:
<!DOCTYPE mapper PUBLIC “-//mybatis.org//DTD Mapper 3.0//EN” “http://mybatis.org/dtd/mybatis-3-mapper.dtd”>
<mapper namespace=”com.ssm.mapper.UserInfoMapper”>
<resultMap id=”BaseResultMap” type=”com.ssm.po.UserInfo” >

<id column=”user_id” property=”userId” jdbcType=”INTEGER” />
<result column=”user_name” property=”userName” jdbcType=”VARCHAR” />
<result column=”user_sex” property=”userSex” jdbcType=”VARCHAR” />
</resultMap>
</mapper>
Service:
@Autowired
private UserInfoMapper userInfoMapper;
public UserInfo selectById(int id) {
return  userInfoMapper.selectByPrimaryKey(id);
}
controller:
@Autowired
UserInfoService userInfoService;
@RequestMapping(“/addUser.do”)
public String addUser(UserInfo userInfo,Model model){
UserInfo u=userInfoService.selectById(2);
}
spring.xml
<context:component-scan base-package=”com”></context:component-scan>
<!– 配置数据源 –>
<bean id=”dataSource” class=”org.apache.commons.dbcp.BasicDataSource”>
<property name=”driverClassName” value=”com.mysql.jdbc.Driver”></property>
<property name=”url” value=”jdbc:mysql://127.0.0.1:3306/test”></property>
<property name=”username” value=”root”></property>
<property name=”password” value=”root”></property>
</bean>

<bean class=”tk.mybatis.spring.mapper.MapperScannerConfigurer”>
<property name=”basePackage” value=”com.isea533.mybatis.mapper”/>
<!– 通用Mapper通过属性注入进行配置,默认不配置时会注册Mapper<T>接口–>

</bean>
<bean id=”sqlSession” class=”org.mybatis.spring.SqlSessionTemplate” scope=”prototype”>
<constructor-arg index=”0″ ref=”sqlSessionFactory”/>
</bean>
报错信息:
严重: Servlet.service() for servlet [springmvc] in context with path [/ssm] threw exception [Request processing failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name “userInfoController”: Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.ssm.service.UserInfoService com.ssm.controller.UserInfoController.userInfoService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name “userInfoService”: Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: protected tk.mybatis.mapper.common.Mapper com.ssm.service.BaseService.mapper; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [tk.mybatis.mapper.common.Mapper] 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)}] with root cause
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [tk.mybatis.mapper.common.Mapper] 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)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1100)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:960)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:855)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:480)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1185)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1014)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:957)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:855)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:480)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1185)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)

解决方案

10

@Autowired
private UserInfoMapper userInfoMapper;

你这个有实现类没

50

<bean class=”tk.mybatis.spring.mapper.MapperScannerConfigurer”>
<property name=”basePackage” value=”com.isea533.mybatis.mapper”/>
<!– 通用Mapper通过属性注入进行配置,默认不配置时会注册Mapper<T>接口–
</bean>
–这个地方的包路径并不是你Mapper的路径

10

service实现类上加@service没

30

spring4通用mapper报错 mapper应该不需要自动注入的,spring支出mybatis接口直接代理生成bean对象,看http://haohaoxuexi.iteye.com/blog/1843309或许有启发。

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明spring4通用mapper报错
喜欢 (0)
[1034331897@qq.com]
分享 (0)