1. 程式人生 > >如何自己成功搭建一個SSM框架的WEB專案

如何自己成功搭建一個SSM框架的WEB專案

工作中我們或多或少都需要自己搭建一個框架,現在常見的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配置檔案

<?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"
	  xmlns:mvc="http://www.springframework.org/schema/mvc"
		
      xsi:schemaLocation="
	
	  http://www.springframework.org/schema/beans 
	  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
	  
	  http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-3.0.xsd
 	  
	  http://www.springframework.org/schema/aop 
	  http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
	  
	  http://www.springframework.org/schema/tx
      http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    
      http://www.springframework.org/schema/mvc
      http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
      ">
      <!-- 1.啟用spring的註解掃描器 -->
      	<!-- 
      		為了載入service層中的事務能夠成功被spring管理
      		需要設定spring的配置檔案中的註解掃描器不掃描控制層,同時設定springMVC的配置檔案不掃描service層。
      		如果不做此設定,事務無法開啟
      	 -->
      <context:component-scan base-package="scmweb">
      	<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/><!-- expression對應的是註解物件的全類名,而不是開發人員建立的控制層的全類名 -->
      </context:component-scan>
      <!-- 2.配置資料來源 -->
      <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
      	<property name="locations">
      		<value>classpath:config/jdbc.properties</value>
      	</property>
      </bean>
      <bean id="c3p0datasource" class="com.mchange.v2.c3p0.ComboPooledDataSource"  destroy-method="close">
      	<property name="driverClass" value="${jdbc.driverClassName}"></property>
      	<property name="jdbcUrl" value="${jdbc.url}"></property>
      	<property name="user" value="${jdbc.username}"></property>
      	<property name="password" value="${jdbc.password}"></property>
      </bean>
      <!-- 3.配置mybatis相關的東西 -->
      <!-- 3.1 配置mybatis核心sqlsessionfactory -->
      <bean name="sqlsessionfactory" class="org.mybatis.spring.SqlSessionFactoryBean">
      	<!-- 配置mybatis的主配置檔案 -->
      	<property name="configLocation" value="classpath:config/mybatis.xml"></property>
      	<property name="dataSource" ref="c3p0datasource"></property>
      	<!-- 設定自動將指定包下所有的xxxMapper.xml檔案引入mybatis -->
      	<property name="mapperLocations" value="classpath:scmweb/log/scmdao/*.xml"></property>
      </bean>
      <!-- 3.2 配置sqlSessionTemplate持久化模版(包含了增刪查改的模版方法,
      		       如果不配置的話需要利用sqlsessionfactory來生成sqlsession物件實現對資料庫的操作)
       -->
<!--       <bean name="sqlSessionTemplate"  class="org.mybatis.spring.SqlSessionTemplate"> -->
<!--       	<constructor-arg index="0" ref="sqlsessionfactory"></constructor-arg> -->
<!--       </bean> -->
      <!-- 4.配置事務相關得東西 -->
      <!-- 4.1 配置事務管理器 -->
      <bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
      	<property name="dataSource" ref="c3p0datasource"></property>
      </bean>
      <!-- 4.2 配置事務的通知  配置為那種型別的方法加上事務-->
      <tx:advice id="tx" transaction-manager="transactionManager">
      	<tx:attributes>
      		<tx:method name="save*" propagation="REQUIRED" rollback-for="Exception"/><!-- 設定了rollback-for屬性  那麼只要出現異常(無論是否被手動捕獲)都會回滾 -->
      		<tx:method name="update*" propagation="REQUIRED"/>
      		<tx:method name="insert*" propagation="REQUIRED"/>
      		<tx:method name="*"  propagation="SUPPORTS"/>
      	</tx:attributes>
      </tx:advice>
      <!-- 4.3 配置事務的切面 -->
      <aop:config>
      	<aop:pointcut id="acut"  expression="execution(* scmweb.log.scmservice.*.*(..))" />
      	<aop:advisor advice-ref="tx" pointcut-ref="acut"/><!-- 這裡應該是用pointcut-ref屬性  而不是pointcut關聯切入點 -->
      </aop:config>
     
    <!-- 
    	配置mybatis的轉換器,
  		目的:通過該配置,可以使得mybatis.xml檔案中不必再關聯實體對映配置檔案(xxxMapper.xml)了,
  			並最終實現了dao層只需要 介面+xxxMapper.xml配置檔案
		原理:對於在basePackage設定的包(包括子包)下的介面類,掃描所有xxxMapper.xml檔案,如果某個檔案的名稱空間是該介面的全名,
				那麼存在與名稱空間相同的介面將被轉換成spring的BEAN,在呼叫 的地方通過@Autowired方式將可以注入介面例項,
				我們在service實現層呼叫dao介面的方法時,則會自動將介面的全類名當作名稱空間,方法名當作sql語句的id值,來執行
				對應的sql語句,並返回相應結果(因此daoimpl層就沒有作用了)
		結束配置之後 3.2步便可以不再配置了
	-->
		<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 		<property name="sqlSessionFactory" ref="sqlsessionfactory" /> 該配置不用手動配,否則會報錯,spring底層會自動注入-->
			<property name="basePackage" value="scmweb.log.scmdao"/>
		</bean>

     
</beans>
b.jdbc.property配置檔案
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc\:mysql\://localhost\:3306/scm?useUnicode\=true&characterEncoding\=UTF-8
jdbc.username=root
jdbc.password=root
c.mybatis.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>
	<typeAliases>
		 <!-- 
		 	通過package標籤設定實現了將某一個包下的所有的實體類都以簡單類名作為別名的設定,
		 	這樣避免了分別為每一個實體類設定別名的麻煩(配置完成之後,xxxMapper.xml中不用再寫全類名了直接寫別名即可)
		  -->
		 
		 <package name="scmweb.log.domain"/>
 	</typeAliases>
 	
 	<!-- 這種方式在這裡引入 xxxMapper.xml檔案比較麻煩,且有多少個對映檔案就要引入多少次
 		為了解決這個事情,可以在sqlSessionFactory的配置中,設定掃描放置xxxMapper.xml檔案的包
 		這樣可以自動掃描包下面的所有實體對映檔案
 	-->
<!-- 	<mappers>  -->
<!-- 			<mapper resource="scmweb/log/domain/emploeemapper.xml"/>  -->
<!--  	</mappers> -->
</configuration>

d.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:mvc="http://www.springframework.org/schema/mvc"
	  xmlns:context="http://www.springframework.org/schema/context"
      xsi:schemaLocation="
	
	  http://www.springframework.org/schema/beans 
	  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    
      http://www.springframework.org/schema/mvc
      http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
      
	  http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-3.0.xsd 
      ">
      <!-- 設定annotation-driven註解之後,便支援了返回json格式資料到前臺頁面 -->
      <mvc:annotation-driven/>
      <context:component-scan base-package="scmweb">
<!--       	<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> -->
      	<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>
      </context:component-scan>
      <!-- 對映器 與xml形式的解析器不一樣-->
<!--       <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping.class"></bean> -->
     
      <!-- 介面卡 與xml形式的解析器不一樣-->
      <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"></bean>
      
       <!-- xml版的檢視解析器和註解版的檢視解析器是一樣的 -->
      <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      	<!-- 由於這裡是為了檢視通過邏輯地址訪問,因此這裡檢視解析器為必選配置 -->
      	<!-- 配置路徑字首 -->
      	<property name="prefix" value="/WEB-INF/view/"></property>
      	<!-- 配置路徑字尾 -->
      	<property name="suffix" value=".jsp"></property>
      </bean>
      
 	
      
<!--         
	當 springMVC配置檔案中  通過註解引入了spring的配置檔案時,則web.xml檔案中便可以不配置spring的xml配置檔案了
	<import resource="classpath:config/application-context.xml"/> 
-->
</beans>

e.web.xml配置檔案中需要配置spring和springMVC的相關內容
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
  <display-name></display-name>	
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <!-- 配置spring -->
  <listener>
  		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <!-- 當 springMVC配置檔案中  通過註解引入了spring的配置檔案時,則可以配置spring的xml配置檔案-->	
  <context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:config/application-context.xml</param-value>
  </context-param>
  
  <!-- 配置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>classpath:config/springMVC.xml</param-value><!-- 該配置檔案一定是包含springMVC所有屬性配置得配置檔案 -->
  	</init-param>
  </servlet>
  <servlet-mapping>
  	<servlet-name>DispatcherServlet</servlet-name>
  	<url-pattern>*.action</url-pattern>
  </servlet-mapping>
    <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>
  </filter>
  <filter-mapping>
  	<filter-name>CharacterEncodingFilter</filter-name>
  	<url-pattern>/*</url-pattern>
  </filter-mapping>
  <!-- 配置springMVC結束 -->
  
  
</web-app>

配置到這一步之後,會發現,現在需要手動建立javabean例項、dao層的介面以及javabean對應表格的mapper配置檔案,因此,這裡用到了一個mybatis逆向工程的外掛,用於自動生成上述三個部分

4.配置mybatis的逆向工程外掛

a.首先需要在myeclipse的安裝目錄中新增逆向工程

首先找見安裝目錄中的MyEclipse 10下建立add-plugins\mybatis-generater資料夾,然後將逆向工程壓縮包解壓之後的兩個資料夾放到改目錄下

D:\myprogram1\myeclipse10\MyEclipse 10\add-plugins\mybatis-generater

其次在myeclipse的安裝目錄D:\myprogram1\myeclipse10\MyEclipse 10\dropins下建立mybatis.link檔案(字尾為.link),檔案內容為:path=D:\\myprogram1\\myeclipse10\\MyEclipse 10\\add-plugins\\mybatis-generater

重啟myecplise,將中generatorConfig.xml新增到專案中

b.generatorConfig.xml配置檔案

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
  PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
  "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<!-- 
	mybatis的逆向工程外掛,用於幫助開發人員自動生成資料庫中指定表格所對應的:
	javabean、xxxMapper.xml檔案(javabean與表格的對映檔案)和dao層的介面類
	這樣開發人員不用自己去一張一張的建表格對應的各種類和配置檔案了。
	
	當配置結束之後,右擊檔名會有generator Mybatis/Ibatis artifacts選項   點選即可順利執行
	 執行結果為:資料庫中一張表對應:一個javabean,一個dao介面物件,一個xxxMapper.xml檔案
 -->
<generatorConfiguration>
<!--
	<properties resource="conn.properties" />
	  -->
	  <!-- 處理1 -->
	 <classPathEntry location="E:\mybatis\mysql-connector-java-5.1.7-bin.jar"/>
	<!-- 指定執行環境是mybatis3的版本 -->
	<context id="testTables" targetRuntime="MyBatis3">

		<commentGenerator>
		<!-- 是否取消註釋 -->
			<property name="suppressAllComments" value="true" />
			 <!-- 是否生成註釋代時間戳 -->
			<property name="suppressDate" value="true" />
		</commentGenerator>
		<!-- 處理2   jdbc 連線資訊 -->
		<jdbcConnection driverClass="com.mysql.jdbc.Driver"
			connectionURL="jdbc:mysql://localhost:3306/scm?useUnicode=true&characterEncoding=UTF-8" userId="root" password="root">
		</jdbcConnection>
		
        <!--處理3   targetPackage指定模型(即表所對應的實體類)生成在哪個包 ,targetProject指定專案的src目錄,-->        
		<javaModelGenerator targetPackage="scmweb.log.domain"
			targetProject="SCM/src">
			<!-- 去除欄位前後空格 -->
			<property name="trimStrings" value="false" />
		</javaModelGenerator>
		<!--處理4   配置xxxMapper.xml檔案生成到專案中哪個包中 -->
		<sqlMapGenerator targetPackage="scmweb.log.scmdao"
			targetProject="SCM/src" />
		<!-- 處理5   配置dao介面生成資訊(由mybatis逆向工程生成的dao介面類全是以Mapper結尾的)-->
		<javaClientGenerator type="XMLMAPPER" targetPackage="scmweb.log.scmdao" targetProject="SCM/src" />
		
		<!-- 指定要為資料庫中哪些表格生成對應的介面物件和實體類(javabean)以及對應的xxxMapper.xml檔案
			切忌一張表格只能自動生成一次,如果多次生成的話,會造成生成的事物內部屬性一直重複
		-->
		<table tableName="account" domainObjectName="Account"/>
			
		<table tableName="supplier" domainObjectName="Supplier"/>
	</context>
</generatorConfiguration>

當配置結束之後,右擊檔名會有generator Mybatis/Ibatis artifacts選項   點選即可順利執行
 執行結果為:資料庫中一張表對應:一個javabean,一個dao介面物件,一個xxxMapper.xml檔案

6.專案原始碼地址:https://gitee.com/willbeahero/ssmproject