Hibernate 入门简介

本文介绍 Hibernate 3.0 的基本配置与使用方法,包括搭建环境、配置文件详解、ORM 映射方式及核心 API 使用等。
部署运行你感兴趣的模型镜像

最近学习了一下hibernate 自己一个简单的总结


hibernate是一个基于ORM原理的数据持久化框架,用于构架我们应用的数据层及数据访问层;通过将对数据库的操作转化为对对象的操作,使得数据层的操作更容易和面向对象。


hibernate 3.0 入门

jar包下载地址  http://sourceforge.net/projects/hibernate/files/hibernate3/


创建我们的java project并且引入requir目录下的所有jar包

引入slf4j的jar包:slf4j-nop-jar   用于日志记录


建立配置文件 默认在根目录下 默认名字为 hibernate.cfg.xml

配置详解见http://blog.youkuaiyun.com/dare_/article/details/19483409


<?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://192.168.1.241:3306/test</property>
        <property name="connection.username"
>dingyuming</property>
        <property name="connection.password"
>uniware</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>

        <!-- 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.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/hibernate/test/hibernate_IP.xml"/>
        <mapping class="com.hibernate.test.Student"/>

    </session-factory>

</hibernate-configuration
>

其中映射文件有两种读取方式:

详细介绍:http://blog.youkuaiyun.com/dare_/article/details/19539799

1、XML

<?xml version="1.0"?>
<!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.hibernate.test">

    <class name="IP_List" table="IP_LIST">
        <id name="ip" column="Ip">
        </id>
        <property name="Status"></property>
    </class>

</hibernate-mapping
>


2、anotation注解 添加于get方法上

/**
 * 文件名:Student.java
 *
 * 版本信息:
 * 日期:2014-2-19
 * Copyright 足下 Corporation 2014
 * 版权所有
 *
 */
package com.hibernate.test;

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

/**
 * 此类描述的是:
 * 
 * @author: wangxiongdx@163.com
 * @version: 2014-2-19 上午11:32:55
 */
@Entity
@Table(name="Student")
public class Student {
	private int id;
	private String name;

	/**
	 * id
	 * 
	 * @return the id
	 * @since CodingExample Ver(编码范例查看) 1.0
	 */
	@Id
	public int getId() {
		return id;
	}

	/**
	 * 
	 * @param id
	 *            the id to set
	 */
	public void setId(int id) {
		this.id = id;
	}

	/**
	 * name
	 * 
	 * @return the name
	 * @since CodingExample Ver(编码范例查看) 1.0
	 */
	public String getName() {
		return name;
	}

	/**
	 * 
	 * @param name
	 *            the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}
}


映射文件用作类与数据库表中的关系对应,这两种方式均可以正确建立我们的ORM映射模型



这样hibernate的基本配置就完成了  


接下来就可以在测试类中使用了


/**
 * 文件名:Test.java
 *
 * 版本信息:
 * 日期:2014-2-18
 * Copyright 足下 Corporation 2014
 * 版权所有
 *
 */
package com.hibernate.test;

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

/**
 * 此类描述的是:
 * 
 * @author: wangxiongdx@163.com
 * @version: 2014-2-18 下午4:35:54
 */
public class Test {
	public static void main(String[] args) {
		IP_List ip_List = new IP_List();
		ip_List.setIp("d");
		Student student =new Student();
		student.setId(1);
		student.setName("test");
		
		Configuration cfgConfiguration=new Configuration();
		SessionFactory s = cfgConfiguration.configure().buildSessionFactory();
		Session ss=s.openSession();
		ss.beginTransaction();
		ss.save(ip_List);
		ss.save(student);
		ss.getTransaction().commit();
		ss.close();
		s.close();
	}
}


hibernate的基本类使用:核心API见 http://blog.youkuaiyun.com/dare_/article/details/19548207

Configuration读取配置文件

SessionFactory用于创建Session

save方法用于执行insert语句;




您可能感兴趣的与本文相关的镜像

Llama Factory

Llama Factory

模型微调
LLama-Factory

LLaMA Factory 是一个简单易用且高效的大型语言模型(Large Language Model)训练与微调平台。通过 LLaMA Factory,可以在无需编写任何代码的前提下,在本地完成上百种预训练模型的微调

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值