hibernate之1.环境搭建
来源:互联网 发布:电子cad软件 编辑:程序博客网 时间:2024/06/05 12:06
持久化概念:
持久化就是把数据(如内存中的对象)同步保存到数据库或某些存储设备中(如磁盘文件中、XML数据文件中)等等。在软件的分层体系结构中,持久化层是与数据库打交道的逻辑层.
JDBC-的问题:
代码繁琐:
一个插入对象的例子
public void addAccount(final Account account) throws DAOException,AccountAlreadyExistException{final Connection conn=getConnection();PreparedStatement pstmt=conn.prepareStatement(“insert into account values(?,?,?)”);pstmt.setString(1.account.getUserName));pstmt.setInt(2,account.getPassWord));pstmt.setString(3.account.getSex);pstmt.execute();conn.close();
如果account表有上百个字段,则pstmt.setXX()语句要写上百次,这明显不合适
非面向对象
对于数据库的操作不是面向对象,是面向关系数据库的
ORM简介
ORM(object relational mapping)是对象到关系的映射
它把对表直接进行的操作变成对持久化类的属性和方法的直接操作
ORM作为分层体系中的持久层.
ORM技术可以极大地提高开发效率和开发时间,同时开发质量也更容易得到保证
hibernate核心接口
Session:Session、SessionFactory、Transaction、Query和Configuration
Session负责执行被持久化对象的CRUD操作(CRUD的任务是完成与数据库的交流,包含了很多常见的SQL语句。
但需要注意的是Session对象是非线程安全的。同时,Hibernate的session不同于JSP应用中的HttpSession。
这里当使用session这个术语时,其实指的是Hibernate中的session,而以后会将HttpSesion对象称为用户session。
SessionFactory
SessionFactroy负责初始化Hibernate。它充当数据存储源的代理,并负责创建Session对象。
这里用到了工厂模式。需要注意的是SessionFactory并不是轻量级的,因为一般情况下,
一个项目通常只需要一个SessionFactory就够,当需要操作多个数据库时,可以为每个数据库指定一个SessionFactory。
Configuration
Configuration负责配置并启动Hibernate,创建SessionFactory对象。
在Hibernate的启动的过程中,Configuration类的实例首先定位映射文档位置、读取配置,然后创建SessionFactory对象。
Transaction
Transaction负责事务相关的操作。
它是可选的,可发人员也可以设计编写自己的底层事务处理代码.
Query:
环境搭建:Query负责执行各种数据库查询。它可以使用HQL语言或SQL语句两种表达方式。
依赖包:
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/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.demo</groupId><artifactId>my-hibernate</artifactId><version>0.0.1-SNAPSHOT</version><dependencies><dependency><groupId>org.hibernate</groupId><artifactId>hibernate-core</artifactId><version>3.6.10.Final</version></dependency><dependency><groupId>org.hibernate.javax.persistence</groupId><artifactId>hibernate-jpa-2.0-api</artifactId><version>1.0.0.Final</version></dependency><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.17</version></dependency><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>1.7.7</version></dependency><dependency><groupId>org.javassist</groupId><artifactId>javassist</artifactId><version>3.18.2-GA</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.10</version></dependency><dependency><!-- 自己手动添加到maven本地仓库 --><groupId>oracle</groupId><artifactId>ojdbc</artifactId><version>14</version></dependency></dependencies></project>
注意:ojdbc14.jar需要手动添加到maven本地仓库,自定义坐标
hibernate.cfg.xml:
<!DOCTYPE hibernate-configuration PUBLIC"-//Hibernate/Hibernate Configuration DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><hibernate-configuration><session-factory name="foo"><!-- 显示sql --><property name="show_sql">true</property><property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property><property name="hibernate.connection.url">jdbc:oracle:thin:@127.0.0.1:1521:orcl</property><property name="hibernate.connection.username">diankun</property><property name="hibernate.connection.password">diankun</property><!-- 生成sql语句采用的语法 --><property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property><!-- 自动创建表结构 --><property name="hibernate.hbm2ddl.auto">create</property><!-- bean与表的映射 --><mapping resource="com/demo/model/Student.hbm.xml"/></session-factory></hibernate-configuration>
Student:package com.demo.model;public class Student {private int studentId ;private String studentName ;private int age;public int getStudentId() {return studentId;}public void setStudentId(int studentId) {this.studentId = studentId;}public String getStudentName() {return studentName;}public void setStudentName(String studentName) {this.studentName = studentName;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}}
Student.hbm.xml<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><hibernate-mapping package="com.demo.model"> <class name="Student" table="t_student"> <id name="studentId" column="student_id"> <generator class="sequence"/> </id> <property name="studentName" column="student_name"/> <property name="age" /> </class></hibernate-mapping>
Testpackage com.demo;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;import org.junit.Before;import org.junit.Test;public class StudentTest {SessionFactory sessionFactory ;@Beforepublic void init(){sessionFactory=new Configuration().configure().buildSessionFactory();}@Testpublic void createTest(){}}运行mvn test ,数据库自动生成表结构
- hibernate之1.环境搭建
- Hibernate学习之----环境搭建
- 初学Hibernate之环境搭建
- 初识Hibernate之环境搭建
- Hibernate学习之 环境搭建
- Hibernate之环境搭建及demo
- 走近SSH之Hibernate--Hibernate环境搭建(MyEclipse+MySql)
- Java 框架之Hibernate (一、Hibernate环境搭建)
- 【hibernate框架】搭建hibernate环境
- hibernate--2.搭建hibernate环境
- 1.Hibernate环境搭建与入门
- 第一章 Hibernate环境搭建
- 搭建hibernate开发环境
- Hibernate环境搭建
- Hibernate环境的搭建
- Hibernate环境搭建
- Hibernate环境的搭建
- Hibernate环境搭建
- PHP之道
- Android代码优化(一)
- 记忆深刻的菱形结构
- hdu 1044Collect More Jewels(bfs+dfs)
- poj 1548 Robots 最小路径覆盖
- hibernate之1.环境搭建
- UVA - 439 Knight Moves
- arc strong weak mrc retain
- Find Minimum in Rotated Sorted Array
- python模拟登陆杂记
- localForage Web 离线存储
- SSL certificate problem, verify that the CA cert is OK. Details:
- 2014.10.14错误修改
- ViewPager和FragmentTabHost的使用