【SSM框架】SSM整合的配置文件

本文详细介绍SSM(Spring+SpringMVC+MyBatis)框架的整合过程,包括JDBC数据库配置、web.xml核心配置、Spring核心配置、MyBatis整合及SpringMVC核心配置。涵盖数据源设置、业务组件注册、REST风格URI配置、事务管理等关键环节。

JDBC数据库配置文件

文件名:jdbc.properties

文件路径:src/main/resources/jdbc.properties

文件内容:

jdbc.driver=com.mysql.jdbc.Driver

jdbc.url=jdbc:mysql:///ssmcrud?useSSL=true

jdbc.username=root

jdbc.password=123456

web.xml核心配置文件

文件名:web.xml

路径:src/main/webapp/WEB-INF/web.xml

配置内容:

1. 文件头

<?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/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd

id=“WebApp_ID” version=“2.5”>

2. 注册spring配置文件的位置

  <param-name>contextConfigLocation</param-name>

  <param-value>classpath:applicationContext.xml</param-value>

3. 注册servletcontext监听器

  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

4. 注册字符集过滤器

  <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>forceReponseEncoding</param-name>

    <param-value>true</param-value>

  </init-param>
  <filter-name>CharacterEncodingFilter</filter-name>

  <url-pattern>/*</url-pattern>

5. 配置springMVC中央调度器

  <servlet-name>springmvc</servlet-name>

  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

  <init-param>

    <param-name>contextConfigLocation</param-name>

    <param-value>classpath:spring-mvc.xml</param-value>

  </init-param>

  <load-on-startup>1</load-on-startup>
  <servlet-name>springmvc</servlet-name>

  <url-pattern>/</url-pattern>

6. 配置Rest风格的URI(可选)

  <filter-name>HiddenHttpMethodFilter</filter-name>

  <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
  <filter-name>HiddenHttpMethodFilter</filter-name>

  <url-pattern>/*</url-pattern>

7. 解决Tomcat对PUT请求不封装数据体的问题(可选)

  <filter-name>HttpPutFormContentFilter</filter-name>

  <filter-class>org.springframework.web.filter.HttpPutFormContentFilter</filter-class>
  <filter-name>HttpPutFormContentFilter</filter-name>

  <url-pattern>/*</url-pattern>

Spring核心配置文件

文件名:applicationContext.xml

路径:/src/main/resource/applicationContext.xml

配置内容:

1. 文件头

<?xml version="1.0"encoding="UTF-8"?>

<beans xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance

xmlns=“http://www.springframework.org/schema/beans

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/beans

http://www.springframework.org/schema/beans/spring-beans-4.2.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-4.2.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop-4.2.xsd

http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx.xsd">

2. 数据源配置

<context:property-placeholder location=“classpath:jdbc.properties”/>

<bean name="C3P0DataSource"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>

3. 业务注册

<context:component-scan base-package=“com.crud.service” />

  1. 整合Mybatis

<bean id="sqlSessionFactory"class=“org.mybatis.spring.SqlSessionFactoryBean”>

  <!-- 指定mybatis主配置文件的位置 -->

  <property name="configLocation"value="classpath:mybatis.xml"/>

  <!-- 连接池注入 -->

  <property name="dataSource"ref="C3P0DataSource"/>

  <!-- 指定mapper文件的位置 -->

  <property name="mapperLocations"value="classpath:mapper/*.xml"/>
  <property name="sqlSessionFactoryBeanName"value="sqlSessionFactory"/>

  <!-- 扫描所有的DAO接口加入到IOC容器中 -->

  <property name="basePackage"value="com.crud.dao" />

5. 配置批量执行的sqlsession(可选)

<bean id="sqlSession"class=“org.mybatis.spring.SqlSessionTemplate”>

  <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory" />

  <constructor-arg name="executorType" value="BATCH" />

6. 事务配置

<bean id=“transactionManager”

class=“org.springframework.jdbc.datasource.DataSourceTransactionManager”>

  <!-- 控制住数据源 -->

  <property name="dataSource"ref="C3P0DataSource"/>

<tx:advice id="transactionAdvice"transaction-manager=“transactionManager”>

  <tx:attributes>

    <!-- 所有方法都是事务方法 -->

    <tx:method name="*" isolation="DEFAULT"propagation="REQUIRED"/>

    <!-- get查询方法为只读,优化查询效率 -->

    <tx:method name="get*" read-only="true"/>

  </tx:attributes>

</tx:advice>

aop:config

  <aop:pointcut expression="execution(* com.crud.service..*(..))"id="myPointCut"/>

  <aop:advisor advice-ref="transactionAdvice" pointcut-ref="myPointCut"/>

</aop:config>

SpringMVC核心配置文件

文件名:spring-mvc.xml

路径:/src/main/resources/spring-mvc.xml

配置内容:

1. 表头

<?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:p="http://www.springframework.org/schema/p

xmlns:context=“http://www.springframework.org/schema/context

xmlns:mvc=“http://www.springframework.org/schema/mvc

xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.0.xsd

   http://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd

   http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.0.xsd">

2. 配置组件扫描器

<context:component-scan base-package=“com.crud.controller” />

3. 配置视图解析器

  <!-- 前缀 -->

  <property name="prefix"value="/WEB-INF/views/" />

  <!-- 后缀 -->

  <property name="suffix"value=".jsp" />
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值