一、首先我们要知道依赖传递这个名词。我们在项目中添加组件的时候会发现点击添加一个组建时候,在pom.xml文件里会发现许多jar包.
这就是依赖传递的结果。
二、依赖也会带来jar包版本的冲突。我们可以采用以下几种方法来解决版本冲突。
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> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>4.2.4.RELEASE</version> </dependency>
</dependency>
2.
二、依赖也会带来jar包版本的冲突。我们可以采用以下几种方法来解决版本冲突。
1.第一声明优先原则
1、 路径近者优先原则
自己添加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>
〈!--如下代码将说明项目将该jar包排除-->
4.版本锁定原则<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.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页面上显示客户信息。准备数据库
)
1.创建数据库。
2.创建数据表
3.完善pomlx.(把ssh相关依赖都添加上去)
<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>pds.ssh</groupId> <artifactId>sshmaven</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <name>sshmaven项目</name> <description>这是我利用maven搭建ssh框架</description> <!-- 属性 --> <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> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> <configuration> <port>8888</port> <path>/sshmaven</path> </configuration> </plugin> <!-- 设置编译版本为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> </plugins> </build> </project>
4.完成实体代码的书写
6.完成其他代码的书写
mport 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; } }
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>
7发布运行即可二、依赖也会带来jar包版本的冲突。我们可以采用以下几种方法来解决版本冲突。
1.第一声明优先原则