Spring2.5+Struts2+Hibernate3整合

aihaiye_99 贡献于2011-07-16

作者 999宝藏网  创建于2010-10-14 03:03:00   修改者999宝藏网  修改于2010-10-14 04:59:00字数33461

文档摘要:
关键词:

 Spring2.5+Struts2+Hibernate3 一、 整合Spring2.5+Hibernate3 1、添加jar包 在classpath下加入的所有jar文件如下图所示: 2、在src下创建spring配置文件applicationContext.xml,其代码如下: 在spring整合hibernate时并没有单独编写hibernate的配置文件hibernate.cfg.xml,而是将hibernate sessionFactory和mappingResources相关的配置直接在spring中以bean的形式进行配置 com/zyg/ssh/bean/Student.hbm.xml hibernate.dialect=org.hibernate.dialect.MySQLDialect hibernate.hbm2ddl.auto=update hibernate.show_sql=true hibernate.format_sql=true 三、编写bean和bean的hibernate配置文件 package com.zyg.ssh.bean; public class Student { private Integer stuId; private String stuName; public Student() {} public Student(String stuName) { this.stuName = stuName; } public Integer getStuId() { return stuId; } public void setStuId(Integer stuId) { this.stuId = stuId; } public String getStuName() { return stuName; } public void setStuName(String stuName) { this.stuName = stuName; } @Override public String toString() { String result = "学号:"+this.stuId+",姓名:"+this.stuName; return result; } } 四、编写DAO层代码 创建接口 package com.zyg.ssh.dao; import java.util.List; import com.zyg.ssh.bean.Student; public interface StudentDao { /** * 保存学生信息 * @param student */ public void addStudent(Student student); /** * 根据学号删除学生信息 * @param studentId */ public void delStudentById(int id); /** * 更新学生信息 * @param student */ public void updateStudentById(Student student); /** * 根据学号获取学生信息 * @param studentId */ public Student queryStudentById(int id); /** * 根据姓名获取学生信息 * @param studentId */ public List queryStudentByName(String name); /** * 获取全部学生信息 * @param studentId */ @SuppressWarnings("unchecked") public List queryAllStudents(); } 接口的实现类 package com.zyg.ssh.dao.hibernate; import java.util.List; import org.hibernate.SessionFactory; import org.springframework.transaction.annotation.Transactional; import com.zyg.ssh.bean.Student; import com.zyg.ssh.dao.StudentDao; @Transactional public class StudentDaoImpl implements StudentDao { private SessionFactory sessionFactory; public SessionFactory getSessionFactory() { return sessionFactory; } public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } public void addStudent(Student student) { // TODO Auto-generated method stub sessionFactory.getCurrentSession().save(student); } public void delStudentById(int id) { sessionFactory.getCurrentSession() .delete(sessionFactory.getCurrentSession().load(Student.class, id)); } @SuppressWarnings("unchecked") public List queryAllStudents() { // TODO Auto-generated method stub List stuList = null; stuList=sessionFactory.getCurrentSession().createQuery("from Student").list(); return stuList; } public Student queryStudentById(int id) { Student student=null; student = (Student)sessionFactory.getCurrentSession().get(Student.class, id); return student; } public List queryStudentByName(String name) { // TODO Auto-generated method stub List stuList = null; stuList = sessionFactory.getCurrentSession() .createQuery("from Student s where s.stuName like '%"+name+"%'").list(); return stuList; } public void updateStudentById(Student student) { // TODO Auto-generated method stub sessionFactory.getCurrentSession().update(student); } } 五、编写业务类进行测试 在src下创建包com.zyg.ssh.service,在该包下创建接口StudentService,其代码如下: view plaincopy to clipboardprint? package com.zyg.ssh.service; import java.util.List; import com.zyg.ssh.bean.Student; public interface StudentService { /** * 保存学生信息 * @param student */ public abstract void save(Student student); /** * 根据学号删除学生信息 * @param studentId */ public abstract void delete(Integer studentId); /** * 更新学生信息 * @param student */ public abstract void update(Student student); /** * 根据学号获取学生信息 * @param studentId */ public abstract Student getStudent(Integer studentId); /** * 获取全部学生信息 * @param studentId */ public abstract List getStudents(); /** * 通过学生姓名获取学生信息 * @param studentId */ public abstract List getStudentsByName(String name); } package com.zyg.ssh.service; import java.util.List; import com.zyg.ssh.bean.Student; public interface StudentService { /** * 保存学生信息 * @param student */ public abstract void save(Student student); /** * 根据学号删除学生信息 * @param studentId */ public abstract void delete(Integer studentId); /** * 更新学生信息 * @param student */ public abstract void update(Student student); /** * 根据学号获取学生信息 * @param studentId */ public abstract Student getStudent(Integer studentId); /** * 获取全部学生信息 * @param studentId */ public abstract List getStudents(); /** * 通过学生姓名获取学生信息 * @param studentId */ public abstract List getStudentsByName(String name); } 在src下创建包com.zyg.ssh.service.impl,在该包下创建实现StudentService接口的类StudentServiceImpl,其代码如下: view plaincopy to clipboardprint? package com.zyg.ssh.service.impl; import java.util.List; import javax.annotation.Resource; import com.zyg.ssh.bean.Student; import com.zyg.ssh.dao.StudentDao; import com.zyg.ssh.service.StudentService; public class StudentServiceImpl implements StudentService { @Resource private StudentDao studentDao; /* (non-Javadoc) * @see com.zyg.ssj.service.impl.StudentService#save(com.zyg.ssj.bean.Student) */ public void save(Student student){ studentDao.addStudent(student); } /* (non-Javadoc) * @see com.zyg.ssj.service.impl.StudentService#delete(java.lang.Integer) */ public void delete(Integer studentId){ studentDao.delStudentById(studentId); } /* (non-Javadoc) * @see com.zyg.ssj.service.impl.StudentService#update(com.zyg.ssj.bean.Student) */ public void update(Student student){ studentDao.updateStudentById(student); } /* (non-Javadoc) * @see com.zyg.ssj.service.impl.StudentService#getStudent(java.lang.Integer) */ public Student getStudent(Integer studentId){ return studentDao.queryStudentById(studentId); } /* (non-Javadoc) * @see com.zyg.ssj.service.impl.StudentService#getStudents() */ public List getStudents(){ return studentDao.queryAllStudents(); } /* (non-Javadoc) * @see com.zyg.ssj.service.impl.StudentService#getStudentDao() */ public StudentDao getStudentDao() { return studentDao; } /* (non-Javadoc) * @see com.zyg.ssj.service.impl.StudentService#setStudentDao(com.zyg.ssj.dao.StudentDao) */ public void setStudentDao(StudentDao studentDao) { this.studentDao = studentDao; } public List getStudentsByName(String name) { // TODO Auto-generated method stub return studentDao.queryStudentByName(name); } } package com.zyg.ssh.service.impl; import java.util.List; import javax.annotation.Resource; import com.zyg.ssh.bean.Student; import com.zyg.ssh.dao.StudentDao; import com.zyg.ssh.service.StudentService; public class StudentServiceImpl implements StudentService { @Resource private StudentDao studentDao; /* (non-Javadoc) * @see com.zyg.ssj.service.impl.StudentService#save(com.zyg.ssj.bean.Student) */ public void save(Student student){ studentDao.addStudent(student); } /* (non-Javadoc) * @see com.zyg.ssj.service.impl.StudentService#delete(java.lang.Integer) */ public void delete(Integer studentId){ studentDao.delStudentById(studentId); } /* (non-Javadoc) * @see com.zyg.ssj.service.impl.StudentService#update(com.zyg.ssj.bean.Student) */ public void update(Student student){ studentDao.updateStudentById(student); } /* (non-Javadoc) * @see com.zyg.ssj.service.impl.StudentService#getStudent(java.lang.Integer) */ public Student getStudent(Integer studentId){ return studentDao.queryStudentById(studentId); } /* (non-Javadoc) * @see com.zyg.ssj.service.impl.StudentService#getStudents() */ public List getStudents(){ return studentDao.queryAllStudents(); } /* (non-Javadoc) * @see com.zyg.ssj.service.impl.StudentService#getStudentDao() */ public StudentDao getStudentDao() { return studentDao; } /* (non-Javadoc) * @see com.zyg.ssj.service.impl.StudentService#setStudentDao(com.zyg.ssj.dao.StudentDao) */ public void setStudentDao(StudentDao studentDao) { this.studentDao = studentDao; } public List getStudentsByName(String name) { // TODO Auto-generated method stub return studentDao.queryStudentByName(name); } } 完成Service层代码后,需要编写Junit单元测试代码对Service层进行功能测试。 在项目中加入Junit4支持: 选中项目点击右键选择BuildPath->Configure Build path->Libraries->Add Library->Junit->Junit4。 在src目录下创建包com.zyg.ssh.junit.test,在该包下创建Junit单元测试类StudentServiceTest,其代码如下: view plaincopy to clipboardprint? package com.zyg.ssh.junit.test; import static org.junit.Assert.*; import java.util.Date; import org.junit.BeforeClass; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.zyg.ssh.bean.Student; import com.zyg.ssh.dao.StudentDao; import com.zyg.ssh.service.StudentService; public class StudentServiceTest { private static StudentService studentService; @BeforeClass public static void setUpBeforeClass() throws Exception { try { ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); System.out.println(ctx); studentService = (StudentService)ctx.getBean("studentService"); } catch (RuntimeException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Test public void queryAllStudents(){ for(Student student:studentService.getStudents()){ System.out.println(student); } } @Test public void queryStudentById(){ Student stu = studentService.getStudent(1); System.out.println(stu); } @Test public void deleteStudentById(){ System.out.println("--------------删除前所有学生信息:"); queryAllStudents(); studentService.delete(11); System.out.println("--------------删除后所有学生信息:"); queryAllStudents(); } @Test public void addStudent(){ /*for(int i=0;i<10;i++){ Student stu = new Student(); stu.setStuName("欢迎访问长弓博客"+i); }*/ Student stu = new Student(); stu.setStuName("长弓1"); studentService.save(stu); System.out.println("添加学生后所有学生信息:"); queryAllStudents(); } @Test public void updateStudent(){ Student stu = studentService.getStudent(11); System.out.println("更新前学生姓名:"+stu.getStuName()); stu.setStuName("我是长弓"); studentService.update(stu); stu = studentService.getStudent(11); System.out.println("更新后学生姓名:"+stu.getStuName()); } @Test public void queryStudentByName(){ for(Student student:studentService.getStudentsByName("长弓博客")){ System.out.println(student); } } } 六、spring配置文件applicationContext.xml实现spring整合hibernate的另一种配置。 在Spring2.5+Hibernate3的整合过程中,把hibernate sessionFactory和mappingResources相关的配置直接在spring配置文件applicationContext.xml中以bean的形式进行了配置。 其实可以在hibernate配置文件hibernate.cfg.xml中编写sessionFactory和mappingResources相关的配置,然后在spring配置文件中引用hibernate.cfg.xml。 在src下创建hibernate配置文件hibernate.cfg.xml,其代码如下: view plaincopy to clipboardprint? com.mysql.jdbc.Driver jdbc:mysql://localhost:3306/vote?characterEncoding=utf-8 root zyg org.hibernate.dialect.MySQL5Dialect 1 500 2 1 true update true com.mysql.jdbc.Driver jdbc:mysql://localhost:3306/vote?characterEncoding=utf-8 root zyg org.hibernate.dialect.MySQL5Dialect 1 500 2 1 true update true 修改spring配置文件applicationContext.xml,修改后的代码如下: view plaincopy to clipboardprint? classpath:\hibernate.cfg.xml 二、 在一的基础上整合Struts2 (一) 加入Struts2的jar文件: 在classpath下,加入struts-2.0.11.1\lib下除antlr-2.7.2.jar(该jar文件spring中已有)和所有名称中带有plugin(struts2-spring-plugin-2.0.11.1.jar除外)的jar文件外的其他全部jar文件,如下图:   (二) 修改web.xml文件,修改后的代码如下: view plaincopy to clipboardprint? contextConfigLocation classpath:applicationContext.xml org.springframework.web.context.ContextLoaderListener encoding org.springframework.web.filter.CharacterEncodingFilter encoding UTF-8 encoding /* struts2 org.apache.struts2.dispatcher.FilterDispatcher struts2 /* index.jsp contextConfigLocation classpath:applicationContext.xml org.springframework.web.context.ContextLoaderListener encoding org.springframework.web.filter.CharacterEncodingFilter encoding UTF-8 encoding /* struts2 org.apache.struts2.dispatcher.FilterDispatcher struts2 /* index.jsp 在src下创建struts配置文件struts.xml文件,其代码如下: view plaincopy to clipboardprint? /WEB-INF/pages/message.jsp /WEB-INF/pages/studentlist.jsp /WEB-INF/pages/addStudent.jsp /WEB-INF/pages/message.jsp /WEB-INF/pages/editStudent.jsp action_list.action action_list.action /WEB-INF/pages/message.jsp /WEB-INF/pages/studentlist.jsp /WEB-INF/pages/addStudent.jsp /WEB-INF/pages/message.jsp /WEB-INF/pages/editStudent.jsp action_list.action action_list.action Spring整合Struts1.3.8需要在struts的配置文件struts-config.xml文件添加如下代码: view plaincopy to clipboardprint? 上面配置的意思是让spring来代理对struts的请求。 Spring整合struts2更加简单,连上面的配置都不要,我们加入的struts2-spring-plugin-2.0.11.1.jar已经完成了spring和struts2的整合工作。我们只需要在struts.xml中配置struts的action时将action的class属性设置为spring容器中的bean就可以了。 Src下applicationContext.xml文件的代码如下: view plaincopy to clipboardprint? 三、主要编写整合struts2的bean和action。 在src下创建包com.zyg.ssj.bean,在该包下创建bean类Student,其代码如下: view plaincopy to clipboardprint? package com.zyg.ssj.bean; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; @Entity public class Student { private Integer stuId; private String stuName; public Student() {} public Student(String stuName) { this.stuName = stuName; } @Id @GeneratedValue public Integer getStuId() { return stuId; } public void setStuId(Integer stuId) { this.stuId = stuId; } @Column(length=20,nullable=false) public String getStuName() { return stuName; } public void setStuName(String stuName) { this.stuName = stuName; } } package com.zyg.ssj.bean; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; @Entity public class Student { private Integer stuId; private String stuName; public Student() {} public Student(String stuName) { this.stuName = stuName; } @Id @GeneratedValue public Integer getStuId() { return stuId; } public void setStuId(Integer stuId) { this.stuId = stuId; } @Column(length=20,nullable=false) public String getStuName() { return stuName; } public void setStuName(String stuName) { this.stuName = stuName; } } 在src下创建包com.zyg.ssj.web.action,在该包下创建action类StudentAction,其代码如下: view plaincopy to clipboardprint? package com.zyg.ssj.web.action; import java.util.List; import javax.annotation.Resource; import com.zyg.ssj.bean.Student; import com.zyg.ssj.service.StudentService; public class StudentAction { @Resource private StudentService studentService; private List students; private Student student; private String message; /** * 获取学生列表 * @return */ public String list(){ this.students = studentService.getStudents(); return "list"; } /** * 返回新建学生信息界面 * @return */ public String add(){ student = new Student(); return "add"; } /** * 保存新建的学生信息 * @return */ public String save(){ if(null==student.getStuId()){ //新建学生信息 studentService.save(student); }else{ studentService.update(student); return "editResult"; } this.message="学生【"+student.getStuName()+"】信息保存成功!!!"; return "save"; } /** * 修改学生信息 * @return */ public String edit(){ student = studentService.getStudent(student.getStuId()); return "edit"; } /** * 删除学生信息 * @return */ public String delete(){ System.out.println(student.getStuId()); studentService.delete(student.getStuId()); return "delResult"; } /** * 清空学生信息 */ private void clearStudentInfo(){ student.setStuId(null); student.setStuName(null); } public StudentService getStudentService() { return studentService; } public void setStudentService(StudentService studentService) { this.studentService = studentService; } public List getStudents() { return students; } public void setStudents(List students) { this.students = students; } public Student getStudent() { return student; } public void setStudent(Student student) { this.student = student; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } } 四、主要编写JSP展示页面。 在WebRoot/WEB-INF下创建文件夹pages,在该文件夹下分别创建如下4个JSP页面: 1.学生信息列表页面:studentlist.jsp 其代码如下: view plaincopy to clipboardprint? <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <%@ taglib uri="/struts-tags" prefix="s"%> 学生列表

学生信息列表

Spring2.5+Struts2+Jpa(Hibernate)实现

学号 姓名 操作
修改| 删除
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <%@ taglib uri="/struts-tags" prefix="s"%> 学生列表

学生信息列表

Spring2.5+Struts2+Jpa(Hibernate)实现

学号 姓名 操作
修改| 删除
2.添加学生信息页面:addStudent.jsp 其代码如下: view plaincopy to clipboardprint? <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib uri="/struts-tags" prefix="s"%> 添加学生信息

请添加学生信息

学生姓名: <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib uri="/struts-tags" prefix="s"%> 添加学生信息

请添加学生信息

学生姓名: 3.学生信息添加结果页面: message.jsp 其代码如下: view plaincopy to clipboardprint? <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> 添加学生信息结果 ${message } 查看学生信息 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> 添加学生信息结果 ${message } 查看学生信息 4.修改学生信息页面: editStudent.jsp 其代码如下: view plaincopy to clipboardprint? <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib uri="/struts-tags" prefix="s"%> 修改学生信息

请修改学生信息

学生姓名: <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib uri="/struts-tags" prefix="s"%> 修改学生信息

请修改学生信息

学生姓名: 修改index.jsp页面,修改后的代码如下: view plaincopy to clipboardprint? <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> 学生信息管理

学生信息管理

查看学生信息

下载文档到电脑,查找使用更方便

文档的实际排版效果,会与网站的显示效果略有不同!!

需要 20 金币 [ 分享文档获得金币 ] 2 人已下载

下载文档