開發(fā)步驟
1炼蹦、使用IDEA創(chuàng)建Maven工程掀虎;
2凌盯、添加MySQL和Hibernate依賴,并通過IDEA生成Hibernate配置文件hibernate.cfg.xml烹玉;
3驰怎、通過IDEA生成持久化類和對象-關(guān)系映射文件*.hbm.xml;
4二打、通過Hibernate API編寫訪問MySQL數(shù)據(jù)庫的代碼县忌。
創(chuàng)建Maven工程
創(chuàng)建步驟如圖所示:
添加依賴,并生成hibernate.cfg.xml配置文件
在pom.xml文件中添加Hibernate继效、MySQL症杏、Junit三個依賴:
<dependencies>
<!-- Hibernate依賴 -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.1.1.Final</version>
</dependency>
<!-- MySQL數(shù)據(jù)庫依賴 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.15</version>
</dependency>
<!-- Junit依賴 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>RELEASE</version>
</dependency>
</dependencies>
通過IDEA生成Hibernate.cfg.xml配置文件步驟如圖:
此時要在hibernate.cfg.xml配置文件中寫入配置信息:
代碼如下瑞信,其中connection.username
是MySQL的登錄賬戶名厉颤,connection.password
是密碼,我這里賬戶名是root凡简,密碼是123逼友。
<session-factory>
<!-- MySQL登錄賬戶名 -->
<property name="connection.username">root</property>
<!-- 賬戶密碼 -->
<property name="connection.password">123</property>
<!-- 一些屬性信息 -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306</property>
<property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<!-- 指定在程序運行時在控制臺輸出SQL語句 -->
<property name="show_sql">true</property>
<!-- 輸出的SQL語句格式化 -->
<property name="format_sql">true</property>
<!-- 運行時在數(shù)據(jù)庫自動生成數(shù)據(jù)表,這里選擇update -->
<property name="hbm2ddl.auto">update</property>
</session-factory>
使用IDEA生成持久化類及*.hbm.xml文件
在MySQL數(shù)據(jù)庫中建立students數(shù)據(jù)庫并新建student表秤涩,如圖:
將id列的自增勾選上:
在IDEA中生成持久化類、*.hbm.xml文件:
進(jìn)入Import Database Schema界面溉仑,Choose Data Source項選擇@localhost后可以看到MySQL中的數(shù)據(jù)庫以及其中的表信息,勾選student状植、age浊竟、id怨喘、lastName。Package是存放持久化類StudentEntity和映射文件StudentEntity.hbm.xml的包振定,需要提前建好必怜,我這里在java下建的包名是test。勾選Add to Session Factory和Generate Separ...后單擊OK即可生成持久化類和映射文件后频。
此時需要:
- 將StudentEntity.hbm.xml文件移至resources文件夾下梳庆,否則配置文件hibernate.cfg.xml會找不到它,運行時會報錯卑惜;
- 更改StudentEntity.hbm.xml文件中id的generator膏执,填寫class為native。
再回到hibernate.cfg.xml文件中可以看到IDEA已經(jīng)幫我們填寫好了映射文件的地址:
通過Hibernate API編寫訪問數(shù)據(jù)庫代碼
新建HibernateTest類進(jìn)行測試:
public class HibernateTest {
@Test
public void Test(){
//SessionFactory是生成Session的工廠
SessionFactory sessionFactory=null;
//Configuration類負(fù)責(zé)管理Hibernate的配置信息
Configuration configuration=new Configuration().configure();
//Hibernate4之后新增ServiceRegistry接口露久,所有基于Hibernate 的配置都必須統(tǒng)一向這個ServiceRegistry注冊后才能生效
ServiceRegistry serviceRegistry=new ServiceRegistryBuilder()
.applySettings(configuration.getProperties())
.buildServiceRegistry();
//生成SessionFactory類
sessionFactory=configuration.buildSessionFactory(serviceRegistry);
//生成Session類
Session session=sessionFactory.openSession();
//事務(wù)
Transaction transaction=session.beginTransaction();
//新建一個StudentEntity實例更米,將它插入數(shù)據(jù)庫
StudentEntity studentEntity=new StudentEntity();
studentEntity.setId(1);
studentEntity.setAge(20);
studentEntity.setLastName("喬峰");
//Session的save操作將這個實例從臨時狀態(tài)變?yōu)槌志没癄顟B(tài)
session.save(studentEntity);
//提交事務(wù)
transaction.commit();
//關(guān)閉操作
session.close();
sessionFactory.close();
}
}
運行結(jié)果后如圖,輸出了一條SQL的插入語句毫痕。
查看數(shù)據(jù)庫征峦,已經(jīng)將剛才的實例成功插入了數(shù)據(jù)庫。