Hibernate4入门到精通一——————第一个Hibernate程序


一、Hibernate简介



二、第一个Hibernate4程序

1、新建Java project 。名字为Hibernate4——01

2、在src下新建lib文件夹,并导入必须的包。


并Add to build path

3、在src目录下创建Hibernate的配置文件

hibernate.cfg.xml如下:

<!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>
	<!-- hibernate的方言,用来确定连接的数据库 -->
		<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
	<!-- 数据库的连接类 -->
		<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
	<!-- 数据库的连接字符串和用户名密码 -->
		<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/itat_hibernate</property>
		<!-- 密码为空 -->
		<property name="hibernate.connection.username">root</property>
		<property name="hibernate.connection.password"></property>
	<!-- 在使用hibernate时会显示相应的SQL -->
		<property name="show_sql">true</property>
	<!-- 会自动完成类到数据表的转换 -->
		<property name="hibernate.hbm2ddl.auto">update</property>
	</session-factory>
</hibernate-configuration>

4、在包org.zttc.itat.model下新建实体类

User.java

package org.zttc.itat.model;

import java.util.Date;

public class User {
	private int id;
	private String username;
	private String password;
	private String nickname;
	private Date born;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getNickname() {
		return nickname;
	}
	public void setNickname(String nickname) {
		this.nickname = nickname;
	}
	public Date getBorn() {
		return born;
	}
	public void setBorn(Date born) {
		this.born = born;
	}
	public User(int id, String username, String password, String nickname,
			Date born) {
		
		this.id = id;
		this.username = username;
		this.password = password;
		this.nickname = nickname;
		this.born = born;
	}
	public User() {

	}		
}

5、在包org.zttc.itat.model下新建

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 package="org.zttc.itat.model">
    <class name="User" table="t_user">
        <id name="id">
            <generator class="native"/>
        </id>
		<property name="username"/>
		<property name="password"/>
		<property name="nickname"/>
		<property name="born" type="timestamp"/>
    </class>
</hibernate-mapping>

6、将配置文件添加到hibernate的cfg的配置文件中,即hibernate.cfg.xml改动如下:

<!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>
	<!-- hibernate的方言,用来确定连接的数据库 -->
		<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
	<!-- 数据库的连接类 -->
		<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
	<!-- 数据库的连接字符串和用户名密码 -->
		<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/itat_hibernate</property>
		<!-- 密码为空 -->
		<property name="hibernate.connection.username">root</property>
		<property name="hibernate.connection.password"></property>
	<!-- 在使用hibernate时会显示相应的SQL -->
		<property name="show_sql">true</property>
	<!-- 会自动完成类到数据表的转换 -->
		<property name="hibernate.hbm2ddl.auto">update</property>
	<!-- 加入实体类的映射文件 -->	
		<mapping resource="org/zttc/itat/model/User.hbm.xml"/>
	</session-factory>
</hibernate-configuration>

7、在数据库中新建itat_hibernate数据库。


8、在包org.zttc.itat.test先进测试类TestFirst.java 如下:

package org.zttc.itat.test;
import java.util.Date;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.junit.Test;
import org.zttc.itat.model.User;

public class TestFirst {

	@Test
	public void test01() {
		/*
		 * 创建SessionFactory,SessionFactory是线程安全,所以整个SessionFactory应该基于单例的模式来创建
		 */
		Configuration cfg=new Configuration().configure();
		// cfg.buildSessionFactory(); 在hibernate3中可以,hibernate4中不可以
		ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()
								.applySettings(cfg.getProperties())
								.buildServiceRegistry();
		SessionFactory factory= cfg.buildSessionFactory(serviceRegistry);
		/*
		 * 创建session
		 */
		Session session =null;
		//一下代码完成了对象的添加操作
		try {
			session=factory.openSession();
			/*
			 * 通过session完成各种操作
			 */
			//开启事务
			session.beginTransaction();
			User u=new User();
			u.setNickname("张三");
			u.setPassword("123");
			u.setUsername("zhangsan");
			u.setBorn(new Date());
			session.save(u);
			//提交事务
			session.getTransaction().commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			if(session!=null) session.getTransaction().rollback();
		}finally{
			if(session!=null) session.close();
		}
	}
}


运行测试程序。控制台打印:

Hibernate: insert into t_user (username, password, nickname, born) values (?, ?, ?, ?)


查看数据库发现数据库中自动生成数据表,并添加了数据


附录:项目工程目录


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值