从大二开始就一直关注segmentFault,在问题专区帮忙回答一些本身知晓的问题;在写这篇文章以前我一直会在朋友圈发一些本身遇到的问题以及解决办法,这是第一次写文章,感受有那么一点生疏。
这篇文章主要讲Maven搭建SSM(Spring、SpringMVC、Mybatis)项目,以前在学习搭建项目的时候看过不少相关的文章,虽然网上有许多相似的,可是我仍是想写一写,分享一下本身在搭建项目中的体会。java
Apache Maven is a software project management and comprehension tool. Based on the concept of a project object model (POM), Maven can manage a project's build, reporting and documentation from a central piece of information.mysql
这个是官方给出来的解释,我我的理解为:Maven就是一个不须要手动导入依赖、项目打包和项目构建的工具,这个只是我的的观点,如有不对还请指教。git
了解更多关于Maven知识,能够到官网了解Maven工具的功能和使用方法,在这里主要聊一聊怎么运用Maven搭建SSM项目。github
Maven中重要的两个关键词:继承和聚合。
继承:做用就是避免配置重复,对于子项目来讲应该关心父项目是怎么样配置的。
聚合:字面理解就是“聚在一块儿合做完成工做”,就是将子模块汇集起来完成相应的项目需求web
项目结构:spring
在父工程中,主要负责完成依赖的版本管理,并非实际的依赖。sql
Group Id:项目组织的惟一标识 com为域,后面一个为公司名称
Artifact Id:项目惟一标识,通常为项目名称
pom:为父工程打包类为pom,提供依赖版本管理express
其中mybatis
<dependencyManagement></dependencyManagement>
是依赖管理,并非实际依赖。mvc
Maven插件也是一样的操做,须要注意的是:配置Maven插件maven-compiler-plugin<configuration>表明编译使用jdk1.8来完成,这两步操做完成父工程配置就已经完成。
名称:聚合工程web-manager
此时聚合工程,应该和父工程创建依赖关系,才可使用父工程里面管理的相应版本的依赖。
依赖关系添加之后,pom.xml文件中就会多出<parent>节点
完成了聚合工程的搭建,后面就要开始搭建子模块了,作好准备了吗?
名称:manager-mapper
该模块主要用来管理dao层,其中有pojo以及mapper接口和文件
子模块建立须要注意,建立时须要在聚合工程上点击右键->新建->Maven module
Next:
Next:
该步骤须要注意,该模块打包方式应该为jar,将该模块打成jar包就能够供其余模块使用
聚合项目工程中就会多出一个目录结构:
![图片上传中...]
名称:manager-service
该模块主要用来管理service层,其中有事务管理等
Todo:新建操做同上,都是须要打成jar包
注意:在Service层中,因为Service层须要依赖与dao层,因此须要在pom.xml中配置依赖关系
名称:manager-web
主要用于管理jsp界面以及其余配置文件
注意:项目打包再也不是jar包,而是war包,其余配置一致.]
当建立完web模块,该模块会报错,错误以下:
错误解决办法:因为模块建立时,缺乏WEB-INF/web.xml文件,只须要建立文件,问题就能够解决
此时,各类模块就建立完成,因为时间有限,在这里笼统的介绍一下,每个模块里面都有pom.xml文件应该怎么样配置呢?其实这个问题是这样解决的,每个模块,只须要在pom文件中添加该模块须要的依赖就能够,最重要的是:开始的时候提到,父工程主要用于管理依赖版本信息,在添加依赖是不须要填写版本信息,以下:
范围:配置manager-web模块
目录结构:
web.xml配置:
<!-- Spring IOC配置 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-config/applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
Spring MVC配置:
<servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping>
SpringMVC配置
<!-- 包扫描--> <context:component-scan base-package="com.web"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <!-- 视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"></property> <property name="suffix" value=".jsp"></property> </bean> <mvc:annotation-driven/>
Spring配置
<!-- 包扫描 --> <context:component-scan base-package="com.web"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" /> </context:component-scan> <!-- 加载配置文件 --> <context:property-placeholder location="classpath:properties/mysql.properties" /> <!-- 配置c3p0数据源 --> <bean class="com.mchange.v2.c3p0.ComboPooledDataSource" id="dataSource"> <property name="driverClass" value="${driverClass}"></property> <property name="jdbcUrl" value="${url}"></property> <property name="user" value="${user}"></property> <property name="password" value="${password}"></property> </bean> <!-- sqlSessionFactory --> <bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactory"> <property name="dataSource" ref="dataSource"></property> <property name="configLocation" value="classpath:mybatis/mybatis.xml"></property> <property name="mapperLocations" value="classpath:mapper/*.xml"></property> </bean> <!-- mapper扫描 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> <property name="basePackage" value="com.web.dao"></property> </bean> <!-- 事务配置 --> <bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="transactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <tx:annotation-driven transaction-manager="transactionManager"/>
配置mybatis.xml
<configuration> <settings> <setting name="lazyLoadingEnabled" value="true" /> </settings> </configuration>
Controller
package com.web.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import com.web.service.PersonService; @Controller public class webController { @Autowired PersonService personService; @GetMapping("/test.do") public String get(){ System.out.println(personService.selectPersonList()); return "test"; } }
运行项目:
http://localhost:8080/manager-web/index.jsp
效果:
恭喜!整合成功,如今就须要编写dao层和service层
范围:配置manager-mapper模块
目录结构:
实体类:
package com.web.entity; public class Person { private int id; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Person(int id, String name) { this.id = id; this.name = name; } public Person() { } @Override public String toString() { return "Person [id=" + id + ", name=" + name + "]"; } }
Mapper接口:
package com.web.dao; import java.util.List; import com.web.entity.Person; public interface PersonMapper { public List<Person> selectPersonList(); }
范围:配置manager-service模块
目录结构:
PersonService:
package com.web.service; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import com.web.dao.PersonMapper; import com.web.entity.Person; @Service public class PersonService { @Autowired PersonMapper personMapper; @Transactional public List<Person> selectPersonList(){ return personMapper.selectPersonList(); } }
最后测试结果为:
整合已经成功!因为第一次写文章,如有不正确的地方请你们多多指教,谢谢!