User 接口:

public class User implements java.io.Serializable {private int id;private String name;private String password;private String type;User(){}public int getId(){return this.id;}public void setId(int id){this.id = id;}public void setName(String name){this.name = name;}public String getName(){return this.name;}public void setPassword(String password){this.password =password;}public String getPassword(){return this.password;}public void setType(String type){this.type = type;}public String getType(){return this.type;}
}

User.hbm.xml 映射文件

<?xml version="1.0" encoding="UTF-8">
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<!-- name 指定持久化类名 table 指定数据表-->
<class name="org.hibernate.entity.User" table="USER">
<!--将数据表和bean 映射对应起来-->
<id name="id" type="java.lang.Interger" column="USER_ID">
<generator class="increment">
</id><property name="name" type="java.lang.String" column="NAME" length="20">
</property><property name="password" type="java.lang.String" column="NAME" length="12">
</property><property name="type" type="java.lang.String" column="TYPE" length="6">
</property>
</class>
</hibernate-mapping>

hibernate.cfg.xml  配置文件

<?xml version="1.0" encoding="UTF-8">
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Configration DTD3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 数据库链接设置-->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<!--数据库url-><property name="hibernate.connection.url" >
jdbc:mysql://localhost:3306/mysqldb
</property><property name="hibernate.connection.username" >mysql</property><property name="hibernate.connection.password" >123456</property>
<property name="hibernate.connection.pool_size" >1</property>
<property name="dialect" >org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql" >true</property>
<!--列出所有映射文件-->
<mapping resource="org/hibernate/entity/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>

UserADO 接口类:

public interface UserDAO {void save(User user);User findById(int id);void delete(User user);void update(User user);
}

H会bernateUtil实现类:

public class HibernateUtil{private static SessionFactory sessionFactory;private static final ThreadLocal<Session> threadLocal= new ThreadLocal<Session>();static {try{Configuration cfg = new Configuration().configure();sessionFactory = cfg.buildSessionFactory();}catch (Throwable ex){throew new ExceptionInInitializerError(ex);}}// 获得sessionfactory实例public static SessionFactory getSessionFactory(){return sessionFactory;}public static Sesstion getSession() throws HibernateExcaption{Session session = (Session) threadLocal.get();if(session == null || !session.isOpen()){if(sessionFactory == null){rebuildSesstionFactory();}session = (sessionFactory != null)? sessionFactory.openSession():null;threadLocal.set(session);}return session;}public staitc void closeSession() throws HibernateExcaption{Session session = (Session) threadLocal.get();threadLocal.setnull();if(session == null ) {sesstion.close();}}public static void rebuildSesstionFactory(){try{confuguration.confugure.(/hibernate.cfg.xml);sessionFactory configuration.buildSessionFactory();}catch(Exception e){System.err.println("error ncreate session factory.");e.printStackTrace();}}public static void shutdown(){getSessionFactory().close();}}
UserADOImpl 实现类:public class UserADOImpl implements UserDAO{public void save(User user){Session session = HibernateUtil.getSession();Transaction tx =session.beginTransaction();try{session.save(user);tx.commit();}catch(Exception e){e.printStackTrace();tx.rollback();}finally {HibernateUtil.closeSession();}}public void delete(User user){Session session = HibernateUtil.getSession();Transaction tx =session.beginTransaction();try{session.delete(user);tx.commit();}catch(Exception e){e.printStackTrace();tx.rollback();}finally {HibernateUtil.closeSession();}}public void update(User user){Session session = HibernateUtil.getSession();Transaction tx =session.beginTransaction();try{session.update(user);tx.commit();}catch(Exception e){e.printStackTrace();tx.rollback();}finally {HibernateUtil.closeSession();}}public User findByyId(int id){User user = null;Session session = HibernateUtil.getSession();Transaction tx =session.beginTransaction();try{user = session.get(User.class, id);tx.commit();}catch(Exception e){e.printStackTrace();tx.rollback();}finally {HibernateUtil.closeSession();}return user;}
}

hibernate demo 搭建相关推荐

  1. 简单的hibernate环境搭建、自动生成model/配置/hibernate.xml配置文件

