关闭

hibernate初体验

标签: hibernate
95人阅读 评论(0) 收藏 举报
分类:

hibernate初体验

hibernate,中文意思是“冬眠”, 就是让java对象在db中永久冬眠, 是一种持久化技术。

持久化,就是让内存中的数据保存在磁盘或数据库中,像jdbc,io都算是持久化技术。而平常使用的jdbc在数据处理细节上非常麻烦,尤其是数据库字段很多的时候,也很容易犯错,这就会浪费了很多的时间。而hibernate以面向对象的方式访问数据,通过java对象到数据表的映射,能够有效的减少开发时间。

下面,我们就来初步体验一下hibernate吧!

1.创建数据库和表

<!--MySQL-->
create database person;
create table customer(
	id int primary key,
	name varchar(20),
	age int,
	birthday date,
	photo longblob,
	description text
);

2.创建java工程,导入jar包

本案例使用的是hibernate-release-5.2.2.Final,下载地址:hibernate下载
导入jar包的位置:解压位置/bin/required/*.jar
创建Customer类
package com.hibernate.domain;

import java.sql.Date;

public class Customer {

	private int id;
	private String name;
	private int age;
	private Date birthday;
	private byte[] photo;
	private String description;
	
	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 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;
	}
	
	public byte[] getPhoto() {
		return photo;
	}
	public void setPhoto(byte[] photo) {
		this.photo = photo;
	}
	
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	
}

3.映射Customer.hbm.xml文件配置

该文件和Customer.java文件在同一位置.
对于<!DOCTYPE>标签,我们可以从hibernate-core-5.2.2.Final.jar/org.hibernate/hibernate-mapping-3.0.dtd中复制过来
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<?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>
  <class name="com.hibernate.domain.Customer" table="customer" lazy="false">
  
    <!-- type:映射类型,不是java类型也不是sql类型 -->
    <id name="id" column="id" type="integer">
      <!-- 指定主键生成策略 -->
      <generator class="increment"/>
    </id>
    <property name="name" column="name" type="string"></property>
    <property name="age" column="age" type="integer"></property>
    <property name="birthday" column="birthday" type="date"></property>
    <!-- binary:二进制格式 -->
    <property name="photo" column="photo" type="binary"></property>
    <!-- text:文本格式 -->
    <property name="description" column="description" type="text"></property>
    
  </class>
</hibernate-mapping>

4.资源文件

在资源文件hibernate.properties中保存数据库的连接信息,该文件直接放置在src文件下。对于dialect, 可以理解为数据库方言,指明使用的是哪种数据库,该类可以在org.hibernate.dialect包中找到。show_sql=true,则会输出数据库操作的语句。
hibernate.connection.driver_class=com.mysql.jdbc.Driver
hibernate.connection.url=jdbc:mysql://localhost:3306/person
hibernate.connection.username=root
hibernate.connection.password=mingming
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
hibernate.show_sql=true

5.程序测试

建立App.java类
package com.hibernate;

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

import com.hibernate.domain.Customer;

/**
 * 测试程序
 */
public class App {
	//会话工厂
	public static SessionFactory sf = null;
	
	static {
		Configuration conf = new Configuration();			
		try {
			conf.addClass(Customer.class);
			sf = conf.buildSessionFactory();
		} catch (MappingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void insertCustomer(Customer customer) {
		//开启会话
		Session s = sf.openSession();
		//开启事务
		Transaction tx = s.beginTransaction();
		s.save(customer);
		tx.commit();
		s.close();
	}
	
	public static void main(String[] args) {
		Customer c = new Customer();
		c.setName("Jack");
		c.setAge(20);
		c.setDescription("Jack is good boy.");
		insertCustomer(c);
		
	}
	
	
}

最后成功插入一条记录,
八月 29, 2016 10:59:09 下午 org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.2.2.Final}
八月 29, 2016 10:59:09 下午 org.hibernate.cfg.Environment <clinit>
INFO: HHH000205: Loaded properties from resource hibernate.properties: {hibernate.connection.username=root, hibernate.connection.password=****, hibernate.dialect=org.hibernate.dialect.MySQL5Dialect, hibernate.show_sql=true, hibernate.connection.url=jdbc:mysql://localhost:3306/person, hibernate.bytecode.use_reflection_optimizer=false, hibernate.connection.driver_class=com.mysql.jdbc.Driver}
八月 29, 2016 10:59:09 下午 org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
八月 29, 2016 10:59:10 下午 org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
八月 29, 2016 10:59:11 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
八月 29, 2016 10:59:11 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/person]
八月 29, 2016 10:59:11 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {user=root, password=****}
八月 29, 2016 10:59:11 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
八月 29, 2016 10:59:11 下午 org.hibernate.engine.jdbc.connections.internal.PooledConnections <init>
INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
Mon Aug 29 22:59:11 CST 2016 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
八月 29, 2016 10:59:11 下午 org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
Hibernate: select max(id) from customer
Hibernate: insert into customer (name, age, birthday, photo, description, id) values (?, ?, ?, ?, ?, ?)

6.工程示意图



和jdbc相比,感觉确实方便了好多,没有任何SQL语句,就实现了数据的插入,我惊呆了...




0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:3838次
    • 积分:253
    • 等级:
    • 排名:千里之外
    • 原创:22篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条