1. 程式人生 > >Spring,SpringMVC,Mybatis (SSM)框架的搭建

Spring,SpringMVC,Mybatis (SSM)框架的搭建


搭建SSM框架參照一下步驟:
1.提供ssm所需jar包

這些jar包包括三個框架所需要的,就不一一列舉所屬了
aopalliance-1.0.jar
asm-3.3.1.jar
aspectjweaver.jar
c3p0-0.9.1.jar
cglib-2.2.2.jar
commons-logging-1.1.1.jar
iText-2.1.3.jar
jackson-annotations-2.1.5.jar
jackson-core-2.1.5.jar
jackson-databind-2.1.5.jar
jackson-mapper-lgpl-1.8.1.jar
jackson-module-jaxb-annotations-2.1.5.jar
javassist-3.17.1-GA.jar
json-lib-2.3-jdk15.jar
json-taglib-0.4.1.jar
jstl.jar
log4j-1.2.17.jar
mybatis-3.2.1.jar
mybatis-spring-1.2.0.jar
mysql-connector-java-5.1.22-bin.jar
slf4j-api-1.7.2.jar
slf4j-log4j12-1.7.2.jar
spring-aop-3.2.2.RELEASE.jar
spring-aspects-3.2.2.RELEASE.jar
spring-beans-3.2.2.RELEASE.jar
spring-context-3.2.2.RELEASE.jar
spring-core-3.2.2.RELEASE.jar
spring-expression-3.2.2.RELEASE.jar
spring-jdbc-3.2.2.RELEASE.jar
spring-tx-3.2.2.RELEASE.jar
spring-web-3.2.2.RELEASE.jar
spring-webmvc-3.2.2.RELEASE.jar
standard.jar

2.編寫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_2_5.xsd"
	id="WebApp_ID" version="2.5">
	<display-name>FirstPrj_SSM</display-name>
	<!-- Web App Root Key -->
	<context-param>
		<param-name>webAppRootKey</param-name>
		<param-value>FirstPrj_SSM.root</param-value>
	</context-param>
	
	<!-- 監聽器 -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<!-- 設定Spring容器載入配置檔案的路徑 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath*:/**/applicationContext*.xml</param-value>
	</context-param>
	<!-- 前段控制器(Spring核心控制器) -->
	<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>/</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>
	<welcome-file-list>
		<welcome-file>login.jsp</welcome-file>
	</welcome-file-list>
</web-app>
我假設伺服器容器為tomcat,那麼部署專案,啟動Tomcat時,會載入web.xml配置檔案,在這裡需要配置一個springmvc核心控制器(DispatcherServlet,他會攔截你所指定格式結尾的請求 委派給 HandlerMapping, HandlerMapping根據請求呼叫相應的controller來處理),前段控制器中的
                <init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath*:/**/springmvc.xml</param-value>
		</init-param>
只是用來載入建立springmvc所屬的bean(在springmvc配置檔案中所定義的bean,比如你自定義的異常攔截器等),儲存在上下文中,這裡稱為子上下文.而父上下文需要建立如service,dao等bean也放在上下文中,這裡稱為父上下文.有關這裡的內容請百度.所以你還得配置個listener來載入父上下文內容
ContextLoaderListener  tomcat啟動裝配

