使用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. BeanFactory中Bean的生命周期

    Bean的生命周期图解 集体过程如下: 当调用者通过getBean(beanName)向容器请求某一个Bean时,如果容器注册了org.springframework.beans.factory.co ...

  2. IE浏览器打不开网页的解决方法

    前阵子一下子安装了很多软件,后来使用IE游览器的时候,莫名其妙的打不开网页,虽然用其他浏览器(比如谷歌.火狐)可以正常浏览网页,但是由于很多软件内嵌页面都会调用Windows的IE浏览器来加载,所以I ...

  3. Spring 自动清除缓存的配置

    - spring.resources.chain.strategy.content.enabled=true - spring.resources.chain.strategy.content.pat ...

  4. jquery计算时间差(天、时、分、秒)并使用定时器实时获取

    类似网站抢购需求,会有个时间倒计时的展示(天.时.分.秒) 要拿到最终时间与当前时间对比,算出时间差并用定时器以秒的方式执行 实现代码: $(document).ready(function(){ r ...

  5. Spring Boot属性配置文件详解

    相信很多人选择Spring Boot主要是考虑到它既能兼顾Spring的强大功能,还能实现快速开发的便捷.我们在Spring Boot使用过程中,最直观的感受就是没有了原来自己整合Spring应用时繁 ...

  6. loback学习

    博客链接  http://aub.iteye.com/blog/1101222

  7. leetcode 131. Palindrome Partitioning 、132. Palindrome Partitioning II

    131. Palindrome Partitioning substr使用的是坐标值,不使用.begin()..end()这种迭代器 使用dfs,类似于subsets的题,每次判断要不要加入这个数 s ...

  8. poi导出excel,表头数据动态拼装

    /* * 第一步:拼装表头和数据 */ // 放多个sheet的集合 List<Map<String,Object>> datas = new ArrayList<Map ...

  9. PAT A1111 Online Map (30 分)——最短路径,dijkstra

    Input our current position and a destination, an online map can recommend several paths. Now your jo ...

  10. docker被入侵后.............

    服务器上线后,怎么发现总有个 xmrig 的容器在跑,删了还出来 那么恭喜你!!你的服务器已经被入侵了!! $ docker ps IMAGE               COMMAND       ...