spring4.06 hibernate4.31 struts2.3.16 全注解MAVEN环境搭建

最近使用目录最新版本的SPRING、HIBERNATE、STRUTS使用MAVEN搭建了一个全注解的环境。记录一下

POM文件:

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>myssh</groupId>
  <artifactId>ssh</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>ssh Maven Webapp</name>
  <url>http://maven.apache.org</url>
   <repositories>
   <repository>
        <id>io.spring.repo.maven.release</id>
        <url>http://repo.spring.io/release/</url>
        <snapshots><enabled>false</enabled></snapshots>
    </repository>
      <repository>
          <id>java</id>
          <name>java official repository</name>
          <url>http://download.java.net/maven/2/</url>
      </repository>
  </repositories>
  <properties>
        <org.springframework-version>4.0.6.RELEASE</org.springframework-version>
        <org.hibernate-version>4.3.1.Final</org.hibernate-version>
        <org.struts-version>2.3.16.3</org.struts-version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <!--struts2 -->
    <dependency>
		<groupId>commons-io</groupId>
		<artifactId>commons-io</artifactId>
		<version>2.4</version>
	</dependency>
    <dependency>
	<groupId>org.apache.struts</groupId>
	<artifactId>struts2-core</artifactId>
	<version>${org.struts-version}</version>
	</dependency>
	<dependency>
		<groupId>org.apache.struts</groupId>
		<artifactId>struts2-convention-plugin</artifactId>
		<version>${org.struts-version}</version>
	</dependency>

	<dependency>
	<groupId>org.apache.struts</groupId>
	<artifactId>struts2-spring-plugin</artifactId>
	<version>${org.struts-version}</version>
	<!-- struts2-spring-plugin 依赖的是SPRING3,会与SPRING4冲突,需要排除 -->
	<exclusions>
		<exclusion>
			<groupId>org.springframework</groupId>
			<artifactId>spring-beans</artifactId>
		</exclusion>
		<exclusion>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
		</exclusion>
		<exclusion>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
		</exclusion>
		<exclusion>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
		</exclusion>

	</exclusions>
	</dependency>

	<!-- spring4-->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${org.springframework-version}</version>
    </dependency>
    <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${org.springframework-version}</version>
        </dependency>
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-orm</artifactId>
		<version>${org.springframework-version}</version>
	</dependency>
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-web</artifactId>
		<version>${org.springframework-version}</version>
	</dependency>
	<!-- spring aop 需要 -->
	<dependency>
		<groupId>org.aspectj</groupId>
		<artifactId>aspectjweaver</artifactId>
		<version>1.7.0</version>
	</dependency>
	<!-- hibernate -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>${org.hibernate-version}</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>${org.hibernate-version}</version>
    </dependency>
	<!-- db -->
	<dependency>
			<groupId>c3p0</groupId>
			<artifactId>c3p0</artifactId>
			<version>0.9.1.2</version>
		</dependency>
	<dependency>
			<groupId>oracle</groupId>
			<artifactId>oracle-10g</artifactId>
			<version>1.0</version>
	</dependency>
	<dependency>
			<groupId>commons-dbcp</groupId>
			<artifactId>commons-dbcp</artifactId>
			<version>1.2.2</version>
	</dependency>
  </dependencies>
  <build>
    <finalName>ssh</finalName>
  </build>
</project>

下面是项目结构:

Spring配置文件:

ApplicationContext.xml

<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"
	xmlns:aop="http://www.springframework.org/schema/aop"
	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/tx
						http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
						http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">

	<!-- 注解配置 -->
	<context:annotation-config />
	<context:component-scan base-package="com.ssh"></context:component-scan>
	<context:property-placeholder location="classpath:jdbc.properties"/>
	<!-- HIBERNATE配置 -->
	<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSource"></property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
			</props>
		</property>
		<property name="packagesToScan">
			<list>
				<value>com.ssh.domain</value>
			</list>
		</property>
	</bean>
	<!-- 事务配置 -->
	<bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	<!--
	 -->
	<tx:advice id="txAdvice" transaction-manager="txManager">
		<tx:attributes >
			 <tx:method name="get*" read-only="true" propagation="REQUIRED" />
		</tx:attributes>
	</tx:advice>
	 <aop:config>
	 	<aop:pointcut expression="execution(* com.ssh.server.*.*(..))" id="point"/>
	 	<aop:advisor advice-ref="txAdvice" pointcut-ref="point"/>
	 </aop:config>

