1. 程式人生 > >【Java學習】SSH網上商城——搭建開發環境

【Java學習】SSH網上商城——搭建開發環境

【背景】

最近開始在做SSH網上商城這個專案,順便學習SSH架構,首先就是需求分析,記錄一下。

【內容】

             1.5.1 搭建開發環境

              SSH整合:

                   1、建立一個Web工程;

                   2、引入jar包和配置檔案;

                      *struts2:

                      *jar包:

                          struts-2.5.16\apps\struts2-blank.war\WEB_INF\lib\*.jar

                          struts-2.5.16-all\struts-2.5.16\lib\struts2-json-plugin-2.5.16.jar

                          struts-2.5.16-all\struts-2.5.16\lib\struts2-spring-plugin-2.5.16.jar

                     *配置檔案:

                     *web.html

                            <!--配置Struts2的核心過濾器-->             

  <filter>

          <filter-name>Struts2</filter-name>

          <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>

         </filter>

    <filter-mapping>

          <filter-name>Struts2</filter-name>

          <url-pattern>/*</url-pattern>

  </filter-mapping>

              *struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
	"http://struts.apache.org/dtds/struts-2.1.dtd">
    <struts>
    <constant name="struts.devMode" value="false"/>
	</struts>

            *Spring:

            *jar包:

                Spring3.2 開發最基本jar包

                spring-beans-3.2.0.RELEASE.jar

               spring-context-3.2.0.RELEASE.jar

             spring-core-3.2.0.RELEASE.jar

              spring-expression-3.2.0.RELEASE.jar

com.springsource.org.apache.commons.logging-1.1.1.jar

com.springsource.org.apache.log4j-1.2.15.jar

AOP開發

spring-aop-3.2.0.RELEASE.jar

spring-aspects-3.2.0.RELEASE.jar

com.springsource.org.aopalliance-1.0.0.jar

com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar

Spring Jdbc開發

spring-jdbc-3.2.0.RELEASE.jar

spring-tx-3.2.0.RELEASE.jar

Spring事務管理

spring-tx-3.2.0.RELEASE.jar

Spring整合其他ORM框架

spring-orm-3.2.0.RELEASE.jar

Spring在web中使用

spring-web-3.2.0.RELEASE.jar

Spring整合Junit測試

spring-test-3.2.0.RELEASE.jar

* 配置檔案:

* web.xml

<!--配置Spring的核心監聽器-->

<listener>

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

</listener>



<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath:applicationContext.xml</param-value>

</context-param>

* 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: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.xsd

     http://www.springframework.org/schema/tx

     http://www.springframework.org/schema/tx/spring-tx.xsd

     http://www.springframework.org/schema/aop

     http://www.springframework.org/schema/aop/spring-aop.xsd">

</beans>

* log4j.properties

* Hibernate:

* jar包:

* hibernate-distribution-3.6.10.Final\hibernate3.jar

* hibernate-distribution-3.6.10.Final\lib\required\*.jar

* hibernate-distribution-3.6.10.Final\lib\jpa\*.jar

* slf4j-log4j整合的jar包 :

* 資料庫驅動:

* 連線池:(c3p0連線池)

* 配置檔案:

* 沒有hibernate的核心配置檔案的方式整合:

* 對映檔案:

3.配置基本配置資訊:

* C3P0連線池:

* 引入外部屬性檔案:

* jdbc.properties

jdbc.driver=com.mysql.jdbc.Driver

jdbc.url=jdbc:mysql:///shop

jdbc.user=root

jdbc.password=123

* 配置連線池:

<!-- 配置連線池: -->

<!-- 引入外部屬性檔案 -->

<context:property-placeholder location="classpath:jdbc.properties"/>

<!-- 配置C3P0連線池: -->

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">

<property name="driverClass" value="${jdbc.driver}"/>

<property name="jdbcUrl" value="${jdbc.url}"/>

<property name="user" value="${jdbc.user}"/>

<property name="password" value="${jdbc.password}"/>

</bean>

* Hibernate相關資訊:

<!-- Hibernate的相關資訊 -->

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

<!-- 注入連線池 -->

<property name="dataSource" ref="dataSource"/>

<!-- 配置Hibernate的其他的屬性 -->

<property name="hibernateProperties">

<props>

<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>

<prop key="hibernate.show_sql">true</prop>

<prop key="hibernate.format_sql">true</prop>

<prop key="hibernate.connection.autocommit">false</prop>

<prop key="hibernate.hbm2ddl.auto">update</prop>

</props>

</property>

<!-- 配置Hibernate的對映檔案 -->

</bean>

* 事務管理:

<!-- 事務管理: -->

<!-- 事務管理器 -->

<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">

<property name="sessionFactory" ref="sessionFactory"/>

</bean>

<!-- 開啟註解事務 -->

<tx:annotation-driven transaction-manager="transactionManager"/>