⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 studentdaoimpl.java

📁 基于ssh框架
💻 JAVA
字号:
package com.zzu.dao.impl;

import java.io.Serializable;
import java.util.Iterator;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.zzu.dao.StudentDao;
import com.zzu.dao.entity.Course;
import com.zzu.dao.entity.Sc;
import com.zzu.dao.entity.Student;

public class StudentDaoImpl implements StudentDao {
	
	Session se=null;
	Transaction tx=null;
	public void add(Student student) {
		
		
		try {
			se = HibernateSessionFactory.getSession();
			tx = se.beginTransaction();
			System.out.println(student.getSno());
			se.save(student);
			tx.commit();

			
			System.out.println("学生添加成功!");
			
		} catch (Exception e) {
			if (null != tx)
				tx.rollback();
			e.printStackTrace();
		} finally {
			se.close();
		}
		//return ok;


	}

	public void del(Student student) {
		//Integer ok = 0;
		
		try {
			se = HibernateSessionFactory.getSession();
			tx = se.beginTransaction();
			se.delete(student);
			tx.commit();
			System.out.println("学生删除成功!");
			//ok = 1;
		} catch (Exception e) {
			if (null != tx)
				tx.rollback();
			e.printStackTrace();
		} finally {
			se.close();// 关闭 Session
		}
		
	}

	

	public boolean login(String sno, String spwd) {
		boolean flag=false;
		Session se=HibernateSessionFactory.getSession();
		//Transaction tx=se.beginTransaction();
		String hql="from Student stu where stu.sno=? and stu.spwd=?";
		Query query=se.createQuery(hql);
		query.setString(0, sno);
		query.setString(1, spwd);
	
		List list=query.list();
		if(list.size()>0)
		{
			flag=true;
			Student stu=(Student)list.get(0);
			System.out.println(stu.getSname());
		}
		//tx.commit();
		
		return flag;
	}

	public List serch(Student condition) {
		List stuList = null;
		se = HibernateSessionFactory.getSession();
		String hql = "from Student";
		if(condition==null)
		{
			hql=hql;
		}
		try {
			Query query = se.createQuery(hql);
			stuList = query.list();
		} catch (Exception e) {
			System.out.println("studentDaoImpl 类 Exception");
			e.printStackTrace();
		} finally {
			se.close();
		}
		return stuList;
		
	}

	public Student serchById(String sno) {
		Student student=null;
		 se=HibernateSessionFactory.getSession();
		//Transaction tx=se.beginTransaction();
		String hql="from Student stu where stu.sno='"+sno+"'";
		Query query=se.createQuery(hql);
		List list=query.list();
		if(list.size()>0)
		{
			
			student=(Student)list.get(0);
			System.out.println(student.getSname());
		}
		//tx.commit();
		
		return student;
	}

	public void update(Student student) {
		
		try{
			 se=HibernateSessionFactory.getSession();
			 tx=se.beginTransaction();
	
			se.update(student);
			
			System.out.println("学生信息修改成功!");
			tx.commit();
		}catch(Exception e)
		{
			if(null!=tx)
			{
				tx.rollback();
			}
		}finally{
			se.close();
		}
		
	
	}

	public static void main(String[] args)
	{
		//登陆测试
		//new StudentDaoImpl().login("20052430202", "1");
		//搜索测试
		//new StudentDaoImpl().serchById("20052430206");
		
		//条件搜索测试
//		Student condition=new Student();
//		List list=new StudentDaoImpl().serch(condition);
//		for(Iterator it=list.iterator();it.hasNext();){
//			Student stu=(Student)it.next();
//			System.out.println("students:"+stu.getSname()+"-"
//								+stu.getSdept());
//		}
		
		
		
		//修改测试
//		Student student=new StudentDaoImpl().getStudent(Student.class, "20052430205");
//		student.setSage("77");
//		new StudentDaoImpl().update(student);
		
		
		//删除测试
		//new StudentDaoImpl().del(student);
		//添加测试
//		Student student=new Student();
//		student.setSno("20052430207");
//		student.setSname("yin2");
//		student.setSpwd("12");
//		student.setSage("23");
//		student.setSsex("男");
//		student.setSdept("zzu");
//		new StudentDaoImpl().add(student);
		
		//选课与退课测试
		//Student student=new StudentDaoImpl().serchById("20052430207");
		//Course course=new CourseDaoImpl().serchById("03");
		
		//new StudentDaoImpl().selectCourse(student, course);
		//new StudentDaoImpl().exitCourse(student, course);
		
		//查课
		Student student=new StudentDaoImpl().getStudent(Student.class, "20052430202");
		
		List list=new StudentDaoImpl().findMyCourse2(student);
		for(Iterator it=list.iterator();it.hasNext();)
		{
			Course course=(Course)it.next();
			
			System.out.println("我的选修课程列表cno:"+course.getCno()+" "+course.getCname());
		}
		
	}

