1.什么是maven
maven是基于项目对象模型(POM),是跨平台的管理工具,主要服务于java平台的项目构建,依赖管理和项目信息管理。
2.maven的好处
Maven中使用约定,约定java源代码代码必须放在哪个目录下,编译好的java代码又必须放到哪个目录下,这些目录都有明确的约定。
Maven的每一个动作都拥有一个生命周期,例如执行 mvn install 就可以自动执行编译,测试,打包等构建过程
只需要定义一个pom.xml,然后把源码放到默认的目录,Maven帮我们处理其他事情
使用Maven可以进行项目高度自动化构建,依赖管理(这是使用Maven最大的好处),仓库管理。
3.安装maven
下载地址:http://maven.apache.org/download.cgi
下载完成后,得到一个压缩包
,解压,可以看到maven的组成目录
Maven目录分析
- bin:含有mvn运行的脚本
- boot:含有plexus-classworlds类加载器框架
- conf:含有settings.xml配置文件
- lib:含有Maven运行时所需要的java类库
- LICENSE.txt, NOTICE.txt, README.txt针对Maven版本,第三方软件等简要介绍
注意:在安装maven之前,要先确定本机上是否安装jak (要jdk 1.6+的版本),使用如下的两个命令检查检查JDK安装的情况。
1 Echo %JAVA_HOME% 2 Java -version
*设置环境变量:MAVEN_HOME 本人是配置在本机用户下, 配置在系统下步骤一样。
*设置环境变量:Path,将%MAVEN_HOME%\bin加入Path中,一定要注意要用分号;与其他值隔开,如下图所示:
*测试 maven 搭建是否成功
打开cmd窗口,输入"mvn -v"命令 查看Maven的相关信息。如图
4.ok,进入主话题,搭建Maven 项目 ssm(springMVC+spring+mybatis)
在平时的Javaweb项目开发中为了便于后期的维护,我们一般会进行分层开发,最常见的就是分为domain(域模型层)、dao(数据库访问层)、service(业务逻辑 层)、web(表现层),这样分层之后,各个层之间的职责会比较明确,后期维护起来也相对比较容易,今天我们就是使用Maven来构建以上的各个层。
一:创建 demo_parent(父类,用于给各个子模块继承)
在pom.xml文件中 配置ssm需要的jar包。 maven jar包仓库地址 http://mvnrepository.com/
1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3 <modelVersion>4.0.0</modelVersion>
4 <groupId>com.baidu</groupId>
5 <artifactId>demo_parent</artifactId>
6 <version>0.0.1-SNAPSHOT</version>
7 <packaging>pom</packaging>
8
9 <!-- 统一jar包版本 -->
10 <properties>
11 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
12 <spring.version>3.2.4.RELEASE</spring.version>
13 </properties>
14
15
16
17 <dependencies>
18 <dependency>
19 <groupId>commons-fileupload</groupId>
20 <artifactId>commons-fileupload</artifactId>
21 <version>1.2.1</version>
22 </dependency>
23
24 <dependency>
25 <groupId>commons-io</groupId>
26 <artifactId>commons-io</artifactId>
27 <version>1.4</version>
28 </dependency>
29
30 <dependency>
31 <groupId>com.googlecode.json-simple</groupId>
32 <artifactId>json-simple</artifactId>
33 <version>1.1</version>
34 </dependency>
35
36
37 <dependency>
38 <groupId>junit</groupId>
39 <artifactId>junit</artifactId>
40 <version>4.10</version>
41 <scope>test</scope>
42 </dependency>
43
44 <dependency>
45 <groupId>com.oracle</groupId>
46 <artifactId>ojdbc14</artifactId>
47 <version>10.2.0.1.0</version>
48 </dependency>
49
50 <dependency>
51 <groupId>com.fasterxml.jackson.core</groupId>
52 <artifactId>jackson-core</artifactId>
53 <version>2.2.3</version>
54 </dependency>
55
56
57 <dependency>
58 <groupId>org.springframework</groupId>
59 <artifactId>spring-core</artifactId>
60 <version>${spring.version}</version>
61 </dependency>
62 <dependency>
63 <groupId>org.springframework</groupId>
64 <artifactId>spring-beans</artifactId>
65 <version>${spring.version}</version>
66 </dependency>
67 <dependency>
68 <groupId>org.springframework</groupId>
69 <artifactId>spring-aop</artifactId>
70 <version>${spring.version}</version>
71 </dependency>
72 <dependency>
73 <groupId>org.springframework</groupId>
74 <artifactId>spring-context</artifactId>
75 <version>${spring.version}</version>
76 </dependency>
77 <dependency>
78 <groupId>org.springframework</groupId>
79 <artifactId>spring-web</artifactId>
80 <version>${spring.version}</version>
81 </dependency>
82 <dependency>
83 <groupId>org.springframework</groupId>
84 <artifactId>spring-tx</artifactId>
85 <version>${spring.version}</version>
86 </dependency>
87 <dependency>
88 <groupId>org.springframework</groupId>
89 <artifactId>spring-jdbc</artifactId>
90 <version>${spring.version}</version>
91 </dependency>
92 <dependency>
93 <groupId>commons-dbcp</groupId>
94 <artifactId>commons-dbcp</artifactId>
95 <version>1.4</version>
96 </dependency>
97 <dependency>
98 <groupId>org.mybatis</groupId>
99 <artifactId>mybatis</artifactId>
100 <version>3.2.7</version>
101 </dependency>
102 <dependency>
103 <groupId>org.mybatis</groupId>
104 <artifactId>mybatis-spring</artifactId>
105 <version>1.2.2</version>
106 </dependency>
107
108
109 <dependency>
110 <groupId>org.aspectj</groupId>
111 <artifactId>aspectjweaver</artifactId>
112 <version>1.8.5</version>
113 </dependency>
114
115
116 <dependency>
117 <groupId>log4j</groupId>
118 <artifactId>log4j</artifactId>
119 <version>1.2.12</version>
120 </dependency>
121
122 <dependency>
123 <groupId>com.fasterxml.jackson.core</groupId>
124 <artifactId>jackson-annotations</artifactId>
125 <version>2.2.3</version>
126 </dependency>
127
128 <dependency>
129 <groupId>com.fasterxml.jackson.core</groupId>
130 <artifactId>jackson-databind</artifactId>
131 <version>2.2.3</version>
132 </dependency>
133
134 <dependency>
135 <groupId>org.springframework</groupId>
136 <artifactId>spring-webmvc</artifactId>
137 <version>${spring.version}</version>
138 </dependency>
139
140 <dependency>
141 <groupId>org.springframework</groupId>
142 <artifactId>spring-webmvc-portlet</artifactId>
143 <version>${spring.version}</version>
144 </dependency>
145
146 <dependency>
147 <groupId>org.springframework</groupId>
148 <artifactId>spring-test</artifactId>
149 <version>${spring.version}</version>
150 </dependency>
151
152 <dependency>
153 <groupId>org.springframework</groupId>
154 <artifactId>spring-oxm</artifactId>
155 <version>${spring.version}</version>
156 </dependency>
157
158 <dependency>
159 <groupId>org.springframework</groupId>
160 <artifactId>spring-jms</artifactId>
161 <version>${spring.version}</version>
162 </dependency>
163
164 <dependency>
165 <groupId>org.springframework</groupId>
166 <artifactId>spring-expression</artifactId>
167 <version>${spring.version}</version>
168 </dependency>
169
170 <dependency>
171 <groupId>org.springframework</groupId>
172 <artifactId>spring-context-support</artifactId>
173 <version>${spring.version}</version>
174 </dependency>
175
176 <dependency>
177 <groupId>org.springframework</groupId>
178 <artifactId>spring-instrument</artifactId>
179 <version>${spring.version}</version>
180 </dependency>
181
182 <dependency>
183 <groupId>org.springframework</groupId>
184 <artifactId>spring-instrument-tomcat</artifactId>
185 <version>${spring.version}</version>
186 </dependency>
187
188 <dependency>
189 <groupId>org.springframework</groupId>
190 <artifactId>spring-aspects</artifactId>
191 <version>${spring.version}</version>
192 </dependency>
193
194 <dependency>
195 <groupId>org.springframework</groupId>
196 <artifactId>spring-aop</artifactId>
197 <version>${spring.version}</version>
198 </dependency>
199
200 <dependency>
201 <groupId>javax.servlet</groupId>
202 <artifactId>javax.servlet-api</artifactId>
203 <version>3.0.1</version>
204 <scope>provided</scope>
205 </dependency>
206 <dependency>
207 <groupId>javax.servlet</groupId>
208 <artifactId>jsp-api</artifactId>
209 <version>2.0</version>
210 <scope>provided</scope>
211 </dependency>
212 <dependency>
213
214
215 <groupId>javax.servlet</groupId>
216 <artifactId>jstl</artifactId>
217 <version>1.2</version>
218 </dependency>
219 <dependency>
220 <groupId>commons-pool</groupId>
221 <artifactId>commons-pool</artifactId>
222 <version>1.5.3</version>
223 </dependency>
224
225 </dependencies>
226 </project>
二: 创建 demo_domain (模型层)
步骤如上,右键-》new-》maven project->(直接)next->next->finish
生产项目如下:
在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>
<artifactId>demo_domain</artifactId>
<packaging>jar</packaging>
<name>demo_domain</name>
<url>http://maven.apache.org</url>
<!-- 依赖父类 demo_parent -->
<parent>
<groupId>com.baidu</groupId>
<artifactId>demo_parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../garden_partent/pom.xml</relativePath>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
三:创建demo_service 和 demo_dao 步骤和 demo_domain一样
四:创建demo_web (filter:mave-archetype-webapp)
五:把项目进行整合。加入 ssm整合的 配置文件 在各个项目中配置相应的xml
1.在 demo_web中 配置springMVC.xml文件 扫描controller层
1 <?xml version="1.0" encoding="UTF-8" ?>
2 <beans
3 xmlns="http://www.springframework.org/schema/beans"
4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5 xmlns:p="http://www.springframework.org/schema/p"
6 xmlns:context="http://www.springframework.org/schema/context"
7 xmlns:mvc="http://www.springframework.org/schema/mvc"
8 xsi:schemaLocation="http://www.springframework.org/schema/beans
9 http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
10 http://www.springframework.org/schema/context
11 http://www.springframework.org/schema/context/spring-context-3.2.xsd
12 http://www.springframework.org/schema/mvc
13 http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
14
15
16 <context:component-scan base-package="com.baidu.*.controller"/>
17 <mvc:annotation-driven/>
18 <mvc:default-servlet-handler/>
19
20
21 <!-- ========================= 视图 解析器 ========================= -->
22 <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
23 <property name="prefix" value="/WEB-INF/"/>
24 <property name="suffix" value=".jsp"/>
25 </bean>
26
27
28 </beans>
web.xml的配置(springMVC的核心控制器... ...)
1 <?xml version="1.0" encoding="UTF-8"?>
2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" 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">
3 <display-name>ssm</display-name>
4
5
6
7 <!-- spring MVC核心控制器
8
9 springmvc配置文件 默认
10 位置:WEB-INF
11 名称: [servlet-name]-servlet.xml
12 -->
13 <servlet>
14 <servlet-name>springmvc</servlet-name>
15 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
16
17 <init-param>
18 <param-name>contextConfigLocation</param-name>
19 <param-value>classpath*:conf/**/springmvc.xml</param-value>
20 </init-param>
21
22
23 <!-- 配置加载顺序 -->
24 <load-on-startup>1</load-on-startup>
25
26
27 </servlet>
28
29 <servlet-mapping>
30 <servlet-name>springmvc</servlet-name>
31 <url-pattern>/</url-pattern>
32 </servlet-mapping>
33
34
35
36
37 <!-- 编码过滤器 -->
38 <filter>
39 <filter-name>encoding</filter-name>
40 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
41 <init-param>
42 <param-name>encoding</param-name>
43 <param-value>UTF-8</param-value>
44 </init-param>
45 </filter>
46
47 <filter-mapping>
48 <filter-name>encoding</filter-name>
49 <url-pattern>/*</url-pattern>
50 </filter-mapping>
51
52
53
54 <!-- spring 加载的监听器
55 加载spring配置文件 默认 WEB-INF 名称 applicationContext.xml
56 -->
57
58 <context-param>
59 <param-name>contextConfigLocation</param-name>
60 <param-value>classpath*:conf/**/spring*.xml</param-value>
61 </context-param>
62
63 <listener>
64 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
65 </listener>
66
67
68 <!-- 定义LOG4J监听器 -->
69 <listener>
70 <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
71 </listener>
72
73
74 <welcome-file-list>
75 <welcome-file>index.html</welcome-file>
76 <welcome-file>index.htm</welcome-file>
77 <welcome-file>index.jsp</welcome-file>
78 <welcome-file>default.html</welcome-file>
79 <welcome-file>default.htm</welcome-file>
80 <welcome-file>default.jsp</welcome-file>
81 </welcome-file-list>
82 </web-app>
2.在 demo_service 下的 src/main/resources 中放入spring.xml
1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xmlns:aop="http://www.springframework.org/schema/aop"
5 xmlns:context="http://www.springframework.org/schema/context"
6 xmlns:mvc="http://www.springframework.org/schema/mvc"
7 xmlns:tx="http://www.springframework.org/schema/tx"
8 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
9 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
10 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
11 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
12 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
13
14
15 <context:component-scan base-package="com.baidu.*.service"/>
16
17
18 <!-- 配置事务管理器 -->
19 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
20 <property name="dataSource" ref="dataSource" />
21 </bean>
22
23
24 <!-- 事务处理 -->
25
26 <aop:config>
27 <aop:pointcut expression="execution(* com.baidu.*.service..*.*(..))" id="trPointcut"/>
28 <aop:advisor advice-ref="trAdvice" pointcut-ref="trPointcut"/>
29 </aop:config>
30
31 <tx:advice id="trAdvice" transaction-manager="transactionManager">
32 <tx:attributes>
33 <tx:method name="doAddED*" propagation="REQUIRED"/>
34 <tx:method name="add*" propagation="REQUIRED"/>
35 <tx:method name="insert*" propagation="REQUIRED"/>
36 <tx:method name="update*" propagation="REQUIRED"/>
37 <tx:method name="del*" propagation="REQUIRED"/>
38 <tx:method name="get*" propagation="REQUIRED"/>
39 <tx:method name="*" propagation="REQUIRED" read-only="true"/>
40 </tx:attributes>
41 </tx:advice>
42
43
44
45
46 </beans>
3.demo_dao 下的 src/main/resources 中放入spring.xml
1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xmlns:aop="http://www.springframework.org/schema/aop"
5 xmlns:context="http://www.springframework.org/schema/context"
6 xmlns:mvc="http://www.springframework.org/schema/mvc"
7 xmlns:tx="http://www.springframework.org/schema/tx"
8 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
9 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
10 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
11 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
12 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
13
14
15 <context:property-placeholder location="classpath*:conf/**/jdbc.properties"/>
16 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
17 <property name="driverClassName" value="${jdbc.driverClassName}"/>
18 <property name="url" value="${jdbc.url}"/>
19 <property name="username" value="${jdbc.username}"/>
20 <property name="password" value="${jdbc.password}"/>
21 <!-- 连接池启动时的初始值 -->
22 <property name="initialSize" value="${initialSize}"/>
23 <!-- 连接池的最大值 -->
24 <property name="maxActive" value="${maxActive}"/>
25 <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
26 <property name="maxIdle" value="${maxIdle}"/>
27 <!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
28 <property name="minIdle" value="${minIdle}"/>
29 <!-- 是否启用超时自动移除空闲连接 -->
30 <property name ="removeAbandoned" value="false"></property>
31 <!-- 超时时间(以秒数为单位) -->
32 <property name="removeAbandonedTimeout" value="${removeAbandonedTimeout}"/>
33 <!-- 超时等待时间以毫秒为单位 6000毫秒/1000等于60秒 -->
34 <property name="maxWait" value="${maxWait}"/>
35
36 </bean>
37
38
39
40 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
41 <property name="dataSource" ref="dataSource"></property>
42 <!-- <property name="configLocation">
43 <value>classpath:mybatis-config.xml</value>
44 </property> -->
45 <!-- 配置实体别名 -->
46 <property name="typeAliasesPackage">
47 <value>com.baidu.entity</value>
48 </property>
49 <!-- 配置映射文件加载 -->
50 <property name="mapperLocations">
51 <value>classpath*:com/baidu/entity/*.xml</value>
52 </property>
53 </bean>
54
55
56
57 <!-- 在spring里使用org.mybatis.spring.mapper.MapperScannerConfigurer 进行自动扫描的时候,
58 设置了sqlSessionFactory 的话,可能会导致PropertyPlaceholderConfigurer失效,也就是用${jdbc.username}
59 这样之类的表达式,将无法获取到properties文件里的内容。 导致这一原因是因为,MapperScannerConigurer
60 实际是在解析加载bean定义阶段的,这个时候要是设置sqlSessionFactory的话,会导致提前初始化一些类,这个时候,
61 PropertyPlaceholderConfigurer还没来得及替换定义中的变量,导致把表达式当作字符串复制了。 但如果不设置
62 sqlSessionFactory 属性的话,就必须要保证sessionFactory在spring中名称一定要是sqlSessionFactory ,
63 否则就无法自动注入。
64 解决方案:
65 改用sqlSessionFactoryBeanName注入就没有问题(不要使用sqlSessionFactory属性注入,
66 使用sqlSessionFactoryBeanName注入),因为这时不会立即初始化sqlSessionFactory,传入的只是名字,
67 非bean,所以不会引发提前初始化问题。
68 -->
69
70 <!-- 配置bean 自动扫描所有mapper 自动给Mapper接口产生代理类对象 并且给代理对象注入SqlSessionFactory-->
71 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
72 <property name="basePackage">
73 <value>com.baidu.dao</value>
74 </property>
75
76 <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
77 <!-- <property name="sqlSessionFactory" ref="sqlSessionFactory"></property> -->
78 </bean>
79
80 </beans>
4.在pom.xml配置文件中 配置依赖管理
1.demo_web 中的 pom.xml 加入
1 <dependencies>
2 <dependency>
3 <groupId>com.baidu</groupId>
4 <artifactId>demo_service</artifactId>
5 <version>${project.version}</version>
6 </dependency>
7 </dependencies>
2.demo_service 中的pom.xml 加入
1 <dependencies>
2 <dependency>
3 <groupId>com.baidu</groupId>
4 <artifactId>demo_dao</artifactId>
5 <version>${project.version}</version>
6 </dependency>
7 </dependencies>
3.demo_dao 中的pom.xml 加入
1 <dependencies>
2 <dependency>
3 <groupId>com.baidu</groupId>
4 <artifactId>demo_domain</artifactId>
5 <version>${project.version}</version>
6 </dependency>
7 </dependencies>
4.demo_parent 中的pom.xml 加入
1 <!-- 管理子项目的xml文件和资源文件 -->
2 <build>
3 <resources>
4 <resource>
5 <directory>src/main/java</directory>
6 <includes>
7 <include>**/*.xml</include>
8 </includes>
9 </resource>
10 <resource>
11 <directory>src/main/resources</directory>
12 <includes>
13 <include>**/*.xml</include>
14 <include>**/*.properties</include>
15 </includes>
16 </resource>
17 </resources>
18 </build>
1 <!-- 父类里包含的模块 -->
2 <modules>
3 <module>../demo_dao</module>
4 <module>../demo_service</module>
5 <module>../demo_web</module>
6 <module>../demo_domain</module>
7 </modules>
1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3 <modelVersion>4.0.0</modelVersion>
4
5 <artifactId>demo_domain</artifactId>
6 <packaging>jar</packaging>
7
8 <name>demo_domain</name>
9 <url>http://maven.apache.org</url>
10
11 <!-- 依赖父类 demo_parent -->
12 <parent>
13 <groupId>com.baidu</groupId>
14 <artifactId>demo_parent</artifactId>
15 <version>0.0.1-SNAPSHOT</version>
16 <relativePath>../garden_partent/pom.xml</relativePath>
17 </parent>
18
19 <properties>
20 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
21 </properties>
22
23
24 </
六。 右键 -> run as ->Maven install 可以自动执行编译,测试,打包等构建过程 进行测试
项目 搭建完成。