hibernate开发步骤
(1)创建Hibernate配置文件 (hibernate.cfg.xml)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 配置连接数据库的基本信息 -->
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql:///hibernate</property>
<!-- 配置hibernate基本信息 -->
<!-- hibernate所使用数据库方言 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<!-- 执行操作时是否在打印SQL语句 -->
<property name="hibernate.show_sql">true</property>
<!-- Sql语句格式化 -->
<property name="hibernate.format_sql">true</property>
<!-- 指定自动生成数据表的策略-->
<property name="hbm2ddl.auto">update</property>
<!-- 指定关联的 .hbm.xml 文件 -->
<mapping resource="com/hibernate/helloworld/News.hbm.xml"/>
</session-factory>
</hibernate-configuration>
(2)创建持久化类
package com.hibernate.helloworld;
import java.util.Date;
public class News {
private Integer id;
private String title;
private String author;
private Date date;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public News(String title, String author, Date date) {
super();
this.title = title;
this.author = author;
this.date = date;
}
public News() {
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
return "News [id=" + id + ", title=" + title + ", author=" + author + ", date=" + date + "]";
}
}
(3)创建对象-关系映射关系 (*.hbm.xml)
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2018-7-23 14:48:24 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<class name="com.hibernate.helloworld.News" table="NEWS">
<!-- name="id" 指的上面类中的属性名 -->
<id name="id" type="java.lang.Integer">
<!-- column指表的列名 -->
<column name="ID" />
<!-- 指定主键的生成方式,native:使用数据库本地方式 -->
<generator class="native" />
</id>
<property name="title" type="java.lang.String">
<column name="TITLE" />
</property>
<property name="author" type="java.lang.String">
<column name="AUTHOR" />
</property>
<property name="date" type="java.util.Date">
<column name="DATE" />
</property>
</class>
</hibernate-mapping>
(4)通过Hibernate API编写访问数据库的代码
package com.hibernate.helloworld;
import java.util.Date;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.junit.jupiter.api.Test;
class HibernateTest {
@Test
public void test() {
//1.创建一个SessionFactory对象
SessionFactory sessionFactory = null;
//(1)创建Configuration对象:对象hibernate的基本配置信息和对象关系映射信息
Configuration configuration = new Configuration().configure();
//!!!!和视频中不同
configuration.addClass(News.class);
//(2)创建一个ServiceRegistry 对象
//hibernate的任何配置和服务都需要在该对象中注册后才能有效
ServiceRegistry serviceRegistry=
new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties()).build();
//(3)
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
//2.创建一个Session对象
Session session = sessionFactory.openSession();
//3.开启事务
Transaction transaction = session.beginTransaction();
//4.执行保存操作
News news = new News("java","LIKE", new Date(new java.util.Date().getTime()));
session.save(news);
//5.提交事务
transaction.commit();
//6.关闭Session
session.close();
//7.关闭SessionFactory对象
sessionFactory.close();
}
}