JavaWeb项目管理工具————Maven的使用

1、什么是Maven
Maven是apache下的一个开源项目,是纯java开发,并且只是用来管理java项目的。
1.2 Maven好处

普通的传统项目
JavaWeb项目管理工具————Maven的使用_第1张图片
Maven项目
JavaWeb项目管理工具————Maven的使用_第2张图片
分析:maven项目为什么这么小?没有jar。 需要jar吗?肯定需要。没有存在于maven项目里面,jar存在于哪?
1.3 依赖管理
JavaWeb项目管理工具————Maven的使用_第3张图片
1.4 项目一键构建
编码 编译 测试(junit) 运行 打包 部署
一个 tomcat:run就能把项目运行起来

Maven能干的事:
编译 测试(junit) 运行 打包 部署
1.5 小结
Maven的好处:依赖管理(就是对jar包的统一管理 可以节省空间)、一键构建、可以跨平台、应用于大型项目 可以提高开发效率

分析:
用户管理 订单管理 支付管理 。。。。。
Maven的分模块开发
互联网项目 按业务分
传统项目 按层分 entity dao service web
2、Maven的安装配置
这里写图片描述
从此目录中拷贝文件:
JavaWeb项目管理工具————Maven的使用_第4张图片
直接解压到一个没有中文没有空格的文件夹里解压后的目录如下:
JavaWeb项目管理工具————Maven的使用_第5张图片
Maven软件的核心文件:
JavaWeb项目管理工具————Maven的使用_第6张图片
2.2 Maven环境变量配置
1、要配置jdk, maven3.3.9这个版本所需的jdk版本必须要1.7以上
2、最终要运行的是maven软件中bin目录的mvn命令
所以要配置maven的环境变量
在系统变量添加
环境变量的名称:MAVEN_HOME
变量值:就是maven软甲解压的目录F:\class32\apache-maven-3.3.9
JavaWeb项目管理工具————Maven的使用_第7张图片
3、把MAVEN_HOME添加到path里
JavaWeb项目管理工具————Maven的使用_第8张图片
4、验证maven是否配置成功:
打开dos窗口 输入: mvn –v
JavaWeb项目管理工具————Maven的使用_第9张图片
2.3Maven仓库
三种仓库
1、本地仓库 自己维护

本地仓库的配置只需要修改settings.xml文件就可以
JavaWeb项目管理工具————Maven的使用_第10张图片
2、远程仓库(私服) 公司维护
3、中央仓库 maven团队维护,有两个亿jar包,所能用到的jar包都有
三种仓库的关系如下:
JavaWeb项目管理工具————Maven的使用_第11张图片
3、演示入门程序
3.1Maven的目录结构
JavaWeb项目管理工具————Maven的使用_第12张图片
JavaWeb项目管理工具————Maven的使用_第13张图片
3.2Maven的常用命令
Clean 清理编译的文件
JavaWeb项目管理工具————Maven的使用_第14张图片
Compile 编译了主目录的文件
JavaWeb项目管理工具————Maven的使用_第15张图片
Test 编译并运行了test目录的代码
JavaWeb项目管理工具————Maven的使用_第16张图片
Package 打包
JavaWeb项目管理工具————Maven的使用_第17张图片
Install 就是把项目发布到本地仓库
JavaWeb项目管理工具————Maven的使用_第18张图片
Tomcat:run 一键启动
3.3 Maven的生命周期
ompile test package install deploy(发布到私服)
三种生命周期