	public void selectCourse(Student student, Serializable courseid) {
		try{
		Session se=HibernateSessionFactory.getSession();
		Transaction tx=se.beginTransaction();
		Sc sc=new Sc();
		
		//载入course对象  最好是加上声明式事务,否则应该关闭session
		Course course=(Course)se.load(Course.class, courseid);
		
		
		//Course course=new CourseDaoImpl().getCourse(Course.class, courseid);
		sc.setStudent(student);//注意此处反转之后的参数和数据库表中的不一样
		sc.setCourse(course);
		sc.setGrade(0);
		
		se.save(sc);
		System.out.println("选课ok");
		tx.commit();
		}catch(Exception e)
		{
			if(null!=tx)
			{
				tx.rollback();
			}
		}finally{
			//最好是加上声明式事务,否则应该关闭session
			if(null!=se&&!"".equals(se))
			{
				//se.close();
				se=null;
			}
				
		}
		
		
		
	}

	public void exitCourse(Student student, Course course) {
		
		try{
		Session se=HibernateSessionFactory.getSession();
		Transaction tx=se.beginTransaction();
		Sc sc=new Sc();
		
		ScDaoImpl scdi=new ScDaoImpl();
		//查询到持久化的sc
		sc=scdi.find(student, course);
		
		se.delete(sc);
		System.out.println("退课ok");
		tx.commit();
		}catch(Exception e)
		{
			if(null!=tx)
			{
				tx.rollback();
			}
			e.printStackTrace();
		}finally{
			if(null!=se)
			{
				//se.close();
				se=null;
			}
		}
	}

	public Student getStudent(Class clz, Serializable id) {

		Student student=null;
		se=HibernateSessionFactory.getSession();
		try{
			
			student=(Student)se.get(clz, id);
		}catch(Exception e)
		{
			e.printStackTrace();
		}finally{
			se.close();
		}
		return student;
	}

	public List findAllCourse() {
		List allCourseList = null;
		se = HibernateSessionFactory.getSession();
		String hql = "from Course";
		
		try {
			Query query = se.createQuery(hql);
			allCourseList = query.list();
		} catch (Exception e) {
			System.out.println("studentDaoImpl 类 Exception");
			e.printStackTrace();
		} finally {
			se.close();
		}
		return allCourseList;
		
	}

	public List findMyCourse(Student student) {
		List myCourseList = null;
		se = HibernateSessionFactory.getSession();
		String hql = "from Sc sc where sc.student=?";
		
		try {
			Query query = se.createQuery(hql);
			query.setEntity(0, student);//
			myCourseList = query.list();
		} catch (Exception e) {
			System.out.println("studentDaoImpl 类 Exception");
			e.printStackTrace();
		} finally {
			se.close();
		}
		return myCourseList;
	}

	public List book() {
		List bookList = null;
		se = HibernateSessionFactory.getSession();
		String hql = "from Book";
		
		try {
			Query query = se.createQuery(hql);
			
			bookList = query.list();
		} catch (Exception e) {
			System.out.println("studentDaoImpl 类 Exception");
			e.printStackTrace();
		} finally {
			se.close();
		}
		return bookList;
	}

	public List findMyCourse2(Student student) {
		List myCourseList2 = null;
		se = HibernateSessionFactory.getSession();
		String hql = "select sc.course from Sc sc where sc.student=?";
		
		try {
			Query query = se.createQuery(hql);
			query.setEntity(0, student);
			myCourseList2 = query.list();
		} catch (Exception e) {
			System.out.println("studentDaoImpl 类 Exception");
			e.printStackTrace();
		} finally {
			se.close();
		}
		return myCourseList2;
	}

	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -