maven搭建SSH项目案例


需求:使用Maven搭建SSH版本的CRM系统,完成客户的录入功能

  • 需要有一个sm1234-parent项目控制各个子项目的依赖版本号
  • 创建聚合项目crm_main项目包括四个子项目crm_domain、crm_dao、crm-biz、crm_management 这些子项目之间的依赖关系为:management --> biz --> dao --> domain
  • sm1234-parent和crm-main打包方式为pom
  • crm_domain,,crm_dao,crm_biz打包方式为jar
  • crm_management打包方式为war并且配置tomcat插件

一、创建sm1234_parent父工程

1.1、建立maven project 

1.2、配置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>cn.sm1234</groupId>
	<artifactId>sm1234_parent</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>pom</packaging>

	<!-- 集中定义依赖版本号 -->
	<properties>
		<junit.version>4.10</junit.version>
		<struts.version>2.3.24.1</struts.version>
		<struts.spring.version>2.3.24.1</struts.spring.version>
		<spring.version>4.2.0.RELEASE</spring.version>
		<hibernate.version>5.0.7.Final</hibernate.version>
		<mysql.version>5.1.32</mysql.version>
		<slf4j.version>1.6.4</slf4j.version>
		<c3p0.version>0.9.5.2</c3p0.version>
		<mchange.commons.version>0.2.12</mchange.commons.version>
		<jstl.version>1.2</jstl.version>
		<servlet-api.version>2.5</servlet-api.version>
		<jsp-api.version>2.0</jsp-api.version>
		<commons-lang3.version>3.3.2</commons-lang3.version>
		<commons-io.version>1.3.2</commons-io.version>
	</properties>

	<!-- 设置deploy地址 -->
	<distributionManagement>
		<repository>
			<id>nexus</id>
			<name>Nexus</name>
			<url>http://localhost:8082/nexus/content/repositories/releases/</url>
		</repository>
		<snapshotRepository>
			<id>nexus</id>
			<name>Nexus</name>
			<url>http://localhost:8082/nexus/content/repositories/snapshots/</url>
		</snapshotRepository>
	</distributionManagement>

	<dependencyManagement>
		<dependencies>
			<!-- 单元测试 -->
			<dependency>
				<groupId>junit</groupId>
				<artifactId>junit</artifactId>
				<version>${junit.version}</version>
				<scope>test</scope>
			</dependency>

			<!-- Struts -->
			<dependency>
				<groupId>org.apache.struts</groupId>
				<artifactId>struts2-core</artifactId>
				<version>${struts.version}</version>
			</dependency>
			<dependency>
				<groupId>org.apache.struts</groupId>
				<artifactId>struts2-spring-plugin</artifactId>
				<version>${struts.version}</version>
			</dependency>

			<!-- Spring -->
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-core</artifactId>
				<version>${spring.version}</version>
			</dependency>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-context</artifactId>
				<version>${spring.version}</version>
			</dependency>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-beans</artifactId>
				<version>${spring.version}</version>
			</dependency>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-tx</artifactId>
				<version>${spring.version}</version>
			</dependency>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-aspects</artifactId>
				<version>${spring.version}</version>
			</dependency>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-aop</artifactId>
				<version>${spring.version}</version>
			</dependency>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-expression</artifactId>
				<version>${spring.version}</version>
			</dependency>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-web</artifactId>
				<version>${spring.version}</version>
			</dependency>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-jdbc</artifactId>
				<version>${spring.version}</version>
			</dependency>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-orm</artifactId>
				<version>${spring.version}</version>
			</dependency>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-test</artifactId>
				<version>${spring.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>${mysql.version}</version>
			</dependency>

			<dependency>
				<groupId>org.slf4j</groupId>
				<artifactId>slf4j-log4j12</artifactId>
				<version>${slf4j.version}</version>
			</dependency>

			<!-- 连接池 -->
			<dependency>
				<groupId>com.mchange</groupId>
				<artifactId>c3p0</artifactId>
				<version>${c3p0.version}</version>
			</dependency>

			<!-- JSP相关 -->
			<dependency>
				<groupId>jstl</groupId>
				<artifactId>jstl</artifactId>
				<version>${jstl.version}</version>
			</dependency>
			<dependency>
				<groupId>javax.servlet</groupId>
				<artifactId>servlet-api</artifactId>
				<version>${servlet-api.version}</version>
				<scope>provided</scope>
			</dependency>
			<dependency>
				<groupId>javax.servlet</groupId>
				<artifactId>jsp-api</artifactId>
				<version>${jsp-api.version}</version>
				<scope>provided</scope>
			</dependency>

			<!-- Apache工具组件 -->
			<dependency>
				<groupId>org.apache.commons</groupId>
				<artifactId>commons-lang3</artifactId>
				<version>${commons-lang3.version}</version>
			</dependency>
			<dependency>
				<groupId>org.apache.commons</groupId>
				<artifactId>commons-io</artifactId>
				<version>${commons-io.version}</version>
			</dependency>

		</dependencies>
	</dependencyManagement>
	<build>
		<finalName>${project.artifactId}</finalName>
		<plugins>
			<!-- java编译插件 -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.2</version>
				<configuration>
					<source>1.7</source>
					<target>1.7</target>
					<encoding>UTF-8</encoding>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

二、创建crm_main聚合工程

2.1、建立maven project

2.2、配置pom.xml

去掉groupId和artifactId

<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>cn.sm1234</groupId>
		<artifactId>sm1234_parent</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	<artifactId>crm_main</artifactId>
	<packaging>pom</packaging>

	<dependencies>

		<!-- 单元测试 -->
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
		</dependency>

		<!-- Apache工具组件 -->
		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-lang3</artifactId>
		</dependency>
		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-io</artifactId>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<!-- tomcat7插件 -->
			<plugin>
				<groupId>org.apache.tomcat.maven</groupId>
				<artifactId>tomcat7-maven-plugin</artifactId>
				<version>2.1</version>
				<configuration>
					<port>9080</port>
					<server>tomcat7</server>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

三、创建crm_domain

3.1、建立mave module project

3.2、配置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>cn.sm1234</groupId>
		<artifactId>crm_main</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	<artifactId>crm_domain</artifactId>

	<dependencies>
		<!-- hibernate,使用到jpa注解 -->
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-core</artifactId>
		</dependency>
	</dependencies>
</project>

四、创建crm_dao

4.1、建立mave module project

4.2、配置pom.xml

1)crm_dao项目依赖crm_domain
2)crm_dao引入自己需要的依赖

