所谓聚合项目,实际上就是对项目分模块,互联网项目一般来说按照业务分(订单模块、VIP模块、支付模块、CMS模块...),传统的软件项目,大多采用分层的方式(Dao、Serivce、Controller):
一、多模块结构
bobo-parent pom 管理
|-- bobo-manager pom 管理
|-- bobo-manager-dao jar
|-- bobo-manager-service jar
|-- bobo-manager-model jar
|-- bobo-manager-commons jar
|-- bobo-manager-controller war
二、创建项目
1.创建bobo-parent
pom文件不需要写代码,只是起管理作用
2.创建bobo-manager项目
指定bobo-parent为其父项目
3.分别创建四个jar项目
项目类型为 maven model
相同的步骤创建其他3个jar项目
4.创建bobo-manager-controller项目
<dependencies>
<!-- 因为是web项目所以需要servlet --> <!-- https://mvnrepository.com/artifact/javax.servlet/servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins> <!-- tomcat插件 -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration> <!-- 端口号 -->
<port>8082</port> <!-- /表示访问路径 省略项目名 -->
<path>/</path> <!-- 设置编码方式 -->
<uriEncoding>utf-8</uriEncoding>
</configuration>
</plugin>
</plugins>
</build>
三、设置相互依赖
1.依赖关系介绍
完整依赖关系
graph LR
A[controller] --依赖 --> B[service]
B --依赖--> C[dao]
C --依赖--> D[model]
C--依赖-->E[commons]
控制层的依赖关系
graph LR
A[controller] --依赖 --> B[service]
A --依赖--> C[dao]
A --依赖--> D[model]
A--依赖-->E[commons]
service层的依赖关系
graph LR
B[service] --依赖--> C[dao]
B --依赖--> D[model]
B--依赖-->E[commons]
dao层的依赖关系
graph LR
C[dao] --依赖--> D[model]
C--依赖-->E[commons]
2.实现依赖关系
2.1项目打包
1.先对bobo-parent项目打包
本地仓库就会生成打包的内容
2.再对bobo-manager项目打包
成功后本地仓库
2.2添加依赖关系
1.controller依赖service
<!-- 配置依赖的service坐标 -->
<dependency>
<groupId>com.dpb</groupId>
<artifactId>bobo-manager-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
2.service依赖dao
<!-- 配置依赖的Dao坐标 -->
<dependency>
<groupId>com.dpb</groupId>
<artifactId>bobo-manager-dao</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
同时controller也会依赖dao,间接依赖
3.dao依赖model和Commons
<dependencies>
<!-- 配置依赖的model和Commons的坐标 -->
<dependency>
<groupId>com.dpb</groupId>
<artifactId>bobo-manager-model</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.dpb</groupId>
<artifactId>bobo-manager-commons</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
3.Spring和SpringMVC整合
3.1在dao中引入spring
<properties>
<spirng.version>4.3.3.RELEASE</spirng.version>
</properties>
<dependencies>
<!-- 配置依赖的model和Commons的坐标 -->
<dependency>
<groupId>com.dpb</groupId>
<artifactId>bobo-manager-model</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.dpb</groupId>
<artifactId>bobo-manager-commons</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- 1.spring相关的jar包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spirng.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spirng.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spirng.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spirng.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spirng.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${spirng.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spirng.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.6.2</version>
</dependency>
<!-- 数据库驱动包 这个是从私服上关联的-->
<dependency>
<groupId>com.dpb.oracle</groupId>
<artifactId>ojdbc6-dpb</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
3.2在controller中引入springmvc
在controller中添加的是其他项目不需要用的jar包,比如servlet。
<properties>
<spirng.version>4.3.3.RELEASE</spirng.version>
</properties>
<dependencies>
<!-- 因为是web项目所以需要servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<!-- 配置依赖的service坐标 -->
<dependency>
<groupId>com.dpb</groupId>
<artifactId>bobo-manager-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- 2.和springMvc相关的jar包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spirng.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/jstl/jstl -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
spring配置文件
<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-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<!-- 开启扫描 -->
<context:component-scan base-package="com.dpb.service.impl,com.dpb.dao.impl">
<!-- 扫描的时候显示的排除controller注解 -->
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<bean class="org.springframework.jdbc.datasource.DriverManagerDataSource"
id="ds">
<property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl" />
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="username" value="pms" />
<property name="password" value="pms" />
</bean>
<bean class="org.springframework.jdbc.core.JdbcTemplate" id="jdbcTemplate">
<constructor-arg name="dataSource" ref="ds"/>
</bean>
</beans>
SpringMVC的配置文件
<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:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
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-4.3.xsd">
<!-- 开启注解 -->
<mvc:annotation-driven ></mvc:annotation-driven>
<!-- 开启扫描 -->
<context:component-scan base-package="com.dpb.controller">
<!-- 只扫描指定路径下的controller注解 -->
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
</beans>
3.3在controller中配置web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<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" >
<!-- 这里配置Spring配置文件的位置,param-name是固定的, param-value是文件位置 这个配置可以省略,
如果省略, 系统默认去/WEB-INF/目录下查找applicationContext.xml作为Spring的配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 前端控制器 -->
<servlet>
<servlet-name>spring-mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>spring-mvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.jpg</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.js</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.css</url-pattern>
</servlet-mapping>
</web-app>
3.4实现查询操作
1.在bobo-manager-model中创建User对象
public class User {
private int id;
private String uname;
private String nickname;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
}
2.在bobo-manager-dao实现数据库操作
public interface IUserDao {
public List<User> query();
}
@Repository
public class UserDaoImpl implements IUserDao {
@Resource
JdbcTemplate jdbcTemplate;
@Override
public List<User> query() {
String sql = "select * from t_sysuser";
return jdbcTemplate.query(sql, new BeanPropertyRowMapper<User>(User.class));
}
}
3.在bobo-manager-service中操作Dao
public interface IUserService {
public List<User> query();
}
@Service
public class UserServiceImpl implements IUserService {
@Resource
private IUserDao dao;
@Override
public List<User> query() {
// TODO Auto-generated method stub
return dao.query();
}
}
4.在bobo-manager-controller中处理请求响应
@Controller
public class UserController {
@Resource
private IUserService userService;
@RequestMapping("/query")
public String query(Model m){
m.addAttribute("list", userService.query());
return "/user.jsp";
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<c:forEach items="${list }" var="u">
${u.id } --${u.uname }--${u.nickname }<br>
</c:forEach>
</body>
</html>
5.测试
对bobo-manager打包
然后启动 bobo-manager-controller项目
成功!!!