转载请注明:来自http://blog.csdn.net/M_ChangGong/ 作者:张燕广

该篇主要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整合之五相关推荐

  1. Spring2.5+Struts2+Hibernate3整合之五

    转载请注明:来自http://blog.csdn.net/M_ChangGong/ 作者:张燕广 该篇主要编写Service层代码以及针对Service层功能的Junit测试代码 在src下创建包co ...

  2. Spring2.5+Struts2+Ibatis整合之四

    转载请注明:来自http://blog.csdn.net/M_ChangGong/ 作者:张燕广 该篇主要编写DAO层代码. 在src下创建包com.zyg.ssi.dao,在该包下创建接口Stude ...

  3. spring+struts2+ibatis整合完全步骤

    Ssi整合首先需要导入相关的jar包 <classpath> <classpathentry kind="src" path="src"/&g ...

  4. Spring2.5+Struts2+Hibernate3整合之三

    转载请注明:来自http://blog.csdn.net/M_ChangGong/ 作者:张燕广 该篇主要编写bean和bean的hibernate配置文件. 在src下创建包com.zyg.ssh. ...

  5. Spring2.5+Struts2+Hibernate3整合之四

    转载请注明:来自http://blog.csdn.net/M_ChangGong/ 作者:张燕广 该篇主要编写DAO层代码. 在src下创建包com.zyg.ssh.dao,在该包下创建接口Stude ...

  6. struts2 ibatis Spring系统架构图

    struts2 Ibatis Spring ----------------------------------- 转载于:https://www.cnblogs.com/archie2010/arc ...

  7. spring2.5+struts2+hibernate+mysql

    今天写了二个学习的小实例,功能很简单,就是登录,注册功能. spring2.5+struts2+hibernate3.1+mysql5 http://download.csdn.net/source/ ...

  8. struts2+ibatis+spring框架整合(二)

    MyBatis3.1.1+Spring3.1.2+Struts2.3.4.1框架整合 先来看目录结构 来看配置文件 applicationContext.xml <?xml version=&q ...

  9. Ibatis 整合spring + Struts2 加入OSCACHE

    说明:    使用  spring2.5 +ibatis2.3.4+oscache2.4+struts2+oracle 建表 create table STUDENT ( SID NUMBER(8) ...

最新文章

  1. java类获取声明,获取用于MethodInvocation的实际类而不是声明类 - java
  2. Graphpad Prism 9教程,不会 SPSS,也能搞定卡方检验!
  3. Java多线程与并发库高级应用 学习笔记 1-9课
  4. 排队问题解题思路_1120各数的认识解决问题
  5. 深入理解计算机系统:进程
  6. 从零开始制作 NuGet 源代码包(全面支持 .NET Core / .NET Framework / WPF 项目)
  7. ABB (2020牛客国庆集训派对day1)
  8. PHP 错误与异常 笔记与总结(12 )异常
  9. vestacp 远程mysql_免费使用VestaCP控制面板的文件管理器 | 雷雨博客
  10. mysql left join、right join、inner join、union、union all使用以及图解
  11. python中文人名识别(使用hanlp,LTP,LAC)
  12. 再看lambda/sorted/filter/map
  13. jupyter notebook 中文乱码问题解决
  14. java 表头固定_固定表头在快逸报表中的设定
  15. Nvidia Graphics Card Drive Download 英伟达显卡驱动花屏问题解决处理方式
  16. nominal和ordinal 数据处理中四种基本数据类型
  17. 从零开始手写 VIO
  18. 新药开发相关计算机辅助设计,德研究称药物开发将步入计算机辅助设计时代
  19. 长沙麻将APP思路整理
  20. 小程序服务器搭建前后端交互,手把手带你搭一个简单的微信小程序(包括前后端)...

热门文章

  1. 7805扩流电路(转)
  2. 文献阅读:MT-DNN模型
  3. 计算机考研科目887,2017年华中科技大学887数据结构与算法分析考研试题
  4. 民锋国际期货:母亲的 “沉没成本”
  5. 免费好用的单EXE打包工具——Enigma Virtual Box
  6. Qt-网易云音乐界面实现-6 迷你个人中心实现
  7. Java加密解密算法-SHA加密
  8. T270714 地鼠的远亲
  9. LINUX嵌入式开发板上配置FTP 服务
  10. c语言与单片机技术试卷与答案,哈尔滨工业大学《单片机原理及应用》课件、各章习题解答、试题及答案...