<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>cn.sm1234</groupId>
		<artifactId>crm_main</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	<artifactId>crm_dao</artifactId>

	<dependencies>
		<!-- 依赖crm_domain -->
		<dependency>
			<groupId>cn.sm1234</groupId>
			<artifactId>crm_domain</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>

		<!-- c3p0 -->
		<dependency>
			<groupId>com.mchange</groupId>
			<artifactId>c3p0</artifactId>
		</dependency>

		<!-- mysql驱动 -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
		</dependency>

		<!-- spring的context -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
		</dependency>
		<!-- spring的orm,使用HibernateDaoSupport -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-orm</artifactId>
		</dependency>
	</dependencies>
</project>

五、创建crm_biz

5.1、建立maven module project

5.2、配置pom.xml

1)crm_biz依赖crm_dao
2)crm_biz引入自己需要的依赖

<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>cn.sm1234</groupId>
		<artifactId>crm_main</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	<artifactId>crm_biz</artifactId>

	<dependencies>
		<!-- 依赖crm_dao -->
		<dependency>
			<groupId>cn.sm1234</groupId>
			<artifactId>crm_dao</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>


		<!-- spring框架相关 -->
		<!-- spring的context -->
		<!-- 注意:spring的context一旦导入会同时依赖导入spring-core,spring-expression,spring-aop,spring-beans -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
		</dependency>

		<!-- spring的aspects -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aspects</artifactId>
		</dependency>
		
		<!-- spring的test-->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
		</dependency>
	</dependencies>
</project>

六、创建crm_management

6.1、建立maven module project

这时项目会报错,因为没有web.xml文件
在src/main/webapp下建立WEB-INF/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框架整合WEB的监听器 -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext.xml</param-value>
	</context-param>
	
	<!-- 配置Struts2框架的核心的过滤器 -->
	<filter>
		<filter-name>struts2</filter-name>
		<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

	<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>

6.2、配置pom.xml

1)crm_management依赖crm_biz
2)crm_management引入自己需要的依赖

<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>cn.sm1234</groupId>
		<artifactId>crm_main</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	<artifactId>crm_management</artifactId>
	<packaging>war</packaging>

	<dependencies>
		<!-- 依赖crm_biz -->
		<dependency>
			<groupId>cn.sm1234</groupId>
			<artifactId>crm_biz</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>

		<!-- struts2的核心包 -->
		<dependency>
			<groupId>org.apache.struts</groupId>
			<artifactId>struts2-core</artifactId>
		</dependency>
		
		<!-- struts2-spirng-plugin -->
		<dependency>
			<groupId>org.apache.struts</groupId>
			<artifactId>struts2-spring-plugin</artifactId>
		</dependency>
		
		<!-- servlet的api -->
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>servlet-api</artifactId>
		</dependency>
		
		<!-- jsp的api -->
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jsp-api</artifactId>
		</dependency>
		
		<!-- jstl的api -->
		<dependency>
			<groupId>jstl</groupId>
			<artifactId>jstl</artifactId>
		</dependency>
	</dependencies>
