1.hibernate.cfg.xml 配置
1<!DOCTYPE hibernate-configuration PUBLIC 2 "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 3 "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 4 5<hibernate-configuration> 6 <session-factory> 7 <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> 8 <property name="hibernate.connection.url">jdbc:mysql://127.0.0.1:3306/student</property> 9 <property name="hibernate.connection.username">root</property> 10 <property name="hibernate.connection.password"></property> 11 <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> 12 <property name="hibernate.show_sql">true</property><!-- 将SQL语句打印到控制台 --> 13 <property name="hibernate.format_sql">true</property><!-- 将SQL语句格式化后,打印到控制台 --> 14 15 16 <mapping resource="com/hibernate/User.hbm.xml"/> 17 </session-factory> 18</hibernate-configuration>
2.User.hbm.xml 配置
1<?xml version="1.0" encoding="UTF-8"?> 2<!DOCTYPE hibernate-mapping PUBLIC 3 "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 4 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 5<hibernate-mapping> 6 <class name="com.hibernate.User"> 7 <id name="id"> 8 <generator class="uuid"/><!-- 自动生成一个32位的字符串,必须提供生成策略 --> 9 </id> 10 <property name="name"/> 11 <property name="password"/> 12 </class> 13 14</hibernate-mapping>
3.创建User实体
1package com.hibernate; 2 3public class User { 4 5 private String id; 6 private String name; 7 private String password; 8 //private String rights; 9 public String getId() { 10 return id; 11 } 12 public void setId(String id) { 13 this.id = id; 14 } 15 public String getName() { 16 return name; 17 } 18 public void setName(String name) { 19 this.name = name; 20 } 21 public String getPassword() { 22 return password; 23 } 24 public void setPassword(String password) { 25 this.password = password; 26 } 27}
4.ExportDB类(根据xml文件生成对应数据库的表)
1package com.hibernate; 2 3import org.hibernate.cfg.Configuration; 4import org.hibernate.tool.hbm2ddl.SchemaExport; 5 6/* 7 * 将hbm生成ddl 8 */ 9 10public class ExportDB { 11 12 public static void main(String[] args) { 13 //默认读取hibernate.cfg.xml文件 14 Configuration cfg = new Configuration().configure(); 15 SchemaExport export = new SchemaExport(cfg); 16 export.create(true, true); 17 } 18}
5.测试类Client
1package com.hibernate; 2 3import org.hibernate.Session; 4import org.hibernate.SessionFactory; 5import org.hibernate.cfg.Configuration; 6 7public class Client { 8 public static void main(String[] args) { 9 // org.hibernate.cfg.Configuration类的作用: 10 // 读取hibernate配置文件(hibernate.cfg.xml或hiberante.properties)的. 11 // new Configuration()默认是读取hibernate.properties 12 // 所以使用new Configuration().configure();来读取hibernate.cfg.xml配置文件 13 // 默认读取hibernate.cfg.xml文件 14 Configuration cfg = new Configuration().configure(); 15 16 // 创建SessionFactory 17 // 一个数据库对应一个SessionFactory 18 // SessionFactory是线线程安全的(最好创建一次) 19 SessionFactory factory = cfg.buildSessionFactory(); 20 21 // 创建session 22 // 此处的session并不是web中的session 23 // session只有在用时,才建立concation,session还管理缓存。 24 // session用完后,必须关闭。 25 // session是非线程安全,一般是一个请求一个session. 26 Session session = null; 27 try { 28 session = factory.openSession(); 29 //手动开启事务(可以在hibernate.cfg.xml配置文件中配置自动开启事务) 30 session.beginTransaction(); 31 32 // 保存数据,此处的数据是保存对象,这就是hibernate操作对象的好处, 33 // 我们不用写那么多的JDBC代码,只要利用session操作对象,至于hibernat如何存在对象,这不需要我们去关心它, 34 // 这些都有hibernate来完成。我们只要将对象创建完后,交给hibernate就可以了。 35 User user =new User(); 36 user.setName("Lee"); 37 user.setPassword("00"); 38 session.save(user); 39 //提交事物 40 session.getTransaction().commit(); 41 } catch (Exception e) { 42 e.printStackTrace(); 43 //回滚事物 44 session.getTransaction().rollback(); 45 }finally{ 46 if (session!=null) { 47 if(session.isOpen()) 48 //关闭session 49 session.close(); 50 } 51 } 52 } 53 54}