上一篇已经把大致的理论解释的很清楚了,这次我们来实战。
通过上一篇的文章,大致知道一个hibernate程序需要的文件和基本的设置了。我们以一个简单的例子来作为入门的例子。
主要包含的结构是:
Hibernate的配置文件:hibernate.cfg.xml
实体类(需要映射的实体类):User
映射对象到表的工具类:ExportDB
映射文件:User.hbm.xml
来看看之间的联系吧,我们做这个程序的前提是需要的包已经导入,具体导入哪些包,可以参见前面的博客,这里不再详细的赘述。
1. 首先建立User实体类
创建一个用户,具体信息包括:主键id,姓名,密码,创建日期以及过期日期。如下图所示:
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;
}
并且各个字段生成相应的get和set方法。这里不再一一显示。
2. 建立User实体类的映射文件
Hibernate-mapping映射标签中包含三个标签:class ,id以及property。
Class标签指明是对于那个实体类的,id指明如何去生成主键(具体的主键生成策略会在之后的博客进行讲解),property列出了User实体类中的属性字段。
<?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.bjpowernode.hibernate.User">
<id name="id">
<generator class="uuid"/>
</id>
<property name="name"/>
<property name="password"/>
<property name="createTime"/>
<property name="expireTime"/>
</class>
</hibernate-mapping>
3. Hibernate配置文件
Hibernate-configuration标签中包含着session-factory标签,在session-factory中的配置相应的数据库连接以及进行相应的映射文件的配置。
<!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">bjpowernode</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.show_sql">true</property>
<!--
<property name="hibernate.format_sql">true</property>
-->
<mapping resource="com/bjpowernode/hibernate/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
4. 如何映射到数据库中的表
上面的一切都建立好后,现在该做就是将实体类对象映射到数据库中的表了,建立一个工具类用于实现将数据导入到数据库中的表。
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
/**
* 将hbm生成ddl
* @author Administrator
*
*/
public class ExportDB {
public static void main(String[] args) {
//默认读取hibernate.cfg.xml文件
Configuration cfg = new Configuration().configure();
SchemaExport export = new SchemaExport(cfg);
export.create(true, true);
}
}
在映射之前我们首先需要的创建数据库,关于mysql命令提示符创建库的应用:
Ø 创建数据库(create database hibernate_first)
Ø 显示表和表结构(show tables以及desc user)
Ø 显示生成的sql语句(show create tableuser)
5. 测试程序
一切准备就绪后,我们现在可以对我们的程序进行测试了,
读取配置文件,利用配置文件创建sessionfactory,通过sessionfactory获取到session接口,利用session的方法对数据进行相关的操作。至此,例子基本上完成。
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) {
//读取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 Date());
user.setExpireTime(new Date());
//保存User对象
session.save(user);
//提交事务
session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
//回滚事务
session.getTransaction().rollback();
}finally {
if (session != null) {
if (session.isOpen()) {
//关闭session
session.close();
}
}
}
}
在明白了原理之后,再次进行相关程序的书写是不是简单多了呢,知其然而知其所以然,这样我们的学习才能高效进行~