首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > 其他数据库 >

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

2014-04-23 
SSH+DB2搭建开发环境(下)1.倒入所需jar包(因网站对附件大小的限制,现将所需jar包分两部分上传。位置分别为h

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

1.倒入所需jar包

(因网站对附件大小的限制,现将所需jar包分两部分上传。位置分别为http://zhizizhishou0104.iteye.com/blog/1993956 和http://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 {@Idprivate 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 {@Idprivate 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;@Repositorypublic class ClassRoomDao extends HibernateDaoSupport{@Resourcepublic 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;@Servicepublic class ClassRoomServiceImpl implements ClassRoomService {@Resourceprivate ClassRoomDao classRoomDao;@Overridepublic 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{@Overrideprotected String[] getConfigLocations() {return new String[] {"applicationContext.xml"};}private ClassRoomService classRoomService;public void setClassRoomService(ClassRoomService classRoomService) {this.classRoomService = classRoomService;}@Testpublic 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);//测试类注意要手动提交事务setComplete();}}

?测试类中也可以使用如下方法获取bean?

BeanFactory beanFactory = new ClassPathXmlApplicationContext("applicationContext.xml");classRoomService = (ClassRoomService)beanFactory.getBean("classRoomService");

?

热点排行