国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

香港云服务器
服務(wù)器之家 - 編程語言 - Java教程 - Hibernate中實現(xiàn)增刪改查的步驟詳解

Hibernate中實現(xiàn)增刪改查的步驟詳解

2020-08-21 11:36雨點的名字 Java教程

本篇文章主要介紹了Hibernate中實現(xiàn)增刪改查的步驟與方法,具有很好的參考價值,下面跟著小編一起來看下吧

1.首先我們要知道什么是Hibernate

Hibernate是一個輕量級的ORMapping對象。主要用來實現(xiàn)Java和數(shù)據(jù)庫表之間的映射,除此之外還提供數(shù)據(jù)查詢和數(shù)據(jù)獲取的方法,

可以大幅度減少開發(fā)時人工使用SQL和JDBC處理數(shù)據(jù)的時間,解放編程人員95%的任務(wù)。

2.什么是ORM  Object-Relational-Mapping對象關(guān)系映射

ORM:是通過java對象映射到數(shù)據(jù)庫表,通過操作Java對象可以完成對數(shù)據(jù)表的操作。(假如你用的是Dbutils那么還需要在Java類中寫sql語句,而orm就不用)

Hibernate是一個完全的ORM框架只需要對對象的操作即可生成底層的SQL。

接下來直接進入主題:

先看看使用hibernate的基本流程!下面是簡單的流程圖

Hibernate中實現(xiàn)增刪改查的步驟詳解

1.創(chuàng)建項目:

用myeclipse創(chuàng)建一個web project

2.導(dǎo)入hibernate相關(guān)的架包到項目

Hibernate中實現(xiàn)增刪改查的步驟詳解

第三步: 配置hibernate

在src目錄下新建一個xml文件,名稱為hibernate.cfg.xml(當然,你也可以不叫這個名稱,不過在代碼中要作相應(yīng)的修改),拷貝如下內(nèi)容:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?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 核心 管理數(shù)據(jù)庫連接池 -->
 <session-factory>
  <!-- 1.配置數(shù)據(jù)庫連接參數(shù) -->
  <!-- 1.1配置jdbc四個基本連接參數(shù) -->
  <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.配置其他相關(guān)屬性 -->
  <!-- 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>
  <!-- 開啟事務(wù) -->
  <property name="hibernate.connection.autocommit">true</property>
  <!-- 配置c3p0數(shù)據(jù)庫連接池 -->
  <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表你可以不用去手動創(chuàng)建,但是數(shù)據(jù)庫hibernateexec是要你手動創(chuàng)建的

第四步.創(chuàng)建實體和映射文件

?
1
2
3
4
5
6
7
8
9
10
11
public class Customer {
 private int id;
 private String name;
 private int age;
 private String city;
 private String addr;
}
/*
 * 提供set和get方法
 */
Customer 實體

映射文件和實體對象在同一個包下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?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>
 <!-- 完成實體類 和數(shù)據(jù)表的映射 -->
 <!-- 1.類與表的映射 -->
 <!--
  name 要映射的完整類名
  table 映射到數(shù)據(jù)庫的表名
  catalog 映射到數(shù)據(jù)庫的名字
 -->
 <class name="com.study.model.Customer" table="customer" catalog="hibernateexec">
  <!-- 2.類中屬性 和表中 數(shù)據(jù)列的映射 -->
  <!-- 2.1主鍵 -->
  <!--
  name 屬性名(類中)
  column 列名(表中)
  type 數(shù)據(jù)類型
  -->
  <id name="id" column="id" type="int">
  <!-- 配置主鍵生成策略 主鍵自動增長-->
  <generator class="identity"></generator>
  </id>
  <!-- 2.2 普通屬性 -->
  <!--
  name 屬性名(類中)
  column 列名(表中)
  type 數(shù)據(jù)類型(也可以直接寫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>
  <!-- 如果什么都不寫,那就默認類的屬性名和數(shù)據(jù)庫中的列名一致都為addr,類型為varchar -->
 <property name="addr"></property>
 </class>
 </hibernate-mapping>
 
Customer.hbm.xml

第五步:創(chuàng)建SessionFactory對象

第六步:獲取Session對象進行相關(guān)操作

第五步和第六步我和在一起,第六步我們發(fā)現(xiàn)不論增刪改查前面四步都是一樣的,我們其實可以提取到一個工具類,再來調(diào)用這樣加快效率。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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 {
 /*
  * 保存數(shù)據(jù)
  */
  @Test
  public void testInsert() {
  // 實例化配置對象 加載映射文件 加載 hibernate.cfg.xml
  Configuration configuration = new Configuration().configure();
  // 創(chuàng)建會話工廠
  SessionFactory sessionFactory = configuration.buildSessionFactory();
  // 創(chuàng)建會話
  Session session = sessionFactory.openSession();
  // 開啟事務(wù)
  Transaction transaction = session.beginTransaction();
  // 編寫自己的邏輯代碼
  Customer customer = new Customer();
  customer.setName("小黃");
  customer.setAge(40);
  customer.setCity("北京");
  // 直接保存
  session.save(customer);
  // 提交事務(wù)
  transaction.commit();
  session.close();
  sessionFactory.close();
  }
 //查詢所有的
 @Test
 public void testFindAllByHQL(){
  // 實例化配置對象 加載映射文件 加載 hibernate.cfg.xml
  Configuration configuration = new Configuration().configure();
  // 創(chuàng)建會話工廠
  SessionFactory sessionFactory = configuration.buildSessionFactory();
  // 創(chuàng)建會話
  Session session = sessionFactory.openSession();
  // 開啟事務(wù)
  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);
  // 提交事務(wù)
  transaction.commit();
 session.close();
  sessionFactory.close();
 }
 // 刪除
 @Test
 public void testDelete() {
  // 實例化配置對象 加載映射文件 加載 hibernate.cfg.xml
  Configuration configuration = new Configuration().configure();
  // 創(chuàng)建會話工廠
  SessionFactory sessionFactory = configuration.buildSessionFactory();
  // 創(chuàng)建會話
  Session session = sessionFactory.openSession();
  // 開啟事務(wù)
  Transaction transaction = session.beginTransaction();
 Customer customer =new Customer();
 customer.setId(2);
  session.delete(customer);
  // 提交事務(wù)
  transaction.commit();
  session.close();
  sessionFactory.close();
 }
 // 修改
 @Test
 public void testUpdate() {
  // 實例化配置對象 加載映射文件 加載 hibernate.cfg.xml
  Configuration configuration = new Configuration().configure();
  // 創(chuàng)建會話工廠
  SessionFactory sessionFactory = configuration.buildSessionFactory();
  // 創(chuàng)建會話
  Session session = sessionFactory.openSession();
  // 開啟事務(wù)
  Transaction transaction = session.beginTransaction();
  Customer customer = (Customer) session.get(Customer.class, 2);
  customer.setCity("杭州");
  session.update(customer);
  // 提交事務(wù)
  transaction.commit();
  session.close();
  sessionFactory.close();
 }
 // 查詢 根據(jù)id查詢
 @Test
 public void testFindById() {
  // 實例化配置對象 加載映射文件 加載 hibernate.cfg.xml
  Configuration configuration = new Configuration().configure();
  // 創(chuàng)建會話工廠
  SessionFactory sessionFactory = configuration.buildSessionFactory();
  // 創(chuàng)建會話
  Session session = sessionFactory.openSession();
  // 開啟事務(wù)
  Transaction transaction = session.beginTransaction();
  Customer customer = (Customer) session.get(Customer.class, 1);
  System.out.println(customer);
  // 提交事務(wù)
  transaction.commit();
  session.close();
  sessionFactory.close();
 }
 }

運行效果:當你運行第一個增加用戶的時候,運行結(jié)束數(shù)據(jù)庫會自動創(chuàng)建customer表格,和往表格里添加數(shù)據(jù)。

Hibernate中實現(xiàn)增刪改查的步驟詳解

這樣就通過hibernate進行基礎(chǔ)的增刪改查了。

以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時也希望多多支持服務(wù)器之家!

原文鏈接:http://www.cnblogs.com/qdhxhz/p/6478317.html

延伸 · 閱讀

精彩推薦
515
Weibo Article 1 Weibo Article 2 Weibo Article 3 Weibo Article 4 Weibo Article 5 Weibo Article 6 Weibo Article 7 Weibo Article 8 Weibo Article 9 Weibo Article 10 Weibo Article 11 Weibo Article 12 Weibo Article 13 Weibo Article 14 Weibo Article 15 Weibo Article 16 Weibo Article 17 Weibo Article 18 Weibo Article 19 Weibo Article 20 Weibo Article 21 Weibo Article 22 Weibo Article 23 Weibo Article 24 Weibo Article 25 Weibo Article 26 Weibo Article 27 Weibo Article 28 Weibo Article 29 Weibo Article 30 Weibo Article 31 Weibo Article 32 Weibo Article 33 Weibo Article 34 Weibo Article 35 Weibo Article 36 Weibo Article 37 Weibo Article 38 Weibo Article 39 Weibo Article 40
主站蜘蛛池模板: 精品一区二区三区免费毛片爱 | 国产一区二区精品久久岳 | 91免费在线播放 | 激情综合五月 | 天堂成人av| 欧美一级片在线观看 | 国产理论在线 | 高清一区在线 | 久久久久久亚洲一区二区三区蜜臀 | 99热在线观看免费 | 成人刺激视频在线 | 亚洲国产精品久久久 | 午夜999| 日韩精品中文字幕在线观看 | 乱人伦xxxx国语对白 | 一级片在线免费观看视频 | 亚洲 中文 欧美 日韩 在线观看 | 91丝袜 | 中文字幕视频一区 | 黄色一级片看看 | 伊人精品成人久久综合软件 | 中文字幕亚洲专区 | 99免费视频 | 欧美浮力 | 超黄网站在线观看 | 91最新网站 | 日韩一区二区三区福利视频 | 黄色小网站免费观看 | 亚洲国产精品99久久久久久久久 | 一区二区三区视频在线观看 | 一级黄色国产片 | 久久大| 国产精品永久免费视频 | 国产高清在线精品一区二区三区 | 国产日韩精品视频 | 欧美极品视频 | 久草网站 | 欧美久久综合 | 91免费在线看 | 久久久久国产一级毛片高清片 | 亚洲成人免费在线播放 |