SSH基本環境搭建

搭建過程:

1.加入 Spring
  • 1). 加入 jar 包 
  • 2). 配置 web.xml 文件 
  • 3). 加入 Spring 的配置文件.
2.加入 Hibernate
  • 1). 同時建立持久化類, 和其對應的 .hbm.xml 文件, 生成對應的數據表 
  • 2). Spring 整合 Hibernate 
  • 3). 步驟:
  • ①. 加入 jar 包 
  • ②. 在類路徑下加入 hibernate.cfg.xml 文件, 在其中配置 hibernate 的基本屬性 
  • ③. 建立持久化類, 和其對應的 .hbm.xml 文件 
  • ④. 和 Spring 進行整合
  • i. 加入 c3p0 和 MySQL 的驅動 
  • ii. 在 Spring 的配置文件中配置: 數據源, SessionFactory, 聲明式事務
  • ⑤. 啓動項目, 會看到生成對應的數據表

3.加入 Struts2
  • 1). 加入 jar 包: 若有重複的 jar 包, 則需要刪除版本較低的. javassist-3.11.0.GA.jar 
  • 2). 在 web.xml 文件中配置 Struts2 的 Filter 
  • 3). 加入 Struts2 的配置文件 
4). 整合 Spring
  • ①. 加入 Struts2 的 Spring 插件的 jar 包 
  • ②. 在 Spring 的配置文件中正常配置 Action, 注意 Action 的 scope 爲 prototype 
  • ③. 在 Struts2 的配置文件中配置 Action 時, class 屬性指向該 Action 在 IOC 中的 id

項目截圖:


web.xml:
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext*.xml</param-value>
</context-param>

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

<!-- 配置 Struts2 的 Filter -->
<filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>

<filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
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:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    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/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

    <!-- 導入資源文件 -->
    <context:property-placeholder location="classpath:db.properties"/>

    <!-- 配置 C3P0 數據源 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="user" value="${jdbc.user}"></property>
        <property name="password" value="${jdbc.password}"></property>
        <property name="driverClass" value="${jdbc.driverClass}"></property>
        <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>

        <property name="initialPoolSize" value="${jdbc.initPoolSize}"></property>
        <property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
    </bean>

    <!-- 配置 SessionFactory -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
        <property name="mappingLocations" value="classpath:com/zhou/entities/*.hbm.xml"></property>
    </bean>

    <!-- 配置 Spring 的聲明式事務 -->
    <!-- 1. 配置 hibernate 的事務管理器 -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>

    <!-- 2. 配置事務屬性 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="get*" read-only="true"/>
            <tx:method name="lastNameIsValid" read-only="true"/>
            <tx:method name="*"/>
        </tx:attributes>
    </tx:advice>

    <!-- 3. 配置事務切入點, 再把事務屬性和事務切入點關聯起來 -->
    <aop:config>
        <aop:pointcut expression="execution(* com.zhou.service.*.*(..))" id="txPointcut"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
    </aop:config>

</beans>
dp.properties:
jdbc.user=root
jdbc.password=123456
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql:///ssh_crud

jdbc.initPoolSize=5
jdbc.maxPoolSize=10
#...
hibernate.cfg.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <!-- 配置 hibernate 的基本屬性 -->

        <!-- 方言 -->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>

        <!-- 是否顯示及格式化 SQL -->
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.format_sql">true</property>

        <!-- 生成數據表的策略 -->
        <property name="hibernate.hbm2ddl.auto">update</property>

        <!-- 二級緩存相關 -->

    </session-factory>

</hibernate-configuration>
添加Department.java和Employee.java:
public class Department {

    private Integer id;
    private String departmentName;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getDepartmentName() {
        return departmentName;
    }

    public void setDepartmentName(String departmentName) {
        this.departmentName = departmentName;
    }

    @Override
    public String toString() {
        return "Department [id=" + id + ", departmentName=" + departmentName
                + "]";
    }

}
public class Employee {

    private Integer id;
    // 不能被修改
    private String lastName;
    private String email;
    // 從前端傳入的是 String 類型, 所以需要注意轉換
    private Date birth;

    // 不能被修改
    private Date createTime;
    // 單向 n-1 的關聯關係
    private Department department;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public Date getBirth() {
        return birth;
    }

    public void setBirth(Date birth) {
        this.birth = birth;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public Department getDepartment() {
        return department;
    }

    public void setDepartment(Department department) {
        this.department = department;
    }

    @Override
    public String toString() {
        return "Employee [id=" + id + ", lastName=" + lastName + ", email="
                + email + ", birth=" + birth + ", createTime=" + createTime
                + ", department=" + department + "]";
    }

}
配置Department.hbm.xml和Employee.hbm.xml:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2014-7-22 11:21:48 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
    <class name="com.zhou.entities.Department" table="SSH_DEPARTMENT">

        <id name="id" type="java.lang.Integer">
            <column name="ID" />
            <generator class="native" />
        </id>

        <property name="departmentName" type="java.lang.String">
            <column name="DEPARTMENT_NAME" />
        </property>

    </class>
</hibernate-mapping>
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2014-7-22 11:21:48 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
    <class name="com.zhou.entities.Employee" table="SSH_EMPLOYEE">

        <id name="id" type="java.lang.Integer">
            <column name="ID" />
            <generator class="native" />
        </id>

        <property name="lastName" type="java.lang.String">
            <column name="LAST_NAME" />
        </property>

        <property name="email" type="java.lang.String">
            <column name="EMAIL" />
        </property>

        <property name="birth" type="java.util.Date">
            <column name="BIRTH" />
        </property>

        <property name="createTime" type="java.util.Date">
            <column name="CREATE_TIME" />
        </property>

        <!-- 映射單向 n-1 的關聯關係 -->
        <many-to-one name="department" class="com.zhou.entities.Department">
            <column name="DEPARTMENT_ID" />
        </many-to-one>

    </class>
</hibernate-mapping>
struts.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="true" />

    <package name="default" namespace="/" extends="struts-default">

    </package>

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

</beans>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章