Struts2.1+Spring3.0+JPA1.0(Hibernate3.3实现)例子

阅读更多

本文代码是传智播客黎活明老师所讲的《Spring2.5视频教程》各种示例的综合,在此把所有框架升级到最新版本整合一下。

 

所用jar包:

 

Struts:

struts-2.1.8.1

\lib下除了各种plugin(保留struts2-spring-plugin-2.1.8.1.jar)的所有文件

 

Spring:

spring-framework-3.0.0.RELEASE

\dist下的所有文件

 

Hibernate:

hibernate-distribution-3.3.2.GA下的hibernate3.jar

\lib\bytecode\cglib下的cglib-2.2.jar

\lib\optional\ehcache下的ehcache-1.2.3.jar

\lib\required下的所有文件

hibernate-annotations-3.4.0.GA下的hibernate-annotations.jar

\lib下的ejb3-persistence.jar,hibernate-commons-annotations.jar

hibernate-entitymanager-3.4.0.GA下的hibernate-entitymanager.jar

\lib\test下的log4j.jar,slf4j-log4j12.jar

 

MySQL:mysql-connector-java-5.1.10-bin.jar

 

JUnit:4

JDK:jdk-6u14-windows-i586.exe

Tomcat:apache-tomcat-6.0.18.zip

 

项目名:test

Person.java

package com.test.bean;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
 
@Entity
public class Person implements Serializable {
	private Integer id;
	private String name;
	
	public Person(){}
	
	public Person(String name){
		this.name = name;
	}
	
	@Id @GeneratedValue
	public Integer getId() {
		return id;
	}

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

	@Column(length=10,nullable=false)
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}
	
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		final Person other = (Person) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}
	
}

 PersonService.java

package com.test.service;

import java.util.List;

import com.test.bean.Person;

public interface PersonService {
	
	public void save(Person person);
	
	public void update(Person person);
	
	public void delete(Integer personid);
	
	public Person getPerson(Integer personid);
	
	@SuppressWarnings("unchecked")
	public List getPersons();
	
}

 PersonServiceBean.java

package com.test.service.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.hibernate.exception.SQLGrammarException;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.test.bean.Person;
import com.test.service.PersonService;

@Transactional
public class PersonServiceBean implements PersonService {

	@PersistenceContext EntityManager em;

	public void delete(Integer personid) {
		em.remove(em.getReference(Person.class, personid));

	}

	@Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
	public Person getPerson(Integer personid) {
		return em.find(Person.class, personid);
		
	}

	@Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
	@SuppressWarnings("unchecked")
	public List getPersons() throws SQLGrammarException {
		return em.createQuery("select o from Person o").getResultList();
		
	}

	public void save(Person person) {
		em.persist(person);

	}

	public void update(Person person) {
		em.merge(person);

	}

}

 PersonAction.java

package com.test.web.action;

import java.util.List;

import javax.annotation.Resource;

import com.test.bean.Person;
import com.test.service.PersonService;

public class PersonAction {
	@Resource PersonService personService;
	private String message;
	private List persons;
	private Person person;
	
	public Person getPerson() {
		return person;
	}
	public void setPerson(Person person) {
		this.person = person;
	}
	/**
	 * 人员列表显示
	 */
	public String list(){
		this.persons = personService.getPersons();
		return "list";
	}
	/**
	 * 人员添加界面
	 */
	public String addUI(){
		return "add";
	}
	/**
	 * 人员添加
	 */
	public String add(){
		this.personService.save(this.person);
		this.message="添加成功";
		return "message";
	}
	public List getPersons() {
		return persons;
	}

	public void setPersons(List persons) {
		this.persons = persons;
	}

	public String getMessage() {
		return message;
	}

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

PersonServiceTest.java

package junit.test;

import java.util.List;

import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.test.bean.Person;
import com.test.service.PersonService;

public class PersonServiceTest {
	private static PersonService personService;

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
		try {
			ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
			personService = (PersonService) applicationContext.getBean("personService");
		} catch (RuntimeException e) {
			e.printStackTrace();
		}
	}

	@Test
	public void testSave() {
		personService.save(new Person("小张"));
	}

	@Test
	public void testUpdate() {
		Person person = personService.getPerson(1);
		person.setName("小丽");
		personService.update(person);
	}

	@Test
	public void testGetPerson() {
		Person person = personService.getPerson(2);
		System.out.println(person.getName());
		try {
			System.out.println("请关闭数据库");
			Thread.sleep(1000 * 15);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("第二次开始获取");
		person = personService.getPerson(2);
		System.out.println(person.getName());
	}

	@Test
	public void testDelete() {
		personService.delete(1);
	}

	@Test
	public void testGetPersons() {
		List persons = personService.getPersons();
		for (Person person : persons) {
			System.out.println(person.getName());
		}
	}

} 

META-INF\persistence.xml


  
      
      	
        
		
        
		
        
		
        
		
        
		
        
		
        
		
		
		
		
		
		
		
		
      
  

 src\beans.xml


	
	
      
    
    
  	  
   
 	
	
	

 src\ehcache.xml



    
    
	

src\struts.xml



    
    
    
    
    
    
    
    
    
    
     
    
    
    
 	
 		
 			/page/message.jsp
 		
		
		
			page/personlist.jsp
			page/addperson.jsp
		
		
    

 WebContent\page\addperson.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>


  
   
    人员添加
    
	
	
	    
	
	
	

  
  
  
   	
   		名称:
   		
   	
  

  WebContent\page\message.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>



  
    My JSP 'message.jsp' starting page
    
	
	
	    
	
	
	

  
  
  
     
返回

  WebContent\page\personlist.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>


  
   
    人员列表
    
	
	
	    
	
	
	

  
  
  
   
   	id=,name=
添加

  WebContent\WEB-INF\web.xml


  test
  
  
	   contextConfigLocation
	   classpath:beans.xml
	
	
	
	      org.springframework.web.context.ContextLoaderListener
	
	
		encoding
		org.springframework.web.filter.CharacterEncodingFilter
		
			encoding
			UTF-8
		
	
	
		encoding
		/*
	
	
	        Spring OpenEntityManagerInViewFilter
	        org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter
	 
	 
	        Spring OpenEntityManagerInViewFilter
	        /*
	 
	 
	 
	
        struts2
        org.apache.struts2.dispatcher.FilterDispatcher
    
    
        struts2
        /*
   
	
	
  
    index.jsp
  

 WebContent\index.jsp

<% response.sendRedirect("action_list.do"); %>

你可能感兴趣的:(Hibernate,Struts,Spring,Bean,JSP)