HardBirch

Spring2.5+Struts2+Hibernate3整合之五

时间:09-05-17 栏目:iOS移动应用开发技术 作者:张飞不张,文采横飞 评论:0 点击: 1,910 次

该篇主要编写Service层代码以及针对Service层功能的Junit测试代码

src下创建包com.zyg.ssh.service,在该包下创建接口StudentService,其代码如下:

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<Student> getStudents();

/**
* 通过学生姓名获取学生信息
* @param studentId
*/
public abstract List<Student> getStudentsByName(String name);

}

src下创建包com.zyg.ssh.service.impl,在该包下创建实现StudentService接口的类StudentServiceImpl,其代码如下:

 

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<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.ssh.junit.test在该包下创建Junit单元测试类StudentServiceTest,其代码如下:

 

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);
}
}
}

   运行StudentServiceTest保证CRUD操作能够正常运行,Spring2.5整合Hibernate3成功。

至此,完成了Spring2.5+Struts2+Hibernate3整合的第1步,即Spring2.5+Hibernate3的整合,对于第2步中在Spring2.5+Hibernate3的基础上再整合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+Hibernate3基础上再整合Struts2,项目首页面如下图所示:

  下一篇作为补充,修改spring配置文件applicationContext.xml实现spring整合hibernate的另一种配置。

声明: 本文由( 张飞不张,文采横飞 )原创编译,转载请保留链接: Spring2.5+Struts2+Hibernate3整合之五

Spring2.5+Struts2+Hibernate3整合之五:等您坐沙发呢!

发表评论


QQ群互动

Linux系统与内核学习群:194051772

WP建站技术学习交流群:194062106

魔豆之路QR

魔豆的Linux内核之路

魔豆的Linux内核之路

优秀工程师当看优秀书籍

优秀程序员,要看优秀书!

赞助商广告

友荐云推荐