hibernate系列笔记(1)---Hibernate增删改查
Hibernate增删改查
1.首先我们要知道什么是Hibernate
Hibernate是一个轻量级的ORMapping对象。主要用来实现Java和数据库表之间的映射,除此之外还提供数据查询和数据获取的方法,
可以大幅度减少开发时人工使用SQL和JDBC处理数据的时间,解放编程人员95%的任务。
2.什么是ORM Object-Relational-Mapping对象关系映射
ORM:是通过java对象映射到数据库表,通过操作Java对象可以完成对数据表的操作。(假如你用的是Dbutils那么还需要在Java类中写sql语句,而orm就不用)
Hibernate是一个完全的ORM框架只需要对对象的操作即可生成底层的SQL。
接下来直接进入主题:
先看看使用hibernate的基本流程!下面是简单的流程图

1.创建项目:
用myeclipse创建一个web project
2.导入hibernate相关的架包到项目

第三步: 配置文件hibernate
hibernate的配置有两种形式!
一种是使用hibernate.properties文件!
另一种是使用hibernate.cfg.xml文件!这里我们使用hibernate.cfg.xml进行配置
a. 采用properties方式,必须手动编程加载hbm文件或者持久化类
b. 采用XML配置方式,可以配置添加hbm文件
在src目录下新建一个xml文件,名称为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>
<!-- 配置会话工厂 hibernate 核心 管理数据库连接池 -->
<session-factory>
<!-- 1.配置数据库连接参数 -->
<!-- 1.1配置jdbc四个基本连接参数 -->
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.connection.url">jdbc:mysql:///hibernateexec</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- 1.2配置 hibernate使用的方言 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <!-- 2.配置其他相关属性 -->
<!-- 2.1自动建表 -->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 2.2在日志中输出sql -->
<property name="hibernate.show_sql">true</property>
<!-- 2.3格式化sql -->
<property name="hibernate.format_sql">true</property> <!-- 开启事务 -->
<property name="hibernate.connection.autocommit">true</property> <!-- 配置c3p0数据库连接池 -->
<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property> <property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.max_size">50</property>
<property name="hibernate.c3p0.timeout">120</property>
<property name="hibernate.c3p0.idle_test_period">3000</property> <!-- 3.加载映射文件 -->
<mapping resource="com/study/model/Customer.hbm.xml"/> </session-factory> </hibernate-configuration>
配置hibernate.cfg.xml
这里提醒一点:customer表你可以不用去手动创建,但是数据库hibernateexec是要你手动创建的
第四步.创建实体和映射文件
public class Customer {
private int id;
private String name;
private int age;
private String city;
private String addr;
}
/*
* 提供set和get方法
*/
Customer 实体
映射文件和实体对象在同一个包下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<!-- 完成实体类 和数据表的映射 -->
<!-- 1.类与表的映射 -->
<!--
name 要映射的完整类名
table 映射到数据库的表名
catalog 映射到数据库的名字
-->
<class name="com.study.model.Customer" table="customer" catalog="hibernateexec">
<!-- 2.类中属性 和表中 数据列的映射 -->
<!-- 2.1主键 -->
<!--
name 属性名(类中)
column 列名(表中)
type 数据类型
-->
<id name="id" column="id" type="int">
<!-- 配置主键生成策略 主键自动增长-->
<generator class="identity"></generator>
</id>
<!-- 2.2 普通属性 -->
<!--
name 属性名(类中)
column 列名(表中)
type 数据类型(也可以直接写String)
-->
<property name="name" column="name" type="java.lang.String"></property>
<property name="age" column="age" type="int"></property>
<!-- 也可以分开写 -->
<property name="city">
<column name="city" sql-type="varchar(20)"></column>
</property>
<!-- 如果什么都不写,那就默认类的属性名和数据库中的列名一致都为addr,类型为varchar -->
<property name="addr"></property> </class> </hibernate-mapping>
Customer.hbm.xml
第五步:创建SessionFactory对象
第六步:获取Session对象进行相关操作
第五步和第六步我和在一起,第六步我们发现不论增删改查前面四步都是一样的,我们其实可以提取到一个工具类,再来调用这样加快效率。
import java.util.List;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test; import com.study.model.Customer; public class HibernateTest {
/*
* 保存数据
*/
@Test
public void testInsert() {
// 实例化配置对象 加载映射文件 加载 hibernate.cfg.xml
Configuration configuration = new Configuration().configure();
// 创建会话工厂
SessionFactory sessionFactory = configuration.buildSessionFactory();
// 创建会话
Session session = sessionFactory.openSession();
// 开启事务
Transaction transaction = session.beginTransaction();
// 编写自己的逻辑代码
Customer customer = new Customer();
customer.setName("小黄");
customer.setAge(40);
customer.setCity("北京");
// 直接保存
session.save(customer); // 提交事务
transaction.commit();
session.close();
sessionFactory.close();
} //查询所有的
@Test
public void testFindAllByHQL(){
// 实例化配置对象 加载映射文件 加载 hibernate.cfg.xml
Configuration configuration = new Configuration().configure();
// 创建会话工厂
SessionFactory sessionFactory = configuration.buildSessionFactory();
// 创建会话
Session session = sessionFactory.openSession();
// 开启事务
Transaction transaction = session.beginTransaction(); //编写HQL语句(面向类和属性的查询
String hql =" from Customer";//这里是Customer不是表名 是类名 查询Customer
Query query =session.createQuery(hql); List<Customer> customers=query.list();
System.out.println(customers); // 提交事务
transaction.commit();
session.close();
sessionFactory.close();
} // 删除
@Test
public void testDelete() {
// 实例化配置对象 加载映射文件 加载 hibernate.cfg.xml
Configuration configuration = new Configuration().configure();
// 创建会话工厂
SessionFactory sessionFactory = configuration.buildSessionFactory();
// 创建会话
Session session = sessionFactory.openSession();
// 开启事务
Transaction transaction = session.beginTransaction(); Customer customer =new Customer();
customer.setId(2);
session.delete(customer); // 提交事务
transaction.commit();
session.close();
sessionFactory.close();
} // 修改
@Test
public void testUpdate() {
// 实例化配置对象 加载映射文件 加载 hibernate.cfg.xml
Configuration configuration = new Configuration().configure();
// 创建会话工厂
SessionFactory sessionFactory = configuration.buildSessionFactory();
// 创建会话
Session session = sessionFactory.openSession();
// 开启事务
Transaction transaction = session.beginTransaction(); Customer customer = (Customer) session.get(Customer.class, 2);
customer.setCity("杭州");
session.update(customer); // 提交事务
transaction.commit();
session.close();
sessionFactory.close(); } // 查询 根据id查询
@Test
public void testFindById() {
// 实例化配置对象 加载映射文件 加载 hibernate.cfg.xml
Configuration configuration = new Configuration().configure();
// 创建会话工厂
SessionFactory sessionFactory = configuration.buildSessionFactory();
// 创建会话
Session session = sessionFactory.openSession();
// 开启事务
Transaction transaction = session.beginTransaction(); Customer customer = (Customer) session.get(Customer.class, 1);
System.out.println(customer); // 提交事务
transaction.commit();
session.close();
sessionFactory.close();
}
}
hibernate增删改查
运行效果:当你运行第一个增加用户的时候,运行结束数据库会自动创建customer表格,和往表格里添加数据。

