认识了Hibernate项目以后,下面来搭建一个Hibernate项目。
1、首先,创建一个java项目。
2、创建User Library,加入依赖包。
Windows-> Preferences,然后按下图操作
建好jar包库后,点上图右边按钮(Add JARs)引入jar包如下。
*HIBERNATE_HOME/lib/*.jar
*HIBERNATE_HOME/hibernate3.jar
*加入数据库驱动(mysql驱动)
Project->Properties,然后如下图操作。
3、提供hibernate.cfg.xml文件,完成基本的配置。
<span style="font-size:18px;"><!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate_first</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">130427</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
</session-factory>
</hibernate-configuration></span>
4、建立实体类User.java
<span style="font-size:18px;">package com.tgb.hibernate;
import java.sql.Date;
public class User {
private String id;
private String name;
private String password;
private Date createTime;
private Date expireTime;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public Date getExpireTime() {
return expireTime;
}
public void setExpireTime(Date expireTime) {
this.expireTime = expireTime;
}
}
</span>
5、提供User.hbm.xml文件,完成实体类的映射
<span style="font-size:18px;"><?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.tgb.hibernate.User">
<id name="id">
<generator class="uuid"/><!--写上这个之后,会生成一个32位的字符串 -->
</id>
<property name="name"/><!--如果想用别名,那么就用Column="user_name" -->
<property name="password"/>
<property name="createTime"/>
<property name="expireTime"/>
</class>
</hibernate-mapping></span>
6、将User.hbm.xml文件加入到hibernate.cfg.xml文件中
<span style="font-size:18px;"><mapping resource="com/tgb/hibernate/User.hbm.xml"/></span>
7、编写工具类ExportDB.java,将hbm生成ddl,也就是hbm2ddl。
<span style="font-size:18px;">package com.tgb.hibernate;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
/**
* 将hbm生成ddl
* @author David
*
*/
public class ExportDB {
public static void main(String[] args) {
//默认读取hibernate.fig.xml文件
Configuration cfg = new Configuration().configure();
SchemaExport export = new SchemaExport(cfg);
export.create(true, true);
}
}
</span>
运行此main方法,自动根据User实体导出数据库表。8、建立客户端类Client,添加用户数据到mysql
<span style="font-size:18px;">package com.tgb.hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class Client {
public static void main(String[] args) {
//读取hibernate.cfg.xml文件
Configuration cfg = new Configuration().configure();
//建立SessionFactory
SessionFactory factory = cfg.buildSessionFactory();
//取得session
Session session = null;
try{
session = factory.openSession();
//开启事务
session.beginTransaction();
User user = new User();
user.setName("张三");
user.setPassword("123");
user.setCreateTime(new java.sql.Date(new java.util.Date().getTime()));
user.setExpireTime(new java.sql.Date(new java.util.Date().getTime()));
//保存User对象
session.save(user);
//提交事务
session.getTransaction().commit();
}
catch(Exception e)
{
e.printStackTrace();
//回滚事务
session.getTransaction().rollback();
}finally{
if(session.isOpen()){
//关闭session
session.close();
}
}
}
}
</span>
运行此main方法,向数据库表中插入数据
最好在hibernate.cfg.xml中加入如下配置,方便观察hibernate sql生成:
<span style="font-size:18px;"> <property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property></span>
最好加入log4j配置文件,将该配置文件拷贝到src下,便于程序的调试。小结
此为一个简单的hibernate的项目的建立实例,供大家参考学习!