SSM整合时配置文件的编写

编写web.xml

1.配置项目初始化时启动Spring容器

传递参数,spring配置文件的位置

<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:applicationContext.xml</param-value>
 </context-param>

<!-- Bootstraps the root web application context before servlet initialization -->
 <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>

其中applicationContext.xml为spring配置文件,位置在src/main/resource目录下。

2.编写SpringMVC前段控制器,拦截所有请求

方式一:

在param中通过location指定springMVC配置文件的位置

<servlet>
  <servlet-name>dispatcherServlet</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>location</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
 </servlet>

 <!-- Map all requests to the DispatcherServlet for handling -->
 <servlet-mapping>
  <servlet-name>dispatcherServlet</servlet-name>
  <url-pattern>/</url-pattern>
 </servlet-mapping>

方式二:

去掉init-param,名字统一改为dispatcherServlet,然后在与web.xml同级目录下新建dispatcherServlet-servlet.xml

其中dispatcherServlet要与上面的servlet-name一致。 

<servlet>
  <servlet-name>dispatcherServlet</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <!-- <init-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>location</param-value>
  </init-param> -->
  <load-on-startup>1</load-on-startup>
 </servlet>

 <!-- Map all requests to the DispatcherServlet for handling -->
 <servlet-mapping>
  <servlet-name>dispatcherServlet</servlet-name>
  <url-pattern>/</url-pattern>
 </servlet-mapping>

3.编写springMVC带的字符编码过滤器,字符编码器要放在所有过滤器之前

<!-- 字符编码过滤器,一定要放在所有过滤器之前 -->
 <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>forceRequestEncoding</param-name>
   <param-value>true</param-value>
  </init-param>
  <init-param>
   <param-name>forceResponseEncoding</param-name>
   <param-value>true</param-value>
  </init-param>
 </filter>
 <filter-mapping>
  <filter-name>CharacterEncodingFilter</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>

4.配置Rest风格的URL的过滤器

<!-- 使用Rest风格的URI,将页面普通的post请求转为指定的delete或者put请求 -->
 <filter>
  <filter-name>HiddenHttpMethodFilter</filter-name>
  <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
 </filter>
 <filter-mapping>
  <filter-name>HiddenHttpMethodFilter</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>
 <filter>
  <filter-name>HttpPutFormContentFilter</filter-name>
  <filter-class>org.springframework.web.filter.HttpPutFormContentFilter</filter-class>
 </filter>
 <filter-mapping>
  <filter-name>HttpPutFormContentFilter</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>

5.完整的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_2_5.xsd"
 id="WebApp_ID" version="2.5">
 
 <!--1、启动Spring的容器  -->
 <!-- needed for ContextLoaderListener -->
 <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:applicationContext.xml</param-value>
 </context-param>

 <!-- Bootstraps the root web application context before servlet initialization -->
 <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>
 
 <!--2、springmvc的前端控制器,拦截所有请求  -->
 <!-- The front controller of this Spring Web application, responsible for handling all application requests -->
 <servlet>
  <servlet-name>dispatcherServlet</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <!-- <init-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>location</param-value>
  </init-param> -->
  <load-on-startup>1</load-on-startup>
 </servlet>

 <!-- Map all requests to the DispatcherServlet for handling -->
 <servlet-mapping>
  <servlet-name>dispatcherServlet</servlet-name>
  <url-pattern>/</url-pattern>
 </servlet-mapping>
 
 <!-- 3、字符编码过滤器,一定要放在所有过滤器之前 -->
 <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>forceRequestEncoding</param-name>
   <param-value>true</param-value>
  </init-param>
  <init-param>
   <param-name>forceResponseEncoding</param-name>
   <param-value>true</param-value>
  </init-param>
 </filter>
 <filter-mapping>
  <filter-name>CharacterEncodingFilter</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>
 
 <!-- 4、使用Rest风格的URI,将页面普通的post请求转为指定的delete或者put请求 -->
 <filter>
  <filter-name>HiddenHttpMethodFilter</filter-name>
  <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
 </filter>
 <filter-mapping>
  <filter-name>HiddenHttpMethodFilter</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>
 <filter>
  <filter-name>HttpPutFormContentFilter</filter-name>
  <filter-class>org.springframework.web.filter.HttpPutFormContentFilter</filter-class>
 </filter>
 <filter-mapping>
  <filter-name>HttpPutFormContentFilter</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>
 
 
