一步一步教你在eclipse下使用maven搭建模块化的SSH框架

一,下载安装maven

http://maven.apache.org/download.cgi

解压后。将maven所在目录配置到MAVEN_HOME中,并将%MAVEN_HOME%\bin目录加入path

然后在输入mvn --version查看maven是否安装成功。

 

二,安装maven-eclipse插件

使用用link的方式安装,先下载插件http://download.csdn.net/download/qq185501667/7076183

然后将他解压,然后在eclipse安装目录下,新建一个文件夹命名为:links(名字无所谓),然后在里面新建一个maven.link的文件,写入:path=E:/eclipsePlugins/maven(你的插件所在目录),重启eclipse,如果看到welcome页面就说明安装成功了。

三,新建maven project

先新建父模块

选择maven project

下一步,然后再直接下一步,不要勾选create simple project,然后进入选archetype页面,也直接选默认的maven-archetype-quickstart。最后要填GroupId(填工作组id,例如org.zyx) ArtifactId (可以填项目名称,例如myapp)。包名就不说了。

然后完成。然后修改pom.xml文件,因为我们的目标是创建模块化的WEB应用,我们当前新建的是父模块,它并不需要真正的构建,只是负责组织子模块,所以要将packaging修改

<span style="font-size:14px;"><packaging>pom</packaging></span>  

四,新建entity

在父项目上右键新建,选择maven module,其余同上。

由于本项目使用的是注解的方式配置hibernate 实体,所以要添加注解包的相关依赖

修改pom.xml,加入

<span style="font-size:14px;">  <dependencies>  
        <dependency>  
            <groupId>org.hibernate.javax.persistence</groupId>  
            <artifactId>hibernate-jpa-2.0-api</artifactId>  
            <version>1.0.0.Final</version>  
        </dependency>  
    </dependencies></span>  

这样 maven就会帮我们导入相关jar包了,而且如果我们进入里面的class,maven还会自动帮我们下载源码。

下面新建实体类:

 在src/main/java目录下新建类

<span style="font-size:14px;">package com.zyx.model.entity;

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name = "T_USER")
public class User {
	private int id;
	private String name;
	private String password;
	
	@Id
	@GeneratedValue
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	@Column(unique = true)
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
}
</span>

entity模块就完成了。

四,新建dao层

 创建 module 过程同上。

然后修改pom.xml,加入 必要的dependency,dao层需要hibernate和spring的相关jar包

添加以下依赖

<span style="font-size:14px;">		<!-- 引用实体模块 -->
		<dependency>
			<groupId>com.zyx</groupId>
			<artifactId>entity</artifactId>
			<version>${project.version}</version>
		</dependency>
		
		<!-- junit4 -->
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.4</version>
			<scope>test</scope>
		</dependency>

		<!-- c3p0数据源 -->
		<dependency>
			<groupId>c3p0</groupId>
			<artifactId>c3p0</artifactId>
			<version>0.9.1.2</version>
		</dependency>

		<!-- 依赖log4j slf4j -->
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.14</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
			<version>1.7.2</version>
		</dependency>

		<dependency>
			<groupId>cglib</groupId>
			<artifactId>cglib</artifactId>
			<version>2.2.2</version>
		</dependency>

		<dependency>
			<groupId>javassist</groupId>
			<artifactId>javassist</artifactId>
			<version>3.8.0.GA</version>
			<type>jar</type>
			<scope>compile</scope>
		</dependency> 

		<!-- Hiberante --> 
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-core</artifactId>
			<version>3.3.2.GA</version>
		</dependency>
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-annotations</artifactId>
			<version>3.3.1.GA</version>
		</dependency>
</span>

新建dao接口:

<span style="font-size:14px;">package com.zyx.model.dao;

public interface GenericDao {
	public <T> void save(T t);
	public <T> void delete(T t);
	public <T> void update(T t);
	public <T> T findById(Class<T> clazz, int id);
}
</span>
新建实现类:

<span style="font-size:14px;">package com.zyx.model.dao.impl;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zyx.model.dao.GenericDao;

@Service
@Transactional
public class GenericDaoImpl extends HibernateDaoSupport implements GenericDao {

	public <T> void save(T t) {
		getHibernateTemplate().save(t);
	}

	public <T> void delete(T t) {
		getHibernateTemplate().delete(t);
	}

	public <T> void update(T t) {
		getHibernateTemplate().update(t);
	}

	@SuppressWarnings("unchecked")
	public <T> T findById(Class<T> clazz, int id) {
		return (T) getHibernateTemplate().get(clazz, id);
	}

}
</span>
在src/main/resources里添加hibernate和spring的配置文件

1,applicationContext.xml

<span style="font-size:14px;"><?xml version="1.0" encoding="UTF-8"?>
<!--
	  - Middle tier application context definition for the image database.
	  -->
<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:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
	http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
	default-autowire="byName" default-lazy-init="false">
	<!-- 不加default-autowire="byName" 会得不到hibernateTemplate -->
	
	<!-- 配置spring扫描注解的范围 -->
	<context:component-scan base-package="com.zyx.**"  />

	<!-- 定义占位符所在的文件 -->
	<context:property-placeholder location="classpath:jdbc.properties"/>
	
	<!-- 配置数据源,使用了占位符 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
		<property name="driverClass" value="${jdbc.driverClassName}"/>
		<property name="jdbcUrl" value="${jdbc.url}"/>
		<property name="user" value="${jdbc.username}"/>
		<property name="password" value="${jdbc.password}"/>
	</bean>


	<!-- 配置sessionFactory,使用了占位符 -->
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
		<property name="dataSource">
			<ref bean="dataSource" />			
		</property>
		<!-- 全包扫描必须设置成实体类的上两层以上包!!! -->
		<property name="packagesToScan" value="com.zyx.model.*" />
		
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl}</prop>
				<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
				<prop key="hibernate.dialect">${hibernate.dialect}</prop>
				<prop key="hibernate.query.factory_class">org.hibernate.hql.ast.ASTQueryTranslatorFactory</prop>
				<prop key="connection.useUnicode">true</prop>
				<prop key="connection.characterEncoding">UTF-8</prop>
			</props>
		</property>
	</bean>
	
	<!-- 配置事务管理器 -->
	<bean id="transactionManager"
		class="org.springframework.orm.hibernate3.HibernateTransactionManager"  lazy-init="false" destroy-method="close">
		<property name="sessionFactory" ref="sessionFactory" />
		<property name="globalRollbackOnParticipationFailure" value="false" />		
	</bean>
	
	<!-- Activates @Transactional for DefaultImageDatabase -->
	<!--	<tx:annotation-driven transaction-manager="transactionManager"/>-->
