通过maven对项目进行拆分、聚合

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_33248299/article/details/83353151

思路:对现在已有maven ssh项目进行拆分,拆分思路:将dao层的代码已经配置文件全体提取出来到一个表现上独立的工程中 同样service,action拆分

工程我们用maven整合ssh.

ssh-parent:父工程
ssh-dao(子模块)
ssh-service	
ssh-web
  • 拆分完成对拆分后的项目进行聚合,提出概念父工程

一.创建父工程

在这里插入图片描述

在这里插入图片描述

  • 创建好父工程目录结构:只有pom.xml 可以推断父工程不进行编码
  • 项目需要的依赖信息,在父工程中定义,子模块继承过程
  <properties>
        <!-- 统一源码的编码方式 -->
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <!-- 统一各个框架版本 -->
        <struts.version>2.5.10</struts.version>
        <spring.version>4.3.8.RELEASE</spring.version>
        <hibernate.version>5.1.7.Final</hibernate.version>
    	</properties>
   <dependencies>
        <!-- Junit依赖 -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.9</version>
            <scope>test</scope>
        </dependency>
        <!-- Spring 核心依赖 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!-- Spring web依赖 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!-- Spring整合ORM框架依赖 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!-- Struts2 核心依赖 -->
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-core</artifactId>
            <version>${struts.version}</version>
        </dependency>
        <!-- Struts2和Spring整合依赖 -->
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-spring-plugin</artifactId>
            <version>${struts.version}</version>
        </dependency>
        <!-- Hibernate 核心依赖 -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>${hibernate.version}</version>
        </dependency>
        <!-- MySQL 依赖 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.42</version>
        </dependency>
        <!-- C3P0 依赖 -->
        <dependency>
            <groupId>com.mchange</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.5</version>
        </dependency>
        <!-- AspectJ依赖 -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.10</version>
        </dependency>
        <!-- SLF4J依赖 -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.25</version>
        </dependency>
         <!-- 导入java ee jar 包 -->  
        <dependency>  
            <groupId>javax</groupId>  
            <artifactId>javaee-api</artifactId>  
            <version>7.0</version>  
        </dependency>  
        <dependency> 
            <groupId>org.springframework</groupId>  
            <artifactId>spring-test</artifactId>  
            <version>4.2.4.RELEASE</version>  
       </dependency>
    </dependencies>
  • 将各个子模块聚合到一起
  • 将创建父工程发布到本地仓库
  • 将来service,dao工程发布到本地仓库 发布的service工程会报错
  • 如果忘记此步骤 将父工程发布到本地仓库

在这里插入图片描述
在这里插入图片描述
二.创建子模块ssh-dao

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

  • applicationContext-basic:项目基础信息
  • applicationContext-dao:dao层bean信息

实体类Customer

public class Customer {
         private String custId;
         private String custName;
         private String address;
		
		public String getCustId() {
			return custId;
		}
		public void setCustId(String custId) {
			this.custId = custId;
		}
		public String getCustName() {
			return custName;
		}
		public void setCustName(String custName) {
			this.custName = custName;
		}
		public String getAddress() {
			return address;
		}
		public void setAddress(String address) {
			this.address = address;
		}
         
}

CustomerDaoImpl.java

public class CustomerDaoImpl extends HibernateDaoSupport implements CustomerDao {

	public Customer findOne(String custId) {
		
		return this.getHibernateTemplate().get(Customer.class,custId);
	}

}

接口CustomerDao

public interface CustomerDao {

	Customer findOne(String custId);





}

Customer.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- 引入Hibernate映射文件约束 -->
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

    <!-- class指定POJO类和数据库表之间的对应 -->
    <class name="cn.playmaker.domain.Customer" table="t_customer" >

       <!-- id指定数据库中表的主键,以及主键生成策略 -->
       <id name="custId" type="java.lang.String" >
       <column name="id" length="32"></column>
           <generator class="uuid" />
       </id>

       <!-- property指定表字段和POJO中的属性的对应 -->
       <property name="custName" column="custName" type="java.lang.String">
       </property>

       <property name="address" column="address" type="java.lang.String">
       </property>

    </class>

</hibernate-mapping>

applicationContext-basic

<?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.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx.xsd">
    
    <!-- 加载属性文件 -->
    <context:property-placeholder location="classpath:db.properties"/>
    
   <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
                 <!-- 注入属性值 -->
                   <property name="driverClass" value="${jdbc.driverClass}"></property>
                    <property name="jdbcUrl" value="${jdbc.url}"></property>
                     <property name="user" value="${jdbc.username}"></property>
                      <property name="password" value="${jdbc.password}"></property>
         </bean>

      <!-- 配置Hibernate的SessionFactory -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <!-- 注入连接池 -->
        <property name="dataSource" ref="dataSource"/>
        <!-- 配置Hibernate属性 -->
          <property name="configLocations" value="classpath:hibernate.cfg.xml"></property>
    </bean>

     
     <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
	    <property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	
	<!-- xml管理事务 -->
     <tx:advice id="txAdvice">
            <tx:attributes>
                <!-- 匹配业务类中方法名 -->
                 <tx:method name="save*"/>
                  <tx:method name="update*"/>
                   <tx:method name="delete*"/>
                    <tx:method name="find*" read-only="true"/>
                    <tx:method name="*"/>
            </tx:attributes>     
     </tx:advice>
     
     <!-- 配置aop -->
     <aop:config>
        <!-- 配置切点:具体哪些方法要增强(真正被增强的方法) -->
        <aop:pointcut expression="execution(* cn.playmaker.service.*.*(..))" id="cut"/>
         <!-- 配置切面:将增强逻辑作用到切点(通知+切入点)-->
         <aop:advisor advice-ref="txAdvice" pointcut-ref="cut"/>
     </aop:config>
     
     <!-- 1.开启注解驱动 2.在service类或者方法上使用注解@Transactional-->
     <tx:annotation-driven transaction-manager="transactionManager" />
    
