当前位置:   article > 正文

解决ssm项目整合出现的问题:Could not autowire.No beans of ‘xxxMapper‘ type found及其他问题_could not autowire. no beans of 'tdtestmapper' typ

could not autowire. no beans of 'tdtestmapper' type found

本章内容只分享个人问题:例如service层没有添加@service注解等的问题不会阐述。

背景:使用了spring的ioc,springmvc的dispatcherservlet及thymelaf,mybatis的逆向工程

问题1:Could not autowire.No beans of 'xxxMapper' type found

问题2:Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required

问题3:Result Maps collection already contains value for com.mapper.FriendMapper.BaseResultMap

解决问题1:

错误原因:spring无法注入mapper的bean。

解决方法:在spring的配置文件中配置扫描mapper接口(注意:这里的扫描不同于spring的组件扫描)

  1. <!-- 扫描所有的mapper接口的实现,让这些mapper能够自动注入;
  2. base-package:指定mapper接口的包名
  3. -->
  4. <mybatis-spring:scan base-package="com.mapper"/>

解决问题2:

错误原因:sqlSessionFactory或sqlSessionTemplate是必须的

解决方法:在spring的配置文件中配置sqlsessionfactory及其属性

  1. <!-- 引入数据库的配置文件 -->
  2. <context:property-placeholder location="classpath:jdbc.properties" />
  3. <!-- 配置数据源-->
  4. <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
  5. <property name="Url" value="${jdbc.url}"></property>
  6. <property name="driverClassName" value="${jdbc.driver}"></property>
  7. <property name="username" value="${jdbc.username}"></property>
  8. <property name="password" value="${jdbc.password}"></property>
  9. </bean>
  10. <!-- 创建出SqlSessionFactory对象 -->
  11. <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
  12. <property name="dataSource" ref="dataSource"></property>
  13. <!-- configLocation指定全局配置文件的位置 -->
  14. <property name="configLocation" value="classpath:mybatis-config.xml"></property>
  15. <!--mapperLocations: 指定mapper文件的位置-->
  16. <property name="mapperLocations" value="classpath:com/mapper/*.xml"></property>
  17. </bean>

解决问题3:

错误原因:映射集合已经有了BaseResultMap,即出现了重复了映射关系。

解决方法:将mybatis的配置文件相关内容注释掉

  1. <!--引入映射文件-->
  2. <!-- <mappers>-->
  3. <!-- <package name="com.mapper"/>-->
  4. <!-- </mappers>-->

在此贴上ssm和web.xml的配置文件

spring:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd">
  7. <!-- 除了controller注解的组件,都扫描-->
  8. <context:component-scan base-package="com">
  9. <context:exclude-filter type="annotation"
  10. expression="org.springframework.stereotype.Controller" />
  11. </context:component-scan>
  12. <!-- 引入数据库的配置文件 -->
  13. <context:property-placeholder location="classpath:jdbc.properties" />
  14. <!-- Spring用来控制业务逻辑。数据源、事务控制、aop -->
  15. <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
  16. <property name="Url" value="${jdbc.url}"></property>
  17. <property name="driverClassName" value="${jdbc.driver}"></property>
  18. <property name="username" value="${jdbc.username}"></property>
  19. <property name="password" value="${jdbc.password}"></property>
  20. </bean>
  21. <!--
  22. <!- - spring事务管理 -->
  23. <!-- <bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">-->
  24. <!-- <property name="dataSource" ref="dataSource"></property>-->
  25. <!-- </bean>-->
  26. <!-- 开启基于注解的事务 -->
  27. <!-- <tx:annotation-driven transaction-manager="dataSourceTransactionManager"/>-->
  28. <!--创建出SqlSessionFactory对象 -->
  29. <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
  30. <property name="dataSource" ref="dataSource"></property>
  31. <!-- configLocation指定全局配置文件的位置 -->
  32. <property name="configLocation" value="classpath:mybatis-config.xml"></property>
  33. <!--mapperLocations: 指定mapper文件的位置-->
  34. <property name="mapperLocations" value="classpath:com/mapper/*.xml"></property>
  35. </bean>
  36. <!--配置一个可以进行批量执行的sqlSession -->
  37. <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
  38. <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactoryBean"></constructor-arg>
  39. <constructor-arg name="executorType" value="BATCH"></constructor-arg>
  40. </bean>
  41. <!-- 扫描所有的mapper接口的实现,让这些mapper能够自动注入;
  42. base-package:指定mapper接口的包名
  43. -->
  44. <mybatis-spring:scan base-package="com.mapper"/>
  45. <!-- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  46. <property name="basePackage" value="com.atguigu.mybatis.dao"></property>
  47. </bean> -->
  48. </beans>

