hibernate的正向使用

本文详细介绍了一个使用Hibernate框架进行数据库操作的基本示例,包括项目搭建、配置文件编写、实体类定义及测试代码实现等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1,例如,新建项目hiber02,

2,在hiber02的基础上,再新建src文件夹,倒入11个jar,其中包括数据库的驱动,


3,选中这11个jar文件,右键单击,build path只有一个选项,选中之后生成:


4,在src下新建


取名为hiber.cfg.xml


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
	"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
	"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>

	<session-factory>
		<!-- Database connection settings -->
		<!-- 连接驱动类 -->
		<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
		<!-- 连接数据库 -->
		<property name="connection.url">jdbc:mysql://localhost:3306/jd</property>
		<!-- 连接数据库的用户名 -->
		<property name="connection.username">root</property>
		<!-- 连接数据库的用户密码 -->
		<property name="connection.password">root</property>
		<!-- SQL dialect -->
		<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
		<!-- Echo all executed SQL to stdout -->
		<property name="show_sql">true</property>
		<!-- Drop and re-create the database schema on startup -->
		<!-- 
		   create
		   create-drop
		   update
		   validate
		 -->
		<property name="hbm2ddl.auto">update</property>
		<!-- 引入实体bean的映射文件 -->
		<mapping resource="hiber02/domain/User.hbm.xml" />

	</session-factory>

</hibernate-configuration>

5,UserTest测试

package hiber02.unit;

import java.sql.Date;

import hiber02.domain.User;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.junit.Test;

public class UserTest {

	@Test
	public void test() {
	/*	// 创建出configuration对象
		Configuration config = new Configuration()
				.configure("hiber.cfg.xml");
		
		//获取sessionFactory工厂对象
		SessionFactory factory = config.buildSessionFactory();
		//获取session对象
		Session session = factory.openSession();
		//获取事务对象
		Transaction ts = session.getTransaction();
		//开始事务
		ts.begin();
		
		//保存数据
		session.save(new User("chjx", "男", 29, new Date(System.currentTimeMillis())));
		//提交事务
		ts.commit();
		//释放资源
		session.close();
		
		factory.close();*/
		
		
		// 创建出configuration对象
				Configuration config = new Configuration()
						.configure("hiber.cfg.xml");
	     SchemaExport export = new SchemaExport(config);
	     export.create(true, true);
	     
	}

}

6,User.java

package hiber02.domain;

import java.io.Serializable;
import java.sql.Date;

/**
 * 创建实体类
 * @author zhao
 *
 */
public class User  implements Serializable{

	private Integer id;
	private String name;
	private String sex;
	private Integer age;
	private Date birthday;
	public User() {
		super();
		// TODO Auto-generated constructor stub
	}
	public User(String name, String sex, Integer age, Date birthday) {
		super();
		this.name = name;
		this.sex = sex;
		this.age = age;
		this.birthday = birthday;
	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	public Date getBirthday() {
		return birthday;
	}
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	
	
}

7,User.hbm.xml

<!DOCTYPE hibernate-mapping PUBLIC
	"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
	"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="hiber02.domain">
	<class name="User" table="user" catalog="jd">
		<id name="id" column="user_id">
			<generator class="native"/>
		</id>
		
		<!-- 属性配置 -->
	
		<property name="name" column="user-name" length="50" not-null="true" unique="true"/>
			<property name="age" column="user_age"/>
			<property name="sex" column="user_sex" length="2"/>
			<column name="birthday" column="user_birthday"/>
	</class>
</hibernate-mapping>

8,程序结构如下


9,数据库效果


这只是在学习中的总结,还有许多不足之处

10,hibernate的工作原理概述:

1.读取并解析配置文件
2.读取并解析映射信息,创建SessionFactory
3.打开Sesssion
4.创建事务Transation
5.持久化操作
6.提交事务
7.关闭Session
8.关闭SesstionFactory

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值