您的位置:首页 > 其它

第一个Hibernate例子

2015-03-13 08:59 309 查看

Hibernate开发的环境搭建

(一)环境搭建首先得下载Hibernate需要的jar包,通常导入hibernqte的核心包和

required文件夹中的所有jar包。
(二)加入数据库驱动。下面的例子中主要是采用Mysql数据库来演示的,

所以引入MysqL的JDBC驱动mysql-connector-java-5.1.26-bin.jar。
(三)提供核心配置文件hibernate.cfg.xml文件(在src文件夹下即可)。其中的

配置如下(针对mysql)

<?xml version="1.0" encoding="UTF-8"?>
<!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">root</property>
<!-- 方言:为每一种数据库提供适配器,方便转换 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hbm2ddl.auto">update</property>

<mapping resource="hibernateExample/User.hbm.xml"/>

</session-factory>
</hibernate-configuration>


该例子的目录结构如下图:



其中hibernate4里面是hibernate核心包,Referenced Libraries里包含了所

需要的hibernate的依赖包。

1:新建一个普通的java项目,按照上面的步骤导入相关的jar包和配置文件。

2:建立User实体类

package hibernateExample;

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;
}
}
3.User.hbm.xml,完成实体类的映射

<?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="hibernateExample.User">
<id name="id">
<generator class="uuid.hex"/>
</id>
<property name="username"/>
<property name="password"/>
<property name="createTime"/>
<property name="expireTime"/>
</class>
</hibernate-mapping>


4.生成表:编写工具类ExportDB.java,将hbm生成ddl

package hibernateExample;

import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

public class ExoprtDB {
public static void main(String[] args){
//默认读取hibernate.cfg.xml文件
Configuration cfg = new Configuration().configure();
//创建SchemaExport对象
SchemaExport export = new SchemaExport(cfg);
//创建数据库表
export.create(true,true);
}
}


5.向表中添加数据
package hibernateExample;

import java.util.Date;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.HibernateException;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

public class Client {
public static void main(String[] args){
/*读取配置文件*/
Configuration cfg = new Configuration().configure();
/*为了获得实例,先创建工厂*/
SessionFactory factory = cfg.buildSessionFactory();

Session session = null;
try{
/*session通过SessionFactory打开,在所有的工作完成后关闭 */
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();
}
}
}
}
}


完成后,执行Client.java就可以向表中增加数据了:





                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: