您的位置:首页 > 编程语言 > Java开发

Spring2.5+Struts2+Hibernate3整合之五

2014-07-21 09:46 393 查看
该篇主要编写Service层代码以及针对Service层功能的Junit测试代码

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

[java]
view plaincopy

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<Student> getStudents();  
      
    /** 
     * 通过学生姓名获取学生信息 
     * @param studentId 
     */  
    public abstract List<Student> getStudentsByName(String name);  
  
}  

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

 

[b][java]
view plaincopy[/b]

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<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,其代码如下:
 

[java]
view plaincopy

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  
    publ
c5fe
ic 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的另一种配置。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: