1. 程式人生 > >SSM框架搭建詳細解析

SSM框架搭建詳細解析

總結了一下搭建SSM框架流程,在以後用到的時候方便回頭使用。

使用工具:MyEclipse 2015;Tomcat 8版本;jdk1.8版本。

 

首先:

1:建立一個WebProject專案,jdk1.8 Tomcat8  最後勾選web.xml配置檔案。

 

 

然後:
2.將相應的Jar包匯入lib檔案下。總共35個Jar包,將OJBDBC也匯入進去。

 

3.配置web.xml檔案。

配置2個內容。一個是Spring,一個是Spring MVC的配置。

Spring配置資訊


1:通過全域性上下文引數來載入Spring配置檔案
2:配置監聽器。

 


在web.xml中繼續配置Spring MVC;

Spring MVC的配置資訊。


1:首先配置servlet。通過Servlet標籤配置dispatchServlet。需要一個初始化引數 ,載入spring MVC配置檔案。
2:配置mapping。

 


然後還需要配置一下中文亂碼解決問題。繼續在web.xml中配置相關資訊。


然後,進行下一步。
4:加入3個配置檔案。Spring,Spring MVC,Mybatis 這三個配置檔案需要加入。
 
將配置檔案放在src根目錄下即可。

Spring的掃描包:配置了事物。(applicationContext.xml);


1:自動掃描:根據註解建立例項化,控制反轉。(4種方式)
2:引入配置檔案。jdbc的驅動包等資訊。
3:配置資料來源。需要的資訊根據第二步中的jdbc中的配置檔案來引用。
4:配置MyBatis的SqlSessionFactory:有了它才可以使用MyBatis(1:資料來源:第三步配置的資料來源。2:自動掃描mappers.xml檔案。所有的對映檔案。放在一個對應的路徑下。3:載入MyBatis的配置檔案。)
5:DAO層介面包。該包下的所有都會被例項化。
6:配置事物管理:交由Spring來管理。(1:定義事物傳播屬性。)
7:配置事物切面。
8:異常處理相關。
 
關於Spring的配置資訊以程式碼形式展現給大家:

 


<?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:aop="http://www.springframework.org/schema/aop"  
    xmlns:context="http://www.springframework.org/schema/context
    xmlns:jee="http://www.springframework.org/schema/jee
    xmlns:tx="http://www.springframework.org/schema/tx
    xsi:schemaLocation="   
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd 
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd 
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd 
        http://www.springframework.org/schema/jee
        http://www.springframework.org/schema/jee/spring-jee.xsd 
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">   
    <!-- 自動掃描 -->
    <context:component-scan base-package="com.sys.dao" />
    <context:component-scan base-package="com.sys.service" />
    <context:component-scan base-package="com.sys.entity"/>
    <!-- 引入配置檔案,可以使用${}語法,location:指定讀取檔案的路徑 -->
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <!-- 配置資料來源 -->
    <bean id="dataSource"
        class="com.mchange.v2.c3p0.ComboPooledDataSource"
        p:driverClass="${jdbc.driverClassName}"
        p:jdbcUrl="${jdbc.url}"
        p:user="${jdbc.username}"
        p:password="${jdbc.password}"
        p:initialPoolSize="${jdbc.initialSize}"
        p:maxPoolSize="${jdbc.maxActive}"/>
    <!-- 配置mybatis的sqlSessionFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <!-- 自動掃描mappers.xml檔案 -->
        <property name="mapperLocations" value="classpath:mybatis/mappers/*.xml"></property>
        <!-- mybatis配置檔案 -->
        <property name="configLocation" value="classpath:mybatis/mybatis-config.xml"></property>
    </bean>
    <!-- DAO介面所在包名,Spring會自動查詢其下的類 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.sys.dao" />
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
    </bean>
    <!-- (事務管理)transaction manager, use JtaTransactionManager for global tx -->
    <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="append*" propagation="REQUIRED" />
            <tx:method name="insert*" propagation="REQUIRED" />
            <tx:method name="save*" propagation="REQUIRED" />
            <tx:method name="update*" propagation="REQUIRED" />
            <tx:method name="modify*" propagation="REQUIRED" />
            <tx:method name="edit*" propagation="REQUIRED" />
            <tx:method name="delete*" propagation="REQUIRED" />
            <tx:method name="remove*" propagation="REQUIRED" />
            <tx:method name="repair" propagation="REQUIRED" />
            <tx:method name="delAndRepair" propagation="REQUIRED" />
            <tx:method name="get*" propagation="SUPPORTS" />
            <tx:method name="find*" propagation="SUPPORTS" />
            <tx:method name="load*" propagation="SUPPORTS" />
            <tx:method name="search*" propagation="SUPPORTS" />
            <tx:method name="datagrid*" propagation="SUPPORTS" />
            <tx:method name="*" propagation="SUPPORTS" />
        </tx:attributes>
    </tx:advice>
    <!-- 配置事務切面 --> 
    <aop:config> 
        <aop:pointcut id="serviceOperation" 
            expression="execution(* com.sys.service.*.*(..))" /> 
        <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation" /> 
    </aop:config> 
    <!-- 異常統一處理 -->
    <!-- <bean id="exceptionResolver" class="com.sys.util.HandlerException"/> -->
