赞
踩
本章内容只分享个人问题:例如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的组件扫描)
- <!-- 扫描所有的mapper接口的实现,让这些mapper能够自动注入;
- base-package:指定mapper接口的包名
- -->
- <mybatis-spring:scan base-package="com.mapper"/>
解决问题2:
错误原因:sqlSessionFactory或sqlSessionTemplate是必须的
解决方法:在spring的配置文件中配置sqlsessionfactory及其属性
- <!-- 引入数据库的配置文件 -->
- <context:property-placeholder location="classpath:jdbc.properties" />
-
- <!-- 配置数据源-->
- <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
- <property name="Url" value="${jdbc.url}"></property>
- <property name="driverClassName" value="${jdbc.driver}"></property>
- <property name="username" value="${jdbc.username}"></property>
- <property name="password" value="${jdbc.password}"></property>
- </bean>
-
- <!-- 创建出SqlSessionFactory对象 -->
- <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
- <property name="dataSource" ref="dataSource"></property>
- <!-- configLocation指定全局配置文件的位置 -->
- <property name="configLocation" value="classpath:mybatis-config.xml"></property>
- <!--mapperLocations: 指定mapper文件的位置-->
- <property name="mapperLocations" value="classpath:com/mapper/*.xml"></property>
- </bean>
解决问题3:
错误原因:映射集合已经有了BaseResultMap,即出现了重复了映射关系。
解决方法:将mybatis的配置文件相关内容注释掉
- <!--引入映射文件-->
- <!-- <mappers>-->
- <!-- <package name="com.mapper"/>-->
- <!-- </mappers>-->
在此贴上ssm和web.xml的配置文件
spring:
- <?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:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
- 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">
-
- <!-- 除了controller注解的组件,都扫描-->
- <context:component-scan base-package="com">
- <context:exclude-filter type="annotation"
- expression="org.springframework.stereotype.Controller" />
- </context:component-scan>
-
- <!-- 引入数据库的配置文件 -->
- <context:property-placeholder location="classpath:jdbc.properties" />
- <!-- Spring用来控制业务逻辑。数据源、事务控制、aop -->
- <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
- <property name="Url" value="${jdbc.url}"></property>
- <property name="driverClassName" value="${jdbc.driver}"></property>
- <property name="username" value="${jdbc.username}"></property>
- <property name="password" value="${jdbc.password}"></property>
- </bean>
- <!--
- <!- - spring事务管理 -->
- <!-- <bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">-->
- <!-- <property name="dataSource" ref="dataSource"></property>-->
- <!-- </bean>-->
- <!-- 开启基于注解的事务 -->
- <!-- <tx:annotation-driven transaction-manager="dataSourceTransactionManager"/>-->
- <!--创建出SqlSessionFactory对象 -->
- <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
- <property name="dataSource" ref="dataSource"></property>
- <!-- configLocation指定全局配置文件的位置 -->
- <property name="configLocation" value="classpath:mybatis-config.xml"></property>
- <!--mapperLocations: 指定mapper文件的位置-->
- <property name="mapperLocations" value="classpath:com/mapper/*.xml"></property>
- </bean>
-
- <!--配置一个可以进行批量执行的sqlSession -->
- <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
- <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactoryBean"></constructor-arg>
- <constructor-arg name="executorType" value="BATCH"></constructor-arg>
- </bean>
-
- <!-- 扫描所有的mapper接口的实现,让这些mapper能够自动注入;
- base-package:指定mapper接口的包名
- -->
- <mybatis-spring:scan base-package="com.mapper"/>
- <!-- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
- <property name="basePackage" value="com.atguigu.mybatis.dao"></property>
- </bean> -->
-
- </beans>
springmvc:
- <?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:mvc="http://www.springframework.org/schema/mvc"
- xmlns:context="http://www.springframework.org/schema/context"
- 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">
-
- <!-- 只扫描controller控制器-->
- <context:component-scan base-package="com" use-default-filters="false">
- <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
- </context:component-scan>
-
- <!-- <context:component-scan base-package="com.controller"></context:component-scan>-->
- <!-- 配置Thymeleaf视图解析器 -->
- <bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
- <property name="order" value="1"/>
- <property name="characterEncoding" value="UTF-8"/>
- <property name="templateEngine">
- <bean class="org.thymeleaf.spring5.SpringTemplateEngine">
- <property name="templateResolver">
- <bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
-
- <!-- 视图前缀 -->
- <property name="prefix" value="/"/>
-
- <!-- 视图后缀 -->
- <property name="suffix" value=".html"/>
- <property name="templateMode" value="HTML5"/>
- <property name="characterEncoding" value="UTF-8" />
- </bean>
- </property>
- </bean>
- </property>
- </bean>
-
- <!--
- 处理静态资源,例如html、js、css、jpg
- 若只设置该标签,则只能访问静态资源,其他请求则无法访问
- 此时必须设置<mvc:annotation-driven/>解决问题
- -->
- <mvc:default-servlet-handler/>
-
- <!-- 开启mvc注解驱动 -->
- <mvc:annotation-driven>
- <mvc:message-converters>
- <!-- 处理响应中文内容乱码 -->
- <bean class="org.springframework.http.converter.StringHttpMessageConverter">
- <property name="defaultCharset" value="UTF-8" />
- <property name="supportedMediaTypes">
- <list>
- <value>text/html</value>
- <value>application/json</value>
- </list>
- </property>
- </bean>
- </mvc:message-converters>
- </mvc:annotation-driven>
- </beans>
mybatis:
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE configuration
- PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
- "http://mybatis.org/dtd/mybatis-3-config.dtd">
- <configuration>
- <properties resource="jdbc.properties"></properties>
-
- <!-- 使用mybatis提供的配置,使得在执行sql语句时,自动将_类型的字段名换为驼峰-->
- <!-- <settings>-->
- <!-- <setting name="mapUnderscoreToCamelCase" value="true"/>-->
- <!-- </settings>-->
-
- <typeAliases>
- <package name="com.pojo"/>
- </typeAliases>
- <!--设置连接数据库的环境-->
- <environments default="development">
- <environment id="development">
- <transactionManager type="JDBC"/>
- <dataSource type="POOLED">
- <property name="driver" value="${jdbc.driver}"/>
- <property name="url" value="${jdbc.url}"/>
- <property name="username" value="${jdbc.username}"/>
- <property name="password" value="${jdbc.password}"/>
- </dataSource>
- </environment>
- </environments>
- <!--引入映射文件-->
- <!-- <mappers>-->
- <!-- <package name="com.mapper"/>-->
- <!-- </mappers>-->
- </configuration>
web.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
- version="4.0">
- <!-- 设置Request和Response编码集-->
- <filter>
- <filter-name>CharacterEncodingFilter</filter-name>
- <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
- <init-param>
- <param-name>encoding</param-name>
- <param-value>UTF-8</param-value>
- </init-param>
- <init-param>
- <param-name>forceResponseEncoding</param-name>
- <param-value>true</param-value>
- </init-param>
- <init-param>
- <param-name>forceRequestEncoding</param-name>
- <param-value>true</param-value>
- </init-param>
- </filter>
- <filter-mapping>
- <filter-name>CharacterEncodingFilter</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
-
-
- <!--配置spring ioc的配置路径-->
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:applicationContext.xml</param-value>
- </context-param>
- <!-- 注册spring的监听器用于初始化ioc-->
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
-
-
-
- <!-- 配置SpringMVC的前端控制器,对浏览器发送的请求统一进行处理 -->
- <servlet>
- <servlet-name>springMVC</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <!-- 通过初始化参数指定SpringMVC配置文件的位置和名称 -->
- <init-param>
- <!-- contextConfigLocation为固定值 -->
- <param-name>contextConfigLocation</param-name>
- <!-- 使用classpath:表示从类路径查找配置文件,例如maven工程中的src/main/resources -->
- <param-value>classpath:springMVC.xml</param-value>
- </init-param>
- <!--
- 作为框架的核心组件,在启动过程中有大量的初始化操作要做
- 而这些操作放在第一次请求时才执行会严重影响访问速度
- 因此需要通过此标签将启动控制DispatcherServlet的初始化时间提前到服务器启动时
- -->
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>springMVC</servlet-name>
- <!--
- 设置springMVC的核心控制器所能处理的请求的请求路径
- /所匹配的请求可以是/login或.html或.js或.css方式的请求路径
- 但是/不能匹配.jsp请求路径的请求
- -->
- <url-pattern>/</url-pattern>
- </servlet-mapping>
- </web-app>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。