springmvc:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
  6. <!-- 只扫描controller控制器-->
  7. <context:component-scan base-package="com" use-default-filters="false">
  8. <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
  9. </context:component-scan>
  10. <!-- <context:component-scan base-package="com.controller"></context:component-scan>-->
  11. <!-- 配置Thymeleaf视图解析器 -->
  12. <bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
  13. <property name="order" value="1"/>
  14. <property name="characterEncoding" value="UTF-8"/>
  15. <property name="templateEngine">
  16. <bean class="org.thymeleaf.spring5.SpringTemplateEngine">
  17. <property name="templateResolver">
  18. <bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
  19. <!-- 视图前缀 -->
  20. <property name="prefix" value="/"/>
  21. <!-- 视图后缀 -->
  22. <property name="suffix" value=".html"/>
  23. <property name="templateMode" value="HTML5"/>
  24. <property name="characterEncoding" value="UTF-8" />
  25. </bean>
  26. </property>
  27. </bean>
  28. </property>
  29. </bean>
  30. <!--
  31. 处理静态资源,例如html、js、css、jpg
  32. 若只设置该标签,则只能访问静态资源,其他请求则无法访问
  33. 此时必须设置<mvc:annotation-driven/>解决问题
  34. -->
  35. <mvc:default-servlet-handler/>
  36. <!-- 开启mvc注解驱动 -->
  37. <mvc:annotation-driven>
  38. <mvc:message-converters>
  39. <!-- 处理响应中文内容乱码 -->
  40. <bean class="org.springframework.http.converter.StringHttpMessageConverter">
  41. <property name="defaultCharset" value="UTF-8" />
  42. <property name="supportedMediaTypes">
  43. <list>
  44. <value>text/html</value>
  45. <value>application/json</value>
  46. </list>
  47. </property>
  48. </bean>
  49. </mvc:message-converters>
  50. </mvc:annotation-driven>
  51. </beans>

mybatis:

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE configuration
  3. PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-config.dtd">
  5. <configuration>
  6. <properties resource="jdbc.properties"></properties>
  7. <!-- 使用mybatis提供的配置,使得在执行sql语句时,自动将_类型的字段名换为驼峰-->
  8. <!-- <settings>-->
  9. <!-- <setting name="mapUnderscoreToCamelCase" value="true"/>-->
  10. <!-- </settings>-->
  11. <typeAliases>
  12. <package name="com.pojo"/>
  13. </typeAliases>
  14. <!--设置连接数据库的环境-->
  15. <environments default="development">
  16. <environment id="development">
  17. <transactionManager type="JDBC"/>
  18. <dataSource type="POOLED">
  19. <property name="driver" value="${jdbc.driver}"/>
  20. <property name="url" value="${jdbc.url}"/>
  21. <property name="username" value="${jdbc.username}"/>
  22. <property name="password" value="${jdbc.password}"/>
  23. </dataSource>
  24. </environment>
  25. </environments>
  26. <!--引入映射文件-->
  27. <!-- <mappers>-->
  28. <!-- <package name="com.mapper"/>-->
  29. <!-- </mappers>-->
  30. </configuration>

web.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
  5. version="4.0">
  6. <!-- 设置Request和Response编码集-->
  7. <filter>
  8. <filter-name>CharacterEncodingFilter</filter-name>
  9. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  10. <init-param>
  11. <param-name>encoding</param-name>
  12. <param-value>UTF-8</param-value>
  13. </init-param>
  14. <init-param>
  15. <param-name>forceResponseEncoding</param-name>
  16. <param-value>true</param-value>
  17. </init-param>
  18. <init-param>
  19. <param-name>forceRequestEncoding</param-name>
  20. <param-value>true</param-value>
  21. </init-param>
  22. </filter>
  23. <filter-mapping>
  24. <filter-name>CharacterEncodingFilter</filter-name>
  25. <url-pattern>/*</url-pattern>
  26. </filter-mapping>
  27. <!--配置spring ioc的配置路径-->
  28. <context-param>
  29. <param-name>contextConfigLocation</param-name>
  30. <param-value>classpath:applicationContext.xml</param-value>
  31. </context-param>
  32. <!-- 注册spring的监听器用于初始化ioc-->
  33. <listener>
  34. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  35. </listener>
  36. <!-- 配置SpringMVC的前端控制器,对浏览器发送的请求统一进行处理 -->
  37. <servlet>
  38. <servlet-name>springMVC</servlet-name>
  39. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  40. <!-- 通过初始化参数指定SpringMVC配置文件的位置和名称 -->
  41. <init-param>
  42. <!-- contextConfigLocation为固定值 -->
  43. <param-name>contextConfigLocation</param-name>
  44. <!-- 使用classpath:表示从类路径查找配置文件,例如maven工程中的src/main/resources -->
  45. <param-value>classpath:springMVC.xml</param-value>
  46. </init-param>
  47. <!--
  48. 作为框架的核心组件,在启动过程中有大量的初始化操作要做
  49. 而这些操作放在第一次请求时才执行会严重影响访问速度
  50. 因此需要通过此标签将启动控制DispatcherServlet的初始化时间提前到服务器启动时
  51. -->
  52. <load-on-startup>1</load-on-startup>
  53. </servlet>
  54. <servlet-mapping>
  55. <servlet-name>springMVC</servlet-name>
  56. <!--
  57. 设置springMVC的核心控制器所能处理的请求的请求路径
  58. /所匹配的请求可以是/login或.html或.js或.css方式的请求路径
  59. 但是/不能匹配.jsp请求路径的请求
  60. -->
  61. <url-pattern>/</url-pattern>
  62. </servlet-mapping>
  63. </web-app>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/一键难忘520/article/detail/946601
推荐阅读
相关标签
  

闽ICP备14008679号