</beans>
</span>

2, jdbc.properties

<span style="font-size:14px;">jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@localhost:1521:ORCL
jdbc.username=scott
jdbc.password=tiger
hibernate.hbm2ddl=update
hibernate.show_sql=true
hibernate.dialect=org.hibernate.dialect.OracleDialect</span>

dao层基本完成了,现在来测试一下。

在 src/test/java里新建测试类

<span style="font-size:14px;">package com.zyx.model.dao.impl;

import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

import com.zyx.model.dao.impl.GenericDao;
import com.zyx.model.entity.User;

public class UserDaoTest {
	ApplicationContext ctx = null;
	GenericDao dao = null;

	@Before
	public void beforeClass() {
		ctx = new FileSystemXmlApplicationContext(
				"classpath*:applicationContext.xml");
		dao = (UserDao) ctx.getBean("genericDaoImpl");
	}
	
	@Test
	public void testSave() {
		User user = new User();
		user.setName("张文辉");
		user.setPassword("23");
		dao.save(user);
	}	
}
</span>

我只写了一个测试方法,愿意的话可以写多个,然后在dao项目上右键run as maven test

测试通过就完成了。

 

四:service层

service层比较简单,只需要依赖dao层完成相关的业务逻辑就行了。

我贴出service层的pom.xml

<span style="font-size:14px;"><?xml version="1.0"?>
<project
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
	xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<artifactId>mr</artifactId>
		<groupId>com.zyx</groupId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	<groupId>com.zyx</groupId>
	<artifactId>service</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>service</name>
	<url>http://maven.apache.org</url> 
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>
	<dependencies>
		<!-- 引用dao层,因为dao层已经引用了entity 所以不需要再引用 -->
		<dependency>
			<groupId>com.zyx</groupId>
			<artifactId>dao</artifactId>
			<version>${project.version}</version>
		</dependency>		
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.4</version>
			<scope>test</scope>
		</dependency>
	</dependencies>
</project>
</span>

在这里需要提的是,依赖是有传递性的,dao层引用到的东西,一般service层都能引用到,除非你将scope配小,例如junit,我们将它的scope配置成了<scope>test</scope>这样引用该模块的模块是无法直接使用junit的。

 

六:web层

新建module的过程稍有不同,选择archetype的时候要选择maven-archetype-webapp,你可以在filter中输入maven-archetype-webapp,就直接过滤出maven-archetype-webapp了。

这样该模块的packagin属性就被设置成了war。构建出来就是一个war包,之前的都是jar。

web项目在src/main下会有一个webapp目标,相当于在eclipse下的webContent(MyEclipse是 web)

然后在pom.xml添加struts2依赖

<span style="font-size:14px;"><!-- 引用service层 -->
		<dependency>
			<groupId>com.zyx</groupId>
			<artifactId>service</artifactId>
			<version>${project.version}</version>
		</dependency>

		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.4</version>
			<scope>test</scope>
		</dependency>

		<dependency>
			<groupId>org.apache.struts</groupId>
			<artifactId>struts2-core</artifactId>
			<version>2.2.1</version>
		</dependency>

		<dependency>
			<groupId>org.apache.struts</groupId>
			<artifactId>struts2-spring-plugin</artifactId>
			<version>2.3.16.1</version>
		</dependency></span>

在src/main/resource下增加struts2的配置文件struts.xml

<span style="font-size:14px;"><?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.enable.DynamicMethodInvocation" value="false" />
	<constant name="struts.devMode" value="true" />
	<constant name="struts.objectFactory" value="spring" />

	<package name="default" namespace="/" extends="struts-default">

		<default-action-ref name="index" />

		<global-results>
			<result name="error">/error.jsp</result>
		</global-results>

		<global-exception-mappings>
			<exception-mapping exception="java.lang.Exception"
				result="error" />
		</global-exception-mappings>
		<action name="regUser">
			<result>/WEB-INF/page/registerUser.jsp</result>
		</action>

		<action name="*User" class="userAction" method="{1}">
			<result>/WEB-INF/page/{1}User.jsp</result>
		</action>
	</package>

	<include file="example.xml" />

	<!-- Add packages here -->

</struts>
</span>
配置src/main/webapp/WEB-INF/web.xml

<span style="font-size:14px;"><?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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">
	<display-name>meeting</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>
  
	<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>

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

  
</web-app></span>

<span style="font-size:14px;"></span>  

然后在src/main/java目录(如果没有就自己建)里面写入相应的Action

写完之后,就可以发到服务器上了。

可以使用MAVEN的命令行,来部署。

在CMD下,进入最后一个模块也就是web的目录,输入:mvn install

安装完成后输入: mvn tomcat:run

就部署完成了,然后就可以进网页访问了。

使用MAVEN搭建的SSH框架就完成了!






展开阅读全文

没有更多推荐了,返回首页