Clean生命周期
Clean
Default生命周期
Compile test package install deploy
Site生命周期
Site
3.4 命令和生命周期的阶段的关系
不同的生命周期的命令可以同时执行
Mvn clean package
4、项目构建
Maven在eclipse的配置
1、选择本地安装好的3.3.9版本的maven软件
JavaWeb项目管理工具————Maven的使用_第19张图片
2、修改默认的本地仓库地址
JavaWeb项目管理工具————Maven的使用_第20张图片
项目构建
1、新建maven项目
JavaWeb项目管理工具————Maven的使用_第21张图片
2、跳过骨架,如果不跳过骨架选择创建出的项目目录是不全的
JavaWeb项目管理工具————Maven的使用_第22张图片
3、填写坐标
JavaWeb项目管理工具————Maven的使用_第23张图片
4、创建工程
JavaWeb项目管理工具————Maven的使用_第24张图片
4、处理红色叉号
手动在webapp文件夹下创建一个WEB-INF文件夹,在里面放一个web.xml文件
JavaWeb项目管理工具————Maven的使用_第25张图片
5、处理java编译版本(固定写法)

在pom.xml中添加如下代码

<build>
        
        <plugins>
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-compiler-pluginartifactId>
                <version>3.5.1version>  
                <configuration>
                    <source>1.7source>
                    <target>1.7target>
                    <encoding>UTF-8encoding>
                configuration>
            plugin>
        plugins>
    build>

6、创建一个servlet
JavaWeb项目管理工具————Maven的使用_第26张图片
修改web.xml
删除重复的代码

xmlns=http://java.sun.com/xml/ns/javaee

JavaWeb项目管理工具————Maven的使用_第27张图片
添加jar包(固定写法)
在pom中添加如下代码:

<dependencies>
        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>4.9version>
            <scope>testscope>
        dependency>
        <dependency>
            <groupId>javax.servletgroupId>
            <artifactId>servlet-apiartifactId>
            <version>2.5version>
            <scope>providedscope>
        dependency>
        <dependency>
            <groupId>javax.servletgroupId>
            <artifactId>jsp-apiartifactId>
            <version>2.0version>
            <scope>providedscope>
        dependency>

    dependencies>

会发现jar包已添加到项目中
JavaWeb项目管理工具————Maven的使用_第28张图片
启动项目
右击项目—run as –maven build ….
JavaWeb项目管理工具————Maven的使用_第29张图片
5、依赖管理
Jar包的管理

需求:整合struts2 页面上传一个客户id 跳转页面
5.1 添加依赖:
打开maven仓库的视图:
JavaWeb项目管理工具————Maven的使用_第30张图片
5.2 重建索引
JavaWeb项目管理工具————Maven的使用_第31张图片
1、创建maven项目(同上)
2、跳过骨架(同上)
3、填写坐标信息(同上)
4、添加web.xml文件(同上)
5、修改编译版本(同上)
6、添加坐标 选择Dependencies标签 点击add
JavaWeb项目管理工具————Maven的使用_第32张图片
7、手动输入要添加的坐标,选择版本
JavaWeb项目管理工具————Maven的使用_第33张图片
8、可以看到 pom.xml文件中多出了如下代码
JavaWeb项目管理工具————Maven的使用_第34张图片
9、同样的方式添加servlet-api.jar和jsp-api.jar 注意选择scope为provided
10、写action代码

import com.opensymphony.xwork2.ActionSupport;
public class CutomerAction extends ActionSupport {
    private Long custId;
    public Long getCustId() {
        return custId;
    }
    public void setCustId(Long custId) {
        this.custId = custId;
    }
    public String findById(){

        return SUCCESS;
    }

}

11、添加struts.xml文件放到resources目录中
这里写图片描述
内容:




<struts>
    
    
    <constant name="struts.i18n.encoding" value="UTF-8">constant>
    
    <constant name="struts.devMode" value="true">constant>

    
    <package name="customer" namespace="/" extends="struts-default">

        <action name="find" class="cn.itcast.action.CutomerAction"  
            method="findById">
            <result name="success">/jsp/info.jspresult>
        action>

    package>
struts>

12、添加jsp页面
JavaWeb项目管理工具————Maven的使用_第35张图片
13、修改web.xml文件 添加过滤器

