一、回顾
1、什么是maven
    是一个软件开发的管理工具,主要管理的工作是:依赖管理、项目构建
2、使用maven的好处
    能够集中管理jar包,提供一键构建
3、maven的安装及配置
    配置MAVEN_HOME,PATH路径配置
    本地仓库<localRepository>
    运行:mvn -v
4、常用的maven命令
    complie,test,package,install,deploy
5、maven工程是具有一定的目录结构的
    src
        main
            java(程序主要代码)
            resources(配置文件)
        test
            java(测试代码)
            resources(测试的配置文件)
    pom.xml
6、eclipse工具下的maven工具开发
7、在pom.xml文件中如何引入坐标
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
        <scope>provided</scope>
    </dependency>

二、Maven工程的拆分和聚合 
一个完整的早期开发的crm项目,现在使用maven工程对其进行拆分,可以将dao拆解出来形成表现独立的工程,同样service和action也可以拆分
工程拆分之后,将来还要聚合(就是将拆分的工程进行进一步组合在一起,又形成一个完整的项目)
为了达到聚合的目标,所以会引入
父工程(maven project)
模块(maven module)dao,service,web

开发步骤:
1、创建一个maven父工程
    创建后的父工程结构很简单
        从目录结构可以看出,父工程本身不写代码,里面有一个pom.xml文件,这个文件可以将多个子模块中通用的jar包所对应的坐标,
        集中在父工程中配置,将来的子模块就可以不需要在pom.xml中配置通用jar的坐标了

    packaging->pom  pom打包方式用于父工程    
2、如何创建这个父工程的一个子模块
    maven module -》next
3、再次查看父工程的pom.xml文件
4、查看子模块的pom.xml发现多了一个parent节点
  <parent>
    <groupId>my.mime.ssh</groupId>
    <artifactId>ssh_parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  并且内部所包含的节点,其实就是父工程的坐标
  坐标=groupId+artifactId+version
        组织名  项目名      版本
三、冲突问题
当一个项目依赖的构件比较多时,它们相互之间存在依赖,当需要对依赖版本统一管理时如果让maven自动来处理可能不太好
org.apache.struts依赖spring-beans-3.0.5,spring-context依赖spring-beans-4.2.4,但是spring-beans-3.0.5加入到工程中,我们希望spring-beans-4.2.4加入工程
1、通过添加<exclusion>标签来解决jar包冲突
在父工程中引入struts-core,hibernate-core发现jar包有冲突
javassist存在版本上冲突问题
 dependenct Hierarchy->jar包上右键execlude maven artifact

父工程的pom.xml文件中添加了 exclusions 排除了之前排除的jar包

依赖调解原则
maven自动按照下面的原则调解
1、第一声明者优先原则
    在pom文件定义依赖,先声明的依赖为准

2、路径近者优先原则
    A依赖spring-beans-4.2.4,A依赖B依赖spring-beans-3.0.5,则spring-beans-4.2.4优先被依赖在A,因为spring-beans-4.2.4相对spring-beans-3.0.5被A依赖的路径最近

3、使用版本锁定解决冲突
    面对众多的依赖,不用考虑依赖路径,声明优化等因素可以采用直接锁定版本的方法确定依赖构件的版本,版本锁定后则不考虑依赖的声明顺序或依赖的路径,以锁定的版本的为准添加到工程中。
    <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>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-web</artifactId>
          <!--不需要写版本信息,因为上面已经锁定版本-->
        </dependency>
    </dependencies>

在使用坐标时,对于同一个框架,引入多次时,它的版本信息就会多次出现,所以可以借用常量的思想,将这些版本号提取出来,在需要用到的时候,直接写版本的常量名称就可以了

三、依赖关系
传递依赖
当A依赖B,B依赖C,在A中导入B后会自动导入C,C是A的传递依赖,如果C依赖D则D也可能是A的传递依赖

依赖具有传递性,但不是无限传递的

解决方法:
    再导入一次坐标

因为junit的scope为test所以传递不下去
如果修改junit的scope为complie 则maven-first所以来的的junit的jar包会加入到maven-web工程中


四、编写service模块
1、创建一个maven module项目
    父工程中多了个ssh_service
    父工程的pom.xml多了   <module>ssh_service</module>
2、在service的pom.xml文件中引入dao 的jar包
    <dependency>
        <groupId>my.mime.ssh</groupId>
        <artifactId>ssh_dao</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>

