学习网址:https://www.linuxidc.com/Linux/2015-08/121498.htm
1、新建一个Maven项目,导包
(1)目录结构:
(2)因为Hibernate是为了数据持久化的操作更简单,所以必须有mysql-connector-java包
<!-- hibernate 是在jdbc基础上的封装-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
<!-- hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.3.8.Final</version>
</dependency>
2、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>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/blog?useUnicode=true&characterEncoding=UTF-8</property>
<property name="connection.username">root</property>
<property name="connection.password">1</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect方言 -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<!-- 启用Hibernate的自动会话上下文管理 -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache 禁用二级缓存-->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<!-- 显示sql语句 -->
<property name="show_sql">true</property>
<!--格式化输出 -->
<property name="format_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<!-- update会先检查数据库中表是否存在,不存在才创建,存在就不创建;且表结构和映射文件不同时会给表加减相应的列 -->
<!-- create不管表是否存在,都会先删除,再重新创建 -->
<!-- validate检查映射关系和数据库中的表结构是否一致,一致才正常执行,不一致就抛异常 -->
<property name="hbm2ddl.auto">update</property>
<!-- 映射到实体类的配置文件 -->
<mapping resource="mapping/Blog.hbm.xml"/>
</session-factory>
</hibernate-configuration>
3、实体类Blog和对应的Blog.hbm.xml
package entity;
public class Blog {
private Integer id;
private String title;
private String content;
private String type;
private String date;
public Blog() {
super();
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping SYSTEM "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<!-- 一个class标签对应一个实体类,name属性指定实体类名称,table属性指定关联的数据库表 -->
<class name="entity.Blog" table="t_blog_hibernate">
<!-- 主键 -->
<id name="id" column="b_id">
<!-- 主键的生成策略 -->
<generator class="native"></generator>
</id>
<!-- 其他属性,name对应实体类的属性,column对应关系型数据库表的列 -->
<property name="title" column="b_title"></property>
<property name="content" column="b_content"></property>
<property name="type" column="b_type"></property>
<property name="date" column="b_date"></property>
</class>
</hibernate-mapping>
4、Run.java中createTable的作用是hibernate逆向帮我们在数据库中建立表
package run;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.junit.Test;
public class Run {
@Test
public void createTable(){
//读取并初始化hibernate.cfg.xml配置文件
Configuration cfg=new Configuration().configure();
//SchemaExport对象根据Blog.hbm.xml的配置可以用来逆向生成建表语句,即可以创建表
SchemaExport se=new SchemaExport(cfg);
se.create(true, true);
}
}
5、结果