S2H sql+entity+dao+biz

sql 语句

--创建用户及授权
CREATE USER stuDB IDENTIFIED BY 123456;
GRANT CONNECT,RESOURCE to stuDB;

--drop user stuDB cascade

--班级信息表
create table classes(
        id number(10) primary key not null,--班级编号
        cname varchar2(30) not null--班级名称
)

--学生信息表
create table student(
       id number(10) primary key not null,--学员编号
       sname varchar2(30) not null,--学员姓名
       birthday date not null,--生日
       gender varchar2(10) not null,--性别
       telephone varchar2(20) not null,--电话
       email varchar2(50) not null,--Email
       classid number(10) not null,--班级编号
       foreign key(classid) references classes(id)
)



--创建序列
CREATE SEQUENCE seq_student INCREMENT BY 1 START WITH 1 CACHE 10;
CREATE SEQUENCE seq_classes INCREMENT BY 1 START WITH 1 CACHE 10;

--插入班级信息表
insert into classes
  (id, cname)
values
  (seq_classes.nextval, 'Y2E315');
insert into classes
  (id, cname)
values
  (seq_classes.nextval, 'S2E234');
insert into classes
  (id, cname)
values
  (seq_classes.nextval, 'S1E271');
insert into classes
  (id, cname)
values
  (seq_classes.nextval, 'Y2E123');
insert into classes
  (id, cname)
values
  (seq_classes.nextval, 'Y2E695');
  
--插入学生信息表
insert into student
  (id, sname, birthday, gender, telephone, email, classid)
values
  (seq_student.nextval, '景临境', to_date('1991-10-18','yyyy-mm-dd'), '男', '15123456789', '[email protected]', 2);
insert into student
  (id, sname, birthday, gender, telephone, email, classid)
values
  (seq_student.nextval, '何开', to_date('1992-5-4','yyyy-mm-dd'), '男', '15123456789', '[email protected]', 3);
insert into student
  (id, sname, birthday, gender, telephone, email, classid)
values
  (seq_student.nextval, '赵柔', to_date('1989-5-4','yyyy-mm-dd'), '女', '15123456789', '[email protected]', 4);
insert into student
  (id, sname, birthday, gender, telephone, email, classid)
values
  (seq_student.nextval, '左阻', to_date('1989-5-4','yyyy-mm-dd'), '男', '15123456789', '[email protected]', 5);
insert into student
  (id, sname, birthday, gender, telephone, email, classid)
values
  (seq_student.nextval, '陆梦', to_date('1996-5-4','yyyy-mm-dd'), '女', '15123456789', '[email protected]', 2);


--查看
select id, cname from classes;
select id, sname, birthday, gender, telephone, email, classid from student

 

Student 实体类及注解

package cn.entity;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
/**
 * 学生 实体类及注解
 * */
@Entity  
@Table
public class Student {
	@Id  
	@GeneratedValue(generator="seq_student",strategy=GenerationType.SEQUENCE)  
	@SequenceGenerator(name="seq_student",sequenceName="seq_student",allocationSize=1,initialValue=1)  
	private Integer id;//学员编号
	@Column(nullable=false) 
	private String sname;//学员姓名
	@Column(nullable=false) 
	private Date birthday;//生日
	@Column(nullable=false) 
	private String gender;//性别
	@Column(nullable=false) 
	private String telephone;//电话
	@Column(nullable=false) 
	private String email;//Email
	@ManyToOne(targetEntity=Classes.class)  
	@JoinColumn(name="classid")  
	private Classes classes;//班级编号,多对一,多个学生对一个班级
	public Student() {
	}
	public Student(String sname, Date birthday, String gender,
			String telephone, String email, Classes classes) {
		this.sname = sname;
		this.birthday = birthday;
		this.gender = gender;
		this.telephone = telephone;
		this.email = email;
		this.classes = classes;
	}