</web-app>

编写springMVC的配置文件

在上面方式二新建的dispatcherServlet-servlet.xml中进行配置

主要配置网站的跳转逻辑的控制、配置视图解析器,方便页面返回等。

所以我们先在项目下新建相应的mvc结构的包。

1.配置扫描组件

<!--SpringMVC的配置文件,包含网站跳转逻辑的控制,配置  -->
 <context:component-scan base-package="com.badao" use-default-filters="false">
  <!--只扫描控制器。  -->
  <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
 </context:component-scan>

SpringMVC默认是扫描所有的,所以为了让它只扫描控制器,要加上use-default-filters="false"

2.配置视图解析器

<!--配置视图解析器,方便页面返回  -->
 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="prefix" value="/WEB-INF/views/"></property>
  <property name="suffix" value=".jsp"></property>
 </bean>

在WEB-INF目录下新建views目录,用来存放jsp页面,配置好前缀与后缀,就能解析视图,返回相应页面。

3.SpringMVC的两个标准配置

 <!--两个标准配置  -->
 <!-- 将springmvc不能处理的请求交给tomcat,这样动态与静态资源就都能访问了 -->
 <mvc:default-servlet-handler/>
 <!-- 能支持springmvc更高级的一些功能,JSR303校验,快捷的ajax...映射动态请求 -->
 <mvc:annotation-driven/>

4.完整配置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: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.3.xsd
  http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">

 <!--SpringMVC的配置文件,包含网站跳转逻辑的控制,配置  -->
 <context:component-scan base-package="com.badao" use-default-filters="false">
  <!--只扫描控制器。  -->
  <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
 </context:component-scan>
 
 <!--配置视图解析器,方便页面返回  -->
 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="prefix" value="/WEB-INF/views/"></property>
  <property name="suffix" value=".jsp"></property>
 </bean>
 
 <!--两个标准配置  -->
 <!-- 将springmvc不能处理的请求交给tomcat,这样动态与静态资源就都能访问了 -->
 <mvc:default-servlet-handler/>
 <!-- 能支持springmvc更高级的一些功能,JSR303校验,快捷的ajax...映射动态请求 -->
 <mvc:annotation-driven/>

</beans>

编写spring的配置文件

在src/main/resource目录下新建applicationContext.xml

这里主要配置和业务逻辑有关的,比如数据源、事务控制等。

1.配置数据源

<context:property-placeholder location="classpath:dbconfig.properties" />
 <bean id="pooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
  <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
  <property name="driverClass" value="${jdbc.driverClass}"></property>
  <property name="user" value="${jdbc.user}"></property>
  <property name="password" value="${jdbc.password}"></property>
 </bean>

其中location="classpath:dbconfig.properties" 就是属性文件存放的位置。

通过<context:property-placeholder location="classpath:dbconfig.properties" />就可以将属性文件引入。

2.新建属性文件dbconfig.properties

在resource下新建dbconfig.properties文件,用来存取数据源的相关属性,在applicationContext.xml中就可以通过

${}来获取属性。

jdbc.jdbcUrl=jdbc:mysql://localhost:3306/ssm_crud
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.user=root
jdbc.password=123456

这里ssm_crud是建立的mysql的数据库的名字

root与123456是对应自己的连接数据库的用户名和密码

3.配置扫描业务逻辑组件

<context:component-scan base-package="com.badao">
  <context:exclude-filter type="annotation"
   expression="org.springframework.stereotype.Controller" />
 </context:component-scan>

4.配置和Mybatis整合

<!--================== 配置和MyBatis的整合=============== -->
 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  <!-- 指定mybatis全局配置文件的位置 -->
  <property name="configLocation" value="classpath:mybatis-config.xml"></property>
  <property name="dataSource" ref="pooledDataSource"></property>
  <!-- 指定mybatis,mapper文件的位置 -->
  <property name="mapperLocations" value="classpath:mapper/*.xml"></property>
 </bean>

所以在resource下新建mapper目录用来存放mapper文件,在上面已经声明位置。

5.配置扫描器

将mybatis接口的实现加入到ioc容器中

<!-- 配置扫描器,将mybatis接口的实现加入到ioc容器中 -->
 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  <!--扫描所有dao接口的实现,加入到ioc容器中 -->
  <property name="basePackage" value="com.badao.ssm.dao"></property>
 </bean>

