SSM框架整合

web.xml配置

步骤

1.配置Spring的ContextLoaderListener,作用:在启动Web容器时,自动装配Spring applicationContext.xml的配置信息

2.设置Spring配置文件路径

3.配置SpringMVC,设置DispatcherServlet,以及拦截的路径

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
	<display-name>mybatis-ssm</display-name>
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
	</welcome-file-list>
    
	
	<!-- 
		配置ContextLoaderListener  加载器
		作用:在启动Web容器时,自动装配Spring applicationContext.xml的配置信息
		Bootstraps the root web application context before servlet initialization 
	-->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
    
	<!-- 
		配置spring配置文件路径
		needed for ContextLoaderListener 
	-->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext.xml</param-value>
	</context-param>
	
	<!-- 
		注册springMVC
		The front controller of this Spring Web application, responsible for handling all application requests 
	-->
	<servlet>
		<servlet-name>spring</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<!-- 
			配置SpringMVC配置文件加载路径 ,如果不配置则使用 spring-servlet默认名称
		-->
		<init-param>
		    <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring-servlet.xml</param-value>
		</init-param>
		<!-- 被加载的顺序,如果为负数,在访问时加载 -->
		<load-on-startup>1</load-on-startup>
	</servlet>

	<!--
		配置拦截路径
	 	Map all requests to the DispatcherServlet for handling 
	 -->
	<servlet-mapping>
		<servlet-name>spring</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
</web-app>

SpringMVC配置文件

步骤

1.配置context:component-scan,自动扫描

2.启用mvc:annotation-driven和mvc:default-servlet-handler

3.配置InternalResourceViewResolver,视图解析器

spring-servlet.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"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.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.0.xsd">
		
	<!-- 配置自动扫描 -->
	<context:component-scan base-package="cn.zyx.demo">
	    <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
	</context:component-scan>
	
	<!-- 
		相当于注册了DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter两个bean,
		配置一些messageconverter。即解决了@Controller注解的使用前提配置。
		要使用spring mvc中的@Controller注解,就必须要配置<mvc:annotation-driven />
	-->
	<mvc:annotation-driven></mvc:annotation-driven>
	
	<!-- 
		DispatcherServlet请求映射配置为"/"时解决对静态资源的支持
	 -->
	<mvc:default-servlet-handler/>
	
	<!-- 
		配置视图解析器
	 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix"  value="/WEB-INF/pages/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>
</beans>

Spring 配置文件

步骤

1.配置context:component-scan,自动扫描

2.配置c3p0连接池

3.开启Spring事务管理的,以及基于注解的事务管理tx:annotation-driven

3.整合Mybatis,配置SqlSessionFactoryBean

4.mybatis-spring:scan,扫描所有的Mapper接口的实现,实现自动注入

applicationConext.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"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
	xsi:schemaLocation="http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.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.0.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

	<!-- 管理业务逻辑 -->
	<context:component-scan base-package="cn.zyx.demo" use-default-filters="false">
		<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
	</context:component-scan>
	
	<!-- 引入数据配置文件 -->
	<context:property-placeholder location="classpath:dbconfig.properties"/>
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="jdbcUrl" value="${jdbc.url}"></property>
		<property name="driverClass" value="${jdbc.driver}"></property>
		<property name="user" value="${jdbc.username}"> </property>
		<property name="password" value="${jdbc.password}"></property>
	</bean>
	
	<!-- Spring事务管理 -->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	<!-- 开启基于注解的事务管理 -->
	<tx:annotation-driven transaction-manager="transactionManager"/>
	
	<!-- 整合mybatis 
		目的:1.spring管理所有组件。mapper实现类
		        service==>dao  @Autowired:自动注入Mapper
		    2.spring管理事物  声明事物
	-->
	<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource"></property>
		<property name="configLocation" value="classpath:mybatis-config.xml"></property>
		<property name="mapperLocations" value="classpath:mybatis/mapper/*.xml"></property>
	</bean>
	
	<!-- 扫描所有的Mapper接口的实现,实现自动注入 -->
	<mybatis-spring:scan base-package="cn.zyx.demo.dao"/>
	
</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 name="cacheEnabled" value="true" />
        <setting name="mapUnderscoreToCamelCase" value="true"></setting>
    </settings>
    
</configuration>

dbconfig.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mydata?serverTimezone=UTC
jdbc.username=root
jdbc.password=123456
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值