`

struts2.2+spring3.1+hibernate3.2整合方法三

 
阅读更多
beans.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-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/aop
           http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
           http://www.springframework.org/schema/tx 
           http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
	<context:annotation-config />
	<context:component-scan base-package="demo" />
	<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<value>classpath:jdbc.properties</value>
		</property>
	</bean>

	<bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName" value="${jdbc.driverClassName}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
	</bean>
	
	<bean id="sf" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<!-- <property name="annotatedClasses">
			<list><value>com.bjsxt.model.User</value><value>com.bjsxt.model.Log</value></list>
		</property> -->
		 <property name="packagesToScan">
			<list>
				<value>demo.model</value>
			</list>
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">
					org.hibernate.dialect.MySQLDialect
				</prop>
				<prop key="hibernate.show_sql">true</prop>
			</props>
		</property>
	</bean>
	
	<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
		<property name="sessionFactory" ref="sf"></property>
	</bean>

	<bean id="txManager"
		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sf" />
	</bean>

	<aop:config>
		<aop:pointcut id="bussinessService"
			expression="execution(public * demo.service.*.*(..))" />
		<aop:advisor pointcut-ref="bussinessService"
			advice-ref="txAdvice" />
	</aop:config>

	<tx:advice id="txAdvice" transaction-manager="txManager">
		<tx:attributes>
			<tx:method name="exists" read-only="true" />
			<tx:method name="add*" propagation="REQUIRED"/>
		</tx:attributes>
	</tx:advice>

</beans>

struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
	"http://struts.apache.org/dtds/struts-2.1.dtd">

<struts>
	<package name="registration" extends="struts-default">
		<action name="u" class="u">
			<result name="success">/registerSuccess.jsp</result>
			<result name="fail">/registerFail.jsp</result>
			<result name="list">/userlist.jsp</result>
			<result name="load">/user.jsp</result>
		</action>
	</package>
</struts>
	

web.xml
<?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">
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>

	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
		<!-- default: /WEB-INF/applicationContext.xml -->
	</listener>

	<context-param>
		<param-name>contextConfigLocation</param-name>
		<!-- <param-value>/WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml</param-value>  -->
		<param-value>classpath:beans.xml</param-value>
	</context-param>
	
	
	<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>
	
	<filter>
		<filter-name>openSessionInView</filter-name>
		<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
		<init-param>
			<param-name>sessionFactoryBeanName</param-name>
			<param-value>sf</param-value>
		</init-param>
	</filter>
	
	<filter-mapping>
		<filter-name>openSessionInView</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	
	<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>
</web-app>

UserAction.java
package demo.action;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import demo.model.User;
import demo.service.UserManager;
import demo.vo.UserRegisterInfo;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

@Component("u")
@Scope("prototype")
public class UserAction extends ActionSupport implements ModelDriven {
	
	
	private UserRegisterInfo info = new UserRegisterInfo();
	
	private UserManager userManager;
	
	private List<User> users;
	
	private User user;
	
	public UserAction(){
		System.out.println("useraction created!");
	}
	
	@Override
	public String execute() throws Exception {
		User u = new User();
		u.setUsername("aa");
		u.setPassword("aa");
		if(userManager.exists(u)) {
			return "fail";
		}
		userManager.add(u);
		if(userManager.exists(u)) {
			System.out.println("00000000000000000");
			return "fail";
		}
		System.out.println("111111111111111");
		return "success";
	}

	public UserRegisterInfo getInfo() {
		return info;
	}

	public void setInfo(UserRegisterInfo info) {
		this.info = info;
	}
	
	//@Override
	public Object getModel() {
		return info;
	}
	
	public String list() {
		this.users = this.userManager.getUsers();
		return "list";
	}
	
	public List<User> getUsers() {
		return users;
	}

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

	public String load() {
		this.user = this.userManager.loadById(info.getId());
		return "load";
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public UserManager getUserManager() {
		return userManager;
	}
	
	@Resource
	public void setUserManager(UserManager userManager) {
		this.userManager = userManager;
	}
	
}

UserDaoImpl.java
package demo.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Component;

import demo.dao.UserDao;
import demo.model.User;

@Component("userDao")
public class UserDaoImpl implements UserDao {
	
	private HibernateTemplate hibernateTemplate; 
	public void save(User u) {
		hibernateTemplate.save(u);
		
	}

	public boolean checkUserExistsWithName(String username) {
		List<User> users = hibernateTemplate.find("from User u where u.username = '" + username + "'");
		
		
		if(users != null && users.size() > 0) {
			return true;
		}
		return false;
		/*long count = (Long)hibernateTemplate.getSessionFactory()
					.getCurrentSession().createQuery("select count(*) from User u where u.username = :username")
					.setString("username", username).uniqueResult();
		if(count > 0) return true;
		return false;*/
	}

	public HibernateTemplate getHibernateTemplate() {
		return hibernateTemplate;
	}
	
	@Resource
	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}

	public List<User> getUsers() {
		// TODO Auto-generated method stub
		return (List<User>)this.hibernateTemplate.find("from User");
	}

	public User loadById(int id) {
		
		return (User)this.hibernateTemplate.load(User.class, id);
	}

}

User.java
package demo.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class User {
	private int id;
	private String username;
	private String password;
	@Id
	@GeneratedValue
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
	

}

UserManagerImpl.java
package demo.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import demo.dao.UserDao;
import demo.model.User;
import demo.service.UserManager;

@Component("userManager")
public class UserManagerImpl implements UserManager {
	
	private UserDao userDao;
	
	public UserDao getUserDao() {
		return userDao;
	}
	@Resource
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	public boolean exists(User u) throws Exception {
		return userDao.checkUserExistsWithName(u.getUsername());
		
	}
	
	public void add(User u) throws Exception {
		userDao.save(u);
	}

	public List<User> getUsers() {
		return this.userDao.getUsers();
	}

	public User loadById(int id) {
		return this.userDao.loadById(id);
	}
}

HibernateUtil.java
package demo.util;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

public class HibernateUtil {
	private static SessionFactory sf;
	static {
		sf = new AnnotationConfiguration().configure().buildSessionFactory();
	}
	
	public static SessionFactory getSessionFactory() {
		return sf;
	}
}
分享到:
评论

相关推荐

    Struts1.2+Spring2.0+Hibernate3.1 Struts1.2+Spring2.0+Hibernate3.1

    Struts1.2+Spring2.0+Hibernate3.1Struts1.2+Spring2.0+Hibernate3.1Struts1.2+Spring2.0+Hibernate3.1Struts1.2+Spring2.0+Hibernate3.1

    Struts2.2+Spring3.1+Hibernate3.6整合(登录示例及CRUD操作)

    Struts2.2、Spring3.1和Hibernate3.6是经典的Java企业级开发框架组合,常被用于构建高效、可扩展的Web应用程序。这个整合示例提供了登录功能以及基本的CRUD(创建、读取、更新、删除)操作,帮助开发者理解如何将这...

    Struts2.0+Spring2.0+Hibernate3.1登录及增删改查的完整演示示例

    应广大网友需求,自己抽了点时间写了一个Struts2.0+Spring2.0+Hibernate3.1的登录及增删改查的完整演示示例,以此奉献给大家,由于本人水平有限,有写的不好的地方还望大家多提宝贵意见,如果有不明白的地方请关注我的...

    Struts2.3+Spring3.1+Hibernate3.6整合的jar包(全)

    Struts2.3、Spring3.1和Hibernate3.6是三个非常重要的Java Web开发框架,它们的整合在企业级应用开发中极为常见。这个压缩包"Struts2.3+Spring3.1+Hibernate3.6整合的jar包(全)"包含了进行SSH整合所需的全部依赖库,...

    学生管理系统SSH(Struts2+Spring3.1+Hibernate3.6)

    笔者开发时所用到的软件: apache-tomcat-7.0.25.exe eclipse-jee-indigo-SR2-win32.zip mysql-5.5.20-win32.msi navicat100_mysql_en.zip ...适合于初学Struts2+Spring+Hibernate的开发人员参考学习。

    struts2.2.3+spring3.1+hibernate3.67整合

    这个"struts2.2.3+spring3.1+hibernate3.67整合"项目旨在将这三个框架集成在一起,以实现更高效、更灵活的后端开发。 Struts2作为MVC(Model-View-Controller)架构的一部分,主要负责处理用户的请求,并将其映射到...

    ssh Struts2.2+Hibernate3.6+Spring3.1+oracle 分页示例.zip

    在这个"ssh Struts2.2+Hibernate3.6+Spring3.1+oracle 分页示例.zip"压缩包中,开发者提供了一个使用这些技术实现的分页功能示例。下面将详细介绍SSH框架以及Oracle数据库的分页技术。 **Struts2** 是一个基于MVC...

    struts2.3+spring3.1+hibernate3.6整合jar包

    本整合包"struts2.3+spring3.1+hibernate3.6"提供了这三大框架的最新版本,便于开发者进行快速开发。 Struts2是MVC(Model-View-Controller)架构的实现,主要负责处理HTTP请求,提供了一种组织业务逻辑和表现层的...

    struts2.3.4+spring3.1+hibernate4.0整合

    Struts2.3.4、Spring3.1和Hibernate4.0是三个非常重要的Java开源框架,它们在企业级Web应用开发中广泛使用。本文将详细介绍这三个框架的整合过程及其核心概念,帮助开发者理解如何构建一个高效、灵活的Java Web应用...

    Struts1.2+Spring1.2+HIbernate3.1整合示例代码

    Struts1.2+Spring1.2+HIbernate3.1整合一个用户登录的例子,原以为这个很简单没有必要上传上去,但问我的人太多了,所以就发布上来了,我随后会在博客里做详细的介绍,请大家继续关注我的博客.

    Struts2.1.6+Struts2.0+Hibernate3.1整合示例

    目的:给学习SSH(Struts2.1.6+Spring2.0+Hibernate3.1)整合的学生。从使用角度看,Struts2的SSH事例比Struts1.x的SSH简单。至于声明式事务部分,你可以参见另外一个资源“Spring 2.0 + Struts 1.2 + Hibernate 3.2 +...

    SSH整合实例教程Struts1.2 + hibernate3.1 + spring2.0

    eclipse整合Struts1.2 + hibernate3.1 + spring2.0 相关链接:http://blog.csdn.net/shellwin/archive/2010/07/02/5708865.aspx

    Struts2.1+Hibernate3.1+Spring2.5 整合实现增,删,改,查

    Struts2.1、Hibernate3.1和Spring2.5是经典的Java企业级开发框架组合,简称SSH。这个整合项目主要是为了实现基本的数据操作:增加(Create)、删除(Remove)、修改(Update)和查询(Retrieve),也就是CRUD操作。...

    Struts1.2+Struts2.1.6+spring 2.0+hibernate3.1+Ibatis2.3(第二部分)

    Struts1.2+Struts2.1.6+spring 2.0+hibernate3.1+Ibatis2.3 SSH

    SSH:Struts2.2+Hibernate3.6+Spring3.1分页示例

    本示例将重点介绍如何在Struts2.2、Hibernate3.6和Spring3.1集成环境下实现分页功能。 首先,Struts2是Action-based的MVC框架,它提供了一种灵活的方式来处理用户请求,并通过拦截器机制增强了应用的可扩展性。...

    Struts2.3+Spring3.1+Hibernate3.6+HTML5+CSS3技术整合开发

    Struts2.3、Spring3.1和Hibernate3.6是Java企业级应用中常用的三大框架,它们的整合开发能够实现MVC(Model-View-Controller)模式,提供强大的数据持久化、业务逻辑管理和用户界面支持。 1. **Struts2.3**: - **...

    struts2.4+spring3.1+hibernate4.1的SSH框架

    SSH 为 struts2.4+spring3.1+hibernate4.1的一个集成框架,是目前较流行的一种Web应用程序开源框架。  集成SSH框架的系统从职责上分为四层:表示层、业务逻辑层、数据持久层和域模块层,以帮助开发人员在短期内搭建...

    struts1.2+spring2.0+hibernate3.1框架整合实例2

    struts1.2+spring2.0+hibernate3.1框架整合实例 part2 part1地址:http://download.csdn.net/source/1555175

    struts1.2+spring2.0+hibernate3.1框架整合实例

    采用在web.xml中加载spring配置文件的方法降低struts和spring的耦合度,使用自定义的MyDelegatingRequestProcessor类来代替spring的DelegatingRequestProcessor类来解决spring中action的bean的重复配置问题。...

Global site tag (gtag.js) - Google Analytics