📄 tcdaoimpl.java
字号:
package com.zzu.dao.impl;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.zzu.dao.TcDao;
import com.zzu.dao.entity.Course;
import com.zzu.dao.entity.Sc;
import com.zzu.dao.entity.Student;
import com.zzu.dao.entity.Tc;
import com.zzu.dao.entity.Teacher;
public class TcDaoImpl implements TcDao {
Session se=null;
Transaction tx=null;
public void add(Tc tc) {
try {
se = HibernateSessionFactory.getSession();
tx = se.beginTransaction();
System.out.println(tc.getTcId());
se.save(tc);
tx.commit();
System.out.println("教师-课程信息添加成功!");
} catch (Exception e) {
if (null != tx)
tx.rollback();
e.printStackTrace();
} finally {
se.close();
}
}
public void del(Tc tc) {
try {
se = HibernateSessionFactory.getSession();
tx = se.beginTransaction();
se.delete(tc);
tx.commit();
System.out.println("教师-课程信息删除成功!");
} catch (Exception e) {
if (null != tx)
tx.rollback();
e.printStackTrace();
} finally {
se.close();// 关闭 Session
}
}
public Tc find(Teacher teacher, Course course) {
Tc tc=null;
Session se=HibernateSessionFactory.getSession();
String hql="from Tc tc where tc.teacher=? and tc.course=?";
Query query=se.createQuery(hql);
query.setEntity(0, teacher);
query.setEntity(1, course);
tc=(Tc)query.uniqueResult();
if(tc!=null)
{
System.out.println("tc:"+tc.getTeacher().getTname()+tc.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 tc;
}
public void update(Tc tc,Teacher teacher,Course course) {
try{
se=HibernateSessionFactory.getSession();
tx=se.beginTransaction();
tc.setTeacher(teacher);
tc.setCourse(course);
se.update(tc);
System.out.println("教师-课程信息修改成功!");
tx.commit();
}catch(Exception e)
{
if(null!=tx)
{
tx.rollback();
}
}finally{
se.close();
}
}
public List find(Teacher teacher) {
List tcList = null;
se = HibernateSessionFactory.getSession();
String hql = "from Tc tc where tc.teacher=?";
try {
Query query = se.createQuery(hql);
query.setEntity(0, teacher);
tcList = query.list();
} catch (Exception e) {
System.out.println("TcDaoImpl 类 Exception");
e.printStackTrace();
} finally {
se.close();
}
return tcList;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -