Maven创建SSH工程(快速入门)源代码

一、Maven工程拆分与聚合

1、工程的拆分

一个完整的早期开发好的crm项目,现在要使用maven工程对它进行分析,这个时候就可以将dao拆解出来,形成独立的工程,同样service,action也都进行这样从拆分

2、工程的聚合

在这里插入图片描述
拆分后的工程之间相互存在依赖
在这里插入图片描述

3、继承的理解

类似java当中的类,都是为了消除重复。子类继承父类,父类里面有的方法和属性在子类当中就不需要再定义和实现了,使用的时候直接调用父类就可以,我们把crm拆分后,有一个父工程,子工程(crm06_dao,crm06_service,crm06_web)要用到的依赖都可以在父工程(crm06)的pox.xml当中先定义好,将来的子工程在开发的时候就不需要再引入坐标了。

4、开发步骤(详细步骤)

(1)创建maven父工程crm06,打包方式为pom

在这里插入图片描述

在这里插入图片描述

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

(2)在父工程的基础上建立子工程(dao层)

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

(3)在父工程的基础上建立子工程(service层)

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
点击Finish

可以在crm07的目录下的pom.xml当中看到父工程下所包含的子工程多了一个
在这里插入图片描述
在service当中可以看到指向了父工程目录
在这里插入图片描述

(4)在父工程的基础上建立子工程(dao层)

在这里插入图片描述
在这里插入图片描述
点击Finish
在这里插入图片描述
在这里插入图片描述
指向对应的父工程
在这里插入图片描述

(5)在service 层添加依赖

在这里插入图片描述

(6)在dao下创建接口


在这里插入图片描述

接口创建成功
在这里插入图片描述

(7)实现dao的接口

在这里插入图片描述
实现成功
在这里插入图片描述

(8)在service下创建接口

在这里插入图片描述

(9)实现service的接口

在这里插入图片描述

(10)在实现类当中写入如下代码,会发现报错

在这里插入图片描述

(11)需要在service当中添加对dao的依赖

在这里插入图片描述
在这里插入图片描述
引入地址后,并导包项目将不会报错

package com.itzheng.crm.service.impl;
import com.itzheng.crm.dao.ICustomerDao;
import com.itzheng.crm.service.ICustomerService;
public class CustomerService implements ICustomerService {
    
    
	private ICustomerDao customerDao;
	public void setCustomerDao(ICustomerDao customerDao) {
    
    
		this.customerDao = customerDao;
	}
}

(12)web层调用service层,在crm07_web当中的pom.xml当中

在这里插入图片描述
在这里插入图片描述
同时引入了crm07_dao和crm07_service,在上面配置引入的时候只引入了service因为service依赖于dao所以dao也被引入了进来
在这里插入图片描述

5、添加struts2的核心jar

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

6、查看pom.xml的依赖关系(通过Exclude排除jar包冲突依赖)

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
只剩下了最高版本
在这里插入图片描述
pom.xml当中的struct2的配置多了一个配置
在这里插入图片描述

7、依赖传递范围

子模块crm06_dao中添加junit的依赖,scope为test,但是在crm06_service中并不能使用junit
在这里插入图片描述

1、纵坐标:直接依赖 A依赖B,B是A的直接依赖。 在A的pom.xml中添加B的坐标

2、横坐标:传递依赖 B依赖C,C是A的传递依赖

3、中间部分:传递依赖的范围,A依赖C的范围。

(1)编写测试用例在dao当中,在pom.xml当中

在crm07_dao下的pom.xml当中
在这里插入图片描述

在这里插入图片描述

(2)service依赖dao但是,在dao引入junit测试后色service当中没有,如果想让junit在业务层也可以调用

在crm07_dao当中的pom.xml当中删除下面这一句
在这里插入图片描述
删除之后可以在service当中看到junit的jar
在这里插入图片描述

解决办法: 如果在依赖传递的过程当中,导致jar包的丢失,我们的做法很简单,就是再导入一次坐标

8、依赖的两个原则

(一)第一个原则:第一个先声明者优先

(1)在父工程当中添加依赖

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

(2)在crm07/pom.xml当中

在这里插入图片描述

(3)在pom.xml当中更改一下对应的,两个spring的顺序

在这里插入图片描述

优先顺序会改变
在这里插入图片描述

(二)第二个原则:路径近者优先原则

(1)实验如果上面引入的不是自己合适的,再次引入一个新的

在这里插入图片描述
生效版本变为后添加的版本
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

9、版本的锁定

(1)删除dao层当中的junit依赖

在这里插入图片描述

(2)在父工程当中添加对应的junit

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

(3)在子工程下面会自动引入和父工程一样的junit的jar

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

(4)这个时候在上面的会有三个项目有三个junit,

(一)这个时候需要通过版本的锁定来解决这个问题

在父工程的pom.xml当中、
在这里插入图片描述

<!-- 添加版本锁定 -->
	<dependencyManagement>
			<dependencies>
				<dependency>
					<groupId>junit</groupId>
					<artifactId>junit</artifactId>
					<version>4.9</version>
					<scope>test</scope>
				</dependency>
			</dependencies>
	</dependencyManagement>
(二)添加版本锁定以后

子工程当中的junit的jar会消失

(三)在子工程当中添加junit,选择对应锁定版本的junit

在dao当中添加对应的junit
在这里插入图片描述
在子工程的pom.xml当中
在service当中添加对应的junit

10、整合SSH其他jar包

(1)在crm07/pom.xml当中

在这里插入图片描述

(2)添加其他SSH相关jar

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

在这里插入图片描述

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

11、在crm/pom.xml当中定义版本常量

(1)设置对应的值

在这里插入图片描述

(2)替换对应的变量Ctrl + F

在这里插入图片描述
通过定义版本编号常量的方式可以定义替换pom.xml当中的内容
在这里插入图片描述

12、依赖管理总结

在这里插入图片描述

13、dao层的开发

(1)crm07_dao

在这里插入图片描述

package com.itzheng.crm.domain;
import java.io.Serializable;
/**
 * PO(Persistence Object)持久化类
 * 7个规范
 * 1. 公有类
 * 2. 公有无参构造
 * 3. 私有属性
 * 4. 公有的getter与setter
 * 5. 实现java.io.Serializable接口
 * 6. 不能用final修饰
 * 7. 如果是基础类型,要使用它的包装类
 * @author Administrator
 */
package com.itzheng.crm.domain;

import java.io.Serializable;

/**
 * PO(Persistence Object)持久化类 7个规范 1. 公有类 2. 公有无参构造 3. 私有属性 4. 公有的getter与setter
 * 5. 实现java.io.Serializable接口 6. 不能用final修饰 7. 如果是基础类型,要使用它的包装类
 * 
 * @author Administrator
 *
 */
public class Customer implements Serializable {
    
    

	private Long cust_id;
	private String cust_name;
	/*
	 * private String cust_source; private String cust_industry; private String
	 * cust_level;
	 */
	private String cust_phone;
	private String cust_mobile;

	private String cust_image;// 客户资质的图片

	public Long getCust_id() {
    
    
		return cust_id;
	}

	public void setCust_id(Long cust_id) {
    
    
		this.cust_id = cust_id;
	}

	public String getCust_name() {
    
    
		return cust_name;
	}

	public void setCust_name(String cust_name) {
    
    
		this.cust_name = cust_name;
	}

	public String getCust_phone() {
    
    
		return cust_phone;
	}

	public void setCust_phone(String cust_phone) {
    
    
		this.cust_phone = cust_phone;
	}

	public String getCust_mobile() {
    
    
		return cust_mobile;
	}

	public void setCust_mobile(String cust_mobile) {
    
    
		this.cust_mobile = cust_mobile;
	}

	public String getCust_image() {
    
    
		return cust_image;
	}

	public void setCust_image(String cust_image) {
    
    
		this.cust_image = cust_image;
	}

	@Override
	public String toString() {
    
    
		return "Customer [cust_id=" + cust_id + ", cust_name=" + cust_name + ", cust_phone=" + cust_phone
				+ ", cust_mobile=" + cust_mobile + ", cust_image=" + cust_image + "]";
	}

}

(2)配置文件(在src/main/resources下创建xml文件)

a、先创建和dao层一模一样的包

在这里插入图片描述

b、在创建好的包当中创建xml文件

在这里插入图片描述
编写映射文件

c、创建Hibernate的映射文件
<?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.itzheng.crm.domain.Customer" table="cst_customer">
		<!-- 建立类中的属性与表中的主键对应 -->
		<id name="cust_id" column="cust_id">
			<!-- 主键生成策略 -->
			<generator class="native" />
		</id>
		<!-- 建立类中的普通的属性和表的字段的对应映射 -->
		<property name="cust_name" column="cust_name" />
		
		<property name="cust_phone" column="cust_phone" />
		<property name="cust_mobile" column="cust_mobile" />
		<property name="cust_image" column="cust_image" />
    </class>
</hibernate-mapping>

在这里插入图片描述

<?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="dialert">org.hibernate.dialect.Oracle11gDialect</property>
		<property name="show_sql">true</property>
		<property name="format_sql">false</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>
d、创建spring的文件

在这里插入图片描述

e、在spring下创建spring的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 ">		
	<!-- 数据源 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">	
		<property name="driverClass" value="com.mysql.jdbc.Driver"></property>		
		<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/crm?characterEncoding=utf-8"></property>
		<property name="user" value="root" ></property>
		<property name="password" value="root" ></property>	
	</bean>	
	<!-- sessionFactory;SessionFactory接口负责初始化Hibernate。它充当数据存储源的代理,并负责创建Session对象。	-->
	<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/itzheng/crm/domain/*.hbm.xml" ></property>		
	</bean>	
	<!-- 数据访问层 -->
	<bean id="customerDao" class="com.itzheng.crm.dao.impl.CustomerDao">
		<property name="sessionFactory" ref="sessionFactory" ></property>	
	</bean>
</beans>
d、完善CustomerDao类
package com.itzheng.crm.dao.impl;
import java.util.List;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import com.itzheng.crm.dao.ICustomerDao;
import com.itzheng.crm.domain.Customer;
public class CustomerDao extends HibernateDaoSupport implements ICustomerDao {
    
    	
	public List<Customer> findAll() {
    
    	
		return (List<Customer>) getHibernateTemplate().find("from Customer");
	}
}
e、创建好测试类

在这里插入图片描述

package com.itzheng.crm.dao.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.itzheng.crm.dao.impl.CustomerDao;
public class CustomerTest {
    
    
	@Test
	public void tt() {
    
    
		ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-dao.xml");
		
		CustomerDao customerDao = (CustomerDao)ac.getBean("customerDao");
		
		System.out.println(customerDao.findAll().size());
		
	}
}

查询成功
在这里插入图片描述

14、service层的开发

(1)创建spring文件

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

<?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 ">
	<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	<!-- 通知 -->
	<tx:advice id="advice" transaction-manager="transactionManager" >
		<tx:attributes>			
				<tx:method name="add*" propagation="REQUIRED" />
				<tx:method name="save*" propagation="REQUIRED" />
				<tx:method name="update*" propagation="REQUIRED" />
				<tx:method name="delete*" propagation="REQUIRED" />
				<tx:method name="*" read-only="true" />
		</tx:attributes>
	</tx:advice>
	<!-- 切面 -->
	<aop:config>
		<aop:pointcut expression="execution(* com.itzheng.crm.service.impl.*.*(..))" id="myPointCut"/>
		<aop:advisor advice-ref="advice" pointcut-ref="myPointCut" />
	</aop:config>
	<!-- 业务 -->
	<bean id="customerService" class="com.itzheng.crm.service.impl.CustomerService">
		<property name="customerDao" ref="customerDao" ></property>
	</bean>
</beans>

(2)创建测试类

a、在service/pom.xml当中添加junit的配置

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

b、完善dao层的内容
package com.itzheng.crm.dao;
import java.util.List;
import com.itzheng.crm.domain.Customer;
public interface ICustomerDao {
    
    
	public List<Customer> findAll();
}

在这里插入图片描述

package com.itzheng.crm.dao.impl;
import java.util.List;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import com.itzheng.crm.dao.ICustomerDao;
import com.itzheng.crm.domain.Customer;
public class CustomerDao extends HibernateDaoSupport implements ICustomerDao {
    
    
	public List<Customer> findAll() {
    
    	
		return (List<Customer>) getHibernateTemplate().find("from Customer");
	}
}

在这里插入图片描述

c、完善业务层的内容
package com.itzheng.crm.service.impl;
import java.util.List;
import com.itzheng.crm.dao.ICustomerDao;
import com.itzheng.crm.domain.Customer;
import com.itzheng.crm.service.ICustomerService;
public class CustomerService implements ICustomerService {
    
    
	private ICustomerDao customerDao;
	public void setCustomerDao(ICustomerDao customerDao) {
    
    
		this.customerDao = customerDao;
	}
	public List<Customer> findAll() {
    
    
		return customerDao.findAll();
	}
}

在这里插入图片描述

d、创建测试类

在这里插入图片描述

(3)在applicationContext-service.xml当中导入dao层的applicationContext-dao.xml

	<!-- 导入,仅供测试用 -->
	<import resource="classpath:spring/applicationContext-dao.xml"/>

在这里插入图片描述

(4)运行测试类

package com.itzheng.crm.dao.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.itzheng.crm.dao.impl.CustomerDao;
public class CustomerTest {
    
    
	@Test
	public void tt() {
    
    
		ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-dao.xml");	
		CustomerDao customerDao = (CustomerDao)ac.getBean("customerDao");	
		System.out.println(customerDao.findAll().size());
	}
}

在这里插入图片描述

15、web层的开发

(1)在src/main/java下创建CustomerAction

在这里插入图片描述

(2)创建整个目录结构

在这里插入图片描述

(3)创建CustomerAction
package com.itzheng.crm.web.action;
import java.util.List;
import com.itzheng.crm.domain.Customer;
import com.itzheng.crm.service.impl.CustomerService;
import com.opensymphony.xwork2.ActionContext;
public class CustomerAction {
    
    	
	private CustomerService customerService;
	public void setCustomerService(CustomerService customerService) {
    
    
		this.customerService = customerService;
	}	
	public String list() {
    
    	
		//查询所有
		List<Customer> customerList = customerService.findAll();	
		//所有的客户信息都放入到值栈当中	
		ActionContext.getContext().put("list", customerList);
		//返回list.jsp页面
		return "list";
	}
}
(4)创建list.jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"  %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<c:forEach  items="${list }" var="customer"  >
	${
    
     customer.cust_id } --> ${
    
     customer.cust_name } <br>
</c:forEach>
</body>
</html>
(5) 创建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 ">		
	<bean id="customerAction" class="com.itzheng.crm.web.action.CustomerAction">	
		<property name="customerService" ref="customerService" ></property>
	</bean>
</beans>
(6) 创建applicationContext.xml, 加载所有的spring上下文
<?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 ">
	<import resource="classpath:spring/applicationContext-dao.xml" />
	<import resource="classpath:spring/applicationContext-service.xml" />
	<import resource="classpath:spring/applicationContext-action.xml" />
</beans>
(7) 创建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>
	<package name="default" namespace="/" extends="struts-default">
		<action name="customer_*" class="customerAction" method="{1}">
			<result name="list">/list.jsp</result>
		</action>
	</package>
</struts>

(8) 配置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">
	  

	<!-- spring上下文配置文件路径 -->

	<context-param>

		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext.xml</param-value>

	</context-param>

	<!-- 加载spring上下文 -->

	<listener>

		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

	</listener>


	<!-- Hibernate lazy load -->
	<filter>
		<filter-name>openSessionInView</filter-name>
		<filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>openSessionInView</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

	<!-- struts2 -->

	<filter>
		<filter-name>struts</filter-name>
		<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>

	</filter>

	<filter-mapping>
		<filter-name>struts</filter-name>
		<url-pattern>/*</url-pattern>

	</filter-mapping>

</web-app>

16、运行项目

访问成功
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_44757034/article/details/108813796