Introduction to hibernate and first hibernate example.

本文介绍了Hibernate,它是基于JDBC API开发的ORM框架,能大幅减少处理数据库的代码量。还给出环境搭建和代码示例,包括导入jar包、设置配置文件、创建类文件和主类,展示了Hibernate在处理数据库时减少代码行数的优势。

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

1. What is Hibernate

  Simply said hibernate is a ORM framework, where ORM stands for Object (Java) - Relational (DB) mapping, 

  Hibernate is developed based on JDBC api, which has greatly reduced the code work when dealing with Databse. 

 

2. Environment set up and Code example. 

Four steps are concluded as below:

  Step1. Import needed jar files,

  Step 2. set up the "hibernate.cfg.xml" config file.

  Step 3. create the Actor class file. which is the bean we are going to insert into the Database.  

  Step 4. Create the main class

Run output: the data has been successfuly inserted to Database. 

From this example, we could see that Hibernate has helped us reduced greatly on the code lines on dealing with database part

  

 

Details are described as below for reference

Step1. Import needed jar files, which could be downloaded in www.hibernate.org/downloads

 

Step 2. set up the "hibernate.cfg.xml" config file. 

  

<!--
  ~ 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>
      <!-- Database Connection settings  -->
      <property name ="connection.driver.class">  com.mysql.jdbc.Driver</property>
      <property name = "connection.url"> jdbc:mysql://localhost:3306/sakila</property>
         <property name = "connection.username">root</property>
      <property name="connection.password">3792354</property>
    
    <!-- JDBC Connection pool (use the built-in) -->
     <property name ="connection.pool_size">1</property>
    
    <!-- SQL Dialect -->
    <property name = "dialect">org.hibernate.dialect.MySQLDialect</property>
    
    <!-- Disable the second-level Cache -->
    
    <property name = "cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
    
    <!-- ECHO all executed SQL to STDOUT -->
    
    <property name ="show_sql" >true</property>
    
    <property name="hbm2ddl.auto">create</property>
    
    <mapping class = "com.yang.bean.Actor"/>
    
    </session-factory>
</hibernate-configuration>

 

Step 3. create the Actor class file. which is the bean we are going to insert into the Database. 

package com.yang.bean;

import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.Id;


//tell the hibernate that this class is an entity

//class written to persist or hold data into or from database

@Entity
public class Actor {
	@Id
	private int actor_id;
	private String first_name;
	private String last_name;
	private Date last_update;
	
	public int getActor_id() {
		return actor_id;
	}
	public void setActor_id(int actor_id) {
		this.actor_id = actor_id;
	}
	public String getFirst_name() {
		return first_name;
	}
	public void setFirst_name(String first_name) {
		this.first_name = first_name;
	}
	public String getLast_name() {
		return last_name;
	}
	public void setLast_name(String last_name) {
		this.last_name = last_name;
	}
	public Date getLast_update() {
		return last_update;
	}
	public void setLast_update(Date last_update) {
		this.last_update = last_update;
	}
	
	
	

}

 

Step 4. Create the main class

 

package com.yang.bean;


import java.util.Date;

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

public class Main {
    
    public static void main(String[] args) {
        
        Date d = new Date();
        Actor a = new Actor();
        
        a.setActor_id(202);
        a.setFirst_name("Yajing");
        a.setLast_name("Hong");
        a.setLast_update(d);
        
         Configuration cfg = new Configuration();
         
         SessionFactory sf = cfg.configure().buildSessionFactory();
         
         Session session = sf.openSession();
         
         session.beginTransaction();
         
         session.save(a);
         
         session.getTransaction().commit();
         
         session.close();
         
         sf.close();
       
    }
    

}

 

Core part of Hibernate.

image reference link ( https://blog.youkuaiyun.com/jiuqiyuliang/article/details/39078749)

 

转载于:https://www.cnblogs.com/codingyangmao/p/10879022.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值