HardBirch

Spring2.5+Struts1.3.8+JPA(Hibernate实现)整合之三

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

该篇主要编写实体beanDAO层。

src下创建包com.zyg.ssj.bean,并在该包中编写类Student,该类文件代码如下:

 

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class Student {
private Integer stuId;
private String stuName;

public Student() {}

public Student(String stuName) {
this.stuName = stuName;
}

@Id @GeneratedValue
public Integer getStuId() {
return stuId;
}
public void setStuId(Integer stuId) {
this.stuId = stuId;
}

@Column(length=20,nullable=true)
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
}


src下创建包com.zyg.ssj.dao,并在该包下创建接口StudentDao,该接口文件内容如下:

 

import java.util.List;
import com.zyg.ssj.bean.Student;

public interface StudentDao {

/**
* 保存学生信息
* @param student
*/
public abstract void save(Student student);

/**
* 根据学号删除学生信息
* @param studentId
*/
public abstract void delete(Integer studentId);

/**
* 根据学生删除学生信息
* @param studentId
*/
public abstract void delete(Student student);

/**
* 更新学生信息
* @param student
*/
public abstract void update(Student student);

/**
* 根据学号获取学生信息
* @param studentId
*/
public abstract Student getStudent(Integer studentId);

/**
* 获取全部学生信息
* @param studentId
*/
@SuppressWarnings("unchecked")
public abstract List<Student> getStudents();

}


src下创建包com.zyg.ssj.dao.jpa,并在该包下创建接口StudentDao的实现类StudentDaoImpl,该类文件内容如下:

 

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.zyg.ssj.bean.Student;
import com.zyg.ssj.dao.StudentDao;

@Transactional
public class StudentDaoImpl implements StudentDao {
@PersistenceContext
private EntityManager em;

/* (non-Javadoc)
* @see com.zyg.ssj.dao.jpa.StudentDao#save(com.zyg.ssj.bean.Student)
*/
public void save(Student student){
System.out.println(student.getStuName());
em.persist(student);
}

/* (non-Javadoc)
* @see com.zyg.ssj.dao.jpa.StudentDao#delete(java.lang.Integer)
*/
public void delete(Integer studentId){
em.remove(em.getReference(Student.class, studentId));
}

/* (non-Javadoc)
* @see com.zyg.ssj.dao.jpa.StudentDao#delete(com.zyg.ssj.bean.Student)
*/
public void delete(Student student){
em.remove(em.getReference(Student.class, student.getStuId()));
}

/* (non-Javadoc)
* @see com.zyg.ssj.dao.jpa.StudentDao#update(com.zyg.ssj.bean.Student)
*/
public void update(Student student){
em.merge(student);
}

/* (non-Javadoc)
* @see com.zyg.ssj.dao.jpa.StudentDao#getStudent(java.lang.Integer)
*/
@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
public Student getStudent(Integer studentId){
return em.find(Student.class, studentId);
}

/* (non-Javadoc)
* @see com.zyg.ssj.dao.jpa.StudentDao#getStudents()
*/
@SuppressWarnings("unchecked")
@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
public List<Student> getStudents(){
return (List<Student>)em.createQuery("select s from Student s")
.getResultList();
}
}

至此,实体bean和DAO层代码编写完成,下一篇我们编写service层代码。

代码都已经编写完毕,但是现在很困了,明天接着写吧。

声明: 本文由( 张飞不张,文采横飞 )原创编译,转载请保留链接: Spring2.5+Struts1.3.8+JPA(Hibernate实现)整合之三

Spring2.5+Struts1.3.8+JPA(Hibernate实现)整合之三:等您坐沙发呢!

发表评论


QQ群互动

Linux系统与内核学习群:194051772

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

魔豆之路QR

魔豆的Linux内核之路

魔豆的Linux内核之路

优秀工程师当看优秀书籍

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

赞助商广告

友荐云推荐