	public Student(Integer id, String sname, Date birthday, String gender,
			String telephone, String email, Classes classes) {
		this.id = id;
		this.sname = sname;
		this.birthday = birthday;
		this.gender = gender;
		this.telephone = telephone;
		this.email = email;
		this.classes = classes;
	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getSname() {
		return sname;
	}
	public void setSname(String sname) {
		this.sname = sname;
	}
	public Date getBirthday() {
		return birthday;
	}
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	public String getGender() {
		return gender;
	}
	public void setGender(String gender) {
		this.gender = gender;
	}
	public String getTelephone() {
		return telephone;
	}
	public void setTelephone(String telephone) {
		this.telephone = telephone;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public Classes getClasses() {
		return classes;
	}
	public void setClasses(Classes classes) {
		this.classes = classes;
	}
}

 

Classes 实体类及注解

package cn.entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
/**
 * 班级 实体类及注解
 * */
@Entity  
@Table
public class Classes {
	@Id  
	@GeneratedValue(generator="seq_classes",strategy=GenerationType.SEQUENCE)  
	@SequenceGenerator(name="seq_classes",sequenceName="seq_classes",allocationSize=1,initialValue=1)   
	private Integer id;//班级编号
	@Column(nullable=false) 
	private String cname;//班级名称
	public Classes() {
	}
	public Classes(String cname) {
		this.cname = cname;
	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getCname() {
		return cname;
	}
	public void setCname(String cname) {
		this.cname = cname;
	}
}

 

StudentDao  数据访问层接口与实现

package cn.dao;
import java.util.List;
import org.hibernate.HibernateException;
import cn.entity.Student;
/**
 * 学生 数据访问层接口
 * */
public interface StudentDao {
	/**
	 * 获取全部的学生列表,包括班级
	 * @return
	 * @throws HibernateException
	 */
	public List<Student> getAll() throws HibernateException;
	/**
	 * 根据 id 查找学生信息
	 * @param id
	 * @return
	 * @throws HibernateException
	 */
	public Student getStudentById(Integer id) throws HibernateException;
	/**
	 * 修改学生信息
	 * @param student
	 * @return
	 * @throws HibernateException
	 */
	public int update(Student student) throws HibernateException;
}

 

package cn.dao.impl;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import cn.dao.StudentDao;
import cn.entity.Student;
import cn.util.HibernateSessionFactory;
/**
 * 学生 数据访问层实现
 * */
public class StudentDaoImpl implements StudentDao {
	/**
	 * 获取全部的学生列表,包括班级
	 * @return
	 * @throws HibernateException
	 */
	@SuppressWarnings("unchecked")
	public List<Student> getAll() throws HibernateException {
		String hql="from Student";  
		List<Student> students=null;  
		Session session=HibernateSessionFactory.getSession();  
		Query query=session.createQuery(hql);  
		students=query.list();  
		HibernateSessionFactory.closeSession();  
		return students;  
	}
	/**
	 * 根据 id 查找学生信息
	 * @param id
	 * @return
	 * @throws HibernateException
	 */
	public Student getStudentById(Integer id) throws HibernateException {
		String hql="from Student where id =:id";  
		Student student=null;  
        Session session=HibernateSessionFactory.getSession();  
        Query query=session.createQuery(hql);  
        query.setInteger("id", id);  
        student = (Student) query.uniqueResult();  
        HibernateSessionFactory.closeSession();  
        return student;  
	}
	/**
	 * 修改学生信息
	 * @param student
	 * @return
	 * @throws HibernateException
	 */
	public int update(Student student) throws HibernateException {
		Session session = HibernateSessionFactory.getSession();  
        Transaction tx = null;  
        int id =0;
        try {  
            tx = session.beginTransaction();  
            session.update(student);  
            tx.commit();  
            id =1;
        } catch (HibernateException e) {  
            e.printStackTrace();  
            tx.rollback();  
        }finally{  
            HibernateSessionFactory.closeSession();  
        }  
        return id;
	}

}

 

ClassesDao 数据访问层接口与实现

package cn.dao;
import java.util.List;
import org.hibernate.HibernateException;
import cn.entity.Classes;
/**
 * 班级 数据访问层接口
 * */
public interface ClassesDao {
	/**
	 * 获取班级的列表
	 * @return
	 * @throws HibernateException
	 */
	public List<Classes> getClassesList() throws HibernateException;
	/**
	 * 根据 id 获取班级
	 * @param id
	 * @return
	 * @throws HibernateException
	 */
	public Classes getClassesById(Integer id) throws HibernateException;
}

 

package cn.dao.impl;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import cn.dao.ClassesDao;
import cn.entity.Classes;
import cn.util.HibernateSessionFactory;
/**
 * 班级 数据访问层实现
 * */
public class ClassesDaoImpl implements ClassesDao {
	/**
	 * 获取班级的列表
	 * @return
	 * @throws HibernateException
	 */
	@SuppressWarnings("unchecked")
	public List<Classes> getClassesList() throws HibernateException {
		String hql="from Classes";  
		List<Classes> classes=null;  
		Session session=HibernateSessionFactory.getSession();  
		Query query=session.createQuery(hql);  
		classes=query.list();  
		HibernateSessionFactory.closeSession();  
		return classes;  
	}
	/**
	 * 根据 id 获取班级
	 * @param id
	 * @return
	 * @throws HibernateException
	 */
	public Classes getClassesById(Integer id) throws HibernateException {
		String hql="from Classes where id =:id";  
		Classes classes=null;  
        Session session=HibernateSessionFactory.getSession();  
        Query query=session.createQuery(hql);  
        query.setInteger("id", id);  
        classes = (Classes) query.uniqueResult();  
        HibernateSessionFactory.closeSession();  
        return classes; 
	}

}

 

StudentBiz 业务逻辑层接口与实现

package cn.biz;
import java.util.List;
import org.hibernate.HibernateException;
import cn.entity.Classes;
import cn.entity.Student;
/**
 * 学生 业务逻辑层接口
 * */
public interface StudentBiz {
	/**
	 * 获取全部的学生列表,包括班级
	 * @return
	 * @throws HibernateException
	 */
	public List<Student> getAll() throws HibernateException;
	/**
	 * 根据 id 查找学生信息
	 * @param id
	 * @return
	 * @throws HibernateException
	 */
	public Student getStudentById(Integer id) throws HibernateException;
	/**
	 * 修改学生信息
	 * @param student
	 * @return
	 * @throws HibernateException
	 */
	public int update(Student student) throws HibernateException;
	/**
	 * 获取班级的列表
	 * @return
	 * @throws HibernateException
	 */
	public List<Classes> getClassesList() throws HibernateException;
	/**
	 * 根据 id 获取班级
	 * @param id
	 * @return
	 * @throws HibernateException
	 */
	public Classes getClassesById(Integer id) throws HibernateException;
}

 

package cn.biz.impl;
import java.util.List;
import org.hibernate.HibernateException;
import cn.biz.StudentBiz;
import cn.dao.ClassesDao;
import cn.dao.StudentDao;
import cn.dao.impl.ClassesDaoImpl;
import cn.dao.impl.StudentDaoImpl;
import cn.entity.Classes;
import cn.entity.Student;
/**
 * 学生 业务逻辑层实现
 * */
public class StudentBizImpl implements StudentBiz {
	//学生 数据访问层
	private StudentDao studentDao = new StudentDaoImpl();
	//班级 数据访问层
	private ClassesDao classesDao = new ClassesDaoImpl();
	/**
	 * 获取全部的学生列表,包括班级
	 * @return
	 * @throws HibernateException
	 */
	public List<Student> getAll() throws HibernateException {
		return studentDao.getAll();
	}
	/**
	 * 根据 id 查找学生信息
	 * @param id
	 * @return
	 * @throws HibernateException
	 */
	public Student getStudentById(Integer id) throws HibernateException {
		return studentDao.getStudentById(id);
	}
	/**
	 * 修改学生信息
	 * @param student
	 * @return
	 * @throws HibernateException
	 */
	public int update(Student student) throws HibernateException {
		return studentDao.update(student);
	}
	/**
	 * 获取班级的列表
	 * @return
	 * @throws HibernateException
	 */
	public List<Classes> getClassesList() throws HibernateException {
		return classesDao.getClassesList();
	}
	/**
	 * 根据 id 获取班级
	 * @param id
	 * @return
	 * @throws HibernateException
	 */
	public Classes getClassesById(Integer id) throws HibernateException {
		return classesDao.getClassesById(id);
	}

}

 

 

 

 

 

你可能感兴趣的:(entity)