package test01;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import java.util.HashMap;
public class TestMain {
public static void main(String[] args) {
//1.读取hibernate.cfg.xml的配置信息,获取数据库链接
Configuration config = new Configuration().configure();
//2.创建SessionFactory
SessionFactory sessionFactory = config.buildSessionFactory();
//3.打开Sesssion
Session session = sessionFactory.openSession();
//4创建并启动事务Transation
session.beginTransaction();
//5.操作数据,持久化操作
//Product product = new Product();//对象状态是临时的
// product.setPname("iphone12");
// product.setPrice(7000);
// product.setCategory_id("c001");
// session.save(product);//这就是插入数据,对象状态是持久化
Product product = (Product)session.get(Product.class,1);
System.out.println("id=1的产品名称是: "+product.getPname());
//6.提交事务
session.getTransaction().commit();
session.close();//对象脱管
sessionFactory.close();
}
}
hibernate的初探
最新推荐文章于 2021-05-23 11:55:08 发布