    自己亲测的东西才是最有效果的,下面贴出整个编写的过程. 1 hibernate环境搭建,这个博客非常给力:http://www.111cn.net/wy/js-ajax/93142.htm 需要用到的 ...

  2. 【流媒体服务器Mediasoup】环境部署与demo搭建(二)

    目录 前言 服务器环境 NodeJs的安装 下载Demo源码 配置服务端 部署及测试 可能会遇到的问题 前言 上篇文章对MediaSoup进行简单的介绍  [MediaSoup]多人音视频架构.流媒体 ...

  3. 定时任务框架Quartz-(一)Quartz入门与Demo搭建

    一.什么是Quartz 什么是Quartz? Quartz是OpenSymphony开源组织在Job scheduling领域又一个开源项目,完全由Java开发,可以用来执行定时任务,类似于java. ...

  4. Spring+SpringMvc+Hibernate 框架搭建

    2019独角兽企业重金招聘Python工程师标准>>> 框架篇:Spring+SpringMVC+hibernate整合开发 一.建立项目 1.新建一个空项目project 相当于e ...

  5. Hibernate环境搭建以及HelloWorld

    Hibernate是 Java 领域中使用非常广泛的持久化框架.下面使用eclipse搭建并测试一个HelloWorld程序. 一.安装Hibernate插件 为Eclipse安装Hibernate插 ...

  6. [Spring+Hibernate系列]1.Spring+Hibernate 框架搭建

    1. 示例环境 OS版本: Win 7 , 64位 Java 版本: java version "1.6.0_30" 开发IDE: Eclipse,  Version: Indig ...

  7. Hibernate初次搭建与第一个测试例子

    今年学了两个框架,今年重装了两次myeclipse,我能怎么办,我也很无奈 安装myeclipse2017 stable2.0 https://www.52pojie.cn/thread-687683 ...

  8. Licode Demo搭建

    上一篇文章说明了如何搭建 appRtc,但那个 demo 只能提供 1对1 通讯的房间,这篇文章旨在搭建多人视频房间. 说明,Licode 是一个基于  webRTC 的开源项目,目前只支持 ubun ...

  9. rocketmq本地demo搭建

    gitee的地址:RocketMQ: RocketMQ 是一款开源的分布式消息系统,基于高可用分布式集群技术,提供低延时的.高可靠的消息发布与订阅服务. - Gitee.com 我本地(windows ...

最新文章

  1. [转载] K3 K3c 最简单无损开壳方法
  2. 10月机器学习开源项目Top10
  3. c++同时读取文本_VBA专题08:使用VBA操作文本文件
  4. Wix 安装部署教程(十六) -- 自动生成多语言文件
  5. BugKuCTF 杂项 come_game
  6. 多路 IO 转接 :epoll 函数
  7. linux传奇源码,游戏源码 屠龙传奇H5 鲲乃异兽 全套源码+教程
  8. 1022. 宠物小精灵之收服
  9. 概述嵌入式设备驱动,教你怎么“玩”转嵌入式开发
  10. html+lt;input+file样式,不同内核的浏览器中文件选择控件的外观也不相同
  11. 微信小程序 View:flex 布局
  12. 从MFC与ATL的男女关系说起
  13. BackPropagation_01
  14. Windows Server 2012虚拟桌面分辨率支持列表
  15. ps批量修改名片文字_PS批量制作多项字幕条
  16. 射极跟随器实验报告数据处理_射极跟随器实验报告射跟随器实验报告.doc
  17. 小兔子从小就想做一只很酷的兔子。
  18. 固态硬盘是什么接口_M.2 SSD固态硬盘的规格、接口、总线详解
  19. 漫画研发之九:浑水好摸鱼
  20. 米兔机器人恐龙拼图手册_MI 小米 米兔积木机器人 履带版

热门文章

  1. Unable to load native-hadoop library for your platform
  2. Earliest PEP Algorithm Principles
  3. TypeError: 'dict' object is not callable
  4. 5.13 卡尔曼滤波
  5. 机器学习(四)——SVM(2)
  6. 机器学习数据挖掘笔记_15(关于凸优化的一些简单概念)
  7. TCP协议详解----Linux学习笔记
  8. [雪峰磁针石博客]python网络基础工具书籍下载-持续更新
  9. Ettercap内网渗透
  10. 每天一个 liunx 命令 ls