注:附一下源码下载地址吧,是Eclipse项目,如果源码有什么问题可以提出,点击进入下载链接
最近使用目录最新版本的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="query*" 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
完。