web层的子模块 选  <packaging>war</packaging>

五、maven私服
下载nexus
安装
nexus是Maven仓库管理器,通过nexus可以搭建maven仓库,同时nexus还提供强大的仓库管理功能,构建搜索功能
下载地址:www.sonatype.org/nexus/archived
1、进入D:\nexus-2.12.0-01\bin 
    cmd输入 nexus.bat install
    在服务中启动nexus服务
    http://localhost:8081/nexus
    帐号默认admin 密码默认admin123

    查看nexus的配置文件conf/nexus.properties

    # Jetty section
    application-port=8081       #nexus的访问端口配置
    application-host=0.0.0.0  #nexus主机监听配置(不用修改)
    nexus-webapp=${bundleBasedir}/nexus #nexus工程目录
    nexus-webapp-context-path=/nexus #nexus的web访问路径

    # Nexus section
    nexus-work=${bundleBasedir}/../sonatype-work/nexus  #nexus仓库目录
    runtime=${bundleBasedir}/nexus/WEB-INF #nexus运行程序目录

拆分就是将原有的项目分成 ssh_dao,ssh_service,ssh_web,ssh_parent
拆分之后,以后还要聚合(聚合就是将拆分的工程进一步组合在一起,又形成一个完整的项目)

网站中仓库有不同的type
    1、hosted,宿主仓库,部署自己的jar到这个类型的仓库,包括release和snapshot两部分,Releases公司内部发布版本仓库,Snapshots公司内部测试版本仓库
    2、proxy 代理仓库,用于代理远程的公共仓库,如maven中央仓库,用户连接私服,私服自动去中央仓库下载jar包或者插件
    3、group,仓库组,用于合并多个hosted/proxy仓库,通常我们配置自己的maven连接仓库组
    4、virtual(虚拟),兼容Maven1版本的jar或者插件

nexus仓库默认在sonatype-work目录中
central :代理仓库,代理中央仓库
apache-snapshots:代理仓库
    存储snapshots构建,代理地址http://repository.apache.org/snapshots/
central-m1:virtual类型仓库,兼容Maven1版本的jar或者插件
release:本地仓库,存储release构件
snapshots:本地仓库,存储snapshots构件
thirdparty:第三方仓库
public:仓库组

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

此用户名和密码用于私服校验,因为私服需要知道上传的帐号和密码是否和私服中的帐号和一致
    <server>
        <!--访问releases这个私服上的仓库,所使用的帐号,密码-->
        <id>releases</id>
        <username>admin</username>
        <password>admin123</password>
    </server>
    <server>
        <id>snapshots</id>
        <username>admin</username>
        <password>admin123</password>
    </server>



测试
将项目dao工程打成jar包发布到私服
1、首先启动nexus
2、对dao工程执行deploy命令

根据本项目pom.xml中version决定发布到哪个仓库,如果version定义为snapshot,执行deploy后查看nexus的snapshot仓库,如果version定义为release则项目发布到nexus的release仓库,本项目发布到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>对应
  http://localhost:8081/nexus/content/repositories/snapshots/my/mime/ssh/ssh_dao/

从私服下载jar包
没有配置nexus之前,如果本地仓库没有,去中央仓库下载,通常在企业中会在局域网内部部署一台私服服务器,有了私服本地项目首先去本地仓库找jar,如果没有找到则连接私服从私服下载jar包,
如果私服没有jar包私服同时作为代理服务器从中央仓库下载jar包,这样做的好处是一方面由私服对公司项目的依赖jar包统一管理,
一方面提高下载速度,项目连接私服下载jar包的速度比项目连接中央仓库的速度快的多

nexus中包括很多仓库,hosted中存放的是企业自己分布的jar包以及第三方公司的jar包,proxy中存放的是中央仓库的jar,
为了方便从私服下载jar包可以将多个仓库组成一个仓库组,每个工程需要连接私服的仓库组下载jar包。

在客户端的setting.xml中配置私服的仓库,由于setting.xml中没有repositories的配置标签需要使用profile定义仓库
  <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>  

  使用profile定义仓库需要激活才可生效

  pom.xml中会在effective pom 即给计算机看的中自动生成
  <repositories>
    <repository>
      <releases>
        <enabled>true</enabled>
      </releases>
      <snapshots>
        <enabled>true</enabled>
      </snapshots>
      <id>nexus</id>
      <url>http://localhost:8081/nexus/content/groups/public/</url>
    </repository>
    <repository>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
      <id>central</id>
      <name>Central Repository</name>
      <url>https://repo.maven.apache.org/maven2</url>
    </repository>
  </repositories>

nexus proxy仓库 hosted仓库 group仓库_spring


settings.xml

<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
 <localRepository>D:\Repositories</localRepository>
  <pluginGroups>
  </pluginGroups>
  <proxies>
  </proxies>
  <servers>

    <server>
        <id>releases</id>
        <username>admin</username>
        <password>admin123</password>
    </server>
    <server>
        <id>snapshots</id>
        <username>admin</username>
        <password>admin123</password>
    </server>
  </servers>

  <mirrors>

      <mirror>
        <id>nexus-aliyun</id>
        <mirrorOf>central</mirrorOf>
        <name>Nexus aliyun</name>
        <url>http://maven.aliyun.com/nexus/content/repositories/central/</url>
       </mirror>
  </mirrors>

  <profiles>

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

  </profiles>
  <activeProfiles>
    <activeProfile>dev</activeProfile>
  </activeProfiles>
</settings>

applicationContext-service.xml

<?xml version="1.0" encoding="UTF-8"?>
<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="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    <!--  事务通知 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="save*" propagation="REQUIRED"/>
            <tx:method name="insert*" propagation="REQUIRED"/>
            <tx:method name="update*" propagation="REQUIRED"/>
            <tx:method name="delete*" propagation="REQUIRED"/>

            <tx:method name="get*" read-only="true"/>
            <tx:method name="*" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>
    <!-- aop -->
    <aop:config>
        <aop:pointcut id="pointcut" expression="execution(* my.*.*(..))" />
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut" />
    </aop:config>
    <!-- service -->
    <bean id="customerService" class="ssh.service.CustomerServiceImpl">
        <property name="customerDao" ref="customerDao"></property>
    </bean>
</beans>

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<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">
    <!-- service -->
    <import resource="classpath:applicationContext-web.xml"/>
    <!-- *表示jar包中的配置文件 -->
    <import resource="classpath*:applicationContext-service.xml"/>
    <import resource="classpath*:applicationContext-dao.xml"/>
</beans>

xxx.hbm.xml

<hibernate-mapping>

    <class name="my.ssh.domain.Customer" table="cst_customer">
        <id name="cust_id" column="cust_id">
            <!-- 6种主键生成策略:indentity[自增1],native,sequence,uuid,increment,assigned -->
            <generator class="native"/>
        </id>

        <property name="cust_name" column="cust_name"/>
        <property name="cust_user_id" column="cust_user_id"/>

    </class>

</hibernate-mapping>

hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

    <session-factory>
        <property name="dialect">
            org.hibernate.dialect.Oracle10gDialect
        </property>

        <property name="show_sql">true</property>
        <property name="format_sql">true</property>
        <property name="hbm2ddl.auto">none</property>
        <!-- 懒加载,配合web.xml中配置的 openSessionInViewFilter -->
        <property name="hibernate.enable_lazy_load_no_trans">true</property>
        <!--校验模式  JPA  java persistent api-->
        <property name="javax.persistence.validation.mode">none</property>

        <!--  加载映射文件-->
        <mapping resource="my/ssh/domain/Customer.hbm.xml"></mapping>

    </session-factory>
    </hibernate-configuration>

applicationContext-dao.xml

<?xml version="1.0" encoding="UTF-8"?>
<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">
    <!-- dataSource -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
        <property name="jdbcUrl" value="jdbc:mysql:///maven_day01"></property>
        <property name="user" value="root"></property>
        <property name="password" value="123"></property>
    </bean> 
    <!--sessionFactory  -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="configLocation" value="classpath:hibernate.cfg.xml" />
    </bean>  

    <!-- 以后:Dao都需要继承HibernateDaoSupport,注入sessionFactory -->
    <bean id="customerDao" class="my.ssh.dao.CustomerDaoImpl">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

    <!-- <bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
        <property name="sessionFactory"></property>
    </bean> -->

</beans>

dao-test

@SuppressWarnings(value="all")
public class CustomerTest {
    @Test
    public void test() {
        ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext-dao.xml");
        CustomerDao dao = (CustomerDao) ac.getBean("customerDao");
        List<Customer> list = dao.findAll();
        System.out.println(list.size());
    }
}