基于SSH框架的Maven多模块项目聚合详解
Maven多模块项目聚合:
它适用于一些比较大的项目,通过合理的模块拆分,实现代码的复用,便于维护和管理。尤其是一些开源框架,也是采用多模块的方式,提供插件集成,用户可以根据需要配置指定的模块。
Maven多模块项目结构:
本项目分为四个模块:父工程模块maven_ssh、子工程模块maven_ssh_dao(采用Hibernate框架实现)、子工程模块maven_ssh_service(采用Spring框架实现)、子工程模块maven_ssh_web(采用struts2框架实现)。每完成一个模块,模块单元测试完成后,安装到Maven本地仓库或Nexus私有仓库,以供另一个工程模块下载使用。
开发环境:
- 开发工具:Elipse
- Maven:Maven-3.6.0
- JDK:JDK-9.0.4
- 数据库:MySQL-8.0.12
- 服务器:Apache-Tomcat-9.0.12
- SSH框架:Struts-2.2.18 + Spring-4.3.13.RELEASE + Hibernate-5.2.12.Final
1、maven_ssh父工程构建
(1) 使用Eclipse创建maven_ssh父工程
(2) maven_ssh目录结构
(3) maven_ssh文件配置
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>com.ming</groupId> <artifactId>maven_ssh</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>pom</packaging> <!-- 锁定版本 --> <dependencyManagement> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.10</version> <scope>test</scope> </dependency> </dependencies> </dependencyManagement> <!-- 添加依赖 --> <dependencies> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>5.2.12.Final</version> </dependency> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-core</artifactId> <version>2.5.18</version> </dependency> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-spring-plugin</artifactId> <version>2.5.18</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>4.3.13.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>4.3.13.RELEASE</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.13</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.25</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.25</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.10.0</version> <exclusions> <exclusion> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.13</version> <scope>runtime</scope> </dependency> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.2</version> </dependency> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jsp-api</artifactId> <version>2.0</version> <scope>provided</scope> </dependency> </dependencies> </project>
(4) 将maven_ssh父工程发布到Maven本地仓库
maven_ssh父工程创建完后,选中项目右击选中Run AS --> Maven install执行,将父工程发布到Maven本地仓库或Nexus私有仓库,以便父工程下的子工程模块继承。
2、maven_ssh_dao子工程构建
(1) 使用Eclipse创建maven_ssh_dao子工程
(2) maven_ssh_dao目录结构
(3) maven_ssh_dao文件配置
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>com.ming</groupId> <artifactId>maven_ssh</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>maven_ssh_dao</artifactId> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> </dependency> <dependency> <groupId>org.glassfish.jaxb</groupId> <artifactId>jaxb-core</artifactId> <version>2.3.0</version> </dependency> <dependency> <groupId>com.sun.xml.bind</groupId> <artifactId>jaxb-impl</artifactId> <version>2.3.0</version> </dependency> <dependency> <groupId>javax.activation</groupId> <artifactId>javax.activation-api</artifactId> <version>1.2.0</version> </dependency> </dependencies> </project>
customer_manager数据库中customer表的构建:
customer表所对应的实体类Customer:
package com.ming.ssh.domain; import java.io.Serializable; public class Customer implements Serializable{ private static final long serialVersionUID = 1L; private Long c_id; private String c_name; private String c_password; private String c_address; private String c_phone; private String c_email; public Long getC_id() { return c_id; } public void setC_id(Long c_id) { this.c_id = c_id; } public String getC_name() { return c_name; } public void setC_name(String c_name) { this.c_name = c_name; } public String getC_password() { return c_password; } public void setC_password(String c_password) { this.c_password = c_password; } public String getC_address() { return c_address; } public void setC_address(String c_address) { this.c_address = c_address; } public String getC_phone() { return c_phone; } public void setC_phone(String c_phone) { this.c_phone = c_phone; } public String getC_email() { return c_email; } public void setC_email(String c_email) { this.c_email = c_email; } @Override public String toString() { return "Customer [c_id=" + c_id + ", c_name=" + c_name + ", c_password=" + c_password + ", c_address="+ c_address + ", c_phone=" + c_phone + ", c_email=" + c_email + "]"; } }
Customer.hbm.xml:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd" > <hibernate-mapping> <class name="com.ming.ssh.domain.Customer" table="customer"> <id name="c_id"> <generator class="native"></generator> </id> <property name="c_name"></property> <property name="c_password"></property> <property name="c_address"></property> <property name="c_phone"></property> <property name="c_email"></property> </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.MySQLDialect</property> <property name="show_sql">true</property> <property name="format_sql">true</property> <property name="hbm2ddl.auto">none</property> <property name="hibernate.enable_lazy_load_no_trans">true</property> <property name="javax.persistence.validation.mode">none</property> </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-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"> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="com.mysql.cj.jdbc.Driver"></property> <property name="jdbcUrl" value="jdbc:mysql://localhost/customer_manager?serverTimezone=UTC"></property> <property name="user" value="root"></property> <property name="password" value="1314"></property> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="configLocation" value="classpath:hibernate.cfg.xml"></property> <property name="mappingLocations" value="classpath:com/ming/ssh/domain/*.hbm.xml"></property> </bean> <bean id="customerDao" class="com.ming.ssh.dao.impl.CustomerDaoImpl"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> </beans>
log4j2.xml:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE log4j2[]> <Configuration status="warn"> <Appenders> <Console name="Console" target="SYSTEM_OUT"> <PatternLayout pattern="%m%n" /> </Console> </Appenders> <Loggers> <Root level="error"> <AppenderRef ref="Console" /> </Root> </Loggers> </Configuration>
CustomerDao:
package com.ming.ssh.dao; import java.util.List; import com.ming.ssh.domain.Customer; public interface CustomerDao { List<Customer> findAllCustomer(); }
CustomerDaoImpl:
package com.ming.ssh.dao.impl; import java.util.List; import org.springframework.orm.hibernate5.support.HibernateDaoSupport; import com.ming.ssh.dao.CustomerDao; import com.ming.ssh.domain.Customer; @SuppressWarnings("unchecked") public class CustomerDaoImpl extends HibernateDaoSupport implements CustomerDao{ public List<Customer> findAllCustomer() { return (List<Customer>) getHibernateTemplate().find("from Customer"); } }
CustomerDaoTest:
package com.ming.ssh.dao; import java.util.List; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.ming.ssh.domain.Customer; public class CustomerDaoTest { private ApplicationContext ac; @Test public void demo() { ac = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-dao.xml"); CustomerDao customerDao = (CustomerDao) ac.getBean("customerDao"); List<Customer> list = customerDao.findAllCustomer(); for (Customer customer : list) { System.out.println(customer); } } }
(4) 将maven_ssh_dao子工程发布到Maven本地仓库
maven_ssh_dao子工程单元测试无误后,选中项目右击选中Run AS --> Maven build执行,将该子工程发布到Maven本地仓库或Nexus私有仓库,以便maven_ssh_service子工程模块调用。
3、maven_ssh_service子工程构建
(1) 使用Eclipse创建maven_ssh_sevice子工程
(2) maven_ssh_service目录结构
(3) maven_ssh_service文件配置
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>com.ming</groupId> <artifactId>maven_ssh</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>maven_ssh_service</artifactId> <dependencies> <dependency> <groupId>com.ming</groupId> <artifactId>maven_ssh_dao</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> </dependency> </dependencies> </project>
applicationContext-sercive.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-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"> <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="delete*" propagation="REQUIRED" /> <tx:method name="update*" propagation="REQUIRED" /> <tx:method name="find*" propagation="SUPPORTS" read-only="true" /> <tx:method name="get*" propagation="SUPPORTS" read-only="true" /> <tx:method name="query*" propagation="SUPPORTS" read-only="true" /> </tx:attributes> </tx:advice> <aop:config> <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.ming.ssm.service.*.*(..))" /> </aop:config> <bean id="customerService" class="com.ming.ssh.service.impl.CustomerServiceImpl"> <property name="customerDao" ref="customerDao"></property> </bean> </beans>
CustomerService:
package com.ming.ssh.service; import java.util.List; import com.ming.ssh.domain.Customer; public interface CustomerService { List<Customer> findAllCustomer(); }
CustomerServiceImpl:
package com.ming.ssh.service.impl; import java.util.List; import com.ming.ssh.dao.CustomerDao; import com.ming.ssh.domain.Customer; import com.ming.ssh.service.CustomerService; public class CustomerServiceImpl implements CustomerService { private CustomerDao customerDao; public void setCustomerDao(CustomerDao customerDao) { this.customerDao = customerDao; } @Override public List<Customer> findAllCustomer() { return customerDao.findAllCustomer(); } }
CustomerServiceTest:
package com.ming.ssh.service; import java.util.List; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.ming.ssh.domain.Customer; public class CustomerServiceTest { private ApplicationContext ac; @Test public void demo() { ac = new ClassPathXmlApplicationContext("classpath*:spring/applicationContext-*.xml"); CustomerService customerService = (CustomerService) ac.getBean("customerService"); List<Customer> list = customerService.findAllCustomer(); for (Customer customer : list) { System.out.println(customer); } } }
(4) 将maven_ssh_service子工程发布到Maven本地仓库
maven_ssh_service子工程单元测试无误后,选中项目右击选中Run AS --> Maven build执行,将该子工程发布到Maven本地仓库或Nexus私有仓库,以便maven_ssh_web子工程模块调用。
4、maven_ssh_web子工程构建
(1) 使用Eclipse创建maven_ssh_web子工程
(2) 生成maven_ssh_web子工程的部署文件web.xml
maven_ssh_web子工程创建完后,选中项目右击选中Java EE Tools --> Generate Deployment Descriptor Stub执行,生成该工程部署文件web.xml。
(3) maven_ssh_web目录结构
(4) maven_ssh_web文件配置
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>com.ming</groupId> <artifactId>maven_ssh</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>maven_ssh_web</artifactId> <packaging>war</packaging> <dependencies> <dependency> <groupId>com.ming</groupId> <artifactId>maven_ssh_service</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies> </project>
struts.xml:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <constant name="struts.action.extension" value="action"></constant> <package name="defaut" extends="struts-default" namespace="/"> <action name="customer_*" method="{1}" class="customerAction"> <result name="findAllSuccess">menu.jsp</result> <allowed-methods>findAll</allowed-methods> </action> </package> </struts>
applicationContext-web.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-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"> <bean id="customerAction" class="com.ming.ssh.web.CustomerAction" scope="prototype"> <property name="customerService" ref="customerService"></property> </bean> </beans>
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" version="2.5"> <display-name>maven_ssh_web</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <listener 1fff7 > <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:spring/applicationContext-*.xml</param-value> </context-param> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
CustomerAction:
package com.ming.ssh.web; import java.util.List; import org.apache.struts2.ServletActionContext; import com.ming.ssh.domain.Customer; import com.ming.ssh.service.CustomerService; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.ModelDriven; public class CustomerAction extends ActionSupport implements ModelDriven<Customer> { private static final long serialVersionUID = 1L; private Customer customer = new Customer(); @Override public Customer getModel() { return this.customer; } private CustomerService customerService; public void setCustomerService(CustomerService customerService) { this.customerService = customerService; } public String findAll() { List<Customer> list = customerService.findAllCustomer(); ServletActionContext.getRequest().setAttribute("customerList", list); return "findAllSuccess"; } }
menu.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="/struts-tags" prefix="s"%> <!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>客户管理页面</title> </head> <body> <h2 align="center">客户管理系统</h2> <table border="1px" align="center" width="700px"> <tr align="center"> <td>客户ID</td> <td>客户姓名</td> <td>客户密码</td> <td>客户地址</td> <td>客户手机</td> <td>客户邮箱</td> </tr> <s:iterator var="c" value="#request.customerList" > <tr align="center"> <td><s:property value="#c.c_id"/></td> <td><s:property value="#c.c_name"/></td> <td><s:property value="#c.c_password"/></td> <td><s:property value="#c.c_address"/></td> <td><s:property value="#c.c_phone"/></td> <td><s:property value="#c.c_email"/></td> </tr> </s:iterator> </table> </body> </html>
(4) maven_ssh_web子工程测试
在Eclipse中关闭父工程maven_ssh、子工程maven_ssh_dao、子工程maven_ssh_service,更新maven_ssh_web子工程,在Maven Dependencies中新增两个子工程dao和service的jar包。
将maven_ssh_web子工程发布到本地tomcat服务器上,在浏览器地址栏访问http://localhost:8080/maven_ssh_web/customer_findAll.action,其结果如下:
- SSH框架之一详解maven搭建多模块项目
- Maven工程多模块继承和聚合项目创建与数据库测试步骤详解
- SSH框架之一详解maven搭建多模块项目
- SVN中基于Maven的Web项目更新到本地过程详解
- myeclipse+maven实现多模块项目struts+spring+mybatis,详解
- 模块化建立项目流程(Maven聚合模块)
- [1.0.2] 详解基于maven管理-scala开发的spark项目开发环境的搭建与测试
- IntelliJ IDEA中创建Web聚合项目(Maven多模块项目)
- maven(二) maven项目构建ssh工程(父工程与子模块的拆分与聚合)
- 基于maven的ssm项目配置文件详解
- 使用IDEA基于Maven搭建多模块聚合工程(springmvc+spring+mybatis整合)
- IntelliJ IDEA中创建Web聚合项目(Maven多模块项目)
- maven多模块项目聚合
- 基于maven使用IDEA创建多模块项目
- maven(二) maven项目构建ssh工程(父工程与子模块的拆分与聚合)
- 基于maven使用IDEA创建多模块项目
- 基于maven使用IDEA创建多模块项目
- Maven系列五:多模块项目中的聚合和继承
- 基于maven使用IDEA创建多模块项目
- 详解Maven 搭建spring boot多模块项目(附源码)