</beans>

applicationContext-dao

<?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.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx.xsd">
   
     
     <!-- 先注入dao -->
     <bean id="customerDao" class="cn.playmaker.dao.impl.CustomerDaoImpl">
              <property name="sessionFactory" ref="sessionFactory"></property>
     </bean>
</beans>

hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- DTD是约束,可以在核心包里面找 -->
<!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>

			<!-- 输出底层sql语句 -->
			<property name="hibernate.show_sql">true</property>
			<!-- 输出底层格式化语句 -->
			<property name="hibernate.format_sql">true</property>
		       <!-- hibernate帮创建表 需要配置之后
		       update:如果已经有表 更新 如果没有 创建 -->
		        <property name="hibernate.hbm2ddl.auto">update</property>
			
			<!-- 配置数据库方言 
			     在mysql里面实现分页 关键字 limit,只能使用mysql
			   在oracle数据库 实现分页 rownum
			   让hibernate框架识别不同数据库的自己特有的语句 -->
			<property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
			
			<!-- 第三部分:把映射文件放到核心配置文件中 必须的 -->
            <mapping resource="cn/playmaker/domain/Customer.hbm.xml"/>
          </session-factory>
</hibernate-configuration>

三.创建子模块ssh-service

在这里插入图片描述

接口CustomerService

public interface CustomerService {

	Customer findOne(String custId);


}

CustomerServiceImpl

public class CustomerServiceImpl implements CustomerService {
           private CustomerDao customerDao;

		public void setCustomerDao(CustomerDao customerDao) {
			this.customerDao = customerDao;
		}

		@Override
		public Customer findOne(String custId) {
			
			return customerDao.findOne(custId);
		}
         
}
<?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.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx.xsd">
   
     
     <!-- 再注入service -->
     <bean id="customerService" class="cn.playmaker.service.impl.CustomerServiceImpl">
             <property name="customerDao" ref="customerDao"></property>
     </bean>
</beans>

在这里插入图片描述

此时项目是报错的,因为找不到CustomerDao之类的对象

  • 在service工程pow.xml添加dao依赖

在这里插入图片描述

依赖引用的是一个项目
在这里插入图片描述

  • 因为我们之前已经将ssh-dao发布到本地仓库,所以当我们关闭ssh-dao这个项目,它就会去本地仓库去找对应的jar包
    在这里插入图片描述

四.创建子模块 ssh-web

在这里插入图片描述

  • 同样也要在ssh-web pom.xml加入service的依赖
    在这里插入图片描述

CustomerAction

public class CustomerAction extends ActionSupport {

	private CustomerService customerService;

	private String custId;
	
	public void setCustomerService(CustomerService customerService) {
		this.customerService = customerService;
	}
	
   public void setCustId(String custId) {
		this.custId = custId;
	}
  

public String findOne() throws Exception{
	   Customer customer =customerService.findOne(custId);
	   ActionContext.getContext().getValueStack().push(customer);
	   
	   return SUCCESS;
   }
}

applicationContext-action.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.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx.xsd">
    
    <!-- 配置action对象 -->
	<bean id="customerAction" class="cn.playmaker.action.CustomerAction" scope="prototype">
	          <property name="customerService" ref="customerService"></property>
	</bean>
</beans>

struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
    "http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>

    <!-- 默认访问页面 -->
    <package name="default" extends="struts-default" namespace="/">
        <action name="customerAction_*" class="customerAction" method="{1}">
            <result name="success">/index.jsp</result>
            <allowed-methods>findOne</allowed-methods>
        </action>
    </package>

    <!-- 引入资源文件 -->
    <constant name="struts.custom.i18n.resources" value="messageResource"></constant>

</struts>

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</display-name>
  
     <context-param>
    <param-name>contextConfigLocation</param-name>
    <!-- 这里如果bean.xml在包cn.ssh下,那么就应该写为:cn/ssh/bean.xml -->
    <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>
  
  
  <!-- 配置Spring的监听器 默认加载WEB-INF/applicationContext.xml-->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
  
  
  
  
  <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>
</web-app>

五.单元测试

  • 批量加载spring配置文件
classpath:spring/applicationContext-*.xml
classpath*:spring/applicationContext-*.xml 既要加载本项目中配置文件,还要加载jar包的配置文件
  • 对ssh-dao进行测试(创建CustomerDao的测试类)
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring/applicationContext-*.xml")
public class CustomerDaoTest {

	@Autowired
	private CustomerDao dao;
	@Test
	public void testFindOne() {
		Customer customer = dao.findOne("1");
		System.out.println(customer.getCustName());
	}

}

在这里插入图片描述

  • 对ssh-service进行测试(创建CustomerService的测视类)
public class CustomerServiceTest {

	@Test
	public void testFindOne() {
		ClassPathXmlApplicationContext path = 
				new ClassPathXmlApplicationContext("classpath*:spring/applicationContext-*.xml");
		CustomerService service = (CustomerService) path.getBean("customerService");
		service.findOne("1");
	}

}

在这里插入图片描述

六.传递依赖范围

在这里插入图片描述

    A:ssh-service 工程
    B:ssh-dao 工程
    C:junit单元测试
  • 总结:当项目需要的某一个jar包依赖没有传递过来。在自己工程中添加对于依赖就可以了

七.运行方式

  • 运行父工程。父工程将各个子模块聚合到一起。将ssh-web打成war包发布到tomcat

  • 直接运行web工程

  • 注意:这里最好tomcat和jdk版本一致,不然会出现报错的可能

  • 其他方式:部署tomcat上运行

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_33248299/article/details/83353151