</beans>123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384

Spring MVC:(servlet-mvc.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:aop="http://www.springframework.org/schema/aop"  
    xmlns:context="http://www.springframework.org/schema/context
    xmlns:jee="http://www.springframework.org/schema/jee
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:mvc="http://www.springframework.org/schema/mvc
    xsi:schemaLocation="   
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd 
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd 
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd 
        http://www.springframework.org/schema/jee
        http://www.springframework.org/schema/jee/spring-jee.xsd 
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">   

    <!-- 使用註解的包,包括子集 -->
    <context:component-scan base-package="com.sys.controller" />
    <!-- 新增資料轉換的註解驅動 -->
    <mvc:annotation-driven>
        <mvc:message-converters>
            <bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
            <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
        </mvc:message-converters>
    </mvc:annotation-driven>
    <!-- 檢視解析器 -->
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/" />
        <property name="suffix" value=".jsp"></property>
    </bean>
    <!-- 上傳元件 -->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!-- 設定上傳的編碼格式 -->
        <property name="defaultEncoding" value="utf-8"/>
        <!-- 設定最大上傳大小 -->
        <property name="maxUploadSize" value="5242880"/>
    </bean>
    <!-- 靜態資源配置設定:除了控制器一概不管理 -->
    <mvc:default-servlet-handler/>
</beans>  123456789101112131415161718192021222324252627282930313233343536373839404142434445464748

MyBatis 配置檔案(mybatis-config.xml)放置在src目錄下的mabatis資料夾內。

 

<?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 name="com.sys.entity"/>
    </typeAliases>
</configuration>
1234567891011

JDBC檔案與Log4j配置檔案。(日誌檔案,將錯誤資訊儲存在日誌檔案,前臺不能顯示錯誤檔案,也可儲存使用者訪問資訊,以及資料庫操作的資訊)。

log4j.properties配置原始碼:

 

log4j.rootLogger=info,appender1,appender2

log4j.appender.appender1=org.apache.log4j.ConsoleAppender

log4j.appender.appender2=org.apache.log4j.FileAppender
log4j.appender.appender2.File=D:/logs/news/logFile.txt

log4j.appender.appender1.layout=org.apache.log4j.TTCCLayout
log4j.appender.appender2.layout=org.apache.log4j.TTCCLayout  123456789

在jdbc配置檔案中修改相關資訊。(需要自己修改;#代表註釋)。
jdbc.properties配置原始碼

jdbc.driverClassName=oracle.jdbc.driver.OracleDriver    //Oracle資料庫
jdbc.url=jdbc\:oracle\:thin\:@localhost\:1521\:jredu   //資料庫名
jdbc.username=OnlineTest    //資料庫表
jdbc.password=Jredu12345    //資料庫密碼

jdbc.initialSize=0 

jdbc.maxActive=20 

jdbc.maxIdle=20 

jdbc.minIdle=1 

jdbc.maxWait=60000  1234567891011121314

至此,關於SSM框架搭建已經成功,在下一個部落格之中,會實現一個SSM搭建框架,實現一個簡單的登入功能。
---------------------
作者:愛是與世界平行
來源:CSDN
原文:https://blog.csdn.net/An1090239782/article/details/78357627
版權宣告:本文為博主原創文章,轉載請附上博文連結!