springMVC+spring+mybatis整合时遇到的问题

J2EE 码拜 9年前 (2015-04-08) 1308次浏览 0个评论

使用springMVC+Spirng+mybatis整合项目,启动server时最苦恼的一个异常就是:不能完成自动注入。下面我将相关代码贴出来恳请各位大牛帮我分析一下什么原因,谢谢了。
codes:

spring-config.xml文件
 <context:annotation-config />
<context:component-scan base-package=”com.hello.ssm”>
<context:exclude-filter type=”annotation”
expression=”org.springframework.stereotype.Controller” />
</context:component-scan>
<bean
class=”org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter”></bean>
<bean
class=”org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping”></bean>
<bean id=”configProperties”
class=”org.springframework.beans.factory.config.PropertyPlaceholderConfigurer”>
<property name=”locations”>
<value>classpath:db.properties
</value>
</property>
</bean>

spring-servlet.xml文件
<mvc:annotation-driven />
<context:annotation-config />
<context:component-scan base-package=”com.hello.ssm”>
<context:include-filter type=”annotation”
expression=”org.springframework.stereotype.Controller” />
</context:component-scan>
<bean id=”defaultViewResolver”
class=”org.springframework.web.servlet.view.InternalResourceViewResolver”>
<property name=”viewClass”
value=”org.springframework.web.servlet.view.JstlView” />
<property name=”contentType” value=”text/html” />
<property name=”prefix” value=”/WEB-INF/views/jsp/” />
<property name=”suffix” value=”.jsp” />
</bean>

spring-mybatis.xml文件
<!– BoneCP actived –>
<bean id=”mysqldataSource” class=”com.jolbox.bonecp.BoneCPDataSource”
destroy-method=”close”>
<property name=”driverClass” value=”${jdbc.driver}” />
<property name=”jdbcUrl” value=”${jdbc.url}” />
<property name=”username” value=”${jdbc.username}” />
<property name=”password” value=”${jdbc.password}” />
<property name=”idleConnectionTestPeriod” value=”${jdbc.idleConnectionTestPeriod}” />
<property name=”idleMaxAge” value=”${jdbc.idleMaxAge}” />
<property name=”maxConnectionsPerPartition” value=”${jdbc.maxConnectionsPerPartition}” />
<property name=”minConnectionsPerPartition” value=”${jdbc.minConnectionsPerPartition}” />
<property name=”partitionCount” value=”${jdbc.partitionCount}” />
<property name=”acquireIncrement” value=”${jdbc.acquireIncrement}” />
<property name=”statementsCacheSize” value=”${jdbc.statementsCacheSize}” />
<property name=”releaseHelperThreads” value=”${jdbc.releaseHelperThreads}” />
</bean>

<!– mybatis文件配置,扫描所有mapper文件 –>
<bean id=”sqlSessionFactory” class=”org.mybatis.spring.SqlSessionFactoryBean”
p:dataSource-ref=”mysqldataSource” p:configLocation=”classpath:mybatis-config.xml”
p:mapperLocations=”classpath:com/hello/ssm/mapper/**/*Mapper.xml” />
<!– configLocation为mybatis属性 mapperLocations为所有mapper –>

<!– spring与mybatis整合配置,扫描所有dao –>
<bean class=”org.mybatis.spring.mapper.MapperScannerConfigurer”
p:basePackage=”com.hello.ssm.mapper” p:sqlSessionFactoryBeanName=”sqlSessionFactory” />

<!– 对数据源进行事务管理 –>
<bean id=”transactionManager”
class=”org.springframework.jdbc.datasource.DataSourceTransactionManager”
p:dataSource-ref=”mysqldataSource” />

PersonMapper.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.fangda.ssm.dao.PersonMapper” >
  <resultMap id=”BaseResultMap” type=”com.hello.ssm.model.Person” >
    <id column=”id” property=”id” jdbcType=”INTEGER” />
    <result column=”name” property=”name” jdbcType=”VARCHAR” />
    <result column=”age” property=”age” jdbcType=”INTEGER” />
    <result column=”gender” property=”gender” jdbcType=”VARCHAR” />
    <result column=”salary” property=”salary” jdbcType=”INTEGER” />
  </resultMap>
  
  <sql id=”Base_Column_List” >
    id, name, age,gender,salary
  </sql>
  
  <select id=”findOne” resultMap=”BaseResultMap” parameterType=”java.lang.Integer” >
    select 
    <include refid=”Base_Column_List” />
    from person
    where id = #{id}
  </select>
  
  <select id=”findAll” resultMap=”BaseResultMap”>
   SELECT * FROM person
  </select>
  
  <delete id=”delete” parameterType=”java.lang.Integer” >
    delete from person
    where id = #{id}
  </delete>
  
  <insert id=”add” parameterType=”com.hello.ssm.model.Person”  >
    insert into person (name,age,gender,salary
      )
    values ( #{name},#{age}, #{gender},#{salary}
      )
  </insert>
  
  <update id=”update” parameterType=”com.hello.ssm.model.Person” >
    update person
    set name=#{name},age=#{age},gender=#{gender},salary=#{salary}
    where id=#{id}
  </update>
</mapper>

有一个BaseDao接口
public interface BaseDao<T> {
public void add(T t);
public void delete(int id);
public void update(T t);
public  T findById(int id);
public List<T> findAll();
}
有一个PersonDao集成BaseDao
public interface PersonDao extends BaseDao<Person> {
}

还有一个PersonMapper接口
@Repository(“personMapper”)
public interface PersonMapper {
public int add(Person p);
public int delete(int id);
public int update(Person p);
public Person findOne(int id);
public List<Person> findAll();
}

在PersonDaoImpl的实现类中将PersonMapper接口注入

@Repository(“personDAO”)
public class PersonDaoImpl extends BaseDaoImpl implements PersonDao {
@Autowired
private PersonMapper personMapper;

@Override
public void add(Person p) {
try {
personMapper.add(p);
} catch (Exception e) {
e.printStackTrace();
}
}
}
代码就这些,启动tomcat时报错:
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.fangda.ssm.dao.PersonMapper com.hello.ssm.dao.impl.PersonDaoImpl.personMapper; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException

springMVC+spring+mybatis整合时遇到的问题
8分
cache的值一般保存在rptconfig文件里
springMVC+spring+mybatis整合时遇到的问题
8分
PersonMapper com.hello.ssm.dao.impl.PersonDaoImp 没注入
springMVC+spring+mybatis整合时遇到的问题
8分
你先采用JAVA单元测试看看,看能不能获取Mapper
springMVC+spring+mybatis整合时遇到的问题
8分
 你确定你所有的bean包都在com.hello.ssm下面
springMVC+spring+mybatis整合时遇到的问题
8分
还有一个PersonMapper接口
@Repository(“personMapper”)
public interface PersonMapper {
public int add(Person p);
public int delete(int id);
public int update(Person p);
public Person findOne(int id);
public List<Person> findAll();
}

应该在实现类上写
@Repository(“personMapper”)


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明springMVC+spring+mybatis整合时遇到的问题
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!