3.編寫spring配置檔案

  applicationContext.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: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-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/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd"
    default-autowire="no" default-lazy-init="false" >

	<!-- 配置資料來源c3p0,連線池 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass">
			<value>${driver}</value>
		</property>
		<property name="jdbcUrl">
			<value>${url}</value>
		</property>
		<!--MySQL資料庫的使用者名稱 -->
		<property name="user">
			<value>${user}</value>
		</property>
		<!--MySQL資料庫 密碼 -->
		<property name="password">
			<value>${password}</value>
		</property>
		<!--連線池中保留的最小連線數。 -->
		<property name="minPoolSize">
			<value>5</value>
		</property>
		<!--連線池中保留的最大連線數。Default: 15 -->
		<property name="maxPoolSize">
			<value>30</value>
		</property>
		<!--初始化時獲取的連線數,取值應在minPoolSize與maxPoolSize之間。Default: 3 -->
		<property name="initialPoolSize">
			<value>10</value>
		</property>
		<!--最大空閒時間,60秒內未使用則連線被丟棄。若為0則永不丟棄。Default: 0 -->
		<property name="maxIdleTime">
			<value>60</value>
		</property>
		<!--當連線池中的連線耗盡的時候c3p0一次同時獲取的連線數。Default: 3 -->
		<property name="acquireIncrement">
			<value>5</value>
		</property>
		<!--JDBC的標準引數,用以控制資料來源內載入的PreparedStatements數量。但由於預快取的 statements屬於單個connection而不是整個連線池。所以設定這個引數需要考慮到多方面 
			的因素。如果maxStatements與maxStatementsPerConnection均為0,則快取被關閉。Default: 0 -->
		<property name="maxStatements">
			<value>0</value>
		</property>
		<!--每60秒檢查所有連線池中的空閒連線。Default: 0 -->
		<property name="idleConnectionTestPeriod">
			<value>60</value>
		</property>
		<!--定義在從資料庫獲取新連線失敗後重復嘗試的次數。Default: 30 -->
		<property name="acquireRetryAttempts">
			<value>30</value>
		</property>
		<!--獲取連線失敗將會引起所有等待連線池來獲取連線的執行緒丟擲異常。但是資料來源仍有效 保留,並在下次呼叫getConnection()的時候繼續嘗試獲取連線。如果設為true,那麼在嘗試 
			獲取連線失敗後該資料來源將申明已斷開並永久關閉。Default: false -->
		<property name="breakAfterAcquireFailure">
			<value>true</value>
		</property>
		<!--因效能消耗大請只在需要的時候使用它。如果設為true那麼在每個connection提交的 時候都將校驗其有效性。建議使用idleConnectionTestPeriod或automaticTestTable 
			等方法來提升連線測試的效能。Default: false -->
		<property name="testConnectionOnCheckout">
			<value>false</value>
		</property>
	</bean>
	<!-- sqlSessionFactory -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
	    <property name="configLocation" value="classpath:mybatis/Mybatis-config.xml"></property>
	    <property name="dataSource" ref="dataSource"></property>
	    <property name="mapperLocations">
	        <list>
	             <value>classpath:main/java/com/cn/user/mapping/*.xml</value>
	        </list>
	    </property>
	</bean>

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

	<!-- advice:通知 -->
	<tx:advice id="transactionAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="*" propagation="REQUIRED" isolation="DEFAULT" rollback-for="java.lang.Exception"/>
			<tx:method name="select*" read-only="true"/>
		</tx:attributes>
	</tx:advice>

	<!-- 切入 -->
	<aop:config>
	  <aop:advisor advice-ref="transactionAdvice" pointcut="execution(* main.java.com.cn.*.*.*(..))"/>
	</aop:config>

</beans>

這裡配置c3p0資料來源時用到了${}表示式,值是在這裡檔案中配置的  具體請看截圖目錄

applicationContext-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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-2.5.xsd">

	<!-- Property Configurer that replaces ${...} placeholders with values from properties files -->
	<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<list>
				<value>classpath:properties/jdbc.properties</value>
				<value>classpath:properties/log4j.properties</value>
			</list>
		</property>
	</bean>

</beans>
<pre name="code" class="html">jdbc.properties 檔案

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://?.?.?.?:3306/yi
user=root
password=root

min_conn=10
max_conn=30
max_wait=5000
#################C3p0#################
max_idle_time=1800

#################DBCP#################
max_idle=10

4. 編寫Mybatis-config.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>
        <typeAlias alias="User" type="main.java.com.cn.user.pojo.User"/>
    </typeAliases>
</configuration>

請看applicationContext.xml檔案
<span style="white-space:pre">	</span><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
	    <property name="configLocation" value="classpath:mybatis/Mybatis-config.xml"></property>
	    <property name="dataSource" ref="dataSource"></property>
	    <property name="mapperLocations">
	        <list>
	             <value>classpath:main/java/com/cn/user/mapping/*.xml</value>
	        </list>
	    </property>
	</bean>

注意這句話 :<property name="configLocation" value="classpath:mybatis/Mybatis-config.xml"></property>

以上就應該可以啟動了

Mybatis-config.xml  Mybatis的配置檔案

applicationContext.xml  Spring的配置檔案

springmvc.xml  SpringMVC的配置檔案

這裡說幾個可以啟動失敗的錯誤:

1.  可能回報 ClassNotFoundException ...少jar包

2. Setting property 'source' to 'org.eclipse.jst.jee.server:...... 那麼開啟在Eclipse裡Tomcat伺服器頁面,下來 勾選 publish modules contexts to separate XML files選項

3. C3P0有關錯誤,  請檢查資料庫是否啟動,使用者名稱 密碼是否正確(空格),如果一切正常還報 Connect錯誤,請看

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/yi    
user=root
password=root

改成url=jdbc:mysql://10.4.14.188:3306/yi   localhost換你本機ip地址

4.   注意mybatis地址的書寫,否則spring託管不了(找不到配置檔案)

5.  springmvc配置檔案地址的書寫