在settings文件中 增長阿里雲鏡像java
<mirrors> <mirror> <id>nexus-aliyun</id> <mirrorOf>*</mirrorOf> <name>Nexus aliyun</name> <url>http://maven.aliyun.com/nexus/content/groups/public</url> </mirror> </mirrors>
特別注意點(文件的加載)mysql
<!-- 表示把配置文件打包到jar包中 否則會加載不到配置文件 --> <build> <resources> <resource> <directory>src/main/resource</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> </resources> </build>
1.1Maven的好處web
節省空間 對jar包作了統一管理 依賴管理spring
一鍵構建sql
可跨平臺數據庫
應用在大型項目可提升開發效率apache
1.2Maven安裝部署配置api
1.3Maven的倉庫tomcat
本地倉庫服務器
遠程倉庫(私服)
中央倉庫
1.4添加依賴
從網絡上搜索
在本地重建索引,以索引的方式搜索
1.5項目構建
1.6依賴範圍
Compile struts2 框架jar
Provided jsp-api.jar 重點
Runtime 數據庫驅動包
Test junit.jar
1.7總結
<modelVersion> 自帶的xsd約束的版本
座標 GAV
<groupId>cn.itcast</groupId> <artifactId>ssh</artifactId> <version>0.0.1-SNAPSHOT</version>
Packaging 打包方式
Jar war pom
<dependencies> <dependency> 依賴 <build> 裏面放的是插件 <plugins> <plugin>
2.1依賴傳遞
只添加了一個struts2-core依賴,發現項目中出現了不少jar, 這種狀況 叫 依賴傳遞
2.2依賴版本衝突的解決(1,2 爲調節原則,3 爲排除原則,4 爲版本鎖定原則)
一、第一聲明優先原則
<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>
二、路徑近者優先原則 本身添加jar包
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>4.2.4.RELEASE</version> </dependency>
三、排除原則
<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>
四、版本鎖定原則
<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.2四、spring4.2.四、hibernate5.0.7 --> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> </dependencies> </dependencyManagement>
需求:
傳客戶ID 頁面上顯示客戶信息
準備數據庫
2.3構建項目
一、建立數據庫,
二、執行準備好的sql腳本
三、完善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.2四、spring4.2.四、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>
四、完成實體類代碼
五、完成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); } }
六、完成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); } }
七、完成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; } }
八、拷貝配置文件並修改
從以下圖位置拿到配置文件
放入到 src/main/resources目錄中
修改內容 略
九、修改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>
十、運行項目
依賴範圍對依賴傳遞形成的影響(瞭解)
父工程來管理 聚合
3.1建立父工程:
三、在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.2四、spring4.2.四、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>
四、發佈到本地倉庫
dao service web 3.2建立dao子模塊 一、在ssh-parent項目上右擊 ,建立時選擇 Maven Module
二、填寫子模塊名稱ssh-dao
三、把屬於dao的代碼拷貝到 該模塊中:
四、完成後發佈到本地倉庫中
3.3建立service子模塊 一、建立方式如上: 二、把屬於service的代碼拷貝到該工程中
三、發佈到本地倉庫中
3.4建立Action子模塊 一、選擇war的打包方式
五、拷貝屬於action的代碼和配置文件
六、修改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>
四、添加頁面:
啓動失敗的解決方法:
登陸nexus 用戶名/密碼 admin/admin123
倉庫類型
Virtual 虛擬倉庫
Proxy 代理倉庫
Hosted 宿主倉庫 本地倉庫
Group 組
需求:
把dao放到私服上,而後service從私服上下載
需求 :將ssh_dao的這個工程打成jar包,並放入到私服上去.
4.1上傳dao
第一步: 須要在客戶端即部署dao工程的電腦上配置 maven環境,並修改 settings.xml 文件,配置鏈接私服的用戶和密碼 。
此用戶名和密碼用於私服校驗,由於私服須要知道上傳都 的帳號和密碼 是否和私服中的帳號和密碼 一致。
<server> <id>releases</id> <username>admin</username> <password>admin123</password> </server> <server> <id>snapshots</id> <username>admin</username> <password>admin123</password> </server>
第二步: 配置項目pom.xml
配置私服倉庫的地址,本公司的本身的jar包會上傳到私服的宿主倉庫,根據工程的版本號決定上傳到哪一個宿主倉庫,若是版本爲release則上傳到私服的release倉庫,若是版本爲snapshot則上傳到私服的snapshot倉庫
<distributionManagement> <repository> <id>releases</id> <url>http://localhost:8081/nexus/content/repositories/releases/</url> </repository> <snapshotRepository> <id>snapshots</id> <url>http://localhost:8081/nexus/content/repositories/snapshots/</url> </snapshotRepository> </distributionManagement>
注意:pom.xml這裏<id> 和 settings.xml 配置 <id> 對應!
第三步:執行deploy命令發佈到私服
4.2下載dao
第一步 修改settings.xml
<profile> <!--profile的id--> <id>dev</id> <repositories> <repository> <!--倉庫id,repositories能夠配置多個倉庫,保證id不重複--> <id>nexus</id> <!--倉庫地址,即nexus倉庫組的地址--> <url>http://localhost:8081/nexus/content/groups/public/</url> <!--是否下載releases構件--> <releases> <enabled>true</enabled> </releases> <!--是否下載snapshots構件--> <snapshots> <enabled>true</enabled> </snapshots> </repository> </repositories> <pluginRepositories> <!-- 插件倉庫,maven的運行依賴插件,也須要從私服下載插件 --> <pluginRepository> <!-- 插件倉庫的id不容許重複,若是重複後邊配置會覆蓋前邊 --> <id>public</id> <name>Public Repositories</name> <url>http://localhost:8081/nexus/content/groups/public/</url> </pluginRepository> </pluginRepositories> </profile> <activeProfiles> <activeProfile>dev</activeProfile> </activeProfiles>
第二步 刪除本地倉庫中的dao
第三步 update service工程,出現如下信息說明已經成功
maven Console
服務器上一些存儲文件的位置