Hibernate创建步骤
(五大核心接口:Configuration/SessionFactory/Session/Transaction/Query)
1.新建java工程,导入需要的jar包。
2.创建hibernate.cfg.xml配置文件和Test.java工具类。配置好相应的实体对象User.java User.hbm.xml 生成的主要内容如下:
3.导入mysql的jar包 mysql-connector-java-5.1.7-bin.jar
hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://127.0.0.1:3306/hib</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">admin</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.show_sql">true</property> <property name="show_sql">true</property> <property name="hibernate.hbm2ddl.auto">create</property> <mapping resource="com/entity/User.hbm.xml"/> </session-factory> </hibernate-configuration>
Test.java
package javaHibernateDemo; import org.hibernate.HibernateException; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.hibernate.classic.Session; import com.entity.User; public class Test { public static void main(String[] args) { //通过Configuration调用 configure方法 去查找 项目中classpath下hibernate.cfg.xml文件 //通过hibernate.cfg.xml文件中的信息 创造sessionfactory SessionFactory factory = new Configuration().configure() .buildSessionFactory(); //SessionFactory 制造session 类似与JDBC中的连接,通过session可以访问数据库 Session session = factory.openSession(); Transaction tx = session.beginTransaction();// 开启事务 try { User user = new User();// 造对象 user.setAge(34); user.setName("litiang"); user.setPassword("123456"); session.save(user);// 把对象保存到数据库 tx.commit();// 提交事务 } catch (HibernateException e) { if (null != tx) tx.rollback(); } finally { session.close(); } } }
User.hbm.xml
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.entity.User" table="User"> <id name="id" column="ID" type="java.lang.Integer"> <generator class="native" /> </id> <property name="name" column="name" type="java.lang.String" /> <property name="password" column="password" type="java.lang.String" /> <property name="age" column="age" type="java.lang.Integer" /> </class> </hibernate-mapping>
User.java
package com.entity; public class User { private int id; private String name; private String password; private int age; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }