Spring2.5+Struts2+Ibatis整合之五
2009-05-21 13:15
696 查看
转载请注明:来自http://blog.csdn.net/M_ChangGong/ 作者:张燕广
该篇主要Service层代码以及针对Service层功能的Junit测试代码
在src下创建包com.zyg.ssi.service,在该包下创建接口StudentService,其代码如下:
在src下创建包com.zyg.ssi.service.impl,在该包下创建实现StudentService接口的类StudentServiceImpl,其代码如下:
完成Service层代码后,需要编写Junit单元测试代码对Service层功能进行测试。
在项目中加入Junit4支持:
选中项目点击右键选择BuildPath->Configure Build path->Libraries->Add Library->Junit->Junit4。
在src目录下创建包com.zyg.ssi.junit.test,在该包下创建Junit单元测试类StudentServiceTest,其代码如下:
运行StudentServiceTest保证CRUD操作能够正常运行,Spring2.5整合Ibaits成功。
至此,完成了Spring2.5+Struts2+Ibaits整合的第1步,即Spring2.5+Ibaits的整合,对于第2步中在Spring2.5+Ibaits的基础上再整合Struts2的操作,跟Spring2.5+Struts2+Jpa系列文章中在Spring2.5+Jpa的基础上再整合Struts2的操作完全相同,故不再赘述。下面提供在Spring2.5+Jpa的基础上再整合Struts2操作的链接地址,供有兴趣的读者参考:
Spring2.5+Struts2+Jpa(Hibernate实现)整合之一
Spring2.5+Struts2+Jpa(Hibernate实现)整合之二
Spring2.5+Struts2+Jpa(Hibernate实现)整合之三
Spring2.5+Struts2+Jpa(Hibernate实现)整合之四
参考上面5篇文章的内容,在Spring2.5+Ibaits基础上再整合Struts2,项目首页面如下图所示:
该篇主要Service层代码以及针对Service层功能的Junit测试代码
在src下创建包com.zyg.ssi.service,在该包下创建接口StudentService,其代码如下:
package com.zyg.ssi.service; import java.util.List; import com.zyg.ssi.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<Student> getStudents(); /** * 通过学生姓名获取学生信息 * @param studentId */ public abstract List<Student> getStudentsByName(String name); }
在src下创建包com.zyg.ssi.service.impl,在该包下创建实现StudentService接口的类StudentServiceImpl,其代码如下:
package com.zyg.ssi.service.impl; import java.util.List; import javax.annotation.Resource; import com.zyg.ssi.bean.Student; import com.zyg.ssi.dao.StudentDao; import com.zyg.ssi.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<Student> 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<Student> 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.ssi.junit.test,在该包下创建Junit单元测试类StudentServiceTest,其代码如下:
package com.zyg.ssi.junit.test; import org.junit.BeforeClass; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.zyg.ssi.bean.Student; import com.zyg.ssi.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 addStudent(){ for(int i=0;i<10;i++){ Student stu = new Student(); stu.setStuName("欢迎访问长弓博客"+(i+1)); } /*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 queryAllStudents(){ for(Student student:studentService.getStudents()){ System.out.println(student); } } @Test public void queryStudentByName(){ for(Student student:studentService.getStudentsByName("博客")){ 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(10); System.out.println("--------------删除后所有学生信息:"); queryAllStudents(); } }
运行StudentServiceTest保证CRUD操作能够正常运行,Spring2.5整合Ibaits成功。
至此,完成了Spring2.5+Struts2+Ibaits整合的第1步,即Spring2.5+Ibaits的整合,对于第2步中在Spring2.5+Ibaits的基础上再整合Struts2的操作,跟Spring2.5+Struts2+Jpa系列文章中在Spring2.5+Jpa的基础上再整合Struts2的操作完全相同,故不再赘述。下面提供在Spring2.5+Jpa的基础上再整合Struts2操作的链接地址,供有兴趣的读者参考:
Spring2.5+Struts2+Jpa(Hibernate实现)整合之一
Spring2.5+Struts2+Jpa(Hibernate实现)整合之二
Spring2.5+Struts2+Jpa(Hibernate实现)整合之三
Spring2.5+Struts2+Jpa(Hibernate实现)整合之四
参考上面5篇文章的内容,在Spring2.5+Ibaits基础上再整合Struts2,项目首页面如下图所示:
相关文章推荐
- Spring2.5+Struts2+Ibatis整合之四
- Spring2.5+Struts2+Ibatis整合之一
- spring2.5+struts2+ibatis整合jar包
- Spring2.5+Struts2+Ibatis整合之一
- spring2.5 + struts2 + ibatis2.3.4 框架整合开发
- Spring2.5+Struts2+Ibatis整合之二
- spring2.5 + struts2 + ibatis2.3.4 框架整合开发
- Spring2.5+Struts2+Ibatis整合之三
- [置顶] spring2.5 + struts2 + ibatis2.3.4 框架整合开发
- Spring2.5整合Ibatis入门级开发实例
- Spring2.5+Struts2+Hibernate3整合之三
- Spring2.5、Struts2、Ibatis开发框架搭建之二
- SSI(Struts2, Spring, iBatis)框架整合小结
- SSI(Struts2, Spring, iBatis)框架整合小结
- 框架学习之Spring 第五节 SSH整合开发[Spring2.5+Hibernate3.3+Struts2]
- Spring2.5、Struts2、Ibatis开发框架搭建之二
- spring2.5 + ibatis2.3.4 整合开发
- Spring2+struts2+ibatis整合
- SSH整合开发[Spring2.5+Hibernate3.3+Struts2]
- 学习笔记-----------------struts2 hibernate3 spring2.5整合