SpringMVC——Spring整合Mybatis和SpringMVC

Posted Johnny*

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringMVC——Spring整合Mybatis和SpringMVC相关的知识,希望对你有一定的参考价值。

SSM 完结撒花

Spring先整合Mybatis

  1. 读取dp.properties文件
  2. 配置数据库信息、数据源(datasource)
  3. 在SpringIOC容器中,创建MyBatis的核心类SqlSessionFactory
  4. 将Mybaits的SqlSessionFactory交Spring

Spring整合SpringMVC

web.xml中配置控制器

  1. 配置视图解析器
  2. 开启注解(SpringMVC标配)

在这里插入图片描述

applicationContext.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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
	
	
	
	<!-- 1. 读取dp.properties文件  -->
	<bean  id="config" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
		<!--  PropertiesLoaderSupport类提供了
		private org.springframework.core.io.Resource[] locations -->
		<property name="locations">
			<array>
				<value>classpath:config/db.properties</value>
			</array>
		
		</property>
	</bean>
	<!-- 2. 配置数据库信息、数据源(datasource) -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName" value="${driver}"></property>
		<property name="url" value="${url}"></property>
		<property name="username" value="${username}"></property>
		<property name="password" value="${password}"></property>
	</bean>
	
	<!-- 3. 在SpringIOC容器中,创建MyBatis的核心类SqlSessionFactory -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- 配置数据源 -->
		<property name="dataSource" ref="dataSource"></property> 
		<!-- 指定Mybaits核心配置文件 -->
		<property name="configLocation" value="classpath:config/mybatis-config.xml"></property>
	</bean>
	
	<!-- 4. 将Mybaits的SqlSessionFactory交Spring -->
	<bean name="mappers" class ="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<!-- 配置mapper扫描器  -->
	 	<!-- String basePackage 指定批量产生该包下的Mapper对象  -->
	 	<property name="basePackage" value="com.johnny.dao"></property>
	 	<!-- String sqlSessionFactoryBeanName -->	
	 	<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
	 </bean>
	 
	 <!-- 扫描带注解的包 -->
	<context:component-scan base-package="com.johnny.*" />
	 
</beans>

mybatis-config.xml

<?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>
	<settings>
		<!-- 开启日志 -->
		<setting name="logImpl" value="LOG4J"></setting>
	</settings>

	<!-- 设置类别名 -->
	<typeAliases>
		<package name="com.johnny.entity" />
	</typeAliases>

</configuration>

springmvc.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:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
	
	
	<!-- 1.  配置视图解析器 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/views/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>
	<!--2. 开启注解(SpringMVC标配) -->
	<mvc:annotation-driven></mvc:annotation-driven>
	
	<!--3. 配置默认Servlet使得静态资源可以通过 链接直接访问
	默认servlet:SpringMVC没有相应requestMapping时会转交给Tomcat默认的servlet处理 -->
	<mvc:default-servlet-handler/>
	
	<!-- 4.  配置注解扫描器 -->
	<context:component-scan base-package="com.johnny.controller"></context:component-scan>
	
</beans>

log4j.properties

# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# MyBatis logging configuration...
log4j.logger.org.mybatis.example.BlogMapper=TRACE
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

Caused by: java.lang.IllegalArgumentException: Could not resolve resource location pattern [com/johnny/dao/*.xml]: ServletContext resource [/com/johnny/dao/] cannot be resolved to URL because it does not exist
	at org.springframework.core.io.support.ResourceArrayPropertyEditor.setAsText(ResourceArrayPropertyEditor.java:120)
	at org.springframework.beans.TypeConverterDelegate.doConvertTextValue(TypeConverterDelegate.java:429)
	at org.springframework.beans.TypeConverterDelegate.doConvertValue(TypeConverterDelegate.java:402)
	at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:155)
	at org.springframework.beans.AbstractNestablePropertyAccessor.convertIfNecessary(AbstractNestablePropertyAccessor.java:585)
	... 47 more
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource"></property> 	
		<!-- 加载mapper.xml路径 -->
		<property name="mapperLocations" value="com/johnny/dao/*.xml"></property>
	</bean>

在Mybaits和Spring中是可以的,SSM整合的时候却提示找不到 ,那么就指定路径

	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource"></property> 	
		<!-- 加载mapper.xml路径 -->
		<property name="mapperLocations" value="classpath:com/johnny/dao/*.xml"></property>
	</bean>

以上是关于SpringMVC——Spring整合Mybatis和SpringMVC的主要内容,如果未能解决你的问题,请参考以下文章

Spring Boot整合Mybati之逆向工程

MyBatis与Spring整合

SSM框架整合—详细整合教程(Spring+SpringMVC+MyBatis)

SSM框架整合—详细整合教程(Spring+SpringMVC+MyBatis)

SpringMVC学习--springmvc和mybatis整合

Spring+SpringMVC+Mybatis+Mysql整合实例