【j2ee spring】12、整合SSH框架(终结版)

【j2ee spring】12、整合SSH框架(终结版)

最后,我们把整个项目的截图,代码发一下,大家不想下载那个项目的话,可以在这里看到所有的代码(因为那个项目需要一个下载积分,真不多= =,我觉得我搞了那么久,收点积分应该不过分吧。。。嘿嘿)

这里,我尽量用截图来搞,,免得复制粘贴,怪烦的

一、项目整体截图

二、开始全部代码Person.java

Person.hbm.xml

PersonService.java

package cn.cutter_point.service;import java.util.List;import cn.cutter_point.bean.Person;public interface PersonService {//这个业务bean实现几个方法,保存,更新,删除,获取,获取全部public abstract void save(Person persnon);public abstract void update(Person person);public abstract void delete(Integer personid);public abstract Person getPerson(Integer personid);public abstract List<Person> getPersons();}

PersonServiceBean

/** * 功能:实现SSH的整合hibernate4+spring4+struts2,这个是一个实体bean * 时间:2015年3月28日21:13:10 * author:cutter_point * 文件:Person.java */package cn.cutter_point.service.impl;import java.util.List;import javax.annotation.Resource;import org.hibernate.SessionFactory;import org.springframework.transaction.annotation.Propagation;import org.springframework.transaction.annotation.Transactional;import cn.cutter_point.bean.Person;import cn.cutter_point.service.PersonService;@Transactionalpublic class PersonServiceBean implements PersonService {@Resource//这个就是依赖注入private SessionFactory sessionFactory;//这个业务bean实现几个方法,保存,更新,删除,获取,获取全部@Overridepublic void save(Person person){sessionFactory.getCurrentSession().persist(person);}@Overridepublic void update(Person person){sessionFactory.getCurrentSession().merge(person);}@Overridepublic void delete(Integer personid){sessionFactory.getCurrentSession().delete(sessionFactory.getCurrentSession().get(Person.class, personid));}@Override@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)public Person getPerson(Integer personid){return (Person) sessionFactory.getCurrentSession().get(Person.class, personid);}@SuppressWarnings("unchecked")//取消警告@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)@Overridepublic List<Person> getPersons(){return sessionFactory.getCurrentSession().createQuery("from Person").list();}}

PersonAction.java

/** * 功能:集成ssh框架 * author:cutter_point * 时间:2015年3月29日17:30:07 */package cn.cutter_point.web.action;import javax.annotation.Resource;import javax.servlet.ServletContext;import javax.servlet.http.HttpServletRequest;import org.apache.struts2.ServletActionContext;import org.springframework.web.context.WebApplicationContext;import org.springframework.web.context.support.WebApplicationContextUtils;import cn.cutter_point.bean.Person;import cn.cutter_point.service.PersonService;import com.opensymphony.xwork2.ActionSupport;public class PersonAction extends ActionSupport {@Resource private PersonService personService;//先按名字注入,如果找不到的话就按类型注入private String name;//名字public String getName() {return name;}public void setName(String name) {this.name = name;}public String add() throws Exception{personService.save(new Person(name));return "add";}public String list() throws Exception {/*//获取实例,方法1ServletContext sc = ServletActionContext.getRequest().getSession().getServletContext();WebApplicationContext wac = (WebApplicationContext) sc.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);//方法2WebApplicationContext webApplicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletActionContext.getServletContext());if(wac == webApplicationContext){System.out.println("ok!!!");}PersonService personService = (PersonService) wac.getBean("personServiceBean");*/HttpServletRequest request = ServletActionContext.getRequest();request.setAttribute("persons", personService.getPersons());return "list";}}

PersonServiceTest.java

package junit.test;import static org.junit.Assert.*;import java.util.List;import org.junit.BeforeClass;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import cn.cutter_point.bean.Person;import cn.cutter_point.service.PersonService;public class PersonServiceTest {private static PersonService personService;@BeforeClasspublic static void setUpBeforeClass() throws Exception {try {ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");personService = (PersonService) applicationContext.getBean("personServiceBean");} catch (Exception e) {e.printStackTrace();}}@Testpublic void testSave() {//fail("Not yet implemented");personService.save(new Person("cutter_point"));}@Testpublic void testUpdate() {//fail("Not yet implemented");Person person = personService.getPerson(2);//….person.setName("小丽");personService.update(person);}@Testpublic void testDelete() {//fail("Not yet implemented");personService.delete(9);}@Testpublic void testGetPerson() {Person person = personService.getPerson(2);System.out.println(person.getName());System.out.println("关闭数据区==========");try {Thread.sleep(1000*30);//15秒} catch (InterruptedException e) {e.printStackTrace();}System.out.println("第二次获取数据");person = personService.getPerson(2);System.out.println(person.getName()+" ++++++++++");}@Testpublic void testGetPersons() {List<Person> persons = personService.getPersons();for(Person person : persons){System.out.println(person.getName());}}}

applicationContext.xml别让别人徘徊的脚步踩碎你明天美好的梦想,

【j2ee spring】12、整合SSH框架(终结版)

相关文章:

你感兴趣的文章:

标签云: