日期:2014-05-16  浏览次数:20452 次

SSH+DB2搭建开发环境(下)

1.倒入所需jar包

(因网站对附件大小的限制,现将所需jar包分两部分上传。位置分别为http://zhizizhishou0104.iteye.com/blog/1993956http://zhizizhishou0104.iteye.com/blog/1993988 )

2.编写entity,这里以一对多关系为例

ClassRoom.java(one端)

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name="CLASS_ROOM")
public class ClassRoom {
	@Id
	private int id;
	private String classroom_number;
	private String classroom_name;
	@OneToMany(cascade=CascadeType.ALL,mappedBy="classRoom")
	private List<Student> students;
	
	//这里省略setter和getter
}

?Student.java(many端)

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

@Entity
@Table(name="STUDENT")
public class Student {
	@Id
	private int id;
	private String stu_number;
	private String stu_name;
	@ManyToOne(fetch=FetchType.LAZY)
	@JoinColumn(updatable=false,nullable=false)
	private ClassRoom classRoom;
	
	//这里省略setter和getter
}

?3.dao层

import javax.annotation.Resource;

import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import com.aia.domain.ClassRoom;

@Repository
public class ClassRoomDao extends HibernateDaoSupport{
	@Resource
	public void setHibenrateSessionFactory(SessionFactory sessionFactory) {
		super.setSessionFactory(sessionFactory);
	}
	public ClassRoom saveClassRoom(ClassRoom classRoom) {
		getHibernateTemplate().persist(classRoom);
		return classRoom;
	}
}

?4.service层(这里跳过接口,直接上传实现类)

?

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.aia.dao.ClassRoomDao;
import com.aia.domain.ClassRoom;
import com.aia.service.ClassRoomService;

@Service
public class ClassRoomServiceImpl implements ClassRoomService {
	@Resource
	private ClassRoomDao classRoomDao;
	@Override
	public ClassRoom saveClassRoom(ClassRoom classRoom) {
		return classRoomDao.saveClassRoom(classRoom);
	}
}

?5.测试类

import java.util.ArrayList;
import java.util.List;

import org.junit.Test;
import org.springframework.test.AbstractTransactionalSpringContextTests;

import com.aia.domain.ClassRoom;
import com.aia.domain.Student;
import com.aia.service.ClassRoomService;

public class ClassRoomServiceImplTest extends AbstractTransactionalSpringContextTests{
	@Override
	protected String[] getConfigLocations() {
		return new String[] {"applicationContext.xml"};
	}
	private ClassRoomService classRoomService;
	public void setClassRoomService(ClassRoomService classRoomService) {
		this.classRoomService = classRoomService;
	}

	@Test
	public void testSaveClassRoom(){
		ClassRoom classRoom = new ClassRoom();
		classRoom.setClassroom_name("classroom_name");
		classRoom.setClassroom_number("123");
		
		Student student1 = new Student();
		student1.setStu_name("student_name1");
		student1.setStu_number("1001");
		student1.setId(5);
		student1.setClassRoom(classRoom);
		
		Student student2 = new Student();
		student2.setStu_name("student_name2");
		student2.setStu_number("1002");
		student2.setId(6);
		student2.setClassRoom(classRoom);
		
		List<Student> lists = new ArrayList<Student>();
		lists.add(student1);
		lists.add(student2);
		
		classRoom.setStudents(lists);
		classRoomService.saveClassRoom(classRoom);
		//测试类注意要手动提交事务
		setCo