Hibernate (操作步骤)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Hibernate (操作步骤)相关的知识,希望对你有一定的参考价值。
在java工程里导入Hibernate架包:
在添加数据库架包如:
Hibernate开发步骤:
1、Eclipse下创建Hibernate配置文件(需要tools插件)
new---->other---->Hibernate Configuration File(cfg.xml) 点击next----finish 创建下图文件,然后配置dtd文件
然后通过下图的properties文件,编写我们的hibernate.cfg.xml文件
常用的配置如下(至此第一步完成)
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE hibernate-configuration PUBLIC 3 "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 4 "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 5 <hibernate-configuration> 6 <session-factory> 7 <!-- 配置连接数据库的基本信息 --> 8 <property name="connection.username">root</property> 9 <property name="connection.password">root</property> 10 <property name="connection.driver_class">com.mysql.jdbc.Driver</property> 11 <property name="connection.url">jdbc:mysql:///hibernate5</property> 12 13 <!-- 配置hibernate的基本信息 --> 14 <!-- hibernate所使用的数据库方言 --> 15 <property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property> 16 17 <!-- 执行操作时是否在控制台打印SQL --> 18 <property name="show_sql">true</property> 19 20 <!-- 是否对SQL进行格式化 (分行显示)--> 21 <property name="format_sql">true</property> 22 23 <!-- 指定自动生成数据表的策略 --> 24 <property name="hbm2ddl.auto">update</property> 25 26 27 </session-factory> 28 </hibernate-configuration>
2、创建持久化类
1 package com.tzy.hibernate; 2 3 import java.util.Date; 4 5 public class News { 6 private Integer id; 7 private String title; 8 private String author; 9 private Date date; 10 public Integer getId() { 11 return id; 12 } 13 public void setId(Integer id) { 14 this.id = id; 15 } 16 public String getTitle() { 17 return title; 18 } 19 public void setTitle(String title) { 20 this.title = title; 21 } 22 public String getAuthor() { 23 return author; 24 } 25 public void setAuthor(String author) { 26 this.author = author; 27 } 28 public Date getDate() { 29 return date; 30 } 31 public void setDate(Date date) { 32 this.date = date; 33 } 34 35 public News(String title, String author, Date date) { 36 super(); 37 this.title = title; 38 this.author = author; 39 this.date = date; 40 } 41 public News() { 42 super(); 43 } 44 @Override 45 public String toString() { 46 return "News [id=" + id + ", title=" + title + ", author=" + author + "]"; 47 } 48 49 }
3、创建对象-关系-映射文件
new---->other---->Hibernate XML Mapping file(hbm.xml) 点击next-next-next----finish 创建下图文件
1 <?xml version="1.0"?> 2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 4 <!-- Generated 2017-11-19 16:22:35 by Hibernate Tools 3.4.0.CR1 --> 5 <hibernate-mapping> 6 <class name="com.tzy.hibernate.News" table="NEWS"> 7 <id name="id" type="java.lang.Integer"> 8 <column name="ID" /> 9 <!-- 指定逐渐生成的方式,native:使用数据库本地方式 --> 10 <generator class="native" /> 11 </id> 12 13 <property name="title" type="java.lang.String"> 14 <column name="TITLE" /> 15 </property> 16 17 <property name="author" type="java.lang.String"> 18 <column name="AUTHOR" /> 19 </property> 20 21 <property name="date" type="java.util.Date"> 22 <column name="DATE" /> 23 </property> 24 25 </class> 26 </hibernate-mapping>
然后在hibernate.cfg.xml里面建立映射关系
4、通过Hibernate API编写访问数据库的代码
1 package com.tzy.hibernate; 2 3 import java.util.Date; 4 5 import org.hibernate.Session; 6 import org.hibernate.SessionFactory; 7 import org.hibernate.Transaction; 8 import org.hibernate.boot.MetadataSources; 9 import org.hibernate.boot.registry.StandardServiceRegistry; 10 import org.hibernate.boot.registry.StandardServiceRegistryBuilder; 11 import org.junit.Test; 12 13 public class HibernateTest { 14 15 @Test 16 public void test() { 17 //1.创建一个SessionFactory对象 18 SessionFactory sessionFactory = null; 19 20 //hibernate4获取sessionFactory办法 21 //1).创建Configuration对象:对应hibernate的基本配置信息和关系映射信息 22 //Configuration configuration = new Configuration().configure(); 23 //4.0之前这样创建 24 //sessionFactory = configuration.buildSessionFactory(); 25 //2).创建一个ServiceRegistry对象:hibernate 4.x新添加的对象 26 //hibernate的任何配置和服务都需要在该对象中注册后才能有效。 27 //ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()) 28 // .buildServiceRegistry(); 29 //3). 30 //sessionFactory = configuration.buildSessionFactory(serviceRegistry); 31 32 //4). 33 //hibernate5获取sessionFactory办法 34 //创建StandardServiceRegistry对象(标准服务注册) 35 StandardServiceRegistry standardServiceRegistry = new StandardServiceRegistryBuilder().configure().build(); 36 37 sessionFactory = new MetadataSources(standardServiceRegistry).buildMetadata().buildSessionFactory(); 38 //2.创建一个Session对象 39 Session session = sessionFactory.openSession(); 40 41 //3.开启事物 42 Transaction transaction = session.beginTransaction(); 43 44 //4.执行保存操作 45 News news = new News("Java", "Tzy", new Date()); 46 session.save(news); 47 48 //5.提交事物 49 transaction.commit(); 50 51 //6.关闭Session 52 session.close(); 53 54 //7.关闭SessionFactory对象 55 sessionFactory.close(); 56 } 57 58 }
1.创建一个SessionFactory对象2.创建一个Session对象3.开启事物4.执行保存操作5.提交事物6.关闭Session7.关闭SessionFactory对象
操作执行完毕(表自动创建---cfg.xml里的生成数据表策略)
以上是关于Hibernate (操作步骤)的主要内容,如果未能解决你的问题,请参考以下文章