1.hibernate的配置文件,一般放在classpath的根目录下,默认命名为hibernate.cfg.xml,代码例子如下:

<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.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>
<property name="connection.url">jdbc:mysql:///test</property>
<property name="connection.username">root</property>
<property name="connection.password">admin</property> <property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="hbm2ddl.auto">create-drop</property>
<property name="show_sql">true</property>
<mapping resource="cn/itcast/hibernate/domain/User.hbm.xml" />
</session-factory>
</hibernate-configuration>

2.hibernate的映射文件,User.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 package="cn.itcast.hibernate.domain"> <class name="User">
<id name="id">
<generator class="native" />
</id>
<property name="name"/>
<property name="birthday"/>
</class>
</hibernate-mapping>

3.数据操作类,代码例子:

package cn.itcast.hibernate;

import java.io.Serializable;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration; public final class HibernateUtil {
private static SessionFactory sessionFactory;
private static ThreadLocal session = new ThreadLocal(); private HibernateUtil() {
} static {
Configuration cfg = new Configuration();
cfg.configure(); //如果配置文件路径或名字发生改变,那么就是cfg.configure("配置文件路径及名称");
sessionFactory = cfg.buildSessionFactory();
} public static Session getThreadLocalSession() {
Session s = (Session) session.get();
if (s == null) {
s = getSession();
session.set(s);
}
return s;
} public static void closeSession() {
Session s = (Session) session.get();
if (s != null) {
s.close();
session.set(null);
}
} public static SessionFactory getSessionFactory() {
return sessionFactory;
} public static Session getSession() {
return sessionFactory.openSession();
} public static void add(Object entity) {
Session s = null;
Transaction tx = null;
try {
s = HibernateUtil.getSession();
tx = s.beginTransaction();
s.save(entity);
tx.commit();
} finally {
if (s != null)
s.close();
}
} public static void update(Object entity) {
Session s = null;
Transaction tx = null;
try {
s = HibernateUtil.getSession();
tx = s.beginTransaction();
s.update(entity);
tx.commit();
} finally {
if (s != null)
s.close();
}
} public static void delete(Object entity) {
Session s = null;
Transaction tx = null;
try {
s = HibernateUtil.getSession();
tx = s.beginTransaction();
s.delete(entity);
tx.commit();
} finally {
if (s != null)
s.close();
}
} public static Object get(Class clazz, Serializable id) {
Session s = null;
try {
s = HibernateUtil.getSession();
Object obj = s.get(clazz, id);
return obj;
} finally {
if (s != null)
s.close();
}
}
}

  

hibernate 基础知识的更多相关文章

  1. Hibernate入门1. Hibernate基础知识入门

    Hibernate入门1. Hibernate基础知识入门 20131127 前言: 之前学习过Spring框架的知识,但是不要以为自己就可以说掌握了Spring框架了.这样一个庞大的Spring架构 ...

  2. Hibernate基础知识总结

    Hibernate是JDBC的轻量级的对象封装(encapsulation),它是一个独立的对象持久persistence层框架. hibernate要做的事,就是让对象投影到关系数据库中,然后实施化 ...

  3. Hibernate基础知识

    Hibernate Hibernate的作用: 1.         Hibernate解决ORM(对象关系映射)的问题,大大减少了持久层的代码量 2.         hql方言,解决了可移植性问题 ...

  4. Hibernate基础知识详解

    一.Hibernate框架        Hibernate是一个开放源代码的对象关系映射框架,它对 JDBC进行了非常轻量级的对象封装,它将POJO类与数据库表建立映射关系,是一个    全自动的O ...

  5. Hibernate基础知识介绍

    一.什么是Hibernate? Hibernate,翻译过来是冬眠的意思,其实对于对象来说就是持久化.持久化(Persistence),即把数据(如内存中的对象)保存到可永久保存的存储设备中(如磁盘) ...

  6. JVM 基础知识

    JVM 基础知识(GC) 2013-12-10 00:16 3190人阅读 评论(1) 收藏 举报 分类: Java(49) 目录(?)[+] 几年前写过一篇关于JVM调优的文章,前段时间拿出来看了看 ...

  7. Struts2入门1 Struts2基础知识

    Struts2入门1 Struts2基础知识 20131130 代码下载: 链接: http://pan.baidu.com/s/11mYG1 密码: aua5 前言: 之前学习了Spring和Hib ...

  8. hibernate基础(1)

    hibernate基础1.hibernate介绍与动手入门体验  问题:模型不匹配(java对象模型与数据库关系模型不匹配)  解决: 1.使用JDBC手工转换        2.使用ORM(Obje ...

  9. Spring框架基础知识

    本人博客文章网址:https://www.peretang.com/basic-knowledge-of-spring-framework/ Spring框架简介 Spring , 一个开源的框架 , ...

随机推荐

  1. MeteoInfoLab脚本示例:水汽通量散度计算

    用ncep数据计算水汽通量散度的脚本.需要air, uwnd, vwnd和rhum变量.数据是4维数据,需要固定时间维和高度维,数据中纬度维的数据是反向的,因此读取时需要特殊的设置(::-1).脚本中 ...

  2. sublime text2的插件

    编写html代码,一定要使用emmet(前身是zencoding),还有以下插件也是可以考虑的:bracketHighter 高亮引号.括号等code Aligment 代码对齐DocBlockr 如 ...

  3. spring boot:thymeleaf给fragment传递参数的方法(spring boot 2.3.3)

    一,thymeleaf如何给fragment传递参数? 1,如果是全局的参数,可以用interceptor中传递 非全局参数,可以从controller中传递 2,引用片断时也可以传递参数 说明:刘宏 ...

  4. spring boot:swagger3的安全配置(swagger 3.0.0 / spring security / spring boot 2.3.3)

    一,swagger有哪些环节需要注意安全? 1,生产环境中,要关闭swagger application.properties中配置: springfox.documentation.swagger- ...

  5. go 爬取页面保存

    package main import ( "bufio" "fmt" "io/ioutil" "net/http" & ...

  6. lumen路由

    $router->get('/', function () use ($router) { return config('options.author'); }); $router->ge ...

  7. 说明资源路径位置类型无法解析The type javax.servlet.http.HttpServletResponse cannot be resolved.

    导入dispatch项目后报错: 解决办法:在项目上单击鼠标右键>  Add Libraries  选择 Server Runtime,下一步  选中Apache Tomcat7 完成  切换标 ...

  8. 忘记MySQL密码怎么办?一招教你搞定!

    在安装完 MySQL 或者是在使用 MySQL 时,最尴尬的就是忘记密码了,墨菲定律也告诉我们,如果一件事有可能出错,那么它一定会出错.那如果我们不小心忘记了 MySQL 的密码,该如何处理呢?别着急 ...

  9. Nacos配置中心使用

    在系统开发过程中,开发者通常会将一些需要变更的参数.变量等从代码中分离出来独立管理,以独立的配置文件的形式存在.目的是让静态的系统工件或者交付物(如 WAR,JAR 包等)更好地和实际的物理运行环境进 ...

  10. GDB使用checkpoint复现bug

    今天面试被问到一个问题,如何调试多进程的程序,我回答gdb attach [pid],之后又问如果程序中有些数据读取不对,但这种现象是偶然发生的,这时候要怎么操作,当时就懵了......,通过查找资料 ...