您的位置:首页 > 其它

Hibernate使用步骤

2016-06-18 21:56 204 查看
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="";<
4000
br />
        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;

    }

    /**********************查询所有学生,返回学生信息***********************/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: