SSM框架的整合

本文介绍了一个基于SpringMVC与MyBatis框架整合的项目配置过程,包括web.xml、applicationContext-*.xml等核心配置文件的详细设置。通过这些配置实现了项目的初始化、数据源管理、事务处理等功能。

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"

   id="WebApp_ID" version="3.0">

   <display-name>carRentalProject</display-name>

   <welcome-file-list>

       <welcome-file>index.html</welcome-file>

       <welcome-file>index.htm</welcome-file>

       <welcome-file>index.jsp</welcome-file>

       <welcome-file>default.html</welcome-file>

       <welcome-file>default.htm</welcome-file>

       <welcome-file>default.jsp</welcome-file>

   </welcome-file-list>

   <!-- 启动spring -->

   <listener>

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

   </listener>

   <context-param>

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

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

   </context-param>

   <!--啓動springmvc  -->

   <servlet>

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

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

   <init-param>

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

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

     </init-param>

     <load-on-startup>1</load-on-startup>

   </servlet>

   <servlet-mapping>

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

   <url-pattern>*.do</url-pattern>

   </servlet-mapping>

</web-app>

applicationContext-dao.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: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/beans http://www.springframework.org/schema/beans/spring-beans.xsd

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

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

   <!-- 获取配置properties解析文件 -->

   <context:property-placeholder location="classpath:config.properties" />

   <!--获取数据源 ,有好多种方法,-->

   <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">

       <property name="driverClassName" value="${jdbc.driver}"/>

       <property name="url" value="${jdbc.url}" />

       <property name="username" value="${jdbc.username}" />

       <property name="password" value="${jdbc.password}" />

       <!--设置数据库最大连接数量和最小连接数量  DriverManagerDataSource-->

       <property name="maxActive" value="20"/>

       <property name="minIdle" value="3"/>

   </bean>

   <!-- 将spring和mybatis整合,提取mybatis的参数信息和配置文件 -->

   <bean class="org.mybatis.spring.SqlSessionFactoryBean">

       <!--获取mybatis的mapper文件 -->

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

       <!-- 配置数据源 -->

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

   </bean>

   <!-- 配置mybatis的代理对象,为dao赋值 -->

   <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">

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

   </bean>

</beans>

applicationContext-service.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: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/beans http://www.springframework.org/schema/beans/spring-beans.xsd

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

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

   <!-- 扫描spring组件,排除扫描controller注解 -->

   <context:component-scan base-package="com.bjsxt">

       <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>

   </context:component-scan>

</beans>

applicationContext-trans.xml:

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

<beans xmlns="http://www.springframework.org/schema/beans"

   xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"

   xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"

   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

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

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

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

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

   <!-- 配置事物管理器 -->

   <bean id="transactionManager"

       class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

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

   </bean>

   <!--配置通知 -->

   <tx:advice id="txAdvice" transaction-manager="transactionManager">

       <tx:attributes>

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

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

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

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

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

       </tx:attributes>

   </tx:advice>

   <!-- 切入點 -->

   <aop:config>

       <aop:pointcut expression="execution(* com.bjsxt.service.*.*(..))"

           id="pointcut" />

       <!-- 织入 -->

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

   </aop:config>

</beans>

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: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/beans http://www.springframework.org/schema/beans/spring-beans.xsd

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

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

<!-- 扫描controller注解 -->

<context:component-scan base-package="com.bjsxt.controller"/>

<!--注解驱动

最后的配置如果没有<mvc:annotation-driven/>,那么所有的Controller可能就没有解析,

所有当有请求时候都没有匹配的处理请求类,就都去<mvc:default-servlet-handler/>

即default servlet处理了。添加上<mvc:annotation-driven/>后,

相应的do请求被Controller处理,而静态资源因为没有相应的Controller就会被default servlet处理。

总之没有相应的Controller就会被default servlet处理就ok了。

 -->

<mvc:annotation-driven/>

<!-- 视图解析器 -->

      <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">

       <property name="prefix" value="/" /><!-- jsp所在的前缀 -->

       <property name="suffix" value=".jsp" />

      </bean>

</beans>

SqlMapperClient.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>

 

 </configuration>

config.properties:文件

jdbc.driver=oracle.jdbc.driver.OracleDriver

jdbc.url=jdbc:oracle:thin:@localhost:1521:orcl

jdbc.username=buff

jdbc.password=buff

 

转载于:https://my.oschina.net/u/3492343/blog/1023468

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值