hibernate连接mysql数据库步骤_Hibernate (操作步骤)
在java工程里導入Hibernate架包:
在添加數據庫架包如:
Hibernate開發步驟:
1、Eclipse下創建Hibernate配置文件(需要tools插件)
new---->other---->Hibernate Configuration File(cfg.xml)? 點擊next----finish? 創建下圖文件,然后配置dtd文件
然后通過下圖的properties文件,編寫我們的hibernate.cfg.xml文件
常用的配置如下(至此第一步完成)
1 <?xml version="1.0" encoding="UTF-8"?>
2 "-//Hibernate/Hibernate Configuration DTD 3.0//EN"4 "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
5
6
7
8 root
9 root
10 com.mysql.jdbc.Driver
11 jdbc:mysql:///hibernate5
12
13
14
15 org.hibernate.dialect.MySQLInnoDBDialect
16
17
18 true
19
20
21 true
22
23
24 update
25
26
27
28
hibernate.cfg.xml
2、創建持久化類
1 packagecom.tzy.hibernate;2
3 importjava.util.Date;4
5 public classNews {6 privateInteger id;7 privateString title;8 privateString author;9 privateDate date;10 publicInteger getId() {11 returnid;12 }13 public voidsetId(Integer id) {14 this.id =id;15 }16 publicString getTitle() {17 returntitle;18 }19 public voidsetTitle(String title) {20 this.title =title;21 }22 publicString getAuthor() {23 returnauthor;24 }25 public voidsetAuthor(String author) {26 this.author =author;27 }28 publicDate getDate() {29 returndate;30 }31 public voidsetDate(Date date) {32 this.date =date;33 }34
35 publicNews(String title, String author, Date date) {36 super();37 this.title =title;38 this.author =author;39 this.date =date;40 }41 publicNews() {42 super();43 }44 @Override45 publicString toString() {46 return "News [id=" + id + ", title=" + title + ", author=" + author + "]";47 }48
49 }
News
3、創建對象-關系-映射文件
new---->other---->Hibernate XML Mapping file(hbm.xml)? 點擊next-next-next----finish? 創建下圖文件
1 <?xml version="1.0"?>
2 /p>
3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
News.hbm.xml
然后在hibernate.cfg.xml里面建立映射關系
4、通過Hibernate API編寫訪問數據庫的代碼
1 packagecom.tzy.hibernate;2
3 importjava.util.Date;4
5 importorg.hibernate.Session;6 importorg.hibernate.SessionFactory;7 importorg.hibernate.Transaction;8 importorg.hibernate.boot.MetadataSources;9 importorg.hibernate.boot.registry.StandardServiceRegistry;10 importorg.hibernate.boot.registry.StandardServiceRegistryBuilder;11 importorg.junit.Test;12
13 public classHibernateTest {14
15 @Test16 public voidtest() {17 //1.創建一個SessionFactory對象
18 SessionFactory sessionFactory = null;19
20 //hibernate4獲取sessionFactory辦法21 //1).創建Configuration對象:對應hibernate的基本配置信息和關系映射信息22 //Configuration configuration = new Configuration().configure();23 //4.0之前這樣創建24 //sessionFactory = configuration.buildSessionFactory();25 //2).創建一個ServiceRegistry對象:hibernate 4.x新添加的對象26 //hibernate的任何配置和服務都需要在該對象中注冊后才能有效。27 //ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties())28 //.buildServiceRegistry();29 //3).30 //sessionFactory = configuration.buildSessionFactory(serviceRegistry);31
32 //4).33 //hibernate5獲取sessionFactory辦法34 //創建StandardServiceRegistry對象(標準服務注冊)
35 StandardServiceRegistry standardServiceRegistry = newStandardServiceRegistryBuilder().configure().build();36
37 sessionFactory = newMetadataSources(standardServiceRegistry).buildMetadata().buildSessionFactory();38 //2.創建一個Session對象
39 Session session =sessionFactory.openSession();40
41 //3.開啟事物
42 Transaction transaction =session.beginTransaction();43
44 //4.執行保存操作
45 News news = new News("Java", "Tzy", newDate());46 session.save(news);47
48 //5.提交事物
49 transaction.commit();50
51 //6.關閉Session
52 session.close();53
54 //7.關閉SessionFactory對象
55 sessionFactory.close();56 }57
58 }
HibernateTest
1.創建一個SessionFactory對象2.創建一個Session對象3.開啟事物4.執行保存操作5.提交事物6.關閉Session7.關閉SessionFactory對象
操作執行完畢(表自動創建---cfg.xml里的生成數據表策略)
總結
以上是生活随笔為你收集整理的hibernate连接mysql数据库步骤_Hibernate (操作步骤)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql怎么创建表视频教程_mySQL
- 下一篇: java快速查找算法_Java实现的快速