1 Maven 简介
Maven 使用项目对象模型(POM,Project Object Model) 的概念,可以通过一小段描述信息来管理项目的构建、报告和文档的 软件项目管理工具
2 Maven 的下载与IDE 的整合
1 下载地址
http://maven.apche.org
2 Eclipse 整合Maven
Step1:打开 Eclipse 中window -> preference -> maven -> InstallLocations
更换为自己下载的 maven ,不使用 eclipse 自带的 maven
Step2:打开 maven -> UserSetting
更换为自己的 settings.xml 配置文件,并修改 settings.xml 文件
3 Maven 仓库与配置
3.1远程仓库
中央仓库 https://mvnrepository.com/
3.2 本地仓库
存储本机的拷贝,以及远程仓库中的下载的构件(Java API 资源)
4 仓库配置
5.1 settings.xml
设置本地仓库的地址 -->
<localRepository>E://Maven//repository</localRepository>
5.2 配置镜像源
<mirrors>
<mirror>
<!-- 镜像的ID -->
<id>alimaven</id>
<name>aliyun maven</name>
<!-- 指定镜像路径 -->
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<!-- 匹配中央仓库 -->
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>
6 仓库优先级
本地仓库 > 指定仓库 > 远程仓库(镜像仓库 > 中央仓库)
6.1 本地仓库
6.2 指定仓库
6.3 远程仓库
镜像仓库
中央仓库
3 Maven 工程
3.1 Pom 工程
pom 工程是逻辑工程。
用在父级工程或者聚合工程。
用来做 jar 包的版本控制
3.2 jar 工程
将会打包成 jar 用作 jar 包使用
3.3 war 工程
将会打包成 war ,发布在服务器上的工程。
4 创建Maven 项目
使用 Eclipse 创建 Maven 项目,勾选简单工程,即创建纯净的项目。
4.1 Maven 创建
Group Id Artifact Id Version 这三个组合起来唯一定位项目的位置
Group Id : 域名倒着写
Artifact Id:项目名称
Version : 版本
Packing :项目类型
Name:任意
Description:描述任意
4.2 Maven 目录
| | | | :---------------- | ------------------------------------------------------------ | | src/main/java | 存储 java 源代码 | | src/man/resources | 存储资源配置文件 | | src/main/test | 测试 java 源代码和资源文件 | | src/ | 存储源代码和资源文件 | | target | 编译后内容放置的文件夹 | | pom.xml | Maven 的基础配置文件。配置项目与项目之间关系,包括配置依赖关系等等 |
5 工程关系
5.1 依赖
如果 A工程 依赖于 B 工程的资源,则称 A 工程依赖于 B 工程
做法:将 B 工程的坐标加入 A 工程的 pom.xml 的依赖中。
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.2</version>
<!--
complie 编译中有效
runtime 运行中有效
system 全部中有效[默认]
provided 当前工程中有效
test 只在测试有效
-->
<scope>test</scope>
</dependency>
5.2 继承
如果 A 工程继承 B 工程,则A 工程可以使用 B 工程所有资源。
前提 B 工程必须是 pom 工程类型.
父工程的配置文件
<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>cn.edu.ahszu</groupId>
<artifactId>ParentProject</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>Demo</name>
<properties>
<!-- 配置当前项目依赖构建的版本
项目名最好见名知意
-->
<junit-version>4.2</junit-version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit-version}</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
子工程的配置文件
<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>
<!-- 继承父项目 -->
<parent>
<groupId>cn.edu.ahszu</groupId>
<artifactId>ParentProject</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>cn.edu.ahszu</groupId>
<artifactId>ChildProject</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
</dependencies>
</project>
5.3 聚合
当我们开发的工程拥有 2 个以上模块的时候,每个模块都是一个独立的功能集合。
注意:在创建聚合工程的过程中,总的工程必须是 pom 类型的工程,子模块的工程类型任意。
<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>cn.edu.ahszu</groupId>
<artifactId>04-Maven-Managerment</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<!-- 子模块 -->
<modules>
<module>04-Maben-sub</module>
<module>04-Maven-sub2</module>
</modules>
</project>
6 创建 war 类型的项目
创建war 类型的项目后,
war 默认没有 WEB-INF 目录及下面的 web.xml 文件,所以需要手动添加。
在 webapp 目录下创建 WEB-INF 目录该目录下的 web.xm 文件
7 Maven 常用插件
7.1 编译器插件
前提是本机必须有 JDK ,并且JDK 的版本要对应
在 pom.xml 配置编译器插件,用于选择 JDK 的版本,并且对本项目生效。
若需全局配置,需要在 settings.xml 中配置编译器插件。
pom.xml 局部配置
<build>
<plugins>
<!-- java 编译插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
settings.xml 全局配置
<profile>
<!-- 定义的编译器插件 ID,全局唯一 -->
<id>jdk-1.7</id>
<!-- 插件标记,activeByDefault 默认编译器,jdk 提供编译器版本 -->
<activation>
<activeByDefault>true</activeByDefault>
<jdk>1.7</jdk>
</activation>
<!-- 配置信息 source-源信息,target-字节码信息,compilerVersion-编译过程版-->
<properties>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<maven.compiler.compilerVersion>1.7</maven.compiler.compilerVersion>
</properties>
</profile>
7.2 Tomcat 插件
启动 tomcat 命令:tocmat7 :run
命令中的 tomcat7 是插件命名,由插件提供商决定。run 为插件中的具体功能.
本地应用
<build>
<plugins>
<!-- 配置 Tomcat 插件 -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<port>8080</port>
<!--
/ 表示访问 tocmat 中 webapp 中 ROOT 目录
访问 http://localhost:8080/
-->
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>
远程热部署
在 tomcat 容器运行过程中,动态实现 war 工程的部署,重新部署的功能。
使用 maven build ,使用 run:deploy 或者 run:redeploy
配置 tocmat 服务器用户
<role rolename="manager-gui"/>
<role rolename="manager-script"/>
<user username="tomcatUsername" password="tomcatPassword"
roles="manager-gui,manager-script"/>
修改 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.0</modelVersion>
<groupId>cn.szxy</groupId>
<artifactId>07-Maven-tomcat</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<build>
<plugins>
<!-- 配置 Tomcat 插件 -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<port>8080</port>
<!-- path: 上传的 war 包解压后的路径命名 -->
<path>/ROOT</path>
<!-- url : 上传 war 包到什么位置,除 IP 和端口可以修改外其他不变 -->
<url>http://192.168.170.8:8080//manager/text</url>
<!-- 为 tomcat 配置的管理用户名和密码. -->
<username>tomcatUsername</username>
<password>tomcatPassword</password>
</configuration>
</plugin>
</plugins>
</build>
</project>
7.3 资源拷贝插件
使用 maven 命令生成 war 类型的工程,默认打包 src/main/resources 下的配置文件,并不会打包src 目录下的配置文件,所以这时需要配置 pom.xml ,让maven 也打包src目录下的配置文件。
注意:当在 pom.xml 配置 src 目录配置文件时,还需再添加原来默认打包路径下 src/mian/resources 下的配置文件。
<!-- 配置资源插件 -->
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/**.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/**.xml</include>
<include>**/**.properties</include>
</includes>
</resource>
</resources>
8 Maven 常用命令
install
本地安装,包含编译,打包,安装到本地仓库
编译 javac
打包 jar
安装到本地仓库,将打包 jar 文件,保存到本地仓库中。
clean
清除已编译信息,删除工程中的 target 目录。
compile
只编译,javac
deploy
部署。
package
打包。包括编译,打包的两个功能
9 Maven 私服
私服是一种特殊的远程仓库,它是架设在局域网的仓库服务,
私服代理广域网上的远程仓库,供局域网使用。
安装私服 Nexus
默认端口为8081
修改防火墙,开放端口
service stop iptables
开启 nexus 服务器
./nexus start
访问网站
http://IP:8081/nexus/
配置私服
登录
Nexus 默认用户名是 admin、密码是admin123
仓库类型
hosted
存放自己项目的构件
proxy
代理仓库:代理公共的远程仓库
3rd party
存放第三方构建
设置 proxy 代理仓库准许远程下载
setting.xml
注意:配置了 nexus 服务器,就不需要配置镜像源
当 setting.xml 配置文件出错,则 eclipse maven插件不能读取 setting.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>
<!--Maven是否需要和用户交互以获得输入。如果Maven需要和用户交互以获得输入,则设置成true,反之则应为false。默认为true。-->
<interactiveMode>true</interactiveMode>
<!--表示Maven是否需要在离线模式下运行。如果构建系统需要在离线模式下运行,则为true,默认
为false。 -->
<offline>false</offline>
<!--
当插件的组织Id(groupId)没有显式提供时,供搜寻插件组织Id(groupId)的列表。该元素包含一个 pluginGroup元素列表,每个子元素包含了一个组织Id(groupId)。当我们使用某个插件,
并且没有在命令行为其提供组织Id(groupId)的时候,Maven就会使用该列表。
-->
<pluginGroups>
<pluginGroup>org.mortbay.jetty</pluginGroup>
<pluginGroup>org.jenkins-ci.tools</pluginGroup>
</pluginGroups>
<proxies>
</proxies>
<!--配置服务端的一些设置。一些设置如安全证书不应该和pom.xml一起分发。这种类型的信息应该存在于构建服务器上的settings.xml文件中。-->
<servers>
<server>
<!-- server的id必须和pom.xml文件中的仓库id一致 -->
<!--这是server的id(注意不是用户登陆的id),该id与distributionManagement中 repository元素的id相匹配。-->
<id>nexus-releases</id>
<username>deployment</username>
<password>deployment123</password>
</server>
<server>
<id>nexus-snapshots</id>
<username>deployment</username>
<password>deployment123</password>
</server>
</servers>
<!--根据环境参数来调整构建配置的列表。-->
<profiles>
<profile>
<id>jdk-1.7</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>1.7</jdk>
</activation>
<properties>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<maven.compiler.compilerVersion>1.7</maven.compiler.compilerVersion>
</properties>
</profile>
<profile>
<id>sxt</id>
<activation>
<activeByDefault>false</activeByDefault> <jdk>1.7</jdk>
</activation>
<repositories>
<!-- 私有库配置 -->
<repository>
<!-- 私有库 id -->
<id>nexus</id>
<!-- 私有库地址 -->
<url>http://192.168.120.158:8081/nexus/content/groups/public/</url>
<!-- 私有库是否支持 releases 版本 -->
<releases>
<enabled>true</enabled>
</releases>
<!-- 私有库是否支持 snapshots 版本 -->
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<!-- 插件库配置,具体含义私有库配置 -->
<pluginRepository>
<id>nexus</id>
<url>http://192.168.120.158:8081/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
<!-- 激活 profile -->
<activeProfiles>
<!-- 根据 profile 的 id 标签值激活指定的内容 -->
<activeProfile>sxt</activeProfile>
</activeProfiles>
</settings>
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.0</modelVersion>
<groupId>group</groupId>
<artifactId>project</artifactId>
<version>1.0</version>
<distributionManagement>
<repository>
<id>nexus-releases</id>
<name>Nexus Release Repository</name>
<url>http://192.168.120.158:8081/nexus/content/repositories/releases/</url>
</repository>
<snapshotRepository>
<id>nexus-snapshots</id>
<name>Nexus Snapshot Repository</name>
<url>http://192.168.120.158:8081/nexus/content/repositories/snapshots/</url>
</snapshotRepository>
</distributionManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.1.2</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
在 Maven 工程的 maven build 中,输入命令 deploy,即可实现发布工程信息到私服。
如果同版本工程可能多次发布,需要修改 Nexus 配置。
10 Maven 实战
1 需求分析
基于 SSM 框架 的CRUD 案例
2 项目选型
3 项目架构设计
传统项目设计方式 --单体架构
Project
----------com.szxy
-----------------------Controller
------------------------Serviece
------------------------Pojo
------------------------Mapper
Maven项目设计方式
2 创建工程
2.1 建立 pom 父工程
<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>cn.szy</groupId>
<artifactId>MavenParent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<junit.version>4.12</junit.version>
<spring.version>4.1.3.RELEASE</spring.version>
<mybatis.version>3.2.8</mybatis.version>
<mybatis.spring.version>
1.2.2
</mybatis.spring.version>
<mysql.version>5.1.32</mysql.version>
<slf4j.version>1.6.4</slf4j.version>
<druid.version>1.0.9</druid.version>
<jstl.version>1.2</jstl.version>
<servlet-api.version>2.5</servlet-api.version>
<jsp-api.version>2.0</jsp-api.version>
<tomcat.version>2.2</tomcat.version>
</properties>
<!-- jar包的依赖注入 ,由于该工程是一个父工程,所以jar包在该pom文件中只是声明 -->
<dependencyManagement>
<dependencies>
<!-- 单元测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<!-- 日志处理 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>
<!-- Mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>${mybatis.spring.version}</version>
</dependency>
<!-- MySql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<!-- 连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- JSP相关 -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>${jstl.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>${servlet-api.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>${jsp-api.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!-- 插件的开启 -->
<build>
<!-- tomcat插件,由于子项目不一定每个都是web 项目,所以该插件只是声明,并未开启 -->
<pluginManagement>
<plugins>
<!-- 配置Tomcat插件 -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>${tomcat.version}</version>
</plugin>
</plugins>
</pluginManagement>
<!-- maven的编译器插件,该插件主要是决定了当前项目所使用jre版本 。由于无论是jar,还是war项目都需要制定jar的版本,所以该插件不需要生命,应当是开启的。 -->
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
2.2 创建聚合 pom 工程
pom.xml 配置
当前是子模块全部创建后的 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.0</modelVersion>
<parent>
<groupId>cn.szxy</groupId>
<artifactId>11-manager-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>cn.szxy</groupId>
<artifactId>11-manager</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>11-manager-pojo</module>
<module>11-manager-mapper</module>
<module>11-manger-service</module>
<module>11-manager-controller</module>
</modules>
</project>
2.3 创建子模块 pojo 工程
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.0</modelVersion>
<parent>
<groupId>cn.szxy</groupId>
<artifactId>11-manager</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>11-manager-pojo</artifactId>
</project>
2.4 创建子模块 mapper 工程
pom.xml 配置文件
添加 pojo 工程依赖
配置资源插件,复制映射文件到资源目录下
添加关于 mybatis 的 jar 包
<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>
<parent>
<groupId>cn.szxy</groupId>
<artifactId>11-manager</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>11-manager-mapper</artifactId>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>
<dependencies>
<dependency>
<groupId>cn.szxy</groupId>
<artifactId>11-manager-pojo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- mybatis 依赖 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
</dependency>
<!-- MySql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- 连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
</dependency>
</dependencies>
</project>
2.5 创建子模块 service工程
pom.xml 配置文件
添加 maaper 工程依赖
添加 spring 相关 jar 包
<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>
<parent>
<groupId>cn.szxy</groupId>
<artifactId>11-manager</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>11-manager-service</artifactId>
<dependencies>
<dependency>
<groupId>cn.szxy</groupId>
<artifactId>11-manager-mapper</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</dependency>
</dependencies>
</project>
2.6 创建子模块 controller 工程
spring 配置
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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 配置解析properties文件的工具类 -->
<context:property-placeholder location="classpath:resource/*.properties"/>
<!-- 配置数据源 dataSource 阿里数据源-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="driverClassName" value="${jdbc.driver}" />
<property name="maxActive" value="10" />
<property name="minIdle" value="5" />
</bean>
<!-- 创建mybatis的上下文对象 -->
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
<property name="configLocation">
<value>classpath:mybatis/SqlMapperClient.xml</value>
</property>
</bean>
<!-- 扫描mybatis的接口与映射配置文件 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.szxy.mapper"/>
</bean>
</beans>
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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 扫描bean对象 -->
<context:component-scan base-package="cn.szxy.service"/>
</beans>
applicationContext-tx.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/springutil-4.0.xsd">
<!-- 配置事物管理器的切面 -->
<bean id="transactionMananger" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 配置事物传播行为 :其实就是那些方法应该受什么样的事物控制-->
<tx:advice id="advice" transaction-manager="transactionMananger">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="modify*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="dorp*" propagation="REQUIRED"/>
<tx:method name="del*" propagation="REQUIRED"/>
<tx:method name="find*" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- 那些类下的方法需要参与到当前的事物管理中 。配置切点 -->
<aop:config>
<aop:advisor advice-ref="advice"
pointcut="execution(* com.bjsxt.service.*.*(..))"/>
</aop:config>
</beans>
springmvc.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 包的扫描器主要是扫描@controller -->
<context:component-scan base-package="com.szxy.web.controller" />
<!-- 注册两个新对象 主要是为了来处理 springmvc中的其他anntation 如:@requestmapping -->
<mvc:annotation-driven />
<!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" /><!-- jsp所在的前缀 -->
<property name="suffix" value=".jsp" />
</bean>
<!-- 配置静态资源映射 -->
<mvc:resources location="/WEB-INF/css/" mapping="/css/**" />
<mvc:resources location="/WEB-INF/js/" mapping="/js/**" />
<!-- 文件上传处理器 -->
<!-- <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8"></property>
<property name="maxInMemorySize" value="1024"></property>
单位字节
org.springframework.web.multipart.MaxUploadSizeExc eededException
<property name="maxUploadSize" value="1000000"></property>
</bean> -->
</beans>
SqlMapperClient.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 设置分页 -->
</configuration>
db.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///test
jdbc.username=root
jdbc.password=root
2.7 开启服务器
clean tocmat7:run