第一步:下载hibernate
去hibernate官网下载hibernate的最新版
http://hibernate.org/orm/downloads/
将下载的hibernate-release-5.0.6.Final.zip解压
hibernate的目录结构
documentation目录是hibernate的文档
lib目录是hibernate所需jar包
project是hibernate提供的示例
changelog.txt是hibernate的更新说明
hibernate_logo.gif是hibernate的图标
lgpl.txt是一个LICENSE
第二步:添加jar包
将lib目录下required文件夹中的所有jar包添加到项目的classpath下
将lib/optional/c3p0目录下的所有jar包添加到项目的classpath下
hibernate还需要连接数据库,将mysql-connector-java-xxx.jar添加到项目的classpath下
第三步:编写hibernate配置文件hibernate.cfg.xml
hibernate.cfg.xml如下
<?xml version="1.0" encoding="GBK"?>
<!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="connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- 指定连接数据库的url,其中hibernate是本应用连接的数据库名 -->
<property name="connection.url">jdbc:mysql://localhost/hibernate_test</property>
<!-- 指定连接数据库的用户名 -->
<property name="connection.username">root</property>
<!-- 指定连接数据库的密码 -->
<property name="connection.password">cheng</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.MySQL5InnoDBDialect</property>
<!-- 根据需要自动创建数据表 -->
<property name="hbm2ddl.auto">update</property><!--①-->
<!-- 显示Hibernate持久化操作所生成的SQL -->
<property name="show_sql">true</property>
<!-- 将SQL脚本进行格式化后再输出 -->
<property name="hibernate.format_sql">true</property>
<!-- 避免这个错误信息Disabling contextual LOB creation as createClob() method threw error :java.lang.reflect.InvocationTargetException -->
<property name="hibernate.temp.use_jdbc_metadata_defaults">false</property>
<!-- 罗列所有持久化类的类名 -->
<mapping class="com.test.dao.News"/>
</session-factory>
</hibernate-configuration>
将hibernate.cfg.xml放在src目录下
第四步:编写pojo并添加注解
package com.test.dao;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="news_inf")
public class News {
@Id//主键
@GeneratedValue(strategy=GenerationType.IDENTITY)//自动增长
private Integer id;
// 消息标题
private String title;
// 消息内容
private String content;
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 getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
第五步:测试
package com.test.dao;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
public class Test {
public static void main(String[] args) {
final StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
.configure() // configures settings from hibernate.cfg.xml
.build();
SessionFactory sessionFactory = null;
Session session = null;
try {
sessionFactory = new MetadataSources( registry ).buildMetadata().buildSessionFactory();
session = sessionFactory.openSession();
//开始事务
session.beginTransaction();
//创建对象
News news = new News();
news.setContent("123");
news.setTitle("678");
//保存对象
session.save(news);
//提交事务
session.getTransaction().commit();
//关闭session
}
catch (Exception e) {
session.getTransaction().rollback();
StandardServiceRegistryBuilder.destroy( registry );
}finally{
session.close();
sessionFactory.close();
}
}
}
如图
参考文献