[置顶] 【hibernate进阶】hibernate搭建开发环境+实例讲解

本篇博客将带着大家以一个简单的实例来学习hibernate项目。

1、创建java项目

2、创建User Library,加入依赖包
* HIBERNATE_HOME/lib/*.jar
* HIBERNATE_HOME/hibernate3.jar

* 加入数据库驱动(mysql驱动)

具体的操作步骤参考我的上一篇博客:【hibernate进阶】如何在myeclipse中添加jar包

3、提供hibernate.cfg.xml文件,完成基本的配置

<strong><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">123</property>
        <!-- mysql language adatper,if you want to change database ,you juse need to change there  -->
        <!-- <property name="hibernate.connection.dialect">org.hibernate.dialect.MySQLDialect</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></span></strong>
4、建立实体类User.java

<strong><span style="font-size:18px;"><strong><span style="font-size:18px;">package com.bjpowernode.hibernate;

import java.util.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></strong>
</span></strong>
5、提供User.hbm.xml文件,完成实体类的映射

<strong><span style="font-size:18px;"><?xml version="1.0"?>
<!DOCTYPE hibernate-mapping SYSTEM "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></span></strong>

6、将User.hbm.xml文件加入到hibernate.cfg.xml文件中

这个在3步的时候已经加进去了

7、编写工具类ExoprtDB.java,将hbm生成ddl,也就是hbm2ddl

<strong><span style="font-size:18px;">package com.bjpowernode.hibernate;

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

/**
 * hbm  Generation ddl
 * @author CXC
 *
 */
public class ExoprtDB {
	
	public static void main(String[] args){
		//Read by default hibernate.cfg.xml File
		Configuration cfg=new Configuration().configure();
		
		SchemaExport export=new SchemaExport(cfg);
	
		export.create(true, true);
	}

}</span></strong>
在这一步的时候,就可以生成user表了,run as java application ,查看数据库,就可以生成user表了。



8、建立客户端类Client,添加用户数据到mysql

<strong><span style="font-size:18px;">package com.bjpowernode.hibernate;

import java.text.SimpleDateFormat;
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) {
		
		//read hibernate.cfg.xml file
		Configuration  cfg=new Configuration().configure();
		
		//sessionfactory thread safe
		//build sessionfactory
		SessionFactory factory=cfg.buildSessionFactory();
		
		//acquire session 
		Session session=null;
		try{
			
			session=factory.openSession();
			//open Transaction
			session.beginTransaction();
			User user=new User();
			user.setName("12");
			user.setPassword("123");
			/*  user.setCreateTime(new Date());
	 			user.setExpireTime(new Date());*/
			
			Date date=new Date();                             
            SimpleDateFormat temp=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");  
             String date2=temp.format(date);  
             Date date3=temp.parse(date2);  
             
			
             user.setCreateTime(date3);
 			user.setExpireTime(date3);
			
			//save User object
			session.save(user);
			
			//commit Transaction
			session.getTransaction().commit();
			
		}catch(Exception e){
			e.printStackTrace();
			//rollback  Transaction
			session.getTransaction().rollback();
			
		}finally{
			if(session !=null){
				if(session.isOpen()){
					//close session
					session.close();
					
				}
				
			}
			
		}
		
		
		

	}

}
</span></strong>

在console中,输入的insert into user(.....)信息表示将数据插入到数据库中了。

通过navicat,查看插入的数据



这样,hibernate的项目就完成了。

遇到的问题:

在插入的时候,遇到了日期插不进去的问题。以为是mysql引用的jar包问题,重新引用了没有解决,想到了在之前也遇到过类似的问题,把转了一下日期的格式就解决了。



原文地址:https://www.cnblogs.com/chenxiaochan/p/7237578.html