HibernateUtil.java:
//SessionFaction单态模式
public class HibernateUtil {
private static SessionFactory sf=null;
private static Configuration cfg=null;
static {//在类加载的时候只能加载一次
try {
cfg = new Configuration().configure();
sf=cfg.buildSessionFactory();
} catch (HibernateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static SessionFactory getSessionFactory() {
return sf;
}
public static void closeSessionFactory(){
sf.close();
}
}
实现接口方法
Dao层
public class TeacherDaoImpl implements TeacherDao {
private SessionFactory sf = HibernateUtil.getSessionFactory();
private Session session = sf.getCurrentSession();
private Transaction ts = session.beginTransaction();
private Query query = null;
@Override
public void addTeacher(Teacher t) {
// TODO Auto-generated method stub
session.save(t);
ts.commit();
}
@Override
public void deleteTeacher(int id) {
// TODO Auto-generated method stub
Teacher teacher = session.get(Teacher.class, id);//先获得对象
session.delete(teacher);//再删除对象
ts.commit();
}
@Override
public void updateTeacher(Teacher t,College c) {
// TODO Auto-generated method stub
Teacher teacher =new Teacher();
teacher.setId(t.getId());
teacher.setName(t.getName());
teacher.setName(t.getName());
teacher.setGender(t.getGender());
teacher.setAge(t.getAge());
teacher.setCourse(t.getCourse());
teacher.setPhone(t.getPhone());
teacher.setCollege(t.getCollege());
session.update(teacher);
ts.commit();
}
@Override
public List<Teacher> queryTeacher() {
// TODO Auto-generated method stub
query = session.createQuery(" from Teacher");
List<Teacher> list = query.list();
return list;
}
}
Hibernate.cfg.xml:
<hibernate-configuration>
<session-factory >
<property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
<property name="hibernate.connection.driver_class ">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernateTest?useSSL=false</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password"> </property>
<property name="show_sql">true</property>
<!-- 保证每个读写线程有唯一的session实例 -->
<property name="hibernate.current_session_context_class">thread</property>
<mapping resource="cn/hrbust/pojo/Teacher.hbm.xml"/>
<mapping resource="cn/hrbust/pojo/College.hbm.xml"/>
</session-factory>
</hibernate-configuratio
Teacher.hbm.xml
<hibernate-mapping>
<class name="cn.hrbust.pojo.Teacher" table="T_Teacher" select-before-update="true" dynamic-update="true">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="name"/>
<property name="gender"/>
<property name="age"/>
<property name="course"/>
<property name="phone"/>
<many-to-one name="college" column="collegeid" class="cn.hrbust.pojo.College" cascade="save" not-null="true" />
</class>
</hibernate-mapping>
College.hbm.xml:
<hibernate-mapping>
<class name="cn.hrbust.pojo.College" table="T_College" select-before-update="true" dynamic-update="true">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="name" column="name" />
</class>
</hibernate-mapping>