Hibernate框架的搭建

一、建立java项目

二、引入jar包
概念了解:
1、库中包含多个jar包,可以建立自己的库,过程如下:
2、Add JARs向库中添加jar包(包括:hibernate依赖的所有第三方jar包、hibernate核心包、数据库的驱动包)
3、将库添加到项目中

  三、配置文件

核心配置文件:hibernate.cfg.xml;放到src目录下即可

<!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>
		
		<!--配置解析,现在为mysql,这样在匹配时翻译为mysql的语句-->
		<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
	</session-factory>
</hibernate-configuration>

 
  

四、建立实体类,映射实体类

1、建立实体类

      

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

2、进行映射

<?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"> <!--id的代表主键-->
			<generator class="uuid"/><!--id生成策略使用 UUID-->
		</id>
		<property name="name"/>
		<property name="password"/>
		<property name="createTime"/>
		<property name="expireTime"/>
	</class>
</hibernate-mapping>


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

<mapping resource="com/bjpowernode/hibernate/User.hbm.xml"/>

以上配置到  property  后
六、编写工具类,将实体对应的建立到库中
package com.bjpowernode.hibernate;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
 
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);
	}
}

七、实现添加数据到库
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.cfg.xml ①配置文件连接数据  ②配置映射文件
        二、建立实体类
        三、 配置映射文件: 类名.hbm.xml
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 17
    评论
评论 17
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值