1、建立web项目

2、复制相关的jar文件到 项目的lib目录下
antlr-2.7.7.jar
dom4j-1.6.1.jar
hibernate-commons-annotations-4.0.5.Final.jar
hibernate-core-4.3.7.Final.jar
hibernate-jpa-2.1-api-1.0.0.Final.jar
jandex-1.1.0.Final.jar
javassist-3.18.1-GA.jar
jboss-logging-3.1.3.GA.jar
jboss-logging-annotations-1.2.0.Beta1.jar
jboss-transaction-api_1.2_spec-1.0.0.Final.jar
mysql.jar

3、src目录下建立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="dialect">
org.hibernate.dialect.MySQL5Dialect
</property> <!-- 数据库连接设置 -->
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/db?useUnicode=true&amp;characterEncoding=utf8
</property>
<property name="connection.username">root</property>
<property name="connection.password">fengze</property> <!-- 显示SQL语句 -->
<property name="show_sql">true</property> <!-- 如果表不存在,直接建立或更新对应的表 -->
<property name="hbm2ddl.auto">update</property> <!-- 对象映射的配置文件 -->
<mapping resource="com/db/model/Student.hbm.xml" /> </session-factory> </hibernate-configuration>

4、Student类

package com.db.model;

import java.util.Date;

public class Student {
private int id;
private String name;
private Date birthday;
private double price; public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public Date getBirthday() {
return birthday;
} public void setBirthday(Date birthday) {
this.birthday = birthday;
} public double getPrice() {
return price;
} public void setPrice(double price) {
this.price = price;
} }

5、Student类映射XML(在Student类下 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>
<class name="com.db.model.Student">
<id name="id">
<generator class="native" />
</id>
<property name="name" type="string" length="15" />
<property name="birthday" type="date" />
<property name="price" type="double" precision="4" scale="1" />
</class>
</hibernate-mapping>

6、HibernateSessionFactory.java

package com.db.hibernate;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder; /**
* Configures and provides access to Hibernate sessions, tied to the
* current thread of execution. Follows the Thread Local Session
* pattern, see {@link http://hibernate.org/42.html }.
*/
public class HibernateSessionFactory { /**
* Location of hibernate.cfg.xml file.
* Location should be on the classpath as Hibernate uses
* #resourceAsStream style lookup for its configuration file.
* The default classpath location of the hibernate config file is
* in the default package. Use #setConfigFile() to update
* the location of the configuration file for the current session.
*/
private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
private static org.hibernate.SessionFactory sessionFactory; private static Configuration configuration = new Configuration();
private static ServiceRegistry serviceRegistry; static {
try {
configuration.configure();
serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
} catch (Exception e) {
System.err.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
private HibernateSessionFactory() {
} /**
* Returns the ThreadLocal Session instance. Lazy initialize
* the <code>SessionFactory</code> if needed.
*
* @return Session
* @throws HibernateException
*/
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;
} /**
* Rebuild hibernate session factory
*
*/
public static void rebuildSessionFactory() {
try {
configuration.configure();
serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
} catch (Exception e) {
System.err.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
} /**
* Close the single hibernate session instance.
*
* @throws HibernateException
*/
public static void closeSession() throws HibernateException {
Session session = (Session) threadLocal.get();
threadLocal.set(null); if (session != null) {
session.close();
}
} /**
* return session factory
*
*/
public static org.hibernate.SessionFactory getSessionFactory() {
return sessionFactory;
}
/**
* return hibernate configuration
*
*/
public static Configuration getConfiguration() {
return configuration;
} }

7、index.jsp

<%@page import="java.util.Date"%>
<%@page import="com.db.model.Student"%>
<%@page import="org.hibernate.Transaction"%>
<%@page import="org.hibernate.Session"%>
<%@page import="com.db.hibernate.HibernateSessionFactory"%>
<%@ page language="java" pageEncoding="utf-8"%>
<%
Session s = HibernateSessionFactory.getSession();
Transaction tx = s.beginTransaction();
Student s1 = new Student();
s1.setPrice(800);
s1.setName("中文");
s1.setBirthday(new Date());
s.save(s1);
tx.commit();
%>

Hibernate 4.3 配置文件实现的更多相关文章

  1. Hibernate的主配置文件hibernate.cfg.xml

    1:Hibernate的主配置文件的名字必须是hibernate.cfg.xml(主要配置文件中主要配置:数据库连接信息,其他参数,映射信息):常用配置查看源码:Hibernate\hibernate ...

  2. 2.Hibernate的主配置文件hibernate.cfg.xml

    1.配置 Hibernate 需要事先知道在哪里找到映射信息,这些映射信息定义了 Java 类怎样关联到数据库表.Hibernate 也需要一套相关数据库和其它相关参数的配置设置.所有这些信息通常是作 ...

  3. Hibernate(三)结构-配置文件-实体映射及配置文件

    一.体系结构 SessionFactory:属于单一数据库的编译过的映射文件的一个线程安全的,不可变的缓存快照.Session的工厂.有可能持有一个可选的数据缓存可以进程级别或者群级别保存可以在事务中 ...

  4. Hibernate SQL方言 (hibernate.dialect) Spring配置文件applicationContext.xml

    转自:http://www.cnblogs.com/wj-wangjun/archive/2009/10/21/1587624.html Hibernate SQL方言 (hibernate.dial ...

  5. hibernate.cfg.xml配置文件和hbm.xml配置文件

    http://blog.sina.com.cn/s/blog_a7b8ab2801014m0e.html hibernate.cfg.xml配置文件格式 <?xml version=" ...

  6. hibernate.cfg.xml配置文件和hbm.xml配置文件 模板

    hibernate.cfg.xml配置文件格式 <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE ...

  7. 给Eclipse中hibernate.cfg.xml配置文件加提示

    在hibernate框架需要的jar包中找到hibernate3.jar,并用压缩软件打开,如图: 2 选择org文件夹--打开下一级文件夹 3 点击类型,方便找到dtd文件,下拉查看dtd文件,有两 ...

  8. springMVC+Hibernate常用的配置文件

    每次写一个新的web项目时都要写配置文件.比较麻烦,现在把常用到的配置文件记录下来,方便以后使用 web.xml <?xml version="1.0" encoding=& ...

  9. hibernate.hbm.xml配置文件内容说明

    下面是一个自动生成的配置文件: <?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-configuration PU ...

  10. Hibernate之SchemaExport+配置文件生成表结构

    首先要生成表,得先有实体类,以Person.java为例: /** * * @author Administrator * @hibernate.class table="T_Person& ...

随机推荐

  1. Spring-boot和Spring-Cloud遇到的问题

    1.spring cloud 使用 feign 启动报错  错误信息 org/springframework/cloud/client/loadbalancer/LoadBalancedRetryFa ...

  2. 使用 C# 开发智能手机软件:推箱子(四)

    这是"使用 C# 开发智能手机软件:推箱子"系列文章的第四篇. 在这篇文章中,介绍 Common/FindPath.cs 源程序文件. using System; using Sy ...

  3. bash仅仅读的环境变量

    环境变量名 变量的用途 $0 程序的名字 $1~$9 命令參数1~9的值 $* 全部命令行參数的值 $@ 全部命令行參数的值.假设$@被""包含.即"$@",这 ...

  4. Linux变量内容的删除、代替与替换

    变量内容的删除与代替 范例一:先让小写的 path 自己定义变量配置的与 PATH 内容同样 [root@www ~]# path=${PATH} [root@www ~]# echo $path / ...

  5. Js两种post方式(转)

    第一种提交post的方式是传统方式,判断浏览器进行post请求. <SCRIPT stype=text/javascript> var xmlobj; //定义XMLHttpRequest ...

  6. quilt - 制作patch的工具

    quilt - 制作patch的工具 在尝试为openwrt做一个patch时,查到这个工具.openwrt官方已经有很详细的文档对步骤进行说明了. quilt并不是专为openwrt的开发工具.qu ...

  7. mvn -v 报错解决办法

    由于近期公司需求,我找到了个maven教程:http://wentao365.iteye.com/blog/903396 安装maven其实很简单,就是在Apache官网下载需要的maven包,然后配 ...

  8. Python开发【2.3 模块】

    1.模块导入 import 模块名 from 模块名 import 函数/类/变量 2.模块路径 import sys sys.path 3.模块重新导入 Python3若想在同一次会话中再次运行文件 ...

  9. AndroidCityPicker仿IOS选择效果

    近期的一个项目由于android端与IOS端须要同步,所以在城市选择器这里做了一个相似IOS的CityPicker控件,当然由于本人水平问题显示效果比IOS上面还是有一定差距的.OK先让大家看下效果. ...

  10. BAPI_PO_CEATE 与PO_1