</beans>

dataSource.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd">

	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
  		<property name="driverClass" value="${db.driver}"/>
  		<property name="jdbcUrl" value="${db.url}"/>
		<property name="user" value="${db.username}"/>
		<property name="password" value="${db.password}"/>
        <property name="maxPoolSize" value="40"></property>
        <property name="minPoolSize" value="1"></property>
        <property name="initialPoolSize" value="1"></property>
        <property name="maxIdleTime" value="20"></property>
	</bean>

</beans>

JDBC.properties

db.driver=oracle.jdbc.driver.OracleDriver
db.url=jdbc\:oracle\:thin\:@127.0.0.1\:1521\:orcl
db.username=username
db.password=password

Struts配置:

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.convention.default.parent.package" value="default-package" />
	<package name="default-package" extends="convention-default"></package>
    <constant name="struts.convention.result.path" value="/" />
    <constant name="struts.convention.package.locators" value="action"/>
    <constant name="struts.convention.action.name.separator" value="-"/> 

    <!-- 将STRUTS 交给SPRING管理 -->
	<constant name="struts.objectFactory" value="spring"></constant>
	<!-- 后缀为.ACTION的全部交给STRUTS处理 -->
	<constant name="struts.action.extension" value="action"></constant>
	<!-- struts 动态方法调用 -->
	<constant name="struts.enable.DynamicMethodInvocation" value="true" />
	<!-- Action名称中是否还是用斜线 -->
	<constant name="struts.enable.SlashesInActionNames" value="false" />
</struts>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="starter" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

  <display-name>ssh</display-name>
  <context-param>
  	<param-name>contextConfigLocation</param-name>
  	<param-value>/WEB-INF/classes/spring/**/*.xml</param-value>
  </context-param>
  <listener>
  	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <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>*.action</url-pattern>
	</filter-mapping>

</web-app>

这样配置文件就已经配置好了,之后所有POJO、SERVICE、ACTION都是通过注解来进行配置

User.java

package com.ssh.domain;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="PERSON")
public class User {
	private String id;
	private String name;
	private String password;

	@Id
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	@Column(name="NAME")
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	@Column(name="PASSWORD")
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
}

CommonDao.java

package com.ssh.dao;

import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class CommonDao<T> {
	@Autowired
	private SessionFactory sessionFactory;

	public List<T> query(String hql){
		Query query = sessionFactory.getCurrentSession().createQuery(hql);
		return query.list();
	}

	public void delete(T t){
		sessionFactory.getCurrentSession().delete(t);
	}

	public void update(T t){
		sessionFactory.getCurrentSession().update(t);
	}

	public void save(T t){
		sessionFactory.getCurrentSession().save(t);
	}
}

LoginImpl.java

package com.ssh.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ssh.dao.CommonDao;
import com.ssh.domain.User;
import com.ssh.service.LoginService;
@Service("loginService")
public class LoginImpl implements LoginService<User>{

	@Autowired
	private CommonDao<User>  dao;

	public List<User> getData() {
		String hql = "select t from User t";
		return dao.query(hql);
	}
}

LoginAction.java

package com.ssh.action;

import java.util.List;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionSupport;
import com.ssh.domain.User;
import com.ssh.service.LoginService;

@Namespace("/Login")
@ParentPackage("struts-default")
@Results({
	@Result(name="login",location="/index.jsp")
})
public class LoginAction extends ActionSupport{

	private static final long serialVersionUID = -2420332025324729781L;
	private LoginService<User> service;
	@Autowired
	public void setService(LoginService<User> service) {
		this.service = service;
	}

	public String login(){
		List<User> users = service.getData();
		ServletActionContext.getRequest().setAttribute("user", users);
		return "login";
	}
}

前台面页面访问login方法使用  /ssh/Login/login.action

完。

spring4.06 hibernate4.31 struts2.3.16 全注解MAVEN环境搭建

时间: 2024-12-24 23:48:05

spring4.06 hibernate4.31 struts2.3.16 全注解MAVEN环境搭建的相关文章

(六)Spring4 整合Hibernate4,Struts2

第一节:S2SH 整合所需Jar 包 Struts2.3.16,Spring4.0.6,Hibernate4.3.5 整合所需jar 包: Struts2.3.16 jar 包 Spring4.0.6 jar 包 Hibernate4.3.5 jar 包 第二节:Spring4 整合Hibernate4 Spring4 接管Hibernate4 所有Bean 实例,以及SessionFactory,事务管理器: 泛型注入: 第三节:Spring4 整合Struts2 Spring4 接管Stru

16.Java Web 项目环境搭建

如上图 Gif 演示,在 Eclipse 下,一般中小项目的项目结构基本都是这种模式的,所以我们这里也网络上一个开源项目 JspRun 进行讲解. 下载地址:http://pan.baidu.com/s/1i3zrSf7 Eclipse 项目配置 如上图箭头所示,在 IntelliJ IDEA 下是支持直接打开 Eclipse 项目的,无需导入. 如上图标注 1 所示,选择项目的 .project 文件打开即可. 如上图标注 1 所示,IntelliJ IDEA 能自动识别到可编译的 src 类

struts2.3.16 整合spring4.0.5 和 hibernate4.3.0

1.由struts2 框架自身根据struts.xml 中 的映射实例化Action 对象 Action 类代码如下: package com.hasonger.ssh.action; import java.util.Date; import com.hasonger.ssh.entity.User; import com.hasonger.ssh.service.UserService; import com.opensymphony.xwork2.ActionSupport; import

[转]Struts2.3.16.1+Hibernate4.3.4+Spring4.0.2 框架整合

原文地址:http://blog.csdn.net/ycb1689/article/details/22928519 最新版Struts2+Hibernate+Spring整合 目前为止三大框架最新版本是: struts2.3.16.1 hibernate4.3.4 spring4.0.2 其中struts2和hibernate的下载方式比较简单,但是spring下载有点麻烦,可以直接复制下面链接下载最新版spring http://repo.springsource.org/libs-rele

Struts2.3.16.1+Hibernate4.3.4+Spring4.0.2 框架整合

最新版Struts2+Hibernate+Spring整合 目前为止三大框架最新版本是: struts2.3.16.1 hibernate4.3.4 spring4.0.2 其中struts2和hibernate的下载方式比较简单,但是spring下载有点麻烦,可以直接复制下面链接下载最新版spring http://repo.springsource.org/libs-release-local/org/springframework/spring/4.0.2.RELEASE/spring-f

Spring4+MVC+Hibernate4全注解环境搭建(一)

声明: 以下任何观点.理解,都有可能是错的,那仅代表作者在某一时刻结合自己的学习经历和思考得出的观点,很明显,这样的前提下很多都可能是错的.请各位在看到任何可疑观点时,都不要轻信,如果你们在喷我的时候能把理由一并说出来,那我就非常感激了.像什么“你懂的”,“当然是!不然还能是什么.”那样的话恐怕既说服不了我,也说服不了别人. 目前为止我对这几个框架认识: 我的理解不一定对,但是我还是在此首先明确一下我为什么选择的是Spring4+MVC+Hibernate4. Spring就是用来提供一个IoC

基于IDEA 最新Spirng3.2+hibernate4+struts2.3 全注解配置 登录

原文 基于IDEA 最新Spirng3.2+hibernate4+struts2.3 全注解配置 登录 首先说说 IDEA 12,由于myeclipse越来越卡,我改用idea12 了,发现其功能强悍到eclipse无法比拟,此款ide理解上下文是一等一的,不用什么jquery插件,extjs插件,都可以实现全智能的代码提示.速度什么的都比myeclipse快.而且IDEA提供了android支持struts spring hibernate 支持.下面,我讲教大家完成一个基于全注解的Spirn

spring mvc4.1.6 + spring4.1.6 + hibernate4.3.11 + mysql5.5.25 开发环境搭建及相关说明

一.准备工作 开始之前,先参考上一篇: struts2.3.24 + spring4.1.6 + hibernate4.3.11 + mysql5.5.25 开发环境搭建及相关说明 思路都是一样的,只不过把struts2替换成了spring mvc 二.不同的地方 工程目录及jar包: action包改成controller: 删除struts2 jar包,添加spring mvc包(已有的话,不需添加):     web.xml配置: 跟之前不同的地方是把struts2的过滤器替换成了一个se

springmvc3.2+spring+hibernate4全注解方式整合(一)

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