<filter>
    <filter-name>struts2filter-name>
     <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilterfilter-class>
  filter>
  <filter-mapping>
  <filter-name>struts2filter-name>
  /*
  filter-mapping>

5.3 依赖范围

5.3.1 Compile struts2-core
编译(compile)时需要 测试时需要,运行时需要,打包时需要
5.3.2 Provided jsp-api.jar servlet-api.jar
编译(compile)时需要,测试(test)时也需要 ,运行时不需要,打包时不需要
5.3.3 Runtime 数据库驱动包
编译时不需要,测试时需要,运行时需要,打包时需要
5.3.4 Test junit.jar
编译时不需要,测试时需要,运行时不需要,打包也不需要
添加插件

Maven add plugin
JavaWeb项目管理工具————Maven的使用_第36张图片
如果用tomcat7运行用命令:
Tomcat7:run

常见问题:
JavaWeb项目管理工具————Maven的使用_第37张图片
这是由jre版本与jdk版本不匹配的问题,jdk版本不应该高于jre版本。

<modelVersion>  
坐标  GAV
<groupId>cn.itcastgroupId>
<artifactId>sshartifactId>
<version>0.0.1-SNAPSHOTversion>
Packaging  打包方式  
Jar  war  pom
<dependencies>
 <dependency>
<build>  里面放的是插件

<plugins>
 <plugin>

6、整合ssh框架
6.1 依赖传递
只添加了一个struts2-core依赖,发现项目中出现了很多jar,
这种情况 叫 依赖传递
JavaWeb项目管理工具————Maven的使用_第38张图片
6.2 依赖版本冲突的解决
1、第一声明优先原则

<dependencies>
  
    <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-contextartifactId>
        <version>4.2.4.RELEASEversion>
    dependency>



    <dependency>
        <groupId>org.apache.strutsgroupId>
        <artifactId>struts2-spring-pluginartifactId>
        <version>2.3.24version>
    dependency>

2、路径近者优先原则
自己添加jar包

<dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-beansartifactId>
        <version>4.2.4.RELEASEversion>
    dependency>

3、排除原则(排除spring-beans版本)

<dependency>
        <groupId>org.apache.strutsgroupId>
        <artifactId>struts2-spring-pluginartifactId>
        <version>2.3.24version>
        <exclusions>
          <exclusion>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-beansartifactId>
          exclusion>
        exclusions>
    dependency>

4、版本锁定原则

<properties>
        <spring.version>4.2.4.RELEASEspring.version>
        <hibernate.version>5.0.7.Finalhibernate.version>
        <struts.version>2.3.24struts.version>
    properties>

    
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-contextartifactId>
                <version>${spring.version}version>
            dependency>
dependencies>
dependencyManagement>

需求:
传客户ID 页面上显示客户信息
准备数据库
6.3 构建项目
1、创建数据库
JavaWeb项目管理工具————Maven的使用_第39张图片
2、完善pom.xml文件,把ssh相关的依赖都添加上去(固定写法,直接添加依赖不需要手写)

 
    <properties>
        <spring.version>4.2.4.RELEASEspring.version>
        <hibernate.version>5.0.7.Finalhibernate.version>
        <struts.version>2.3.24struts.version>
    properties>

    
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-contextartifactId>
                <version>${spring.version}version>
            dependency>
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-aspectsartifactId>
                <version>${spring.version}version>
            dependency>
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-ormartifactId>
                <version>${spring.version}version>
            dependency>
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-testartifactId>
                <version>${spring.version}version>
            dependency>
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-webartifactId>
                <version>${spring.version}version>
            dependency>
            <dependency>
                <groupId>org.hibernategroupId>
                <artifactId>hibernate-coreartifactId>
                <version>${hibernate.version}version>
            dependency>
            <dependency>
                <groupId>org.apache.strutsgroupId>
                <artifactId>struts2-coreartifactId>
                <version>${struts.version}version>
            dependency>
            <dependency>
                <groupId>org.apache.strutsgroupId>
                <artifactId>struts2-spring-pluginartifactId>
                <version>${struts.version}version>
            dependency>
        dependencies>
    dependencyManagement>
    
    <dependencies>
        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-contextartifactId>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-aspectsartifactId>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-ormartifactId>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-testartifactId>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-webartifactId>
        dependency>
        
        <dependency>
            <groupId>org.hibernategroupId>
            <artifactId>hibernate-coreartifactId>
        dependency>

        
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>5.1.6version>
            <scope>runtimescope>
        dependency>
        

        <dependency>
            <groupId>c3p0groupId>
            <artifactId>c3p0artifactId>
            <version>0.9.1.2version>
        dependency>


        
        <dependency>
            <groupId>org.apache.strutsgroupId>
            <artifactId>struts2-coreartifactId>
        dependency>
        <dependency>
            <groupId>org.apache.strutsgroupId>
            <artifactId>struts2-spring-pluginartifactId>
        dependency>

        
        <dependency>
            <groupId>javax.servletgroupId>
            <artifactId>servlet-apiartifactId>
            <version>2.5version>
            <scope>providedscope>
        dependency>
        <dependency>
            <groupId>javax.servletgroupId>
            <artifactId>jsp-apiartifactId>
            <version>2.0version>
            <scope>providedscope>
        dependency>
        
        <dependency>
            <groupId>org.slf4jgroupId>
            <artifactId>slf4j-log4j12artifactId>
            <version>1.7.2version>
        dependency>
        
        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>4.9version>
            <scope>testscope>
        dependency>
        
        <dependency>
            <groupId>javax.servletgroupId>
            <artifactId>jstlartifactId>
            <version>1.2version>
        dependency>
    dependencies>

    <build>
        <plugins>
            
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-compiler-pluginartifactId>
                <configuration>
                    <source>1.7source>
                    <target>1.7target>
                    <encoding>UTF-8encoding>
                configuration>
            plugin>

            
            <plugin>
                <groupId>org.codehaus.mojogroupId>
                <artifactId>tomcat-maven-pluginartifactId>
                <version>1.1version>
                <configuration>
                    
                    <path>/sshpath>
                    
                    <port>8080port>
                configuration>
            plugin>
        plugins>
    build>

3、完成实体类代码
JavaWeb项目管理工具————Maven的使用_第40张图片
4、完成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);
    }
}

5、完成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);
    }
}

6、完成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;
    }
}

7、将配置文件放入到 src/main/resources目录中
Customer.hbm.xml




<hibernate-mapping>
    <class name="cn.ctgu.crm.entity.Customer" table="cst_customer"  optimistic-lock="version">
        <id name="custId" type="java.lang.Long">
            <column name="cust_id" />
            <generator class="identity" />
        id>
        <property name="custName" type="string">
            <column name="cust_name" length="32" not-null="true">column>
        property>
        <property name="custUserId" type="java.lang.Long">
            <column name="cust_user_id">column>
        property>
        <property name="custCreateId" type="java.lang.Long">
            <column name="cust_create_id">column>
        property>
        <property name="custIndustry" type="string">
            <column name="cust_industry" length="32">column>
        property>
        <property name="custLevel" type="string">
            <column name="cust_level" length="32">column>
        property>
        <property name="custLinkman" type="string">
            <column name="cust_linkman" length="64">column>
        property>
        <property name="custPhone" type="string">
            <column name="cust_phone" length="64">column>
        property>
        <property name="custMobile" type="string">
            <column name="cust_mobile" length="16">column>
        property>
    class>
hibernate-mapping>

由于applicationContext.xml、hibernate.cfg.xml、struts.xml会在下面的分模块开发中会涉及到在这里就不赘述。

JavaWeb项目管理工具————Maven的使用_第41张图片
8、修改web.xml 添加spring的监听

<listener>
 <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
listener> 
<context-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:applicationContext.xmlparam-value>
context-param>    

9、运行项目
tomcat:run

7、分模块开发
依赖范围对依赖传递造成的影响
JavaWeb项目管理工具————Maven的使用_第42张图片

7.1 创建父工程:
1、父工程来管理 聚合
JavaWeb项目管理工具————Maven的使用_第43张图片
2、创建出的父工程如下
这里写图片描述
3、在pom.Xml中添加以下信息:


    <properties>
        <spring.version>4.2.4.RELEASEspring.version>
        <hibernate.version>5.0.7.Finalhibernate.version>
        <struts.version>2.3.24struts.version>
    properties>

    
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-contextartifactId>
                <version>${spring.version}version>
            dependency>
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-aspectsartifactId>
                <version>${spring.version}version>
            dependency>
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-ormartifactId>
                <version>${spring.version}version>
            dependency>
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-testartifactId>
                <version>${spring.version}version>
            dependency>
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-webartifactId>
                <version>${spring.version}version>
            dependency>
            <dependency>
                <groupId>org.hibernategroupId>
                <artifactId>hibernate-coreartifactId>
                <version>${hibernate.version}version>
            dependency>
            <dependency>
                <groupId>org.apache.strutsgroupId>
                <artifactId>struts2-coreartifactId>
                <version>${struts.version}version>
            dependency>
            <dependency>
                <groupId>org.apache.strutsgroupId>
                <artifactId>struts2-spring-pluginartifactId>
                <version>${struts.version}version>
            dependency>
        dependencies>
    dependencyManagement>
    
    <dependencies>
        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-contextartifactId>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-aspectsartifactId>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-ormartifactId>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-testartifactId>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-webartifactId>
        dependency>
        
        <dependency>
            <groupId>org.hibernategroupId>
            <artifactId>hibernate-coreartifactId>
        dependency>

        
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>5.1.6version>
            <scope>runtimescope>
        dependency>
        

        <dependency>
            <groupId>c3p0groupId>
            <artifactId>c3p0artifactId>
            <version>0.9.1.2version>
        dependency>


        
        <dependency>
            <groupId>org.apache.strutsgroupId>
            <artifactId>struts2-coreartifactId>
        dependency>
        <dependency>
            <groupId>org.apache.strutsgroupId>
            <artifactId>struts2-spring-pluginartifactId>
        dependency>

        
        <dependency>
            <groupId>javax.servletgroupId>
            <artifactId>servlet-apiartifactId>
            <version>2.5version>
            <scope>providedscope>
        dependency>
        <dependency>
            <groupId>javax.servletgroupId>
            <artifactId>jsp-apiartifactId>
            <version>2.0version>
            <scope>providedscope>
        dependency>
        
        <dependency>
            <groupId>org.slf4jgroupId>
            <artifactId>slf4j-log4j12artifactId>
            <version>1.7.2version>
        dependency>
        
        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>4.9version>
            <scope>testscope>
        dependency>
        
        <dependency>
            <groupId>javax.servletgroupId>
            <artifactId>jstlartifactId>
            <version>1.2version>
        dependency>
    dependencies>

    <build>
        <plugins>
            
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-compiler-pluginartifactId>
                <configuration>
                    <source>1.7source>
                    <target>1.7target>
                    <encoding>UTF-8encoding>
                configuration>
            plugin>

            
            <plugin>
                <groupId>org.codehaus.mojogroupId>
                <artifactId>tomcat-maven-pluginartifactId>
                <version>1.1version>
                <configuration>
                    
                    <path>/sshpath>
                    
                    <port>8080port>
                configuration>
            plugin>

        plugins>
    build>

4、发布到本地仓库
JavaWeb项目管理工具————Maven的使用_第44张图片
JavaWeb项目管理工具————Maven的使用_第45张图片
7.2 创建dao子模块
1、在ssh-parent项目上右击 ,创建时选择 Maven Module
JavaWeb项目管理工具————Maven的使用_第46张图片
2、填写子模块名称ssh-dao
JavaWeb项目管理工具————Maven的使用_第47张图片
3、把属于dao的代码拷贝到 该模块中:
JavaWeb项目管理工具————Maven的使用_第48张图片
CustomerDao.java

package cn.ctgu.crm.dao;

import cn.ctgu.crm.entity.Customer;

public interface CustomerDao {
    /*
     * 根据id获取客户信息
     * */
    public Customer findById(Long id);
}

