使用hibernate的四个步骤:
第一:创建一个hibernate.cfg.xml。
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!--声明Hibernate配置文件的开始-->
<hibernate-configuration>
<session-factory>
<!--配置链接数据库的基本属性-->
<property name="connection.url">jdbc:sqlserver://localhost:1433;DatabaseName=Book</property>
<property name="connection.username">sa</property>
<property name="connection.password">123456</property>
<property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property> <!--输出sql语句-->
<property name="show_sql">true</property> <!--数据库使用的sql方言-->
<property name="dialect">org.hibernate.dialect.SQLServerDialect</property> <!--是否格式化sql语句-->
<property name="format_sql">true</property> <!--指定自动生成数据表的策略-->
<!--<property name="hbm2ddl.auto">update</property>--> <!--指定需要关联的映射文件-->//注意在最后要加上这个关联的文件,*.hbm.xml文件。
<mapping resource="NewSpringDAO/Book2.hbm.xml"></mapping>
</session-factory>
</hibernate-configuration> 第二:写一个持久化类
1:必须有一个无参的构造器。
2:非final类
3:set,get方法。
public class Book2 { private int id;
private String name;
private double price;
private String author;
private int bookCount; public Book2(){} public Book2(int id,String name,double price,String author,int bookCount){
this.id=id;
this.name=name;
this.price=price;
this.author=author;
this.bookCount=bookCount;
} public void setId(int id){this.id=id;}
public void setName(String name){this.name=name;}
public void setPrice(double price){this.price=price;}
public void setAuthor(String author){this.author=author;}
public void setBookCount(int bookCount){this.bookCount=bookCount;} public int getId(){return id;}
public String getName(){return name;}
public double getPrice(){return price;}
public String getAuthor(){return author;}
public int getBookCount(){return bookCount;} @Override
public String toString() {
return " "+this.id+" "+this.name+" "+this.price+" "+this.author+" "+this.bookCount;
}
} 第三:在类路径下创建对应的*.hbm.xml
<?xml version="1.0" encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="NewSpringDAO.Book2" table="Bookimformation">
<!--主键的方式用id标签。-->
<id name="id" column="id" type="java.lang.Integer">
<!--<generator class="native"></generator>--><!-- 这个标签用来定义主键生成策略。
有这个标签的时候插入会抛出异常,因为表时我自己建的,并不是由Hibernate自动生成的!-->
</id> <property name="name" column="name" type="java.lang.String" not-null="true">
</property> <!--name属性为javaBean相对应的字段,column属性为数据库中的数据表对应的列名,type为数据库中的字段的类型-->
<property name="author" column="author" type="java.lang.String" not-null="true">
</property> <property name="price" column="price" type="java.lang.Double" not-null="true">
</property> <property name="bookCount" column="bookCount" type="int" not-null="true">
</property>
</class>
</hibernate-mapping> 第四步:
在我的创建的一个HibernateUtil的类的前提下,使用hibernate的API。
public class HibernateUtil {
private static final ThreadLocal<Session>threadLocal=new ThreadLocal<Session>(); private static SessionFactory sessionFactory=null; static {
try {
Configuration configuration = new Configuration().configure(); ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()
.applySettings(configuration.getProperties())
.buildServiceRegistry();//你麻痹你就不能写个类把这些方法包装起来!!! sessionFactory=configuration.buildSessionFactory(serviceRegistry); } catch (Exception e) {
System.out.println("创建会话工厂失败!");
e.printStackTrace();
}
} public static Session getSession() throws HibernateException{
Session session=(Session)threadLocal.get();
if (session==null||!session.isOpen()) {
if (sessionFactory == null)
rebuildSessionFactory();
session = (sessionFactory != null) ? sessionFactory.openSession() : null;
threadLocal.set(session);
}
return session;
} public static void rebuildSessionFactory(){
try {
Configuration configuration = new Configuration().configure(); ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()
.applySettings(configuration.getProperties())
.buildServiceRegistry();//你麻痹你就不能写个类把这些方法包装起来!!! sessionFactory=configuration.buildSessionFactory(serviceRegistry); } catch (Exception e) {
System.out.println("创建会话工厂失败!");
e.printStackTrace();
}
} public static SessionFactory getSessionFactory(){
return sessionFactory;
} public static void closeSession() throws HibernateException{
Session session=(Session)threadLocal.get();
threadLocal.set(null);
if (session!=null)
session.close();
}
}
具体使用hibernate的API,参照包Hibernate

Hibernate的初次使用的更多相关文章

  1. Hibernate笔记——Hibernate介绍和初次环境配置

    Hibernate简介 Hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,使得Java程序员可以随心所欲的使用对象编程思维来操纵数据库. Hibernate ...

  2. hibernate初次配置问题

    1.自动创建表结构 在hibernate.cfg.xml配置文件中修改 <property name="hibernate.hbm2ddl.auto">update&l ...

  3. Hibernate 系列 03 - 使用Hibernate完成持久化操作

    引导目录: Hibernate 系列教程 目录 康姆昂,北鼻,来此狗.动次打次,Hibernate继续走起. 目录: 使用Hibernate实现按主键查询 使用Hibernate实现数据库的增.删.改 ...

  4. 最新版ssh hibernate spring struts2环境搭建

    最新版ssh hibernate spring struts2环境搭建 最新版spring Framework下载地址:spring4.0.0RELEASE环境搭建 http://repo.sprin ...

  5. HIbernate的脏数据检测和延缓加载

    脏数据监测: 在一个事务中,加载的数据,除了返回给用户之外,会复制一份在session中,在事务提交时,会用session中的备份和用户的数据进行比对,如果用户的数据状态改变, 则用户的数据即为:脏数 ...

  6. 【java】spring-data-jpa 集成hibernate实现多条件分页查询

    初次接触spring-data-jpa,实现多条件分页查询. 基础环境 Spring Boot+spring-data-jpa+hibernate+mysql 1.接口 要继承这个接口,这个接口提供了 ...

  7. [转]PO和VO、关于延迟加载(lazy)和强制加载(Hibernate.initialize(Object proxy) )

    摘自http://www.cnblogs.com/kelin1314/archive/2009/11/13/1602778.html PO和VO PO 即Persistence Object VO 即 ...

  8. hibernate one-to-many many-to-one 双向注解

    建表语句: DROP TABLE IF EXISTS `t_company`; CREATE TABLE `t_company` ( `companyId` ) unsigned NOT NULL A ...

  9. 【J2EE】Hibernate

    Hibernate是面向Java环境的对象/关系数据库映射工具,管理Java应用和数据库之间的映射关系,提供数据查询和获取数据的方法,可以大幅减少使用JDBC处理数据持久化的时间. 使用Eclipse ...

随机推荐

  1. [转]mysql和redis的区别

    转自https://www.cnblogs.com/zxh1297/p/9394108.html 1.mysql和redis的数据库类型 mysql是关系型数据库,主要用于存放持久化数据,将数据存储在 ...

  2. 转 VMware虚拟机三种联网方式(图文详细解说)

    原文地址https://blog.csdn.net/lucienduan/article/details/38233147 VMware三种网络模式联网 首先说一下VMware的几个虚拟设备 安装了V ...

  3. MongoDB 4.6.1 c++ driver 编译

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/sheismylife/article/details/25512251 这个版本号已经和之前不一样了 ...

  4. centos7下安装docker(13.1docker存储--data volume)

    我们现在知道docker 有两种存储方式:storage driver和data volume stroage driver这种存储方式主要是存储那些无状态的数据,是镜像层和容器层组成的,而data ...

  5. eureka分区的深入讲解

    背景 用户量比较大或者用户地理位置分布范围很广的项目,一般都会有多个机房.这个时候如果上线springCloud服务的话,我们希望一个机房内的服务优先调用同一个机房内的服务,当同一个机房的服务不可用的 ...

  6. dns与wins的区别

    将主机名字解析称为ip地址有四种办法: dns.winds.hosts文件.lmhosts文件 dns和hosts是很多系统cout采用的一个名称解析的方法,wins和lmhosts是微软的操作系统此 ...

  7. oracle 查询 归档日志最大值和平均值

    select max(ss.size_GB), avg(ss.size_GB)  from (select s.*, rownum rn2          from (select a.*      ...

  8. 说说CDN

    本文今天主要讲解三个方面: 第一.没有CDN之前采取的常用策略是什么; 第二.CND的概念; 第三.CDN的优点和缺点(凡是有利也有弊,任何东西都是相对的); 一.没有CDN之前采取的常用策略是什么 ...

  9. ssh test

    本文以以下需求为背景,介绍详细的做法: 需在同一台服务器同时部署两个不同的 Github 仓库(对 Bitbucket 等 git 服务同样适用) root 用户可在远程登录 SSH 后附上预期的 S ...

  10. PAT A1014 Waiting in Line (30 分)——队列

    Suppose a bank has N windows open for service. There is a yellow line in front of the windows which ...