导入约束参考网站
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driverproperty>
<property name="hibernate.connection.url">jdbc:mysql:///hibernate_32property>
<property name="hibernate.connection.username">rootproperty>
<property name="hibernate.connection.password">1234property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialectproperty>
<property name="hibernate.show_sql">trueproperty>
<property name="hibernate.format_sql">trueproperty>
<property name="hibernate.hbm2ddl.auto">updateproperty>
<mapping resource="cn/itheima/domain/Customer.hbm.xml" />
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driverproperty>
<property name="hibernate.connection.url">jdbc:mysql:///hibernate_32property>
<property name="hibernate.connection.username">rootproperty>
<property name="hibernate.connection.password">1234property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialectproperty>
<property name="hibernate.show_sql">trueproperty>
<property name="hibernate.format_sql">trueproperty>
<property name="hibernate.hbm2ddl.auto">updateproperty>
<mapping resource="cn/itheima/domain/Customer.hbm.xml" />
session-factory>
hibernate-configuration>
<hibernate-mapping package="cn.xxx.domain" >
<class name="Customer" table="cst_customer" >
<id name="cust_id" column:"cust_id">
<generator class="native">generator>
id>
<property name="cust_source" column="cust_source" >property>
//1 创建,调用空参构造
Configuration conf = new Configuration();
//2 读取指定主配置文件 => 空参加载方法,加载src下的hibernate.cfg.xml文件
conf.configure();
//3 读取指定orm元数据(扩展),如果主配置中已经引入映射配置.不需要手动加载
//conf.addResource(resourceName);
//conf.addClass(persistentClass);
//4 根据配置信息,创建 SessionFactory对象
SessionFactory sf = conf.buildSessionFactory();
Session session = sf.openSession();
//4 session获得操作事务的Transaction对象
//获得操作事务的tx对象
//Transaction tx = session.getTransaction();
//开启事务并获得操作事务的tx对象(建议使用)
Transaction tx2 = session.beginTransaction();
tx2.commit();//提交事务
tx2.rollback();//回滚事务
session.close();//释放资源
sf.close();//释放资源
Transaction tx2 = session.beginTransaction();
//----------------------------------------------
Customer c = new Customer();
c.setCust_name("xxxxx");
session.save(c);
//----------------------------------------------
tx2.commit();//提交事务
Transaction tx2 = session.beginTransaction();
//----------------------------------------------
Customer customer = session.get(Customer.class, 1l);
System.out.println(customer);
//----------------------------------------------
tx2.commit();//提交事务
Transaction tx2 = session.beginTransaction();
//----------------------------------------------
//1 获得要修改的对象
Customer c = session.get(Customer.class, 1l);
//2 修改
c.setCust_name("xxxx");
//3 执行update
session.update(c);
//----------------------------------------------
tx2.commit();//提交事务
Transaction tx2 = session.beginTransaction();
//----------------------------------------------
//1 获得要修改的对象
Customer c = session.get(Customer.class, 1l);
//2 调用delete删除对象
session.delete(c);
//----------------------------------------------
tx2.commit();//提交事务
public class HibernateUtils {
private static SessionFactory sf;//写在外面全局使用
static{//static代码块只执行一次
//1 创建,调用空参构造
Configuration conf = new Configuration().configure();
//2 根据配置信息,创建 SessionFactory对象
sf = conf.buildSessionFactory();
}
//获得session => 获得全新session
public static Session openSession(){
//3 获得session
Session session = sf.openSession();
return session;
}
//获得session => 获得与线程绑定的session
public static Session getCurrentSession(){
//3 获得session
Session session = sf.getCurrentSession();
return session;
}}