CustomerDaoImpl.java

package cn.ctgu.crm.dao.impl;

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

import cn.ctgu.crm.dao.CustomerDao;
import cn.ctgu.crm.entity.Customer;

public class CustomerDaoImpl extends HibernateDaoSupport implements CustomerDao {

    @Override
    public Customer findById(Long id) {
        // TODO Auto-generated method stub
        return this.getHibernateTemplate().get(Customer.class, id);
    }

}

Customer.java

package cn.ctgu.crm.entity;

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;
    }
    public void setCustId(Long custId) {
        this.custId = custId;
    }
    public String getCustName() {
        return custName;
    }
    public void setCustName(String custName) {
        this.custName = custName;
    }
    public Long getCustUserId() {
        return custUserId;
    }
    public void setCustUserId(Long custUserId) {
        this.custUserId = custUserId;
    }
    public Long getCustCreateId() {
        return custCreateId;
    }
    public void setCustCreateId(Long custCreateId) {
        this.custCreateId = custCreateId;
    }
    public String getCustIndustry() {
        return custIndustry;
    }
    public void setCustIndustry(String custIndustry) {
        this.custIndustry = custIndustry;
    }
    public String getCustLevel() {
        return custLevel;
    }
    public void setCustLevel(String custLevel) {
        this.custLevel = custLevel;
    }
    public String getCustLinkman() {
        return custLinkman;
    }
    public void setCustLinkman(String custLinkman) {
        this.custLinkman = custLinkman;
    }
    public String getCustPhone() {
        return custPhone;
    }
    public void setCustPhone(String custPhone) {
        this.custPhone = custPhone;
    }
    public String getCustMobile() {
        return custMobile;
    }
    public void setCustMobile(String custMobile) {
        this.custMobile = custMobile;
    }

}

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="123456" />
    bean>

    
    <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        
        <property name="dataSource" ref="dataSource"/>
        
        <property name="configLocations" value="classpath:hibernate.cfg.xml">property>
    bean>

    <bean id="customerDao" class="cn.ctgu.crm.dao.impl.CustomerDaoImpl">
        <property name="sessionFactory" ref="sessionFactory">property>
    bean>


beans>

hibernate.cfg.xml




<hibernate-configuration>
    
    <session-factory>
        
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialectproperty>

        
        <property name="hibernate.show_sql">trueproperty>
        
        <property name="hibernate.format_sql">trueproperty>

        <property name="hibernate.hbm2ddl.auto">noneproperty>

        
        <mapping resource="cn/ctgu/crm/entity/Customer.hbm.xml"/>
    session-factory>
hibernate-configuration>

pom.xml

<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.0modelVersion>
  <parent>
    <groupId>cn.ctgu.ssh-mavengroupId>
    <artifactId>ssh-parentartifactId>
    <version>0.0.1-SNAPSHOTversion>
  parent>
  <artifactId>ssh-daoartifactId>
  <dependencies>
    <dependency>
        <groupId>junitgroupId>
        <artifactId>junitartifactId>
        <version>4.9version>
        <scope>testscope>
    dependency>
  dependencies>
project>

4、完成后发布到本地仓库中

7.3 创建service子模块
1、创建方式如上:
2、把属于service的代码拷贝到该工程中
CustomerService.java

package cn.ctgu.crm.service;

import cn.ctgu.crm.entity.Customer;

public interface CustomerService {
    public Customer findById(Long id);
}

CustomerServiceImpl.java

package cn.ctgu.crm.service.impl;

import cn.ctgu.crm.dao.CustomerDao;
import cn.ctgu.crm.entity.Customer;
import cn.ctgu.crm.service.CustomerService;

public class CustomerServiceImpl implements CustomerService {

    private CustomerDao customerDao;
    @Override
    public Customer findById(Long id) {
        // TODO Auto-generated method stub
        return customerDao.findById(id);
    }
    public void setCustomerDao(CustomerDao customerDao) {
        this.customerDao = customerDao;
    }


}

applicationContext-service.xml

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


    "customerService" class="cn.ctgu.crm.service.impl.CustomerServiceImpl">
        "customerDao" ref="customerDao">
    


pom.xml

<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.0modelVersion>
  <parent>
    <groupId>cn.ctgu.ssh-mavengroupId>
    <artifactId>ssh-parentartifactId>
    <version>0.0.1-SNAPSHOTversion>
  parent>
  <artifactId>ssh-serviceartifactId>
  <dependencies>
    <dependency>
        <groupId>cn.ctgu.ssh-mavengroupId>
        <artifactId>ssh-daoartifactId>
        <version>0.0.1-SNAPSHOTversion>
    dependency>
  dependencies>
project>

JavaWeb项目管理工具————Maven的使用_第49张图片
3、发布到本地仓库中
7.4 创建Action子模块
1、选择war的打包方式
JavaWeb项目管理工具————Maven的使用_第50张图片
2、拷贝属于action的代码和配置文件
JavaWeb项目管理工具————Maven的使用_第51张图片
CustomerAction.java

package cn.ctgu.crm.Action;

import cn.ctgu.crm.entity.Customer;
import cn.ctgu.crm.service.CustomerService;

public class CustomerAction {
    private static final String SUCCESS = null;
    private Long custId;
    private Customer customer;
    private CustomerService customerService;

    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 CustomerService getCustomerService() {
        return customerService;
    }

    public void setCustomerService(CustomerService customerService) {
        this.customerService = customerService;
    }
    public String findCustomerById() {
        customer=customerService.findById(custId);
        System.out.println("前端传过来的客户id是:"+custId);
        return SUCCESS;
    }


}

applicationContext-action.xml

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



    "customerAction" class="cn.ctgu.crm.Action.CustomerAction" scope="prototype">
        "customerService" ref="customerService">

    

struts.xml




<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="cn.ctgu.crm.Action.CustomerAction" method="findCustomerById">
            <result name="success">/info.jspresult>
        action>

    package>
struts>

pom.xml

<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.0modelVersion>
  <parent>
    <groupId>cn.ctgu.ssh-mavengroupId>
    <artifactId>ssh-parentartifactId>
    <version>0.0.1-SNAPSHOTversion>
  parent>
  <artifactId>ssh-webartifactId>
  <packaging>warpackaging>
  <dependencies>
    <dependency>
        <groupId>cn.ctgu.ssh-mavengroupId>
        <artifactId>ssh-serviceartifactId>
        <version>0.0.1-SNAPSHOTversion>
    dependency>
  dependencies>
project>

3、修改web.xml 添加spring监听


<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">


<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
listener>
<context-param>
    <param-name>contextConfigLocationparam-name>
    
    <param-value>classpath*:applicationContext-*.xmlparam-value>
context-param>


<filter>
<filter-name>struts2filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilterfilter-class>
filter>

