Hibernate是一种ORM(对象关系/关系数据库)框架,其简单概念就是将Java中的对象映射为关系数据库中的表。程序员只需要操作Java中的对象,即只关心业务逻辑操作,Hibernate就会自动将业务逻辑操作翻译为底层SQL语句进行处理,程序员无需关系数据库中的操作。
首先建立POJO对象,这个对象将映射数据库中的表Employee:
package com.ydoing.domain;
public class Employee {
private int id;
private String firstName;
private String lastName;
private int salary;
public Employee() {
}
public Employee(String fname, String lname, int salary) {
this.firstName = fname;
this.lastName = lname;
this.salary = salary;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String first_name) {
this.firstName = first_name;
}
public String getLastName() {
return lastName;
}
public void setLastName(String last_name) {
this.lastName = last_name;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
}
然后建立java对象与关系数据库中的表的关系,用Employee.hbm.xml来描述,放在src目录下:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.ydoing.domain">
<class name="Employee" table="EMPLOYEE">
<meta attribute="class-description">
This class contains the employee detail.
</meta>
<id name="id" type="int" column="id">
<generator class="native"/>
</id>
<property name="firstName" column="first_name" type="string"/>
<property name="lastName" column="last_name" type="string"/>
<property name="salary" column="salary" type="int"/>
</class>
</hibernate-mapping>
还需要告诉Hibernate数据库的信息,用hibernate.cfg.xml描述,放在src目录下::
<?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>
<property name="hibernate.dialect">
org.hibernate.dialect.MySQL5Dialect
</property>
<property name="show_sql">true</property>
<property name="hibernate.connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="hibernate.connection.url">
jdbc:mysql://localhost/test
</property>
<property name="hibernate.connection.username">
root
</property>
<property name="hibernate.connection.password">
jiangyu
</property>
<mapping resource="Employee.hbm.xml" />
</session-factory>
</hibernate-configuration>
用 MySQL来储存数据:
CREATE TABLE `employee` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`first_name` varchar(20) DEFAULT NULL,
`last_name` varchar(20) DEFAULT NULL,
`salary` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
测试类:
package com.ydoing.test
import org.hibernate.Session
import org.hibernate.SessionFactory
import org.hibernate.Transaction
import org.hibernate.boot.registry.StandardServiceRegistryBuilder
import org.hibernate.cfg.Configuration
import org.hibernate.service.ServiceRegistry
import com.ydoing.domain.Employee
public class Main {
public static void main(String[] args) {
Configuration conf = new Configuration()
conf.configure()
// ServiceRegistry serviceRegistry = new
// ServiceRegistryBuilder().applySettings(conf.getProperties())
// .buildServiceRegistry()
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(conf.getProperties())
.build()
SessionFactory factory = conf.buildSessionFactory(serviceRegistry)
Session session = factory.openSession()
Employee emp = new Employee()
emp.setFirstName("Mac")
emp.setLastName("Jack")
emp.setSalary(100)
Transaction tx = session.getTransaction()
tx.begin()
session.save(emp)
tx.commit()
session.close()
}
}
控制台输出:
Hibernate: insert into EMPLOYEE (first_name, last_name, salary) values (?, ?, ?)
查看MySQL数据
<script type="text/javascript">
$(function () {
$('pre.prettyprint code').each(function () {
var lines = $(this).text().split('\n').length;
var $numbering = $('<ul/>').addClass('pre-numbering').hide();
$(this).addClass('has-numbering').parent().append($numbering);
for (i = 1; i <= lines; i++) {
$numbering.append($('<li/>').text(i));
};
$numbering.fadeIn(1700);
});
});
</script>
版权声明:本文为博主原创文章,未经博主允许不得转载。
分享到:
相关推荐
hibernate 入门经典实例 hibernate 入门经典实例 hibernate 入门经典实例 hibernate 入门经典实例 hibernate 入门经典实例
这是hibernate框架的一个简单的 容易理解的实例,算是hibernate学习入门的hello world实例吧,适合初学者
hibernate 入门详细操作步骤 包括hibernate包,实例实现文档····
hibernate入门实例,内容是增加人员信息实例,手动配置相关配置文件、pojo类,测试数据。后台数据采用mysql数据库,有实体表创建语句,相关hibernate的jar包与数据库驱动包也上传了,可以直接使用,很方便!
Hibernate入门案例,提供MyEclipse和Eclipse的项目源文件以及war文件
hibernate入门实例 hibernate入门实例 hibernate入门实例
hibernate入门实例DOC,经典入门势力
Struts & Hibernate入门实例
struts+hibernate入门实例(eclipse版) author:朱千平 phone:13522080786 qq:200896066
Eclipse快速上手Hibernate之入门实例
java web Hibernate 快速入门实例
hibernate入门实例的具体操作,包括配置文件的手工和由eclipse生成两种方法
myeclipse下hibernate入门实例介绍
struts+hibernate入门实例,数据库用oracle 10g, 开发工具:Eclipse3.2.1
Hibernate入门实例源代码,eclipse工程,可直接运行。Hibernate入门实例源代码,eclipse工程,可直接运行。
webwork+spring+hibernate入门实例,使用mysql数据库,建一个名为imti的数据库,把数据库文件导入数据库 把imti文件直接导入myeclipse
ajax+hibernate入门实例源码。测试方法: 一、先在mysql数据库中创建表并插入数据(脚本在压缩包中) 二、将WebRoot目录复制到tomcat的webapps中。 运行图: <br>
Struts+Hibernate入门实例(eclipse版)
struts2+hibernate 入门 学习 包含对学生信息的增删查改,包含数据库test.sql,放到项目下能够 运行的实例
基于SpringMVC以及Spring,Hibernate的非注解入门小demo