Hibernate 4.2 简单入门实例

Hibernate 4.2 简单入门实例


本文按照POJO--->配置文件--->DB的顺序编写,类似于正向工程,但配置文件手工编写,不通过MyEclipse自动生成。

另有先写DB,通过MyEclipse生成配置文件和POJO的反向工程。


两者的主要区别为:

正向工程比较符合面向对象的编程思想,官方推荐,但对编程人员水平要求较高。

反向工程则是传统的面向数据设计方式。


第一步:创建hibernate数据库


在MySQL命令行下输入以下命令:

create database hibernate;

第二步:创建Java工程,导入jar包


新建Java工程,取名为hibernate。


程序所需的最少jar包为:

dom4j-1.6.1.jar

hibernate-commons-annotations-4.0.1.Final.jar

hibernate-core-4.2.0.Final.jar

hibernate-jpa-2.0-api-1.0.1.Final.jar

javassist-3.15.0-GA.jar

jboss-logging-3.1.0.GA.jar

jboss-transaction-api_1.1_spec-1.0.0.Final.jar

mysql-connector-java-5.1.24-bin.jar


另外,虽然antlr-2.7.7.jar在required文件夹下,但运行此程序并不需要


第三步:写POJO类


com.domain.Employee:

public class Employee implements Serializable {

	private static final long serialVersionUID = -684317571614793717L;
	
	private Integer id;
	private String name;
	private String email;
	private java.util.Date hiredate;
	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 getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public java.util.Date getHiredate() {
		return hiredate;
	}
	public void setHiredate(java.util.Date hiredate) {
		this.hiredate = hiredate;
	}
}

第四步:写映射配置文件Employee.hbm.xml


在com.domain下创建Employee.hbm.xml文件:

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

<hibernate-mapping package="com.domain">
	<class name="Employee" table="employee">
		<id name="id" column="id" type="java.lang.Integer">
			<generator class="native" />
		</id>
		<property name="name" type="java.lang.String">
			<column name="name" length="64" not-null="true" />
		</property>
		<property name="email" type="java.lang.String">
			<column name="email" length="64" not-null="true" />
		</property>
		<property name="hiredate" type="java.util.Date">
			<column name="hiredate" not-null="true" />
		</property>
	</class>
</hibernate-mapping>


第五步:写主配置文件hibernate.cfg.xml


在src目录下创建hibernate.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>
		<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
		<property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property>
		<property name="connection.username">root</property>
		<property name="connection.password"></property>
		<property name="hibernate.hbm2ddl.auto">update</property>
		<mapping resource="com/domain/Employee.hbm.xml" />
	</session-factory>
</hibernate-configuration>

其中

connection.username为MySQL用户名

connection.password为MySQL密码


第六步:写Test类,测试代码


com.test.Test:

public class Test {

	public static void main(String[] args) {
		Configuration cfg = new Configuration().configure();
		ServiceRegistry sr = new ServiceRegistryBuilder()
			.applySettings(cfg.getProperties()).buildServiceRegistry();
		SessionFactory sf = cfg.buildSessionFactory(sr);
		
		Session session = sf.openSession();
		Transaction ts = session.beginTransaction();
		Employee employee = new Employee();
		employee.setName("hibernate");
		employee.setEmail("hibernate@126.com");
		employee.setHiredate(new java.util.Date());
		session.save(employee);
		ts.commit();
		session.close();
	}
}

第七步:打开MySQL命令行,查看结果


在MySQL命令行下输入以下命令:

use hibernate;
desc employee;
select * from employee;

输出结果:

employee


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值