Maven02——回顾整合ssh框架分模块开发私服
Posted kent
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Maven02——回顾整合ssh框架分模块开发私服相关的知识,希望对你有一定的参考价值。
1 回顾
1.1 Maven的好处
节省空间 对jar包做了统一管理 依赖管理
一键构建
可跨平台
应用在大型项目可提高开发效率
1.2 Maven安装部署配置
1.3 Maven的仓库
本地仓库
远程仓库(私服)
中央仓库
1.4 添加依赖
从网络上搜索:
http://mvnrepository.com/(推荐)
在本地重建索引,以索引的方式搜索
1.5 项目构建
1.6 依赖范围
Compile struts2 框架jar
Provided jsp-api.jar 重点(因为他会和tomcat的包相冲突)
Runtime 数据库驱动包
Test junit.jar
1.7 总结
<modelVersion>
坐标 GAV
<groupId>cn.itcast</groupId>
<artifactId>ssh</artifactId>
<version>0.0.1-SNAPSHOT</version>
Packaging 打包方式
Jar war pom
<dependencies>
<dependency>
<build> 里面放的是插件
<plugins>
<plugin>
2 整合ssh框架
2.1 依赖传递
只添加了一个struts2-core依赖,发现项目中出现了很多jar,
这种情况 叫 依赖传递
2.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 页面上显示客户信息
准备数据库
2.3 构建项目
1、 创建数据库,
2、 执行准备好的sql脚本
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、 完成实体类代码
public class Customer { private Long custId; private String custName; private Long custUserId; private Long custCreateId; private String custIndustry; private String custLevel; private String custLinkman; private String custPhone; private String custMobile; public Long getCustId() { return custId; } ..省略get set方法
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); } }
配置好spring,添加customer的bean注入sessionFactory,然后右键java文件进行junit测试
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、 拷贝配置文件并修改
从如下图位置拿到配置文件
放入到 src/main/resources目录中
修改内容 略
9、 修改web.xml 以上是关于Maven02——回顾整合ssh框架分模块开发私服的主要内容,如果未能解决你的问题,请参考以下文章