Hibernate学习一----------Hibernate初实现

© 版权声明:本文为博主原创文章,转载请注明出处

ORM(Object/Relationship Mapping):对象/关系映射

  - 利用面向对象思想编写的数据库应用程序最终都是把对象信息保存在关系型数据库中,于是要编写很多和底层数据库相关的sql语句,这与面向对象的思想格格不入

SQL语句的局限

  - 1.不同的数据库使用的SQL语法不同。比如PL/SQL(Oracle)、T/SQL(微软)。如果要切换数据库可能会存在问题

  - 2.同样的功能在不同的数据库中有不同的实现方式。比如分页(Oracle:rownum(),MySQL:limit,SQLserver:top)

Hibernate

  - 1.hibernate是Java领域的一款开源的ORM框架技术

  - 2.Hibernate对JDBC进行了非常轻量级的对象封装(本质上还是JDBC)

实例

1.项目结构

2.pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  
  	<modelVersion>4.0.0</modelVersion>
  	
	<groupId>org.hibernate</groupId>
	<artifactId>Hibernate-001</artifactId>
	<packaging>war</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<name>Hibernate-001 Maven Webapp</name>
	<url>http://maven.apache.org</url>
	
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<hibernate.version>5.1.6.Final</hibernate.version>
	</properties>
	
	<dependencies>
		<!-- junit -->
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.12</version>
			<scope>test</scope>
		</dependency>
		<!-- hibernate -->
		<dependency>
		    <groupId>org.hibernate</groupId>
		    <artifactId>hibernate-core</artifactId>
		    <version>${hibernate.version}</version>
		</dependency>
		<!-- mysql -->
		<dependency>
		    <groupId>mysql</groupId>
		    <artifactId>mysql-connector-java</artifactId>
		    <version>5.1.42</version>
		</dependency>
	</dependencies>
	
	<build>
		<finalName>Hibernate-001</finalName>
	</build>
	
</project>

3.hibernate.cfg.xml

<?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="connection.username">root</property><!-- 数据库登录名 -->
		<property name="connection.password">***</property><!-- 数据库登录密码 -->
		<property name="connection.driver_class">com.mysql.jdbc.Driver</property><!-- 数据库连接驱动 -->
		<property name="connection.url"><!-- 数据库连接地址 -->
			jdbc:mysql:///hibernate?useSSL=true&amp;characterEncoding=UTF-8
		</property>
	
		<!-- 设置常用属性 -->
		<property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property><!-- 数据库使用方言 -->
		<property name="show_sql">true</property><!-- 是否展示SQL -->
		<property name="format_sql">true</property><!-- 是否格式化SQL -->
		<property name="hbm2ddl.auto">create</property><!-- 自动创建|更新|验证数据库表结构 -->
	
		<!-- 引入映射文件 -->
		<mapping resource="hbm/Student.hbm.xml"/>
	</session-factory>

</hibernate-configuration>

4.Student.java

package org.hibernate.model;

import java.util.Date;

/**
 * 学生实体类
 *
 */
public class Student {

	private int sid;// 学号
	private String sname;// 姓名
	private String gender;// 性别
	private Date birthday;// 出生日期
	private String address;// 地址
	
	public Student(int sid, String sname, String gender, Date birthday, String address) {
		this.sid = sid;
		this.sname = sname;
		this.gender = gender;
		this.birthday = birthday;
		this.address = address;
	}
	public int getSid() {
		return sid;
	}
	public void setSid(int sid) {
		this.sid = sid;
	}
	public String getSname() {
		return sname;
	}
	public void setSname(String sname) {
		this.sname = sname;
	}
	public String getGender() {
		return gender;
	}
	public void setGender(String gender) {
		this.gender = gender;
	}
	public Date getBirthday() {
		return birthday;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	@Override
	public String toString() {
		return "Student [sid=" + sid + ", sname=" + sname + ", gender=" + gender + ", birthday=" + birthday
				+ ", address=" + address + "]";
	}
	
}

5.Student.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!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="org.hibernate.model.Student" table="STUDENTS">
		<id name="sid" type="int">
			<column name="SID"/>
			<generator class="assigned"/><!-- 自定义主键 -->
		</id>
		<property name="sname" type="java.lang.String">
			<column name="SNAME"/>
		</property>
		<property name="gender" type="java.lang.String">
			<column name="GENDER"/>
		</property>
		<property name="birthday" type="java.util.Date">
			<column name="BIRTHDAY"/>
		</property>
		<property name="address" type="java.lang.String">
			<column name="ADDRESS"/>
		</property>
	</class>

</hibernate-mapping>

6.StudentTest.java

package org.hibernate.test;

import java.util.Date;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.model.Student;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class StudentTest {

	private SessionFactory sessionFactory;
	private Session session;
	private Transaction transaction;
	
	@Before
	public void before() {
		
		// 创建会话工厂对象
		sessionFactory = new Configuration().configure().buildSessionFactory();
		// 创建会话
		session = sessionFactory.openSession();
		// 开启事务
		transaction = session.beginTransaction();
		
	}
	
	@After
	public void after() {
		
		// 提交事务
		transaction.commit();
		// 关闭会话
		session.close();
		// 关闭会话工厂
		sessionFactory.close();
		
	}
	
	@Test
	public void test() {
		
		// 生成学生对象
		Student student = new Student(1, "张三", "男", new Date(), "张家村");
		session.save(student);// 保存对象到数据库中
		
	}
	
}

7.效果预览

 参考:http://www.imooc.com/learn/396

posted @ 2017-05-25 12:51  禁忌夜色153  阅读(205)  评论(0编辑  收藏  举报