`
tianlihu
  • 浏览: 313972 次
  • 性别: Icon_minigender_1
  • 来自: 石家庄
社区版块
存档分类
最新评论

Struts+Spring+Hibernate注解零配置整合

    博客分类:
  • SSH
阅读更多
个人努力,请不要转载,收藏就好

本工程为Maven工程,可以下载maven-eclipse插件进行项目导入
如果在eclipse中用WTP插件配置Tomcat运行,需要把maven项目转成Eclipse工程
请下载附件,解压后在工程文件夹运行以下maven命令:
call mvn eclipse:eclipse -Dwtpversion=2.0


以下是工程的pom.xml文件,用来管理所有依赖的jar包:
<?xml version="1.0" encoding="GBK"?>
<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>com.tianlihu.projects</groupId>
	<artifactId>S2S3H3_demo</artifactId>
	<version>1.0.0</version>
	<packaging>war</packaging>

	<properties>
		<springframe.version>3.1.4.RELEASE</springframe.version>
		<unitils.version>3.1</unitils.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>xalan</groupId>
			<artifactId>xalan</artifactId>
			<version>2.7.1</version>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>servlet-api</artifactId>
			<version>2.5</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jsp-api</artifactId>
			<version>2.0</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>org.apache.struts</groupId>
			<artifactId>struts2-core</artifactId>
			<version>2.3.8</version>
		</dependency>
		<dependency>
			<groupId>org.apache.struts</groupId>
			<artifactId>struts2-convention-plugin</artifactId>
			<version>2.3.8</version>
		</dependency>
		<dependency>
			<groupId>org.apache.struts</groupId>
			<artifactId>struts2-spring-plugin</artifactId>
			<version>2.3.8</version>
		</dependency>
		<dependency>
			<groupId>org.apache.struts</groupId>
			<artifactId>struts2-tiles-plugin</artifactId>
			<version>2.3.8</version>
		</dependency>
		<dependency>
			<groupId>org.apache.tiles</groupId>
			<artifactId>tiles-jsp</artifactId>
			<version>2.0.4</version>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jstl</artifactId>
			<version>1.1.0</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>${springframe.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aop</artifactId>
			<version>${springframe.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context-support</artifactId>
			<version>${springframe.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>${springframe.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aspects</artifactId>
			<version>${springframe.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>${springframe.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-beans</artifactId>
			<version>${springframe.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jdbc</artifactId>
			<version>${springframe.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-asm</artifactId>
			<version>${springframe.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-tx</artifactId>
			<version>${springframe.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>${springframe.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-orm</artifactId>
			<version>${springframe.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-struts</artifactId>
			<version>${springframe.version}</version>
		</dependency>

		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-core</artifactId>
			<version>3.6.10.Final</version>
		</dependency>
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-ehcache</artifactId>
			<version>3.6.10.Final</version>
		</dependency>

		<dependency>
			<groupId>aopalliance</groupId>
			<artifactId>aopalliance</artifactId>
			<version>1.0</version>
		</dependency>
		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjrt</artifactId>
			<version>1.6.9</version>
		</dependency>
		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjweaver</artifactId>
			<version>1.6.9</version>
		</dependency>
		<dependency>
			<groupId>cglib</groupId>
			<artifactId>cglib</artifactId>
			<version>2.2</version>
		</dependency>

		<dependency>
			<groupId>c3p0</groupId>
			<artifactId>c3p0</artifactId>
			<version>0.9.1.2</version>
		</dependency>
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.23</version>
		</dependency>

		<dependency>
			<groupId>commons-dbcp</groupId>
			<artifactId>commons-dbcp</artifactId>
			<version>1.4</version>
		</dependency>
		<dependency>
			<groupId>commons-lang</groupId>
			<artifactId>commons-lang</artifactId>
			<version>2.5</version>
		</dependency>
		<dependency>
			<groupId>commons-io</groupId>
			<artifactId>commons-io</artifactId>
			<version>2.0</version>
		</dependency>
		<dependency>
			<groupId>commons-httpclient</groupId>
			<artifactId>commons-httpclient</artifactId>
			<version>3.1</version>
		</dependency>
		<dependency>
			<groupId>commons-beanutils</groupId>
			<artifactId>commons-beanutils</artifactId>
			<version>1.8.3</version>
		</dependency>
		<dependency>
			<groupId>commons-collections</groupId>
			<artifactId>commons-collections</artifactId>
			<version>3.2.1</version>
		</dependency>

		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.8.1</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
			<version>${springframe.version}</version>
			<scope>test</scope>
		</dependency>

		<dependency>
			<groupId>org.unitils</groupId>
			<artifactId>unitils-core</artifactId>
			<version>${unitils.version}</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.unitils</groupId>
			<artifactId>unitils-dbunit</artifactId>
			<version>${unitils.version}</version>
			<scope>test</scope>
			<exclusions>
				<exclusion>
					<groupId>org.hibernate</groupId>
					<artifactId>hibernate</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		<dependency>
			<groupId>org.unitils</groupId>
			<artifactId>unitils-easymock</artifactId>
			<version>${unitils.version}</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.unitils</groupId>
			<artifactId>unitils-spring</artifactId>
			<version>${unitils.version}</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.unitils</groupId>
			<artifactId>unitils-mock</artifactId>
			<version>${unitils.version}</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.unitils</groupId>
			<artifactId>unitils-inject</artifactId>
			<version>${unitils.version}</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.dbunit</groupId>
			<artifactId>dbunit</artifactId>
			<version>2.4.8</version>
			<scope>test</scope>
		</dependency>

		<dependency>
			<groupId>ch.qos.logback</groupId>
			<artifactId>logback-classic</artifactId>
			<version>0.9.28</version>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>2.3.2</version>
				<configuration>
					<source>1.6</source>
					<target>1.6</target>
					<compilerVersion>1.6</compilerVersion>
					<encoding>GBK</encoding>
					<fork>true</fork>
					<meminitial>128m</meminitial>
					<maxmem>512m</maxmem>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-resources-plugin</artifactId>
				<version>2.6</version>
				<configuration>
					<encoding>GBK</encoding>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>


以下是web.xml,已经配置了Struts和Spring:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

	<!-- 加载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>

	<filter>
		<filter-name>encodingFilter</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>GBK</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>encodingFilter</filter-name>
		<url-pattern>*</url-pattern>
	</filter-mapping>

	<!-- 指明spring配置文件在何处 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath*:applicationContext.xml</param-value>
	</context-param>
	<!-- 加载spring配置文件applicationContext.xml -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>


Spring主配置文件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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
         http://www.springframework.org/schema/context 
         http://www.springframework.org/schema/context/spring-context-3.0.xsd">

	<context:annotation-config />
	<context:component-scan base-package="com.tianlihu.projects.ssh" />

	<context:property-placeholder location="classpath:hibernate.properties"/>
	<import resource="classpath:hibernate.cfg.xml"/>
</beans>


Struts用默认配置,因此不放置struts.xml

Hibernate的Spring配置hibernate.cfg.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">

	<!-- C3P0 数据源 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="${hibernate.connection.driver_class}" />
		<property name="jdbcUrl" value="${hibernate.connection.url}" />
		<property name="user" value="${hibernate.connection.username}" />
		<property name="password" value="${hibernate.connection.password}" />
		<property name="initialPoolSize" value="${hibernate.connection.initialPoolSize}" />
		<property name="minPoolSize" value="${hibernate.connection.minPoolSize}" />
		<property name="maxPoolSize" value="${hibernate.connection.maxPoolSize}" />
		<property name="preferredTestQuery" value="select 1 from dual " />
	</bean>

	<!-- SessionFactory -->
	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="packagesToScan" value="com.tianlihu.projects.ssh.*.po" />
		<property name="useTransactionAwareDataSource" value="true" />
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">${hibernate.dialect}</prop>
				<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
				<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
				<prop key="hibernate.temp.use_jdbc_metadata_defaults">${hibernate.temp.use_jdbc_metadata_defaults}</prop>
				<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
				<prop key="hibernate.cache.provider_class">${hibernate.cache.provider_class}</prop>
				<prop key="hibernate.cache.use_query_cache">${hibernate.cache.use_query_cache}</prop>
				<prop key="hibernate.cache.use_second_level_cache">${hibernate.cache.use_second_level_cache}</prop>
				<prop key="hibernate.connection.autocommit">false</prop>
				<prop key="hibernate.current_session_context_class">thread</prop>
			</props>
		</property>
	</bean>

	<!-- 配置事务管理 -->
	<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>

	<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>

	<aop:aspectj-autoproxy />
	<tx:annotation-driven />
</beans>


以下这句用来指明,数据事务用Spring的声明式事务,即使用@Transactional注解
<tx:annotation-driven />


数据库的属性配置文件hibernate.properties如下:
## hibernate
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.show_sql=true
hibernate.format_sql=true
hibernate.hbm2ddl.auto=create-drop
hibernate.temp.use_jdbc_metadata_defaults=false

## hibernate cache
hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
hibernate.cache.use_query_cache=false
hibernate.cache.use_second_level_cache=true

## C3P0 configuration
hibernate.connection.driver_class=com.mysql.jdbc.Driver
hibernate.connection.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=GBK
hibernate.connection.username=root
hibernate.connection.password=root
hibernate.connection.initialPoolSize=1
hibernate.connection.minPoolSize=1
hibernate.connection.maxPoolSize=3


logback日志配置文件logback.xml如下:
<?xml version="1.0" encoding="GBK"?>
<configuration>
	<appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
		<encoder>
			<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-4relative [%thread] %-5level %logger{36} - %msg %n </pattern>
		</encoder>
	</appender>
	<appender name="logfile" class="ch.qos.logback.core.rolling.RollingFileAppender">
		<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
			<FileNamePattern>
				logs/ssh.%d{yyyy-MM-dd-HH}.log
	   		</FileNamePattern>
			<MaxHistory>10</MaxHistory>
		</rollingPolicy>
		<layout class="ch.qos.logback.classic.PatternLayout">
			<Pattern>
				<![CDATA[
				%d{yyyy-MM-dd HH:mm:ss.SSS} %-4relative [%thread] %-5level %logger{36} - %msg %n
				]]>
			</Pattern>
		</layout>
	</appender>
	<logger name="root" level="ERROR">
		<appender-ref ref="stdout" />
		<appender-ref ref="logfile" />
	</logger>
	<logger name="java.sql" level="DEBUG">
		<appender-ref ref="stdout" />
		<appender-ref ref="logfile" />
	</logger>
	<logger name="com.tianlihu" level="DEBUG">
		<appender-ref ref="stdout" /> 
		<appender-ref ref="logfile" />
	</logger>	
</configuration>


以上是所有的配置文件。
JSP代码不再贴出,请下载附件。

以下是java程序代码
用于接收请求的Struts的Action类:UserAction.java
package com.tianlihu.projects.ssh.account.action;

import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.tianlihu.projects.ssh.account.po.User;
import com.tianlihu.projects.ssh.account.service.UserService;

@ParentPackage("struts-default")
@Namespace("/account")
@Component
public class UserAction {

	@Autowired
	private UserService userService;

	private List<User> users;
	private String userId;
	private String name;
	private String password;
	private String age;

	private String message;

	@Action(value = "createUser", results = { @Result(name = "success", location = "/WEB-INF/userForm.jsp") })
	public String createUser() {
		message = "创建用户";
		return "success";
	}

	@Action(value = "create", results = { @Result(name = "success", type = "redirect", params = { "encode", "true" }, location = "fetchUsers?message=${message}") })
	public String create() {
		User user = getUser();
		userService.createUser(user);
		message = "创建用户'" + userId + "'成功";
		return "success";
	}

	@Action(value = "updateUser", results = { @Result(name = "success", location = "/WEB-INF/userForm.jsp") })
	public String updateUser() {
		User user = userService.findByUserId(userId);
		name = user.getName();
		password = user.getPassword();
		age = String.valueOf(user.getAge());
		message = "修改用户'" + userId + "'";
		return "success";
	}

	@Action(value = "update", results = { @Result(name = "success", type = "redirect", params = { "encode", "true" }, location = "fetchUsers?message=${message}") })
	public String update() {
		User user = getUser();
		userService.updateUser(user);
		message = "修改用户'" + userId + "'成功";
		return "success";
	}

	@Action(value = "delete", results = { @Result(name = "success", type = "redirect", params = { "encode", "true" }, location = "fetchUsers?message=${message}") })
	public String deleteUser() {
		userService.deleteUser(userId);
		message = "删除用户'" + userId + "'成功";
		return "success";
	}

	@Action(value = "fetchUsers", results = { @Result(name = "success", location = "/WEB-INF/users.jsp") })
	public String fetchUsers() {
		users = userService.findAll();
		return "success";
	}

	private User getUser() {
		User user = new User();
		user.setUserId(userId);
		user.setName(name);
		user.setPassword(password);
		user.setAge(Integer.parseInt(age));
		return user;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

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

	public String getAge() {
		return age;
	}

	public void setAge(String age) {
		this.age = age;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public List<User> getUsers() {
		return users;
	}

	public void setUsers(List<User> users) {
		this.users = users;
	}
}


处理逻辑的Service:UserService.java
因为Service只是处理增删改查,没有验证数据,所以比较简单,值得注意的是,@Transactional事务注解只能写在Service层,不能写在Action层,不然会出错,估计是对Spring3对Struts2支持的不太好
package com.tianlihu.projects.ssh.account.service;

import java.util.List;

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

import com.tianlihu.projects.ssh.account.dao.GeneralDao;
import com.tianlihu.projects.ssh.account.po.User;

@Service
public class UserService {

	@Autowired
	private GeneralDao generalDao;

	public User findByUserId(String userId) {
		return generalDao.findById(User.class, userId);
	}

	public List<User> findAll() {
		return generalDao.findAll(User.class);
	}

	@Transactional
	public void createUser(User user) {
		generalDao.save(user);
	}

	@Transactional
	public void updateUser(User user) {
		generalDao.update(user);
	}

	@Transactional
	public void deleteUser(String userId) {
		generalDao.deleteById(User.class, userId);
	}

	@Transactional
	public void deleteUser(User user) {
		generalDao.delete(user);
	}
}


Hibernate的PO对象:User.java
不再需要用XML进行表和实体类的映射了
package com.tianlihu.projects.ssh.account.po;

import java.io.Serializable;

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

@Entity(name = "AC_USER")
public class User implements Serializable {

	private static final long serialVersionUID = -1376216905288707683L;

	@Id
	@Column(name = "USER_ID", length =50, nullable = false)
	private String userId;
	@Column(name = "NAME", length = 50, nullable = false)
	private String name;
	@Column(name = "PASSWORD", length = 50, nullable = false)
	private String password;
	@Column(name = "AGE", length = 20, nullable = false)
	private int age;

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

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

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}
}


数据存储层通用DAO接口定义:GeneralDao.java
package com.tianlihu.projects.ssh.account.dao;

import java.io.Serializable;
import java.util.List;

public interface GeneralDao {

	public <T> T findById(Class<T> type, Serializable id);

	public <T> List<T> findAll(Class<T> type);

	public void save(Object... entities);

	public void update(Object... entities);

	public void saveOrUpdate(Object entity);

	public void delete(Object... entities);

	public void deleteById(Class<?> type, Serializable id);

	public void refresh(Object... entities);

	public void flush();
}


DAO的实现类:GeneralDAOImpl.java
package com.tianlihu.projects.ssh.account.dao;

import java.io.Serializable;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;

@Repository
public class GeneralDAOImpl implements GeneralDao {

	@Autowired
	private HibernateTemplate hibernateTemplate;

	@Override
	public <T> T findById(Class<T> type, Serializable id) {
		return hibernateTemplate.get(type, id);
	}

	@Override
	public <T> List<T> findAll(Class<T> type) {
		return hibernateTemplate.loadAll(type);
	}

	@Override
	public void save(Object... entities) {
		for (Object entity : entities) {
			hibernateTemplate.save(entity);
		}
	}

	@Override
	public void saveOrUpdate(Object entity) {
		hibernateTemplate.saveOrUpdate(entity);
	}

	@Override
	public void update(Object... entities) {
		for (Object entity : entities) {
			hibernateTemplate.update(entity);
		}
	}

	@Override
	public void delete(Object... entities) {
		for (Object entity : entities) {
			if (entity != null) {
				hibernateTemplate.delete(entity);
			}
		}
	}

	@Override
	public void deleteById(Class<?> type, Serializable id) {
		if (id == null) {
			return;
		}
		Object entity = findById(type, id);
		if (entity == null) {
			return;
		}
		delete(entity);
	}

	@Override
	public void refresh(Object... entities) {
		for (Object entity : entities) {
			hibernateTemplate.refresh(entity);
		}
	}

	@Override
	public void flush() {
		hibernateTemplate.flush();
	}
}



这就是整个搭建框架,是一个内核。在应用时,可以配置freemarker之类的其余框架进来,用于实际开发

纯注解形式的代码有助于代码开发和管理,很代码更整洁,测试代码也可以用注解形式,也方便测试。因为注解形式把配置和代码放在一起,维护比较方便,减少了配置和代码的同步,也减少了代码量,使BUG数降低。个人推荐使用注解形式的配置

注解形式的代码,可以算一程契约形式的编程,只关心我想做什么,不关心怎么做,代码的耦合比较小,模块化比较强。JDK7也在主推模块化,这两个方向是以后的发展趋势
分享到:
评论
3 楼 chico 2015-06-08  
一直对maven了解不多,准备借鉴一下。
2 楼 jy34521 2014-03-18  
谢谢 ,学到很多东西
1 楼 di1984HIT 2013-05-01  
恩,写的不错。我借鉴一下。

相关推荐

    struts1.2 + spring2.5 + hibernate3.2框架demo

    2. **配置文件**:struts-config.xml定义Struts的配置,spring-beans.xml管理Spring的bean,hibernate.cfg.xml配置Hibernate的数据库连接,可能还有实体类的映射文件(hbm.xml或使用注解)。 3. **JSP页面**:展示...

    Struts2+Spring3+Hibernate4零配置所需全部jar包

    例如,Struts2的struts-default.xml、Spring的applicationContext.xml和Hibernate的hibernate.cfg.xml等,都已经被预设好或者通过注解方式进行配置。 在实际开发中,这三个框架的集成可以带来诸多优势:Struts2负责...

    struts+spring+hibernate三大框架整合

    SSH三大框架,即Struts、Spring和Hibernate,是Java Web开发中的常用技术组合,它们各自负责不同的职责:Struts作为MVC模式中的Controller层,处理用户请求并转发到相应的业务逻辑;Spring则作为核心容器,负责依赖...

    Struts2.3.28+spring4.1.6+hibernate4.3.8整合总结(注解)

    Struts2.3.28、Spring4.1.6和Hibernate...以上就是Struts2.3.28、Spring4.1.6和Hibernate4.3.8整合的关键知识点,以及注解在整合中的作用。理解并熟练掌握这些内容,能够帮助开发者更有效地构建和维护Java EE应用程序。

    Struts+Spring+Hibernate开发实例.pdf

    **整合Struts+Spring+Hibernate** 整合这三个框架意味着将它们各自的功能结合起来,实现Web应用的全栈开发。Struts用于前端的请求处理和视图页面的跳转,Spring管理业务逻辑和事务处理,并且通过依赖注入提供资源...

    Struts+Spring+Hibernate 整合教程.pdf

    ### Struts+Spring+Hibernate 整合教程知识点详解 #### 一、SSH整合理念与背景 **1.1 框架概述** 在构建复杂的Web应用程序时,开发人员需要面对诸多挑战,包括如何构建用户界面(UI)、业务逻辑(BL)的存放位置以及...

    struts2+spring2.5+hibernate3.2 + freemarker 全新功能实现的增删改查+freemarker 摸版

    struts2+spring2.5+hibernate3.2 + freemarker 全新功能实现的增删改查+freemarker 摸版 struts2 的方式自己去看简单。 spring2.5 是用注释来注入 hibernate3.2 是用ejb3注解映射关系 hibernate3 +个属性可以自动...

    整合struts+spring+hibernate实例

    3. **集成Spring**:在Struts的Action类中,使用Spring的注解或XML配置文件声明依赖,让Spring IoC容器管理这些对象。同时,配置Spring的ApplicationContext,使Struts和Spring能共享同一个上下文。 4. **配置...

    Struts2+Spring4+Hibernate5整合

    整合Struts2、Spring4和Hibernate5,通常遵循以下步骤: 1. **配置环境**:确保项目中包含这三个框架的库文件,并在pom.xml或build.gradle中添加相应的依赖。 2. **配置Struts2**:在struts.xml文件中定义Action类...

    SSH(Struts+Spring+Hibernate)结合项目简单实例

    SSH(Struts+Spring+Hibernate)是Java Web开发中一种常见的技术栈,它将MVC设计模式、依赖注入和持久层框架集成为一个整体,大大提高了开发效率和代码的可维护性。下面我们将深入探讨SSH框架的各个组成部分及其结合...

    整合Struts+Spring+Hibernate简单例子开发

    4. **整合步骤**: 在web.xml中配置Spring的ContextLoaderListener和Struts的ActionServlet,确保在Web应用启动时加载Spring上下文,并在处理请求时使用Struts的控制器。 5. **测试应用**: 创建JSP页面作为视图,...

    Struts+Spring+Hibernate+Ajax的Demo

    Hibernate通过配置文件或注解将Java实体类与数据库表关联,提供了透明的数据持久化。在这个Demo中,Hibernate可能被用于处理数据库的CRUD(创建、读取、更新、删除)操作,简化数据库访问。 **Ajax**(Asynchronous...

    struts2.2.3+spring3.0.1+hibernate3.6.5整合jar包

    4. **整合配置**:在Struts2的配置文件中引入Spring的Action类,使Struts2能够利用Spring的DI特性创建Action实例。 5. **事务管理**:通常使用Spring的PlatformTransactionManager来管理事务,确保数据库操作的一致...

    struts2+spring2.5+Hibernate3.2整合示例

    Struts2、Spring和Hibernate是Java Web开发中的三大框架,它们的整合应用极大地提升了开发效率和项目的可维护性。在本示例中,我们将探讨如何将这三个框架集成在一起,实现一个完整的MVC(模型-视图-控制器)架构。 ...

    基于Struts2+hibernate+spring 注解开发的学生信息管理系统

    【SSH 注解开发】是Java Web开发中一种常见的技术栈组合,由Struts2、Hibernate和Spring框架共同构建。这个学生信息管理系统就是基于这种技术栈,利用注解的方式来简化配置,提高开发效率。 Struts2作为MVC(模型-...

    struts1+spring+hibernate整合所用全部jar包

    Struts1、Spring和Hibernate是Java Web开发中的三大框架,它们各自解决了一部分问题,而将它们整合在一起,可以构建出高效、灵活的企业级应用程序。这里我们主要探讨这三者整合时所需的核心jar包以及它们的功能。 ...

    struts2+spring+hibernate 整合的jar包

    Struts2、Spring和Hibernate是Java Web开发中的三大框架,它们的整合(SSH)极大地提升了开发效率和项目的可维护性。下面将详细讲解这三大框架的核心功能以及整合过程中的关键知识点。 1. **Struts2**:Struts2是一...

    Spring+Struts2+hibernate+Redis整合

    在IT行业中,SSH(Spring、Struts2、Hibernate)是一个经典的Java Web开发框架组合,而Redis则是一个高性能的键值存储系统,常用于缓存和数据持久化。将SSH与Redis整合,可以提升应用程序的性能和响应速度。下面将...

Global site tag (gtag.js) - Google Analytics