Hibernate使用步驟:
1.創建hivernate.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">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<!-- 數據庫方言 -->
<property name="connection.url">
jdbc:mysql:///studentsmanage?useUnicode=true&characterEncoding=UTF-8
</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="myeclipse.connection.profile">mysql-5.1.9</property>
<property name="show_sql">true</property>
<!-- 是否把hibernate運行的sql語句輸出到控制檯 -->
<property name="format_sql">true</property>
<!-- 是否把控制檯的sql語句進行排版,便於閱讀 -->
<property name="hbm2ddl.auto">update</property>
<!--
是否自動生成數據庫表
幫助由java代碼生成數據庫腳本,進而生成具體的表結構
create:刪除之後重新建立
update:保持原有數據
-->
<property name="hibernate.current_session_context_class">thread</property>
<!--
如果使用getCurrentSession需要在hibernate.cfg.xml文件中進行配置
如果是本地事務:(jdbc事務)
<property name="hibernate.current_session_context_class">thread</property>
如果是全局事務:(jta事務)
<property name="hibernate.current_session_context_class">jta</property>
-->
<mapping resource="entity/Students.hbm.xml" />
<mapping resource="entity/Users.hbm.xml" />
</session-factory>
</hibernate-configuration>
2.創建字段的javaBean並生成映射文件(Students.hbm.xml)
package entity;
import java.util.Date;
public class Students {
private String sid;
private String sname;
private String gender;
private Date birthday;
private String address;
public Students(){
}
public Students(String sid, String sname, String gender, Date birthday,
String address) {
super();
this.sid = sid;
this.sname = sname;
this.gender = gender;
this.birthday = birthday;
this.address = address;
}
public String getSid() {
return sid;
}
public void setSid(String sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Students [sid=" + sid + ", sname=" + sname + ", gender="
+ gender + ", birthday=" + birthday + ", address=" + address
+ "]";
}
}
<?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>
<class name="entity.Students" table="STUDENTS">
<id name="sid" type="java.lang.String" length="8">
<column name="SID"></column>
<generator class="assigned"></generator>
<!-- hibernate主鍵生成策略 -->
</id>
<property name="sname" type="java.lang.String">
<column name="SNAME"></column>
</property>
<property name="gender" type="java.lang.String">
<column name="GENDER"></column>
</property>
<property name="birthday" type="date">
<column name="BIRTHDAY"></column>
</property>
<property name="address" type="java.lang.String">
<column name="ADDRESS"></column>
</property>
</class>
</hibernate-mapping>
3創建SessionFactory類
package db;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
public class MyHibernateSessionFactory {
private static SessionFactory sessionFactory;//會話工廠屬性
private MyHibernateSessionFactory(){
}//構造方法私有化,保證單例模式
public static SessionFactory getSessionFactory(){//公有的靜態方法,獲得會話工廠對象
if(sessionFactory==null){
Configuration config=new Configuration().configure();
//創建配置對象
ServiceRegistry serviceRegistry=new
ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
//創建服務註冊對象
SessionFactory sessionFactory=config.buildSessionFactory(serviceRegistry);
//創建會話工廠對象
return sessionFactory;
}
else{
return sessionFactory;
}
}
}
4.在用到的hibernate的類方法中創建session對象,開啓事務
/**********************查詢所有學生,返回學生信息***********************/
public List<Students> queryAllStudents() {
Transaction tx=null;
List <Students> list=null;
String hql="";
try{
Session session = MyHibernateSessionFactory.getSessionFactory().getCurrentSession();
hql=" from Students";
tx=session.beginTransaction();
Query query=session.createQuery(hql);
list=query.list();
tx.commit();
return list;
}
catch(Exception e){
tx.commit();
}
finally{
if(tx!=null){
tx=null;
}
}
return null;
}
/**********************查詢所有學生,返回學生信息***********************/