📄 scdaoimpl.java
字号:
package com.zzu.dao.impl;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.zzu.biz.impl.StudentBizImpl;
import com.zzu.dao.ScDao;
import com.zzu.dao.entity.Course;
import com.zzu.dao.entity.Sc;
import com.zzu.dao.entity.Student;
import com.zzu.dao.entity.Tc;
public class ScDaoImpl implements ScDao {
Session se=null;
Transaction tx=null;
public Sc find(Student student, Course course) {
Sc sc=null;
Session se=HibernateSessionFactory.getSession();
String hql="from Sc sc where sc.student=? and sc.course=?";
Query query=se.createQuery(hql);
query.setEntity(0, student);
query.setEntity(1, course);
sc=(Sc)query.uniqueResult();
if(sc!=null)
{
System.out.println("成功查询到一条:学生-课程信息:"+sc.getStudent().getSname()+sc.getCourse().getCname());
}
// List list=query.list();
// for(Iterator it=list.iterator();it.hasNext();)
// {
// Sc sc=(Sc)it.next();
// System.out.println("sc:"+sc.getStudent().getSname()+sc.getCourse().getCname());
//
// }
return sc;
}
public static void main(String[] args)
{
//new StudentDaoImpl().login("20052430202", "1");
//new StudentDaoImpl().serchById("20052430206");
// Student student=new StudentDaoImpl().serchById("20052430206");
// Course course=new CourseDaoImpl().serchById("01");
//
// new StudentDaoImpl().selectCourse(student, course);
// new ScDaoImpl().find(student, course);
//测试学生查询成绩
Student student=new StudentDaoImpl().getStudent(Student.class, "20052430205");
List list=new ScDaoImpl().findByStudent(student);
for(Iterator it=list.iterator();it.hasNext();)
{
Sc sc=(Sc)it.next();
//System.out.println("我的选修课程列表:"+sc.getGrade());
System.out.println("我的选修课程列表:"+sc.getStudent().getSname()+"-"+sc.getCourse().getCname()+":"+sc.getGrade());
}
}
public void add(Sc sc) {
try {
se = HibernateSessionFactory.getSession();
tx = se.beginTransaction();
System.out.println(sc.getStudent().getSname()+"-"+sc.getCourse().getCname());
se.save(sc);
tx.commit();
System.out.println("学生-课程信息添加成功!");
} catch (Exception e) {
if (null != tx)
tx.rollback();
e.printStackTrace();
} finally {
se.close();
}
}
public void del(Sc sc) {
try {
se = HibernateSessionFactory.getSession();
tx = se.beginTransaction();
se.delete(sc);
tx.commit();
System.out.println("学生-课程信息删除成功!");
} catch (Exception e) {
if (null != tx)
tx.rollback();
e.printStackTrace();
} finally {
se.close();// 关闭 Session
}
}
public void update(Sc sc) {
try{
se=HibernateSessionFactory.getSession();
tx=se.beginTransaction();
//sc.setTeacher(teacher);
//sc.setCourse(course);
se.update(sc);
System.out.println("学生-课程信息修改成功!");
tx.commit();
}catch(Exception e)
{
if(null!=tx)
{
tx.rollback();
}
}finally{
se.close();
}
}
public List findByStudent(Student student) {
List stuGradeList = null;
se = HibernateSessionFactory.getSession();
String hql = "from Sc sc where sc.student=?";
try {
Query query = se.createQuery(hql);
query.setEntity(0, student);
stuGradeList = query.list();
System.out.println("come in");
} catch (Exception e) {
System.out.println("ScDaoImpl 类 Exception");
e.printStackTrace();
} finally {
se.close();
}
return stuGradeList;
}
public void del(String sno, String courseid) {
// TODO Auto-generated method stub
}
public void del(Student student, Course course) {
try {
se = HibernateSessionFactory.getSession();
tx = se.beginTransaction();
Sc sc=new Sc();
sc=this.find(student, course);
se.delete(sc);
System.out.println("成功删除一条:学生-课程信息:"+student.getSname()+course.getCname());
tx.commit();
} catch (Exception e) {
if (null != tx)
tx.rollback();
e.printStackTrace();
} finally {
se.close();// 关闭 Session
}
// List list=query.list();
// for(Iterator it=list.iterator();it.hasNext();)
// {
// Sc sc=(Sc)it.next();
// System.out.println("sc:"+sc.getStudent().getSname()+sc.getCourse().getCname());
//
// }
}
public List findAllSc() {
List scList = null;
se = HibernateSessionFactory.getSession();
String hql = "from Sc sc";
try {
Query query = se.createQuery(hql);
scList = query.list();
} catch (Exception e) {
System.out.println("ScDaoImpl 类 Exception");
e.printStackTrace();
} finally {
se.close();
}
return scList;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -