1. 程式人生 > >自己搭建一個SSM框架

自己搭建一個SSM框架

工作中我們或多或少都需要自己搭建一個框架,現在常見的Java開源框架組合方式主要為:SSH,spring+springMVC+JDBC,SSM。

其中SSM目前無論是培訓機構培訓亦或是招聘。都會將會使用SSM框架作為一個重要能力來作為培訓或是招聘的重要目標之一,下面我將自己自學時搭建的一個SSM專案分享出來,供初學者參閱。

1.第一步,我們需要搭建好自己的開發環境(IDE) 筆者使用的是myeclipse+tomcat+mysql

2.第二步建立一個web工程 工程名自定義,建立好了之後按照MVC設計模式建立好所有的包或資料夾(domain用於存放javabean物件,config用於存放所有的配置檔案),並將SSM框架所需要的所有jar包匯入到專案中

3.編寫專案的配置檔案(配置檔案中每部分的含義,有詳細的註釋說明)

a.spring的配置檔案application-context.xml配置檔案

  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <beans
  3.       xmlns="http://www.springframework.org/schema/beans"
  4.       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5.       xmlns:context="http://www.springframework.org/schema/context"
  6.       xmlns:aop
    ="http://www.springframework.org/schema/aop"
  7.       xmlns:tx="http://www.springframework.org/schema/tx"
  8.       xmlns:mvc="http://www.springframework.org/schema/mvc"
  9.       xsi:schemaLocation="  
  10.       http://www.springframework.org/schema/beans   
  11.       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  12.       http://www.springframework.org/schema/context  
  13.       http://www.springframework.org/schema/context/spring-context-3.0.xsd  
  14.       http://www.springframework.org/schema/aop   
  15.       http://www.springframework.org/schema/aop/spring-aop-3.0.xsd  
  16.       http://www.springframework.org/schema/tx  
  17.       http://www.springframework.org/schema/tx/spring-tx-3.0.xsd  
  18.       http://www.springframework.org/schema/mvc  
  19.       http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd  
  20.       ">
  21.       <!-- 1.啟用spring的註解掃描器 -->
  22.         <!--   
  23.             為了載入service層中的事務能夠成功被spring管理  
  24.             需要設定spring的配置檔案中的註解掃描器不掃描控制層,同時設定springMVC的配置檔案不掃描service層。  
  25.             如果不做此設定,事務無法開啟  
  26.          -->
  27.       <context:component-scanbase-package="scmweb">
  28.         <context:exclude-filtertype="annotation"expression="org.springframework.stereotype.Controller"/><!-- expression對應的是註解物件的全類名,而不是開發人員建立的控制層的全類名 -->
  29.       </context:component-scan>
  30.       <!-- 2.配置資料來源 -->
  31.       <beanclass="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  32.         <propertyname="locations">
  33.             <value>classpath:config/jdbc.properties</value>
  34.         </property>
  35.       </bean>
  36.       <beanid="c3p0datasource"class="com.mchange.v2.c3p0.ComboPooledDataSource"destroy-method="close">
  37.         <propertyname="driverClass"value="${jdbc.driverClassName}"></property>
  38.         <propertyname="jdbcUrl"value="${jdbc.url}"></property>
  39.         <propertyname="user"value="${jdbc.username}"></property>
  40.         <propertyname="password"value="${jdbc.password}"></property>
  41.       </bean>
  42.       <!-- 3.配置mybatis相關的東西 -->
  43.       <!-- 3.1 配置mybatis核心sqlsessionfactory -->
  44.       <beanname="sqlsessionfactory"class="org.mybatis.spring.SqlSessionFactoryBean">
  45.         <!-- 配置mybatis的主配置檔案 -->
  46.         <propertyname="configLocation"value="classpath:config/mybatis.xml"></property>
  47.         <propertyname="dataSource"ref="c3p0datasource"></property>
  48.         <!-- 設定自動將指定包下所有的xxxMapper.xml檔案引入mybatis -->
  49.         <propertyname="mapperLocations"value="classpath:scmweb/log/scmdao/*.xml"></property>
  50.       </bean>
  51.       <!-- 3.2 配置sqlSessionTemplate持久化模版(包含了增刪查改的模版方法,  
  52.                    如果不配置的話需要利用sqlsessionfactory來生成sqlsession物件實現對資料庫的操作)  
  53.        -->
  54. <!--       <bean name="sqlSessionTemplate"  class="org.mybatis.spring.SqlSessionTemplate"> -->
  55. <!--         <constructor-arg index="0" ref="sqlsessionfactory"></constructor-arg> -->
  56. <!--       </bean> -->
  57.       <!-- 4.配置事務相關得東西 -->
  58.       <!-- 4.1 配置事務管理器 -->
  59.       <beanname="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  60.         <propertyname="dataSource"ref="c3p0datasource"></property>
  61.       </bean>
  62.       <!-- 4.2 配置事務的通知  配置為那種型別的方法加上事務-->
  63.       <tx:adviceid="tx"transaction-manager="transactionManager">
  64.         <tx:attributes>
  65.             <tx:methodname="save*"propagation="REQUIRED"rollback-for="Exception"/><!-- 設定了rollback-for屬性  那麼只要出現異常(無論是否被手動捕獲)都會回滾 -->
  66.             <tx:methodname="update*"propagation="REQUIRED"/>
  67.             <tx:methodname="insert*"propagation="REQUIRED"/>
  68.             <tx:methodname="*"propagation="SUPPORTS"/>
  69.         </tx:attributes>
  70.       </tx:advice>
  71.       <!-- 4.3 配置事務的切面 -->
  72.