基于Hibernate实现CRUD

这篇博客介绍了如何在Java中利用HibernateUtil单态模式配置SessionFactory,并在Dao层实现增删改查操作。配置文件包括Hibernate.cfg.xml,定义了数据库连接参数和session工厂配置。Teacher.hbm.xml和College.hbm.xml分别为Teacher和College对象的映射文件,详细描述了对象属性与数据库表的对应关系。
摘要由CSDN通过智能技术生成

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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值