天天看點

maven學習記錄三——maven整合ssh架構6       整合ssh架構7      分子產品開發

6       整合ssh架構

6.1     依賴傳遞

隻添加了一個struts2-core依賴,發現項目中出現了很多jar,

這種情況 叫 依賴傳遞

maven學習記錄三——maven整合ssh架構6       整合ssh架構7      分子產品開發

6.2     依賴版本沖突的解決

1、  第一聲明優先原則

<dependencies>

  <!--   spring-beans-4.2.4 -->

       <dependency>

                 <groupId>org.springframework</groupId>

                 <artifactId>spring-context</artifactId>

                 <version>4.2.4.RELEASE</version>
       </dependency>

<!--   spring-beans-3.0.5 -->

       <dependency>

                 <groupId>org.apache.struts</groupId>

                 <artifactId>struts2-spring-plugin</artifactId>

                 <version>2.3.24</version>

       </dependency>      

2、  路徑近者優先原則

自己添加jar包

<dependency>

                 <groupId>org.springframework</groupId>

                 <artifactId>spring-beans</artifactId>

                 <version>4.2.4.RELEASE</version>

       </dependency>       

3、  排除原則

<dependency>
          <groupId>org.apache.struts</groupId>
          <artifactId>struts2-spring-plugin</artifactId>
          <version>2.3.24</version>
          <exclusions>
            <exclusion>
              <groupId>org.springframework</groupId>
              <artifactId>spring-beans</artifactId>
            </exclusion>
          </exclusions>
      </dependency>      

4、  版本鎖定原則

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

</dependencies>

</dependencyManagement>      

需求:

傳客戶ID 頁面上顯示客戶資訊

準備資料庫

6.3     建構項目

1、  建立資料庫,

2、  執行準備好的sql腳本

3、  完善pom.xml檔案,把ssh相關的依賴都添加上去

<!-- 屬性 -->

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

                   <!-- junit -->

                   <dependency>

                            <groupId>junit</groupId>

                            <artifactId>junit</artifactId>

                            <version>4.9</version>

                            <scope>test</scope>

                   </dependency>

                   <!-- jstl -->

                   <dependency>

                            <groupId>javax.servlet</groupId>

                            <artifactId>jstl</artifactId>

                            <version>1.2</version>

                   </dependency>

         </dependencies>

 

         <build>

                   <plugins>

                            <!-- 設定編譯版本為1.7 -->

                            <plugin>

                                     <groupId>org.apache.maven.plugins</groupId>

                                     <artifactId>maven-compiler-plugin</artifactId>

                                     <configuration>

                                               <source>1.7</source>

                                               <target>1.7</target>

                                               <encoding>UTF-8</encoding>

                                     </configuration>

                            </plugin>

 

                            <!-- maven内置 的tomcat6插件 -->

                            <plugin>

                                     <groupId>org.codehaus.mojo</groupId>

                                     <artifactId>tomcat-maven-plugin</artifactId>

                                     <version>1.1</version>

                                     <configuration>

                                               <!-- 可以靈活配置工程路徑 -->

                                               <path>/ssh</path>

                                               <!-- 可以靈活配置端口号 -->

                                               <port>8080</port>

                                     </configuration>

                            </plugin>

                   </plugins>

         </build>      

4、  完成實體類代碼

maven學習記錄三——maven整合ssh架構6       整合ssh架構7      分子產品開發

5、  完成dao代碼

接口

package cn.itcast.dao;

 

import cn.itcast.entity.Customer;

 

public interface CustomerDao {

        

         public Customer getById(Long id);

 

}      

實作類

package com.itcast.dao.impl;

import org.springframework.orm.hibernate5.support.HibernateDaoSupport;

import cn.itcast.dao.CustomerDao;

import cn.itcast.entity.Customer;

public class CustomerDaoImpl extends HibernateDaoSupport implements CustomerDao {

         @Override

         public Customer getById(Long id) {

                   return this.getHibernateTemplate().get(Customer.class, id);

         }

}      

6、  完成service代碼

接口

package com.itcast.service;

import cn.itcast.entity.Customer;

public interface CustomerService {

         public Customer getById(Long id);

}       

實作類

package com.itcast.service.impl;

 

import com.itcast.service.CustomerService;

 

import cn.itcast.dao.CustomerDao;

import cn.itcast.entity.Customer;

 

public class CustomerServiceImpl implements CustomerService {

         private CustomerDao  customerDao;

         public void setCustomerDao(CustomerDao customerDao) {

                   this.customerDao = customerDao;

         }

         @Override

         public Customer getById(Long id) {

                   return customerDao.getById(id);

         }

}      

7、  完成action代碼

package cn.itcast.action;

import com.itcast.service.CustomerService;

import com.opensymphony.xwork2.ActionSupport;

import cn.itcast.entity.Customer;

public class CutomerAction extends ActionSupport {

         //兩個成員變量

         private Customer  customer;

        

         private Long custId;

         public Customer getCustomer() {

                   return customer;

         }

         public void setCustomer(Customer customer) {

                   this.customer = customer;

         }

         private CustomerService customerService;

         public void setCustomerService(CustomerService customerService) {

                   this.customerService = customerService;

         }

         public Long getCustId() {

                   return custId;

         }

         public void setCustId(Long custId) {

                   this.custId = custId;

         }

         public String findById(){

                   customer = customerService.getById(custId);

                   return SUCCESS;

         }

}      

8、  拷貝配置檔案并修改

從如下圖位置拿到配置檔案

maven學習記錄三——maven整合ssh架構6       整合ssh架構7      分子產品開發

放入到 src/main/resources目錄中

maven學習記錄三——maven整合ssh架構6       整合ssh架構7      分子產品開發

修改内容 略

9、  修改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>              

10、              運作項目

7      分子產品開發

依賴範圍對依賴傳遞造成的影響(了解)

父工程來管理   聚合

7.1     建立父工程:

1、

maven學習記錄三——maven整合ssh架構6       整合ssh架構7      分子產品開發

2、建立出的父工程如下

maven學習記錄三——maven整合ssh架構6       整合ssh架構7      分子產品開發

3、在pom.Xml中添加以下資訊:

<!-- 屬性 -->

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

                   <!-- junit -->

                   <dependency>

                            <groupId>junit</groupId>

                            <artifactId>junit</artifactId>

                            <version>4.9</version>

                            <scope>test</scope>

                   </dependency>

                   <!-- jstl -->

                   <dependency>

                            <groupId>javax.servlet</groupId>

                            <artifactId>jstl</artifactId>

                            <version>1.2</version>

                   </dependency>

         </dependencies>

 

         <build>

                   <plugins>

                            <!-- 設定編譯版本為1.7 -->

                            <plugin>

                                     <groupId>org.apache.maven.plugins</groupId>

                                     <artifactId>maven-compiler-plugin</artifactId>

                                     <configuration>

                                               <source>1.7</source>

                                               <target>1.7</target>

                                               <encoding>UTF-8</encoding>

                                     </configuration>

                            </plugin>

 

                            <!-- maven内置 的tomcat6插件 -->

                            <plugin>

                                     <groupId>org.codehaus.mojo</groupId>

                                     <artifactId>tomcat-maven-plugin</artifactId>

                                     <version>1.1</version>

                                     <configuration>

                                               <!-- 可以靈活配置工程路徑 -->

                                               <path>/ssh</path>

                                               <!-- 可以靈活配置端口号 -->

                                               <port>8080</port>

                                     </configuration>

                            </plugin>

 

                   </plugins>

         </build>      

4、釋出到本地倉庫

  dao  service  web

7.2     建立dao子子產品

1、在ssh-parent項目上右擊 ,建立時選擇 Maven Module

maven學習記錄三——maven整合ssh架構6       整合ssh架構7      分子產品開發

2、填寫子子產品名稱ssh-dao

maven學習記錄三——maven整合ssh架構6       整合ssh架構7      分子產品開發

3、把屬于dao的代碼拷貝到 該子產品中:

maven學習記錄三——maven整合ssh架構6       整合ssh架構7      分子產品開發

4、完成後釋出到本地倉庫中

7.3     建立service子子產品

1、建立方式如上:

2、把屬于service的代碼拷貝到該工程中

maven學習記錄三——maven整合ssh架構6       整合ssh架構7      分子產品開發

3、釋出到本地倉庫中

7.4     建立Action子子產品

1、選擇war的打包方式

maven學習記錄三——maven整合ssh架構6       整合ssh架構7      分子產品開發

5、  拷貝屬于action的代碼和配置檔案

maven學習記錄三——maven整合ssh架構6       整合ssh架構7      分子產品開發

6、  修改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>      

4、添加頁面:

maven學習記錄三——maven整合ssh架構6       整合ssh架構7      分子產品開發

轉載于:https://www.cnblogs.com/PengChengLi/p/8506215.html