HardBirch

Spring2.5+Struts2+Ibatis整合之五

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

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

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

 

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


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

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整合之五:目前有3 条留言

  1. 地板
    gsearch:

    [e03]

    2010-02-24 11:11 [回复]
  2. 板凳
    zsjsgyy:

    [e01]

    2010-08-17 23:02 [回复]
  3. 沙发
    rj0801zxh:

    StudentAction.java类您没有贴出来。能不能贴出来啊。

    2011-08-05 09:06 [回复]

发表评论


QQ群互动

Linux系统与内核学习群:194051772

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

魔豆之路QR

魔豆的Linux内核之路

魔豆的Linux内核之路

优秀工程师当看优秀书籍

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

赞助商广告

友荐云推荐