2019独角兽企业重金招聘Python工程师标准>>>

hibernate4平台搭建:

1、hibernate框架独立搭建(此例以hibernate4为例);
在java应用中,使用hibernate包括4步:

1)创建hibernate配置文件:hibernate-configuartion
    2)创建实体类
    3)创建对象/关系映射文件:hibernate-mapping
    4)使用hibernate API编写访问数据库代码
1.1创建hibernate配置文件
For Hibernate's configuration, we can use a simplehibernate.propertiesfile, a more sophisticatedhibernate.cfg.xmlfile, or even complete programmatic setup. Most users prefer the XML configuration file:

<?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><!-- Database connection settings --><!-- <property name="connection.driver_class">org.hsqldb.jdbcDriver</property><property name="connection.url">jdbc:hsqldb:hsql://localhost</property><property name="connection.username">sa</property><property name="connection.password"></property> --><property name="connection.driver_class">com.mysql.jdbc.Driver</property><property name="connection.url">jdbc:mysql://localhost:3306/shop?characterEncoding=UTF-8</property><property name="connection.username">root</property><property name="connection.password">xyz</property><!-- JDBC connection pool (use the built-in) --><property name="connection.pool_size">1</property><!-- SQL dialect --><!-- <property name="dialect">org.hibernate.dialect.HSQLDialect</property> --><property name="dialect">org.hibernate.dialect.MySQLDialect</property><!-- Enable Hibernate's automatic session context management --><property name="current_session_context_class">thread</property><!-- Disable the second-level cache  --><property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property><!-- Echo all executed SQL to stdout --><property name="show_sql">true</property><!-- Drop and re-create the database schema on startup --><property name="hbm2ddl.auto">update</property><mapping resource="org/hibernate/tutorial/domain/Event.hbm.xml"/></session-factory></hibernate-configuration>

1.2创建实体类

package com.it.app.domain;
import java.util.Date;public class Event {private Long id;private String title;private Date date;public Event() {}public Long getId() {return id;}private void setId(Long id) {this.id = id;}public Date getDate() {return date;}public void setDate(Date date) {this.date = date;}public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}
}

1.3创建对象/关系映射文件
Hibernate needs to know how to load and store objects of the persistent class. This is where the Hibernate mapping file comes into play. The mapping file tells Hibernate what table in the database it has to access, and what columns in that table it should use.

<?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 package="org.hibernate.tutorial.domain"><class name="Event" table="EVENTS"><id name="id" column="EVENT_ID"><generator class="native"/></id><property name="date" type="timestamp" column="EVENT_DATE"/><property name="title"/></class>
</hibernate-mapping>

Save this mapping file assrc/main/resources/org/hibernate/tutorial/domain/Event.hbm.xml.
1.4使用hibernate API编写访问数据库代码
We will create aHibernateUtilhelper class that takes care of startup and makes accessing theorg.hibernate.SessionFactorymore convenient.

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;public class HibernateUtil {private static final SessionFactory sessionFactory = buildSessionFactory();private static SessionFactory buildSessionFactory() {try {// Create the SessionFactory from hibernate.cfg.xmlreturn new Configuration().configure().buildSessionFactory();}catch (Throwable ex) {// Make sure you log the exception, as it might be swallowedSystem.err.println("Initial SessionFactory creation failed." + ex);throw new ExceptionInInitializerError(ex);}}public static SessionFactory getSessionFactory() {return sessionFactory;}}

We are now ready to start doing some real work with Hibernate. Let's start by writing anEventManagerclass with amain()method:

import org.hibernate.Session;import java.util.*;import org.hibernate.tutorial.domain.Event;
import org.hibernate.tutorial.util.HibernateUtil;public class EventManager {public static void main(String[] args) {EventManager mgr = new EventManager();if (args[0].equals("store")) {mgr.createAndStoreEvent("My Event", new Date());}HibernateUtil.getSessionFactory().close();}private void createAndStoreEvent(String title, Date theDate) {Session session = HibernateUtil.getSessionFactory().getCurrentSession();session.beginTransaction();Event theEvent = new Event();theEvent.setTitle(title);theEvent.setDate(theDate);session.save(theEvent);session.getTransaction().commit();}}

2、用spring平台搭建hibernate;
2.1配置hibernate信息
The following excerpt from an XML application context definition shows how to set up a JDBCDataSourceand a HibernateSessionFactoryon top of it

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"><!-- SessionFactory, DataSource, etc. omitted --><bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"><property name="driverClassName" value="com.mysql.jdbc.Driver"/><property name="url" value="jdbc:mysql://localhost:3306?characterEncoding=UTF-8"/><property name="username" value="root"/><property name="password" value="xyz"/></bean><bean id="mySessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"><property name="dataSource" ref="myDataSource"/><property name="mappingResources"><list><value>com/it/app/domain/Event.hbm.xml</value></list></property><property name="hibernateProperties"><value>hibernate.dialect=org.hibernate.dialect.MySQLDialect</value></property></bean><bean id="myProductService" class="product.SimpleProductService"><property name="productDao" ref="myProductDao" /></bean></beans>

3、hibernate3搭建;

4、hibernate4搭建;

转载于:https://my.oschina.net/tiancai/blog/83538

hibernate4平台搭建相关推荐

  1. Windows平台搭建-----C语言

    上期我们已经进行Linux的平台搭建,今期我们就来搭建下我们最常用的.最适合初学者的一种方式,那就是搭建Windows平台开发环境,只需要两种工具即可,一个就是编辑器(编辑代码的工具),另一个就是编译 ...

  2. 手把手教你生信分析平台搭建

    宏基因组按:此系列教程为基因学苑的王通老师原创发布.非常适合刚接受生信,又有服务器管理需求的小伙伴做为入门材料,一共20篇文章,内容涉及服务器选购.Linux系统安装.用户管理.软件安装.软件兼容环境 ...

  3. RocketMQ 实战 集群监控平台搭建

    RocketMQ 实战 集群监控平台搭建 概述 RocketMQ有一个对其扩展的开源项目incubator-rocketmq-externals,这个项目中有一个子模块叫rocketmq-consol ...

  4. 运维企业专题(2)HTTP加速器——Varnish缓存机制后篇(后端服务器集群、负载均衡与CDN推送平台搭建)

    1.实验一:配置后端服务器集群 1)实验目的:定义不同域名站点的后端服务器,通过域名会访问不同的后端主机 2)实验过程: <1>在调度器server1上编写Varnish的配置文件 vim ...

  5. mesos+marathon平台搭建

    2019独角兽企业重金招聘Python工程师标准>>> mesos+marathon平台搭建 博客分类: 虚拟化 mesos 一.安装jdk和maven 修改/etc/profile ...

  6. 影院平台搭建 - (2)FLV发布系统的简单搭建

    使用Wowza Media Server进行FLV的发布,相对于Adobe自己的解决方案的对比在这里.RTMP和HTTP的对比在这里.(需要FQ才能看) 最新Wowza Media Server Pr ...

  7. 基于认证的代理平台搭建配置squid-20130730

    基于认证的代理平台搭建配置squid-20130730 功能:通过squid代理实现 (1)基于用户名密码认证的出口ip路由选择 (2)基于client源ip的出口ip路由选择 (3)基于连接本机ip ...

  8. 直播平台搭建中你需要注意的小细节

    直播平台搭建的意义是为了实现完整的直播流程,为了完成直播平台搭建,就必须其中蕴含着那些步骤! 1.采集 采集又称数据获取,在直播流程的层面上来讲,是利用摄像头等工具,从系统外部采集数据并输入到系统内部 ...

  9. Windows下Android平台搭建_1

    Windows下Android平台搭建_1 一.  Android平台综述 Windows下Android平台搭建需要的软件和工具 1.     JDK        JDK(Java Develop ...

  10. Linux监控平台搭建( zabbix监控)

    2019独角兽企业重金招聘Python工程师标准>>> Linux监控平台搭建( zabbix监控) 一.Linux监控平台介绍 1.监控存在的原因 站点出了问题,没有人知道,等用户 ...

最新文章

  1. Go 使用 append 向切片增加元素
  2. Python之Requests库的异常
  3. Atitit.js图表控件总结
  4. xtrabackup mysql 5.6_MySQL 5.6对于Xtrabackup的影响
  5. hightcharts 3d 堆积图下钻
  6. 利用DOM的方式点击切换图片及修改文字
  7. python单例模式有什么用_python单例模式是什么
  8. iOS 开发 code sign 代码签名深入剖析
  9. python星空画法教程,PS后期打造一张惊艳的星空风景照片后期调色教程
  10. 「需求分析」用户故事和用例是一回事吗?
  11. 熤星传媒:抖音这些方面要注意!
  12. 【水滴石穿】ES must与should组合使用的正确方式
  13. 03 学生免费注册Pycharm专业版
  14. 关于幂级数求和是否弃用首项的理解
  15. ios底部栏设计规范_设计干货:底部导航栏规范设计总结
  16. 手持式频谱分析仪 TFN的715c和760c怎么样
  17. 岚图高管解读近50亿融资:东风跟投9亿 考虑后续IPO
  18. mysql入门视频 吾_学习猿地-全网最新版本MySQL8全套视频教程(学完这个课 MySQL 就精通了)...
  19. 长三角G60科创走廊智能驾驶产业联盟揭牌成立,近80家企业助力智能驾驶行业发展...
  20. JSFuck奇葩的js编码

热门文章

  1. 相对开音节java,L314 单音节词读音规则(二)-元音字母发音规则
  2. 《Android 源码设计模式解析与实战》— Android 书籍
  3. Flutter 深度学习 — 动画(补间动画、Hero动画、交错动画)
  4. L1-029 是不是太胖了 (5 分)—团体程序设计天梯赛
  5. linux svn客户端通过 https访问windows VisualSVN Server Manager
  6. 自己的包增加为第三方包,使用Eclipse环境报Unresolved import错误(pycharm可用正常引用)...
  7. Hover.css:一组超实用的 CSS3 悬停效果和动画
  8. PIM SSM技术原理与实验
  9. Linux系统进程管理详解
  10. NYOJ--60谁获得了最高奖学金