SSM项目整合环境的搭建

来源:互联网 发布:微盘交易源码下载 编辑:程序博客网 时间:2024/05/22 00:55


SSM项目整合环境的搭建


1.需要哪些配置文件? (3个配置文件)
 
 web.xml,springmvc的配置文件,spring和mybatis整合的配置文件




1.web.xml中的配置


(1)spring的配置(加载spring的相关配置),其实就是spring和mybatis整合的配置文件


  第一步:加载listener


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


  第二步:指定spring配置文件的位置和名字(默认的方式和非默认的方式)。




  如:(非默认的方式)


    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/applicationContext.xml</param-value>  (这里的classpath相当于src spring相当于包(这个包名可以随便取))
    </context-param> 


  省略以后,相当于(默认的方式) 我们常用的都是默认的方式


    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param> 


  总结:可以默认将applicationContext.xml放在web-inf目录下,也可以指定配置文件的名称和位置。


(2)spring mvc的配置


 (a)默认的方式:在web.xml中配置springmvc的servlet的名字xxx,默认的springmvc配置文件名为xxx-servlet.xml
 如:


<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
    
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>


 (b)非默认的方式:直接在web.xml中配置springmvc的servlet的时候,指定springmvc配置文件的位置和名字。


<!-- Spring MVC servlet -->
  <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:spring-mvc.xml</param-value>
    </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>SpringMVC</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
我们一般使用的是默认的方式




(3)spring和mybatis整合的配置文件


  1.配置datasource


<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"  destroy-method="close">
    <!-- 初始化连接数量; -->
    <property name="initialSize" value="0" />
    <!-- 最大并发连接数 -->
    <property name="maxActive" value="20" />


    <!-- 最大空闲连接数 -->
    <property name="maxIdle" value="20" />
    <!-- 最小空闲连接数 -->
    <property name="minIdle" value="0" />
    <!-- 最大等待时长 -->
    <property name="maxWait" value="60000" />
    <!-- 超过时间限制是否回收 -->
    <property name="removeAbandoned" value="true" />
    <!-- 超过时间限制多长; -->
    <property name="removeAbandonedTimeout" value="180"/>          
    <!-- 数据源连接参数配置; -->
    <property name="username" value="${db.username}"/>
    <property name="url" value="${db.url}"/>
    <property name="password" value="${db.password}"/>
    <property name="driverClassName" value="${db.driver}"/>
</bean>


2.配置sessionfactory,并且指明mapper映射文件所在的包名。
  
   id是固定的


    <!-- 配置SessionFactory -->
     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
      <property name="dataSource" ref="dataSource"/>    
<!-- <property name="mapperLocations" value="classpath:mapper/*.xml" /> -->
<property name="mapperLocations">
<list>
<value>classpath:mapper/*.xml</value>
</list>
</property>
</bean>   


3.指明mapper接口所在的包名


<!-- 自动扫描mapper接口,注入sqlSessionFactory -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="mapper"/>
</bean>




4.配置事务管理器,注意:注入的是datasource属性


 <!-- 配置事务管理器 -->
  <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
      <property name="dataSource" ref="dataSource"/>
  </bean> 


<!-- <tx:annotation-driven transaction-manager="transactionManager"  proxy-target-class="true"/> -->
  <!-- 定义切面 -->
<aop:config>
    <aop:pointcut expression="execution(* dao.*.* (..))" id="txPointCut"/>
    <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/>
</aop:config>
  
<!-- 声明式事务 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">            
    <tx:attributes>
         <tx:method name="isValid" propagation="REQUIRED" read-only="true"/>
         <tx:method name="add" propagation="REQUIRED" read-only="true"/>
    </tx:attributes>        
</tx:advice>


完成这三个xml文件的配置即完成了SSM项目整合环境的搭建
需要的jar包网上搜索ssm需要的jar包即可搜索出对应的jar包
原创粉丝点击