Hibernate Helloworld教程

1、环境配置

(1)在myeclipse下新建一普通Java Project

        (2)新建lib文件夹,并将hibernate和mysql所需jar包导入lib文件夹,并build path。


2、在mysql数据库中建表

sql语句如下:

use test;
create table employee
(id int NOT NULL auto_increment,
 name VARCHAR(10),
 email varchar(20),
 PRIMARY KEY(id)
)charset=utf8;

3、创建持久化类(POJO)

在com.sdust.www.domain包下创建Employee类

package com.sdust.www.domain;
public class Employee
{
	private Integer id;
	private String name;
	private String email;
	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 Employee(String name, String email)
	{
		super();
		this.name = name;
		this.email = email;
	}
	public Employee()
	{}
	@Override
	public String toString()
	{
		return "Employee [id=" + id + ", name=" + name + ", email=" + email
				+ "]";
	}
}

4、创建对象关系映射文件

在com.sdust.www.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.sdust.www.domain">
	<class name="Employee" table="employee">
	<!-- id用于指定主键属性 -->
		<id name="id" column="id" type="java.lang.Integer"> 
		<generator class="native"></generator>
		</id>
	<!-- 其它属性的配置 -->
	<property name="name" type="java.lang.String">
		<column name="name" not-null="false"></column>
	</property>
	<property name="email" type="java.lang.String">
		<column name="email" not-null="false"></column>
	</property>
	</class>
</hibernate-mapping>	

5、创建Hibernate配置文件

在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>
    <!-- Database connection settings -->
    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="connection.url">jdbc:mysql:///test</property>
    <property name="connection.username">root</property>
    <property name="connection.password">123456</property>


    <!-- JDBC connection pool (use the built-in) -->
    <!--<property name="connection.pool_size">1</property>-->


    <!-- SQL dialect -->
    <property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>


    <!-- Enable Hibernate's automatic session context management -->
    <!--  <property name="current_session_context_class">thread</property> -->


    <!-- Disable the second-level cache  -->
    <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>


    <!-- Echo all executed SQL to stdout -->
    <property name="show_sql">true</property>


    <!-- Drop and re-create the database schema on startup -->
    <property name="hbm2ddl.auto">update</property>
    <mapping resource="com/sdust/www/domain/Employee.hbm.xml"/>
  </session-factory>
</hibernate-configuration>

6、通过Hibernate API编写访问数据库代码

package com.sdust.www.view;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

import com.sdust.www.domain.Employee;

public class TestMain
{
	public static void main(String[] args)
	{
		//1、创建SessionFactory对象
		Configuration configuration = new Configuration().configure();
		ServiceRegistry serviceRegistry = 
				new ServiceRegistryBuilder().applySettings(configuration.getProperties())
											.buildServiceRegistry();
		SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
		//2、创建session对象(相当于jdbc connection)
		Session session = sessionFactory.openSession();
		//3、开启事务
		Transaction transaction = session.beginTransaction();
		//4、执行保存操作
		Employee employee = new Employee();
		employee.setName("whc");
		employee.setEmail("whc@163.com");
		session.save(employee);
		//5、提交事务
		transaction.commit();
		//6、关闭session
		session.close();
		//7、关闭sessionFactory对象
		sessionFactory.close();
	}
}

7、查看结果

程序运行完成后,可在mysql可看到employee表中增加了一条记录。






















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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值