1.hibernate概述
Hibernate是一個開放源代碼的對象關系映射框架,它對JDBC進行了非常輕量級的對象封裝,它將POJO與數據庫表建立映射關系,是一個全自動的orm框架,hibernate可以自動生成SQL語句,自動執行,使得Java程序員可以隨心所欲的使用對象編程思維來操縱數據庫。 Hibernate可以應用在任何使用JDBC的場合,既可以在Java的客戶端程序使用,也可以在Servlet/JSP的Web應用中使用,最具革命意義的是,Hibernate可以在應用EJB的J2EE架構中取代CMP,完成數據持久化的重任。 —— 百度百科
2.hibernater入門
-
環境搭建
文件結構
第一步:導入包
<!--hibernate-jar-->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.11.Final</version>
</dependency>
<!--mysql-jar-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<!--日志文件-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
第二步:創建entity
package com.study.entity;
public class User {
private int uid;
private String username;
private String password;
private String address;
public int getUid() {
return uid;
}
public void setUid(int uid) {
this.uid = uid;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
第三步:配置實體類和數據表的一一對應關系(映射關系)
1.創建xml配置文件: User.hbm.xml
2.引入約束文件:
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
3.配置類和表對應
- class標簽
- name屬性:實體類全路徑
- table屬性:數據庫表名稱
- id標簽
- name屬性:實體類里面的id屬性名稱
- column:生成的表字段
<?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>
<class name="com.study.entity.User" table="h_user">
<id name="uid" column="uid">
<generator class="native"></generator>
</id>
<property name="username" column="username"></property>
<property name="password" column="password"></property>
<property name="address" column="address"></property>
</class>
</hibernate-mapping>
第四步:創建hibernate核心配置文件
- 創建文件:hibernate.cfg.xml
- 引入dtd約束
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
- 配置數據庫信息
<!-- - 配置數據庫信息-->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate?characterEncoding=utf8</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
- 配置hibernate信息
<!-- - 配置hibernate信息(可選)-->
<!--輸出底層sql-->
<property name="hibernate.show_sql">true</property>
<!--輸出底層sql語句格式-->
<property name="hibernate.format_sql">true</property>
<!--hibernate 幫創建表,需要配置。update:如果有表進行更新,沒有的話進行創建-->
<property name="hibernate.hbm2ddl.auto">update</property>
<!--配置數據庫方言 在mysql里面實現分頁,關鍵字limit 只能使用mysql里面 ,在oracle數據庫,實現分頁rownum-->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL55Dialect</property>
- 將映射文件加載到核心文件中
<!-- - 將映射文件加載到核心文件中(必須)-->
<mapping resource="mapping/User.hbm.xml"></mapping>
- 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>
<session-factory>
<!-- - 配置數據庫信息-->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate?characterEncoding=utf8</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<!-- - 配置hibernate信息(可選)-->
<!--輸出底層sql-->
<property name="hibernate.show_sql">true</property>
<!--輸出底層sql語句格式-->
<property name="hibernate.format_sql">true</property>
<!--hibernate 幫創建表,需要配置。update:如果有表進行更新,沒有的話進行創建-->
<property name="hibernate.hbm2ddl.auto">update</property>
<!--配置數據庫方言 在mysql里面實現分頁,關鍵字limit 只能使用mysql里面 ,在oracle數據庫,實現分頁rownum-->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL55Dialect</property>
<!-- - 將映射文件加載到核心文件中(必須)-->
<mapping resource="mapping/User.hbm.xml"></mapping>
</session-factory>
</hibernate-configuration>
第五步:實現添加操作
- 加載hibernate核心配置文件
- 創建SessionFactory對象
- 使用SessionFactory創建session對象
- 開啟事務
- 寫具體邏輯crud代碼操作‘
- 提交事務
- 關閉資源
package com.study.text;
import com.study.entity.User;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;
/**
*
* @author 馬歡歡
* @date 2017/12/5
*/
public class HibernateTest {
@Test
public void testAdd(){
// 加載hibernate核心配置文件
Configuration configuration = new Configuration();
configuration.configure("/hibernate.cfg.xml");
// 創建SessionFactory對象
SessionFactory sessionFactory = configuration.buildSessionFactory();
// 使用SessionFactory創建session對象
Session session = sessionFactory.openSession();
// 開啟事務
Transaction tx = session.beginTransaction();
// 寫具體邏輯crud代碼操作
User user = new User();
user.setUsername("小明");
user.setPassword("456789");
user.setAddress("西安");
session.save(user);
// 提交事務
tx.commit();
// 關閉資源
session.close();
sessionFactory.close();
}
}
注意:我在使用上面文件創建表的時候遇到一個問題,就是在創建的時候會報錯
問題
這個問題我在網上查了,發現是因為:create 語句后面的TYPE=MyISAM TYPE=MyISAM 和 ENGINE=MyISAM 都是設置數據庫存儲引擎的語句 ,(老版本的MySQL使用TYPE而不是ENGINE(例如,TYPE = MYISAM)。 MySQL 5.1為向下兼容而支持這個語法,但TYPE現在被輕視,而ENGINE是首先的用法。
因此需要將配置文件中的mysql方言改為5.5的:
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
改為
<property name="hibernate.dialect">org.hibernate.dialect.MySQL55Dialect</property>
成功創建
完
當前文集 :Hibernate框架學習