6.配置事务控制

<!-- ===============事务控制的配置 ================-->
 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  <!--控制数据源  -->
  <property name="dataSource" ref="pooledDataSource"></property>
 </bean>
 <!--开启基于注解的事务,使用xml配置形式的事务(必要主要的都是使用配置式)  -->
 <aop:config>
  <!-- 切入点表达式 此包下的所有类的所有方法都切入事务-->
  <aop:pointcut expression="execution(* com.badao.ssm.service..*(..))" id="txPoint"/>
  <!-- 配置事务增强 -->
  <aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint"/>
 </aop:config>
 
 <!--配置事务增强,事务如何切入  -->
 <tx:advice id="txAdvice" transaction-manager="transactionManager">
  <tx:attributes>
   <!-- 所有方法都是事务方法 -->
   <tx:method name="*"/>
   <!--以get开始的所有方法  -->
   <tx:method name="get*" read-only="true"/>
  </tx:attributes>
 </tx:advice>

7.完整的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:aop="http://www.springframework.org/schema/aop"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
  http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
  http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">

 <context:component-scan base-package="com.badao">
  <context:exclude-filter type="annotation"
   expression="org.springframework.stereotype.Controller" />
 </context:component-scan>

 <!-- Spring的配置文件,这里主要配置和业务逻辑有关的 -->
 <!--=================== 数据源,事务控制,xxx ================-->
 <context:property-placeholder location="classpath:dbconfig.properties" />
 <bean id="pooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
  <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
  <property name="driverClass" value="${jdbc.driverClass}"></property>
  <property name="user" value="${jdbc.user}"></property>
  <property name="password" value="${jdbc.password}"></property>
 </bean>

 <!--================== 配置和MyBatis的整合=============== -->
 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  <!-- 指定mybatis全局配置文件的位置 -->
  <property name="configLocation" value="classpath:mybatis-config.xml"></property>
  <property name="dataSource" ref="pooledDataSource"></property>
  <!-- 指定mybatis,mapper文件的位置 -->
  <property name="mapperLocations" value="classpath:mapper/*.xml"></property>
 </bean>

 <!-- 配置扫描器,将mybatis接口的实现加入到ioc容器中 -->
 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  <!--扫描所有dao接口的实现,加入到ioc容器中 -->
  <property name="basePackage" value="com.badao.ssm.dao"></property>
 </bean>
 
 <!-- 配置一个可以执行批量的sqlSession -->
 <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
  <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg>
  <constructor-arg name="executorType" value="BATCH"></constructor-arg>
 </bean>
 <!--=============================================  -->

 <!-- ===============事务控制的配置 ================-->
 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  <!--控制数据源  -->
  <property name="dataSource" ref="pooledDataSource"></property>
 </bean>
 <!--开启基于注解的事务,使用xml配置形式的事务(必要主要的都是使用配置式)  -->
 <aop:config>
  <!-- 切入点表达式 此包下的所有类的所有方法都切入事务-->
  <aop:pointcut expression="execution(* com.badao.ssm.service..*(..))" id="txPoint"/>
  <!-- 配置事务增强 -->
  <aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint"/>
 </aop:config>
 
 <!--配置事务增强,事务如何切入  -->
 <tx:advice id="txAdvice" transaction-manager="transactionManager">
  <tx:attributes>
   <!-- 所有方法都是事务方法 -->
   <tx:method name="*"/>
   <!--以get开始的所有方法  -->
   <tx:method name="get*" read-only="true"/>
  </tx:attributes>
 </tx:advice>
 
 
 
</beans>

编写MyBatis的配置文件

在resource下新建mybatis-config.xml,作为全局配置文件

1.开启驼峰命名规则

<settings>
  <setting name="mapUnderscoreToCamelCase" value="true"/>
 </settings>

2.开启别名

<typeAliases>
  <package name="com.badao.ssm.bean"/>
 </typeAliases>

3.完整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="mapUnderscoreToCamelCase" value="true"/>
 </settings>
 
 <typeAliases>
  <package name="com.badao.ssm.bean"/>
 </typeAliases>

</configuration>

代码下载

本阶段示例代码下载

https://download.csdn.net/download/badao_liumang_qizhi/10857091

猜你喜欢

转载自blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/85058256