使用IDEA創建maven項目整合SSH(多模塊聚合)

參考:https://blog.csdn.net/maoyeqiu/article/details/54573294
參考:http://www.cnblogs.com/Brake/p/create_maven_multi_module_project_via_intellij_idea.html
參考:https://blog.csdn.net/u012129558/article/details/78423511

通過一個簡單的demo,練習一下idea多模塊聚合開發,將項目分爲簡單的三層架構:web,service,dao

一、創建項目
創建項目,建立父模塊(這個模塊的主要作用是管理依賴,用來被子模塊繼承用的)

這裏寫圖片描述

這裏寫圖片描述

maven的project和eclipse的workspace的對比
1、maven的project下有多個module,eclipse的workspace下有多個project
2、module之間可以相互獨立,也可以相互依賴,所有的maven組成一個完整的項目
3、eclipse中的project之間沒有啥關係,各自都是獨立的完整項目。

然後 直接 next->finish

建好之後的目錄:
這裏寫圖片描述

父模塊沒有任何代碼邏輯,src可以直接刪除掉

在pom.xml中引入SSH需要的所有依賴:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.xiaowen.webproject</groupId>
    <artifactId>ssh</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>


    <!-- 屬性 -->
    <properties>
        <spring.version>4.2.4.RELEASE</spring.version>
        <hibernate.version>5.0.7.Final</hibernate.version>
        <struts.version>2.3.24</struts.version>
    </properties>

    <!-- 鎖定版本,struts2-2.3.24、spring4.2.4、hibernate5.0.7 -->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-aspects</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-orm</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-test</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-web</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-core</artifactId>
                <version>${hibernate.version}</version>
            </dependency>
            <dependency>
                <groupId>org.apache.struts</groupId>
                <artifactId>struts2-core</artifactId>
                <version>${struts.version}</version>
            </dependency>
            <dependency>
                <groupId>org.apache.struts</groupId>
                <artifactId>struts2-spring-plugin</artifactId>
                <version>${struts.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <!-- 依賴管理 -->
    <dependencies>
        <!-- spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
        </dependency>
        <!-- hibernate -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
        </dependency>

        <!-- 數據庫驅動 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.6</version>
            <scope>runtime</scope>
        </dependency>
        <!-- c3p0 -->

        <dependency>
            <groupId>c3p0</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.1.2</version>
        </dependency>


        <!-- 導入 struts2 -->
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-spring-plugin</artifactId>
        </dependency>

        <!-- servlet jsp -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.0</version>
            <scope>provided</scope>
        </dependency>
        <!-- 日誌 -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.2</version>
        </dependency>

        <!-- jstl -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
    </dependencies>
</project>

右下角出現:
這裏寫圖片描述
選擇:Enable Auto-import

或者:

這裏寫圖片描述

二、創建dao模塊
右擊ssh-parent:
這裏寫圖片描述

這裏寫圖片描述

這裏寫圖片描述

這裏寫圖片描述

finish:
這裏寫圖片描述

這裏寫圖片描述

新建Customer實體類映射數據庫customer表
新建CustomerDao,多Customer做一個簡單的查詢操作
在resources中新建spring配置文件和hibernate配置文件
在spring中管理Customer和CustomerDao

這裏寫圖片描述

applicationContext.dao.xml:

<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/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx.xsd">


    <!-- 數據庫連接池 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver" />
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/maven" />
        <property name="user" value="root" />
        <property name="password" value="root" />
    </bean>

    <!-- 配置sessionFactory -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <!-- 依賴dataSource -->
        <property name="dataSource" ref="dataSource"/>
        <!-- 創建工廠需要加載hibernate映射文件 -->
        <property name="configLocations" value="classpath:hibernate.cfg.xml"></property>
    </bean>

    <bean id="customerDao" class="com.xiaowen.dao.impl.CustomerDaoImpl">
        <property name="sessionFactory" ref="sessionFactory"></property>

    </bean>

</beans>

hibernate.cfg.xml:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <!-- 會話工廠 -->
    <session-factory>
        <!-- 數據庫方言,根據數據庫選擇 -->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>

        <!--爲了方便調試是否在運行hibernate時在日誌中輸出sql語句 -->
        <property name="hibernate.show_sql">true</property>
        <!-- 是否對日誌中輸出的sql語句進行格式化 -->
        <property name="hibernate.format_sql">true</property>
        <!--關閉自動建表-->
        <property name="hibernate.hbm2ddl.auto">none</property>

        <!-- 加載映射文件 -->
        <mapping resource="com/xiaowen/entity/Customer.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

三、創建service模塊

創建流程與創建dao模塊相同:
service模塊需要依賴dao模塊,所以在service的pom.xml中添加對dao的依賴:
這裏寫圖片描述

然後創建CustomerService,放入spring中管理:
在CustomerService中需要用到CustomerDao,所以需要引入applicationContext.dao.xml,如果前面不添加依賴,這裏就無法引入。
applicationContext.service.xml:

<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/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx.xsd">


    <import resource="applicationContext.dao.xml"/>
    <bean id="customerService" class="com.xiaowen.service.impl.CustomerServiceImpl">
        <property name="customerDao" ref="customerDao"></property>
    </bean>

</beans>

四、創建web層
創建web選擇模板進行創建:
這裏寫圖片描述

然後就和前面的流程一樣:
這裏寫圖片描述

目錄補全,需要手動創建一下:
這裏寫圖片描述

web模塊需要用到service模塊,所以就在web模塊的pom.xml文件中添加對service模塊的依賴:

<dependencies>
        <dependency>
            <groupId>com.xiaowen</groupId>
            <artifactId>ssh-service</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

新建頁面,創建Action,配置struts,將Action實體放入Spring管理
這裏寫圖片描述

業務流程:從前臺頁面傳入custId,查詢用戶名稱,然後顯示在前臺:

CustomerAction:

package com.xiaowen.action;

import com.opensymphony.xwork2.ActionSupport;
import com.xiaowen.entity.Customer;
import com.xiaowen.service.CustomerService;

public class CustomerAction extends ActionSupport {
    private Long custId;

    private Customer customer;

    private CustomerService customerService;

    public String findCustomerById(){
        System.out.println("用戶id:"+custId);
        customer = customerService.findById(custId);
        System.out.println("查詢到的用戶名:"+customer.getCustName());
        return SUCCESS;
    }

    public Long getCustId() {
        return custId;
    }

    public void setCustId(Long custId) {
        this.custId = custId;
    }


    public Customer getCustomer() {
        return customer;
    }

    public void setCustomer(Customer customer) {
        this.customer = customer;
    }

    public void setCustomerService(CustomerService customerService) {
        this.customerService = customerService;
    }
}

Action用到了service和dao,所以web模塊需要添加對service模塊的依賴,而service添加了對dao的依賴,又因爲依賴會傳遞 所以在web模塊就不需要主動添加對dao模塊的依賴了。

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.i18n.encoding" value="UTF-8"></constant>
    <!-- 開發模式 -->
    <constant name="struts.devMode" value="true"></constant>
    <!-- 主題 -->
    <constant name="struts.ui.theme" value="simple"></constant>
    <!-- 擴展名 -->
    <constant name="struts.action.extension" value="action"></constant>

    <package name="customer" namespace="/" extends="struts-default">
        <action name="findById" class="customerAction" method="findCustomerById">
            <result name="success">/index.jsp</result>
        </action>
    </package>

</struts>

applicationContext.action.xml:

<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/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!--action的scope要選擇prototype,多例模式,單例就成servlet了-->
    <bean id="customerAction" class="com.xiaowen.action.CustomerAction" scope="prototype">
        <property name="customerService" ref="customerService"></property>
    </bean>
    <import resource="applicationContext.service.xml"></import>
</beans>

index.jsp:

<%--
  Created by IntelliJ IDEA.
  User: xiaowen
  Date: 2018/6/27
  Time: 下午10:19
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <title>Title</title>
</head>
<body>

    大家好我是:
    ${customer.custName}
</body>
</html>

最後一步,配置web.xml,不然前面都是白扯:

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">


    <display-name>Archetype Created Web Application</display-name>


    <!--===================監聽spring====================================-->
    <!--讓spring隨web啓動-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!--配置spring配置文件位置參數-->

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


    <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>

</web-app>

運行後在瀏覽器輸入:http://localhost:8080/sshweb/findById.action?custId=1

這裏寫圖片描述

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