三个准备和七个步骤
准备1:添加hibernate的jar文件
antlr-2.7.6.jar
commons-collections-3.1.jar
dom4j-1.6.1.jar
hibernate-jpa-2.0-api-1.0.0.Final.jar
hibernate3.jar
javassist-3.12.0.GA.jar
jta-1.1.jar
slf4j-api-1.6.1.jar
准备2: 在src下新建hibernate.cfg.xml,并配置hibernate.cfg.xml(核心配置文件)
<hibernate-configuration>
<session-factory>
<!-- 驱动 -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- URL -->
<property name="connection.url">jdbc:mysql://localhost:3306/mydb</property>
<!-- 用户名 -->
<property name="connection.username">root</property>
<!-- 密码 -->
<property name="connection.password">java</property>
<!-- 方言 -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
</session-factory>
</hibernate-configuration>
准备3:编写实体类和映射文件Xxx.hbm.xml
<hibernate-mapping>
<class name="com.xasxt.entity.Userinfo" table="userinfo">
<!-- 主标示,映射数据库中的主键 -->
<id name="uid" column="uid" type="java.lang.Integer">
<!-- 数据库主键自增 -->
<generator class="native"></generator>
</id>
<property name="uname" column="uname"></property>
<property name="upass"></property>
</class>
</hibernate-mapping>
七个步骤:
//1.创建Configuration对象
Configuration cfg = new Configuration().configure();//解析hibernate.cfg.xml
//2.创建SessionFactory
SessionFactory sf = cfg.buildSessionFactory();//解析映射文件xxx.hbm.xml
//3.获取Session:(类似jdbc中的Connection)
Session session = sf.openSession();
//4.开启事务
Transaction tx = session.beginTransaction();
//5.持久化操作
session.save(user);
//6.提交事务
tx.commit();
//7.关闭session
session.close();
准备1:添加hibernate的jar文件
antlr-2.7.6.jar
commons-collections-3.1.jar
dom4j-1.6.1.jar
hibernate-jpa-2.0-api-1.0.0.Final.jar
hibernate3.jar
javassist-3.12.0.GA.jar
jta-1.1.jar
slf4j-api-1.6.1.jar
准备2: 在src下新建hibernate.cfg.xml,并配置hibernate.cfg.xml(核心配置文件)
<hibernate-configuration>
<session-factory>
<!-- 驱动 -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- URL -->
<property name="connection.url">jdbc:mysql://localhost:3306/mydb</property>
<!-- 用户名 -->
<property name="connection.username">root</property>
<!-- 密码 -->
<property name="connection.password">java</property>
<!-- 方言 -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
</session-factory>
</hibernate-configuration>
准备3:编写实体类和映射文件Xxx.hbm.xml
<hibernate-mapping>
<class name="com.xasxt.entity.Userinfo" table="userinfo">
<!-- 主标示,映射数据库中的主键 -->
<id name="uid" column="uid" type="java.lang.Integer">
<!-- 数据库主键自增 -->
<generator class="native"></generator>
</id>
<property name="uname" column="uname"></property>
<property name="upass"></property>
</class>
</hibernate-mapping>
七个步骤:
//1.创建Configuration对象
Configuration cfg = new Configuration().configure();//解析hibernate.cfg.xml
//2.创建SessionFactory
SessionFactory sf = cfg.buildSessionFactory();//解析映射文件xxx.hbm.xml
//3.获取Session:(类似jdbc中的Connection)
Session session = sf.openSession();
//4.开启事务
Transaction tx = session.beginTransaction();
//5.持久化操作
session.save(user);
//6.提交事务
tx.commit();
//7.关闭session
session.close();