一、Hibernate开发的环境搭建
1、在Hibernate的环境搭建开始之前需要先引入Hibernate核心包以及Hibernate依赖包
2、加入数据库驱动。下面例子中主要采用MySQL数据库进行演示,所以也要导入MySQL的JDBC驱动
3、在src文件夹下添加核心配置文件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">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<!-- mysql数据库驱动 -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- mysql数据库名称 -->
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate_first</property>
<!-- 数据库的登录用户名 -->
<property name="hibernate.connection.username">root</property>
<!-- 数据库的登录密码 -->
<property name="hibernate.connection.password">14132</property>
<!-- 方言:为每一种数据库提供适配器吗,方便转换 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<mapping resource="com/hibernate/demo/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
该实例目录结构如下:
1、新建一个java项目(hibernate主要以xml及java project进行实现),配置相关jar包和配置文件
2、建立User实体类
package com.hibernate.demo;
import java.util.Date;
public class User {
private String id;
private String username;
private String password;
private Date createTime;
private Date expireTime;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
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;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!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.hibernate.demo.User">
<id name="id">
<!-- 由Hibernate基于128位UUID算法生成16进制数值(编码后以长度32的字符串表示)作为主键。 -->
<generator class="uuid"/>
</id>
<property name="username"/>
<property name="password"/>
<property name="createTime"/>
<property name="expireTime"/>
</class>
</hibernate-mapping>
其中的property标签是将要生成时数据库表中的字段,在这里不用关心各个字段是什么类型的。因为Hibernate会根据上面的实体类中属性的类型来决定将来表中字段的类型
3、配置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">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<!-- mysql数据库驱动 -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- mysql数据库名称 -->
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate_first</property>
<!-- 数据库的登录用户名 -->
<property name="hibernate.connection.username">root</property>
<!-- 数据库的登录密码 -->
<property name="hibernate.connection.password">14132</property>
<!-- 方言:为每一种数据库提供适配器吗,方便转换 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<mapping resource="com/hibernate/demo/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
此处注意:必须是“/”而不能是“.”。
4、生成表:编写工具类ExopertDB.java,将hbm生成ddl
package com.hibernate.demo;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
public class ExportDB {
/**
* 将hbm生成ddl动态链接工具
* @param args
*/
public static void main(String[] args) {
// 默认读取hibernate.cfg.xml文件
Configuration cfr = new Configuration().configure();
SchemaExport export = new SchemaExport(cfr);
export.create(true, true);
}
}
到这里就可以生成User表了,但是如果直接运行ExoprtDB.java文件是不能生成User表的。因为在mysql数据中还没有建立数据库Hibernate-first。所以在mysql控制台中通过create database hibernate-first; use hibernate-first;之后再执行ExoprtDB.java文件就可以生成表了。(或者可通过SQLyog来进行表的建立,不过不推荐新学者,新学者最好先通过手打来熟悉SQL的操作代码)
5、向表中添加数据
package com.hibernate.demo;
import java.util.Date;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class Client {
public static void main(String[] args) {
// 读取配置文件
Configuration cfg = new Configuration().configure(); //创建配置对象
SessionFactory factory =cfg.buildSessionFactory(); //创建会话工厂对象
Session session=null;
try{
session = factory.openSession(); //打开会话对象
//开启事务
session.beginTransaction();
User user=new User();
user.setUsername("用户名");
user.setPassword("123");
user.setCreateTime(new Date());
user.setExpireTime(new Date());
session.save(user);
//提交事务
session.getTransaction().commit();
}catch(Exception e){
e.printStackTrace();
//回滚事务
session.getTransaction().rollback();
}finally{
if(session!=null){
if(session.isOpen()){
//关闭session
session.close();
}
}
}
}
}
最后执行该java文件就可以完成向表中增加数据了
(命令符)
(SQLyog)
三、总结
项目执行顺序:
1、新建java项目,配置相关jar包和配置文件
2、建立User实体类
3、配置hibernate.cfg.xml文件(在src文件夹下添加数据库驱动核心配置文件)4、生成表:编写工具类ExopertDB.java,将hbm生成ddl
5、向表中添加数据