这样就通过hibernate进行基础的增删改查了。
本文就到这里了,有不足之处欢迎大家指点,谢谢!
hibernate系列笔记(1)---Hibernate增删改查的更多相关文章
- Hibernate通过createSQLQuery( )方法实现增删改查
一.项目结构 二.hibernate核心配置文件: hibernate.cfg.xm <?xml version="1.0" encoding="UTF-8&q ...
- JS组件系列——BootstrapTable+KnockoutJS实现增删改查解决方案(一)
前言:出于某种原因,需要学习下Knockout.js,这个组件很早前听说过,但一直没尝试使用,这两天学习了下,觉得它真心不错,双向绑定的机制简直太爽了.今天打算结合bootstrapTable和Kno ...
- JS组件系列——BootstrapTable+KnockoutJS实现增删改查解决方案(四):自定义T4模板快速生成页面
前言:上篇介绍了下ko增删改查的封装,确实节省了大量的js代码.博主是一个喜欢偷懒的人,总觉得这些基础的增删改查效果能不能通过一个什么工具直接生成页面效果,啥代码都不用写了,那该多爽.于是研究了下T4 ...
- JS组件系列——BootstrapTable+KnockoutJS实现增删改查解决方案(三):两个Viewmodel搞定增删改查
前言:之前博主分享过knockoutJS和BootstrapTable的一些基础用法,都是写基础应用,根本谈不上封装,仅仅是避免了html控件的取值和赋值,远远没有将MVVM的精妙展现出来.最近项目打 ...
- JS组件系列——BootstrapTable+KnockoutJS实现增删改查解决方案(二)
前言:上篇 JS组件系列——BootstrapTable+KnockoutJS实现增删改查解决方案(一) 介绍了下knockout.js的一些基础用法,由于篇幅的关系,所以只能分成两篇,望见谅!昨天就 ...
- Hibernate3回顾-5-简单介绍Hibernate session对数据的增删改查
5. Hibernate对数据的增删改查 5.1Hibernate加载数据 两种:get().load() 一. Session.get(Class arg0, Serializable arg1)方 ...
- 2、hibernate七步走完成增删改查
一.hibernate框架介绍如下 1.框架=模板 2.Hibernate是对象模型与关系数据库模型之间的桥梁 3.hibernate持久化概念 什么是ORM ORM是对象关系映射,是一种数据持久化操 ...
- hibernate对单表的增删改查
ORM: 对象关系映射(英语:Object Relational Mapping,简称ORM,或O/RM,或O/R mapping) 实现对单表的增删改查 向区域表中增加数据: 第一步: 新建一个Da ...
- Hibernate之API初识及增删改查实现
声明:关于hibernate的学习.非常大一部分东西都是概念性的. 大家最好手里都有一份学习资料,在我的博文中.我不会把书本上的概念一类的东西搬过来.那没有不论什么意义.关于hibernate的学习, ...
随机推荐
- Struts2配置dtd约束
Struts2和Struts1的区别: 一.elclipse-ee开发 搭建环境eclipse-ee 1.加入jar包 apps/struts2-blank.war解压 2.在web.xml文件中配 ...
- oralce
1.对数据库SQL2005.ORACLE熟悉吗? SQL2005是微软公司的数据库产品.是一个RDBMS数据库,一般应用在一些中型数据库的应用,不能跨平台. ORACLE是ORACLE公司的数 ...
- easyui datagrid 列排序
1.js设置 //=====================数据加载===================== /** * grid加载数据 * * @returns */ function grid ...
- JS 上传文件 Uploadify 网址及 v3.2.1 参数说明
http://www.uploadify.com/ 一.属性 属性名称 默认值 说明 auto true 设置为true当选择文件后就直接上传了,为false需要点击上传按钮才上传 . buttonC ...
- 添加Action View
ActionBar上除了可以显示普通的Action Item之外,还可以显示普通的UI组件.为了在ActionBar上添加ActionView,可以使用如下两种方式. 定义ActionItem时使用a ...
- phpcms 杂乱总结
1.根据catid 获取 栏目名称 $CATEGORYS = getcache('category_content_'.$siteid,'commons'); $name = {$CATEGORYS[ ...
- 列表视图(ListView)和ListActivity
ListView是手机系统中使用非常广泛的一种组件,它以垂直列表的形式显示所有列表项. 创建ListView有如下两种方式: 直接使用ListView进行创建. 让Activity继承ListActi ...
- WPF ResourceDictionary的使用
作用:一个应用程序中,某个窗口需要使用样式,但是样式非常多,写在一个窗口中代码分类不方便.最好Style写在专门的xaml文件中,然后引用到窗口中,就像HTML引用外部css文件一样. 初衷:就在于可 ...
- oracle的位图索引和函数索引
1.位图索引 位图索引适用于性别.婚姻状态.行政区等只有几列固定值的类型列,身份证号等就不适合位图索引,位图索引适用于静态数据,频繁更新的字段不适用建立位图索引,因为更新会导致索引块区的变更,还会引起 ...
- redis的配置详解
redis 127.0.0.1:6379> CONFIG GET loglevel 1) "loglevel" 2) "notice" Redis 的配置 ...