<filter-mapping>
<filter-name>struts2filter-name>
<url-pattern>/*url-pattern>
filter-mapping>


    <welcome-file-list>
        <welcome-file>index.htmlwelcome-file>
        <welcome-file>index.htmwelcome-file>
        <welcome-file>index.jspwelcome-file>
        <welcome-file>default.htmlwelcome-file>
        <welcome-file>default.htmwelcome-file>
        <welcome-file>default.jspwelcome-file>
    welcome-file-list>

web-app>

8、私服 nexus
下载好nexus后进入到bin文件夹安装nexus
这里写图片描述
启动服务
这里写图片描述
JavaWeb项目管理工具————Maven的使用_第52张图片
启动失败的解决方法:
JavaWeb项目管理工具————Maven的使用_第53张图片
JavaWeb项目管理工具————Maven的使用_第54张图片

登录nexus

http://localhost:8081/nexus

用户名/密码 admin/admin123

仓库类型
JavaWeb项目管理工具————Maven的使用_第55张图片

Virtual 虚拟仓库
Proxy 代理仓库
Hosted 宿主仓库 本地仓库
Group 组
需求:
把dao放到私服上,然后service从私服上下载

需求 :将ssh_dao的这个工程打成jar包,并放入到私服上去.
8.1上传dao
第一步: 需要在客户端即部署dao工程的电脑上配置 maven环境,并修改 Maven中的settings.xml 文件,配置连接私服的用户和密码 。

此用户名和密码用于私服校验,因为私服需要知道上传都 的账号和密码 是否和私服中的账号和密码 一致。

  <server>
      <id>releasesid>
      <username>adminusername>
      <password>admin123password>
    server>
    <server>
      <id>snapshotsid>
      <username>adminusername>
      <password>admin123password>
    server>

第二步: 配置项目ssh-dao中的pom.xml
配置私服仓库的地址,本公司的自己的jar包会上传到私服的宿主仓库,根据工程的版本号决定上传到哪个宿主仓库,如果版本为release则上传到私服的release仓库,如果版本为snapshot则上传到私服的snapshot仓库

<distributionManagement>
    <repository>
        <id>releasesid>
    <url>http://localhost:8081/nexus/content/repositories/releases/url>
    repository> 
    <snapshotRepository>
        <id>snapshotsid>
    <url>http://localhost:8081/nexus/content/repositories/snapshots/url>
    snapshotRepository> 
  distributionManagement>

ssh-dao中最终的为pom.xml

<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.0modelVersion>
  <parent>
    <groupId>cn.ctgu.ssh-mavengroupId>
    <artifactId>ssh-parentartifactId>
    <version>0.0.1-SNAPSHOTversion>
  parent>
  <artifactId>ssh-daoartifactId>
  <dependencies>
    <dependency>
        <groupId>junitgroupId>
        <artifactId>junitartifactId>
        <version>4.9version>
        <scope>testscope>
    dependency>
  dependencies>

   <distributionManagement>
    <repository>
        <id>releasesid>
    <url>http://localhost:8081/nexus/content/repositories/releases/url>
    repository> 
    <snapshotRepository>
        <id>snapshotsid>
    <url>http://localhost:8081/nexus/content/repositories/snapshots/url>
    snapshotRepository> 
  distributionManagement>
project>

注意:pom.xml这里 和 settings.xml 配置 对应!

第三步:执行deploy命令发布到私服
8.2 下载dao
第一步 修改Maven中的settings.xml

<profile>   
    
    <id>devid>   
    <repositories>   
      <repository>  
        
        <id>nexusid>   
        
        <url>http://localhost:8081/nexus/content/groups/public/url>   
        
        <releases>   
          <enabled>trueenabled>   
        releases>   
        
        <snapshots>   
          <enabled>trueenabled>   
        snapshots>   
      repository>   
    repositories>  
     <pluginRepositories>  
        
        <pluginRepository>  
            
            <id>publicid>  
            <name>Public Repositoriesname>  
            <url>http://localhost:8081/nexus/content/groups/public/url>  
        pluginRepository>  
    pluginRepositories>  
  profile>  


  <activeProfiles>
    <activeProfile>devactiveProfile>
  activeProfiles>

第二步 删除本地仓库中的dao

第三步 update service工程,出现以下信息说明已经成功
JavaWeb项目管理工具————Maven的使用_第56张图片
说明:本博客是参照传智播客课程教案所编写的。

你可能感兴趣的:(java)