</project>

七、SSH框架配置

7.1、日志配置

在crm_management项目的src/main/resources下拷贝日志配置

7.2、数据库配置

在crm_management项目的src/main/resources下拷贝db.properties

jdbcUrl=jdbc:mysql:///maven
driverClass=com.mysql.jdbc.Driver
user=root
password=root

7.3、applicationContext.xml

在crm_management项目的src/main/resources下建立applicationContext.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">
	
	<!-- spring读取db.properties文件 -->
	<!-- 
	location:读取的文件路径
	 -->
	<context:property-placeholder location="classpath:db.properties"/>
	
	<!-- 1.建立数据源 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="jdbcUrl" value="${jdbcUrl}"/>
		<property name="driverClass" value="${driverClass}"/>
		<property name="user" value="${user}"/>
		<property name="password" value="${password}"/>
	</bean>
	
	<!-- 2.创建SessinoFactory -->
	<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSource"/>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.format_sql">true</prop>
				<prop key="hibernate.hbm2ddl.auto">update</prop>
				<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
			</props>
		</property>
		<!-- JPA注解 -->
		<property name="packagesToScan">
			<array>
				<value>cn.sm1234.crm.domain</value>
			</array>
		</property>
	</bean>
	
	<!-- 3.1 开启事务注解 -->
	<tx:annotation-driven transaction-manager="transactionManager"/>
	<!-- 3.2 事务管理器 -->
	<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory"/>
	</bean>
	
	<!-- 4.开启IOC注解 -->
	<context:component-scan base-package="cn.sm1234"/>
</beans>

7.4、struts.xml

在crm_management项目的src/main/resources下建立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.devMode" value="true" />

</struts>

八、测试运行

1、配置tomcat启动;注意;启动的是crm_main项目。

注意:tomcat7的插件运行命令为:tomcat7:run

2、运行报错,找不到依赖项目;需要将对应项目安装到本地仓库;

解决方法:
找到对应项目,上述是找不到sm1234-parent,则右击sm1234-parent;然后:

控制台没有错误并且打印了SSH框架运行代表运行成功!

九、代码编写

9.1、实体及映射

@Entity
@Table(name="t_customer")
public class Customer {

	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	@Column(name="cust_id")
	private Integer custId;
	
	@Column(name="cust_name")
	private String custName;
	
	@Column(name="cust_source")
	private String custSource;

9.2、Dao接口和实现

接口:

public interface CustomerDao {
    public void save(Customer customer);
}

实现:

@Repository
public class CustomerDaoImpl extends HibernateDaoSupport implements CustomerDao {
    //注入SessionFactory
    @Resource
    public void setMySessionFactory(SessionFactory sessionFactory){
        super.setSessionFactory(sessionFactory);;
    }
    
    @Override
    public void save(Customer customer) {
        this.getHibernateTemplate().saveOrUpdate(customer);
    }

}

9.3、Biz接口和实现

接口:

public interface CustomerBiz {
    public void save(Customer customer);
}

实现:

@Service
@Transactional
public class CustomerBizImpl implements CustomerBiz {

    //注入dao
    @Resource
    private CustomerDao customerDao;
    
    @Override
    public void save(Customer customer) {
        customerDao.save(customer);
    }

}

9.4、Action

@Controller
@Scope("prototype")
public class CustomerAction extends ActionSupport implements ModelDriven<Customer>{
    private Customer customer = new Customer();
    
    @Override
    public Customer getModel() {
        return customer;
    }
    
    //注入biz
    @Resource
    private CustomerBiz customerBiz;
    
    /**
     * 保存
     */
    public String save(){
        try {
            customerBiz.save(customer);
            ActionContext.getContext().put("msg", "操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            ActionContext.getContext().put("msg", "操作成功:"+e.getMessage());
        }
        return SUCCESS;
    }
    
}

配置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.devMode" value="true" />
	<!-- 把主题配置成simple -->
	<constant name="struts.ui.theme" value="simple" />

	<package name="crm" extends="struts-default" namespace="/">
		<action name="customer_*" class="customerAction" method="{1}">
			<result>/info.jsp</result>
		</action>
	</package>
</struts>

9.5、页面

<%@ 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>
<s:form action="customer_save">
    客户名称:<s:textfield name="custName"/><br/>
    客户来源:<s:textfield name="custSource"/><br/>
    <input type="submit" value="提交"/>
</s:form>

</body>
</html>

 

源码下载: http://pan.baidu.com/s/1bGKNcq