您的位置:首页 > 其它

Hibernate 入门简介

2014-02-19 13:06 204 查看
最近学习了一下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

配置详解见/article/9085485.html

<?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
>


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

详细介绍:/article/9085487.html

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.csdn.net/dare_/article/details/19548207
Configuration读取配置文件

SessionFactory用于创建Session

save方法用于执行insert语句;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: