Hibernate
ORM(Object Relational Mapping)对象关系映射
一、搭建环境
1.JBoss插件
安装JBoss插件,该插件内部已经具备了Hibernate
Help-> EclipseMarketplace中进行搜索JBoss Tool然后安装即可
2.导入所需要的jar包
(1)hibernate包
从百度里边搜索hibernate下载,然后将required中的jar包导入即可
(2)mysql驱动jar包
https://dev.mysql.com/downloads/connector/j/
3.开发流程
(1)创建Hibernate的配置文件
(2)创建持久化类
(3)创建对象—关系映射文件
(4)通过Hibernate API编写访问数据库的代码
3.1创建Hibernate的配置文件
—/project/etc/hibernate.properties 具备了每种数据库需要的配置信息
Eg:
## MySQL
#hibernate.dialect org.hibernate.dialect.MySQLDialect
#hibernate.dialect org.hibernate.dialect.MySQLInnoDBDialect
#hibernate.dialect org.hibernate.dialect.MySQLMyISAMDialect
#hibernate.connection.driver_class com.mysql.jdbc.Driver
#hibernate.connection.url jdbc:mysql:///test
#hibernate.connection.username gavin
#hibernate.connection.password
<session-factory>
<!-- 指定连接数据库所用的驱动 -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- 指定连接数据库的url,hibernate连接的数据库名 -->
<property name="connection.url">jdbc:mysql://localhost/数据库名</property>
<!-- 指定连接数据库的用户名 -->
<property name="connection.username">root</property>
<!-- 指定连接数据库的密码 -->
<property name="connection.password">32147</property>
<!-- 指定连接池里最大连接数 -->
<property name="hibernate.c3p0.max_size">20</property>
<!-- 指定连接池里最小连接数 -->
<property name="hibernate.c3p0.min_size">1</property>
<!-- 指定连接池里连接的超时时长 -->
<property name="hibernate.c3p0.timeout">5000</property>
<!-- 指定连接池里最大缓存多少个Statement对象 -->
<property name="hibernate.c3p0.max_statements">100</property>
<property name="hibernate.c3p0.idle_test_period">3000</property>
<property name="hibernate.c3p0.acquire_increment">2</property>
<property name="hibernate.c3p0.validate">true</property>
<!-- 指定数据库方言 -->
<property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
<!-- 根据需要自动创建数据表 -->
<property name="hbm2ddl.auto">update</property>
<!-- 显示Hibernate持久化操作所生成的SQL -->
<property name="show_sql">true</property>
<!-- 将SQL脚本进行格式化后再输出 -->
<property name="hibernate.format_sql">true</property>
<!-- 罗列所有的映射文件 -->
<mapping resource="映射文件路径/News.hbm.xml"/>
</session-factory>
https://www.cnblogs.com/lcycn/p/8126783.html
<?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 name="">
<!-- 配置连接数据库基本信息 -->
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">XXX</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://xxx.xxx.xxx.xxx/test</property>
<!-- 配置hibernate的基本信息
hibernate所使用的数据库方言 -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 是否在控制台显示SQL语句 -->
<property name="hibernate.show_sql">true</property>
<!-- 根据需要自动创建数据表 -->
<property name="hbm2ddl.auto">update</property>
<!-- 将SQL脚本进行格式化后再输出 -->
<property name="hibernate.format_sql">true</property>
<mapping resource="com/nhfc99/hibernate/helloworld/News.hbm.xml"/>
</session-factory>
</hibernate-configuration>
3.2持久化类
package com.nhfc99.hibernate.helloworld;
import java.sql.Date;
public class News {
private int id;
private String title;
private String author;
private Date date;
public int getId() {
return id;
}
public void setId(int 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() {
super();
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
return "News [id=" + id + ", title=" + title + ", author=" + author + ", date=" + date + "]";
}
}
3.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-12-19 8:57:42 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<class name="com.nhfc99.hibernate.helloworld.News" table="NEWS">
<id name="id" type="int">
<column name="ID" />
<!-- 指定主键的生成方式 -->
<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.sql.Date">
<column name="DATE" />
</property>
</class>
</hibernate-mapping>
3.4通过Hibernate API编写访问数据库的代码
package com.nhfc99.hibernate.helloworld;
import static org.junit.jupiter.api.Assertions.*;
import java.sql.*;
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
void test() {
//1.创建一个SessionFactory对象
SessionFactory sessionFactory = null;
//1)创建Configuration对象:对应hibernate的基本配置信息和对象关系映射信息
Configuration configuration = new Configuration().configure();
configuration.addClass(News.class);
//创建服务注册对象
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
//2.创建一个Session对象
Session session = sessionFactory.openSession();
//3.开启事物
Transaction transaction = session.beginTransaction();
//4.执行保存操作
News news = new News("测试", "作者", new Date(10000));
session.save(news);
//5.提交事物
transaction.commit();
//6.关闭Session对象
session.close();
//7.关闭SessionFactory对象
sessionFactory.close();
}
}