【Hibernate】hibernate框架的搭建
阿新 • • 發佈:2017-08-12
orm 3.1 configure rop height 多個 vax ebe 程序
1, Hibernate 是什麽
Hibernate框架簡化了java應用程序與數據庫交互的開發。 Hibernate是一個開源,輕量級的ORM(對象關系映射)工具。
2,Hibernate框架的優點
Hibernate框架有很多優點。它們分別如下:
- 開源和輕量級: Hibernate框架是根據LGPL許可證和輕量級的開源工具。
- 快速性能: Hibernate框架的性能很快,因為緩存在Hibernate框架內部使用。 hibernate框架中有兩種類型的緩存:一級緩存和二級緩存。一級緩存默認是啟用的。
- 數據庫獨立查詢: HQL(Hibernate查詢語言)是面向對象的SQL版本。 它生成數據庫獨立查詢。 所以你不需要編寫數據庫特定的查詢語句。 在Hibernate之前,如果項目更改了數據庫,我們需要更改SQL查詢,從而導致維護變得非常復雜。
- 自動創建表: Hibernate框架提供了自動創建數據庫表的功能。 因此,無需手動在數據庫中創建表。
- 簡化復雜連接: 在hibernate框架中可輕松獲取多個表中的數據。
- 提供查詢統計和數據庫狀態: Hibernate支持查詢緩存,並提供有關查詢和數據庫狀態的統計信息。
3, Hibernate 框架的搭建
首先下載Hibernate的包,然後和數據庫驅動包一起到導入到項目中。
3.1 註解方式
首先看一看項目結構:
hibernate.cfg.xml文件
<?xml version=‘1.0‘ encoding=‘UTF-8‘?> <!DOCTYPE hibernate-configuration PUBLIChibernate.cfg.xml"-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <!-- Generated by MyEclipse Hibernate Tools. --> <hibernate-configuration> <session-factory> <property name="dialect"> org.hibernate.dialect.MySQLDialect</property> <property name="connection.url"> jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8 </property> <property name="connection.username">root</property> <property name="connection.password">517839</property> <property name="connection.driver_class"> com.mysql.jdbc.Driver </property> <!-- 加載映射描述信息 --> <mapping class="cn.test.bean.User" /> </session-factory> </hibernate-configuration>
User.java文件
package cn.test.bean; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name="user")//表示對應的表名 public class User { @Id//表示主鍵 @Column(name="uid")//對應表中的字段名 private Integer id; @Column(name="uname")//對應表中的字段名 private String name; @Column(name="upass")//對應表中的字段名 private String password; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "User [id=" + id + ", name=" + name + ", password=" + password + "]"; } }User.java
HibernateUtil.java文件
package cn.test.util; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class HibernateUtil { public static Session getSession(){ Configuration conf = new Configuration(); conf.configure("hibernate.cfg.xml");//讀取連接參數和映射描述信息 SessionFactory factory = conf.buildSessionFactory(); Session session = factory.openSession(); return session; } }HibernateUtil.xml
UserTest.java文件
package cn.test.test; import org.hibernate.Session; import org.junit.Test; import cn.test.bean.User; import cn.test.util.HibernateUtil; public class UserTest { @Test public void testName1() throws Exception { Session session = HibernateUtil.getSession(); User user = (User)session.get(User.class, 1); if(user != null){ System.out.println(user); }else{ System.out.println("未找到記錄"); } session.close(); } }UserTest.java
這裏的方法: session.get(User.class, 1); 是Hibernate框架封裝好的一個類,他表示查詢數據表中主鍵為1的值,並且將結果反射到User的對象中。
3.2 非註解的方式
如果是非註解的方式的話,我們只需要把上面的user.java文件替換調,並且加上User.hbm.xml文件,再在hibernate.cfg.xml文件中改一改映射關系就可以了。
hibernate.cfg.xml文件
<?xml version=‘1.0‘ encoding=‘UTF-8‘?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <!-- Generated by MyEclipse Hibernate Tools. --> <hibernate-configuration> <session-factory> <property name="dialect"> org.hibernate.dialect.MySQLDialect </property> <property name="connection.url"> jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8 </property> <property name="connection.username">root</property> <property name="connection.password">517839</property> <property name="connection.driver_class"> com.mysql.jdbc.Driver </property> <!-- 加載映射描述信息 --> <mapping resource="cn/test/hbm/User.hbm.xml" /> </session-factory> </hibernate-configuration>hibernate.cfg.xml
User.hbm.xml文件
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <!-- 指定User類對應user表 --> <class name="cn.test.bean.User" table="user"> <!-- 指定no屬性對應userid字段,類型為integer,主鍵 --> <id name="id" column="uid" type="integer"></id> <property name="name" column="uname" type="string"></property> <property name="password" column="upass" type="string"></property> </class> </hibernate-mapping>User.hbm.xml
User.java文件
package cn.test.bean;
public class User {
private Integer id;
private String name;
private String password;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", password=" + password + "]";
}
}
User.java
HibernateUtil.java文件
package cn.test.util;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
public static Session getSession(){
Configuration conf = new Configuration();
conf.configure("hibernate.cfg.xml");//讀取連接參數和映射描述信息
SessionFactory factory = conf.buildSessionFactory();
Session session = factory.openSession();
return session;
}
}
HibernateUtil.java
UserTest.java文件
package cn.test.test;
import org.hibernate.Session;
import org.junit.Test;
import cn.test.bean.User;
import cn.test.util.HibernateUtil;
public class UserTest {
@Test
public void testName1() throws Exception {
Session session = HibernateUtil.getSession();
User user = (User)session.get(User.class, 1);
if(user != null){
System.out.println(user);
}else{
System.out.println("未找到記錄");
}
session.close();
}
}
UserTest.java
【Hibernate】hibernate框架的搭建