百科定義:
hibernate是java語言下的對象關(guān)系映射解決方案
同類產(chǎn)品:
ibatis:半ORM產(chǎn)品怀估,可以直接寫sql
mybatis:這是ibatis的升級版
springJDBC: 這是spring提供的持久層技術(shù)
hibernate和mybatis各有各的方式,hibernate封裝比較完整前域,完全是面向?qū)ο蠓绞讲僮鲾?shù)據(jù)庫,而mybatis是可以寫sql的宙项,在大多數(shù)互聯(lián)網(wǎng)應(yīng)用中剖张,使用mybatis比較多,但是hibernate還是要學(xué)習(xí)的
安裝配置
配置文件
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/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://127.0.0.1/test"</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<mapping resource="entity/User.hbm.xml" /> -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://127.0.0.1/test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<mapping resource="entity/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
sessionFactory工具類(單例模式)
package util;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;
public class hibernateUtil {
private static SessionFactory factory;
static{
try{
//讀取配置文件hibernate.cfg.xml
//這里最后的函數(shù)configure阅懦,如果不加的話,是讀取.propertys文件
Configuration cfg = new Configuration().configure();
factory = cfg.buildSessionFactory();
}catch(Exception e){
e.printStackTrace();
}
}
public static Session getSession()
{
return factory.openSession();
}
public static void closeSession(Session session)
{
if(session!=null)
{
if(session.isOpen())
{
session.close();
}
}
}
public static SessionFactory getSessionFactory()
{
return factory;
}
}
實(shí)體類:
package entity;
public class User {
private int id;
private String userName;
private String passWd;
public User(){}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassWd() {
return passWd;
}
public void setPassWd(String passWd) {
this.passWd = passWd;
}
}
映射文件:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="entity.User">
<id name="id">
<generator class="native"/>
</id>
<property name="userName" />
<property name="passWd" />
</class>
</hibernate-mapping>
建表代碼:
package util;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
public class ExportDb {
public static void main(String[] args) {
//讀取配置文件
Configuration cfg = new Configuration().configure();
SchemaExport export = new SchemaExport(cfg);
//自動(dòng)生成對應(yīng)的表
export.create(true, true);
}
}