zoukankan      html  css  js  c++  java
  • 新秀学习Hibernate——一个简单的例子

    一个、Hibernate开发。

    上篇博客已经为大家介绍了持久层框架的发展流程,持久层框架的种类。

    为了可以使用Hibernate高速上手,我们先解说一个简单的Hibernate应用实例hibernate_first

    二、开发流程。

    1.首先在MyEclipce中新建一个hibernate_first的项目,然后新建后的项目文件夹为:


    2.配置Hibernate环境。

    3.编写持久化类User.java

    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;
    	}
    }
    


    4.编写生成映射文件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="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>


    5.编写hibernate.cfg.xml文件。

    <!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_frist</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="hibernate.show_sql">true</property>
    <property name="hibernate.format_sql">true</property>
    	<mapping resource="com/bjpowernode/hibernate/User.hbm.xml"/>
    	</session-factory>
    </hibernate-configuration>

    6.生成表的类ExportDB.java。

    package com.bjpowernode.hibernate;
    
    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);
    	}
    }
    


    7.以上六个步骤已经把表建起来了以下我们就保存个数据。新建一个Client.java类来存入一个数据。

    代码例如以下:

    package com.bjpowernode.hibernate;
    
    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();
    				}
    			}
    		}
    	}
    }
    



    三、总结。

    一个简单的Hibernate样例就出来了在Hibernate初学时利用这个样例能够让我们更好的入门。



    版权声明:本文博客原创文章,博客,未经同意,不得转载。

  • 相关阅读:
    #2020征文TV#【鸿蒙基地】鸿蒙从窗口开始:Page Ability诞生记
    设计器打开某表单时提示:[某某表单]已经由用户[xxx]打开需解锁
    有效性设置解疑
    表单打开时显示空白页面解决办法
    工作流_知会设置
    单元格中既有公式又可以录入数据,怎么实现?
    更改系统时间
    下拉框改变后,如何清空后面几个单元格的值?
    如何调整人员的部门?
    如何修改iis访问端口
  • 原文地址:https://www.cnblogs.com/yxwkf/p/4736294.html
Copyright © 2011-2022 走看看