2021-05-10

Hibernate配置流程(Eclipse)


一、准备工作

1.创建java项目

2.创建数据库

3.在Eclipse中连接数据库

二、配置

1.引用相关包

2.创建Hibernate配置文件(hibernate.cfg.xml)

<!--
  ~ Hibernate, Relational Persistence for Idiomatic Java
  ~
  ~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
  ~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
  -->
<!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="hibernate.dialect">MySQLInnoDBDialect</property>
		<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
		<property name="hibernate.connection.url">jdbc:mysql://3306/hellohibernate</property>
		<property name="hibernate.connection.username">root</property>
		<property name="hibernate.connection.password">123456</property>
		<property name="show_sql">true</property>
		<mapping resource="cn/hrbust/pojo/User.hbm.xml"/>
	</session-factory>
</hibernate-configuration>

3.实体类(User.java)

package cn.hrbust.pojo;

import java.util.Date;

public class User {
	private int id;
	private String name;
	private String gender;
	int age;
	Date birthday;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getGender() {
		return gender;
	}
	public void setGender(String gender) {
		this.gender = gender;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public Date getBirthday() {
		return birthday;
	}
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
}

 

4.配置User实体类(User.hbm.xml)

<?xml version="1.0"?>
<!--
  ~ Hibernate, Relational Persistence for Idiomatic Java
  ~
  ~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
  ~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
  -->
<!DOCTYPE hibernate-mapping PUBLIC
	"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
	"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mappin>

    <class name="cn.hrbust.pojo.User" table="T_User">
        <id name="id" column="id">
            <generator class="native"/>
        </id>
        <property name="name"/>
        <property name="gender"/>
        <property name="age"/> 
        <property name="birthday"/>
    </class>
    

</hibernate-mapping>

三、测试

创建测试类(manageUser.java)

package cn.hrbust.dao;

import javax.transaction.Transaction;
import cn.hrbust.pojo.User;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import java.sql.Date;

public class manageUser {
	
	public static void main(String[] args){
		Configuration cfg = null;
		SessionFactory sf = null;
		Session session = null;
		Transaction ts = null;
		
		User u = new User();
		u.setName("孙孝泽");
		u.setGender("男");
		u.setAge(20);
		u.setBirthday(Date.valueOf("2001-1-13"));
		
		try{
			cfg = new Configuration().configure();
			sf = cfg.buildSessionFactory();
			session = sf.openSession();
			ts =session.beginTransaction();
			session.save(u);
			ts.commit();
		}catch(HibernateException e){
			e.printStackTrace();
			if(ts!=null){
				ts.rollback();
			}
		}finally{
			session.close();
			sf.close();
		}
		
	}
}

运行后数据库状态如下:

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值