当前位置: 移动技术网 > IT编程>开发语言>Java > Hibernate中实现增删改查的步骤详解

Hibernate中实现增删改查的步骤详解

2019年07月22日  | 移动技术网IT编程  | 我要评论

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

在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>

这里提醒一点: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();
 }
 }

运行效果:当你运行第一个增加用户的时候,运行结束数据库会自动创建customer表格,和往表格里添加数据。

这样就通过hibernate进行基础的增删改查了。

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持移动技术网!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网