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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值