上篇博客利用Hibernate搭建起一个简单的例子,把数据库的映射显示了出来在上一篇的博客基础上这篇博客讲述如何利用Hinbernate框架实现简单的数据库操作。
1、加入junit.jar
2、新建一个工具类HibernateUtils.Java把一些对session的公共操作封装起来
import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class HibernateUtils { /* *读取Hibernate.cfg.xml文件 */ private static SessionFactory factory; static { try { //读取hibernate.cfg.xml文件 Configuration cfg = new Configuration().configure(); //建立SessionFactory factory = cfg.buildSessionFactory(); }catch(Exception e) { e.printStackTrace(); } } /* *打开Session */ public static Session getSession() { return factory.openSession(); } /* *关闭Session */
菜鸟学习Hibernate——简单的增、删、改、查操作
最新推荐文章于 2021-06-16 15:42:05 发布