开始学习Hibernate技术

编写程序的流程:

 (1)   首先是创建数据库表student_table:

    create table student_table

      (

         id varchar(255) NOT NULL,

         name varchar(255) default NULL,

          cardId varchar(255) default NULL,

          age int default NULL

      )

 

(2)

创建对应的实例类和hbm.xml文件

如下:

   package org.edayup.model;

/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 2009-6-10
 * Time: 14:19:13
 * To change this template use File | Settings | File Templates.
 */
public class Student
{

    private String id;
    private String name;
    private String cardId;
    private int age;

    public void setId(String id) {
        this.id = id;
    }

    public String getId()
    {
        return id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCardId() {
        return cardId;
    }

    public void setCardId(String cardId) {
        this.cardId = cardId;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

 

 

<?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="org.edayup.model.Student" table="student_table">
           <id name="id" unsaved-value="null">
               <generator class="uuid"/>
           </id>
           <!--   影射学号-->
           <property name="cardId" type="string"/>
           <property name="name" type="string"/>
           <property name="age" type="int"/>
       </class>


        </hibernate-mapping>

 

(3)创建其中的持续化类

 

主要的功能是打开session 和关闭session的功能。 

package org.edayup.persistence;

import org.hibernate.SessionFactory;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;

import javax.management.RuntimeErrorException;

/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 2009-6-10
 * Time: 14:21:30
 * To change this template use File | Settings | File Templates.
 */
public class HibernateUtil
{
     private static final SessionFactory sessionFactory ;
    static {

        try{
            sessionFactory = new Configuration().configure().buildSessionFactory();

        }catch (HibernateException ex)
        {
            ex.printStackTrace();
            throw new RuntimeException("Exception building SessionFactory: " + ex.getMessage(), ex);
        }
    }

 

    public static Session currentSession()
        throws HibernateException
    {
        Session s = sessionFactory.openSession();
        return s;

    }

    public static void closeSession(Session s)
    {
        s.close();
    }

}
功能类:创建、更新、删除的stu的功能

package org.edayup.persistence;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.edayup.model.Student;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 2009-6-10
 * Time: 14:37:30
 * To change this template use File | Settings | File Templates.
 */
public class StudentDAOImp
{
    static Session session =null;
    public static void createStu(Student stu){
        try {
        session = HibernateUtil.currentSession();
         Transaction tx=session.beginTransaction();
        session.save(stu);
        tx.commit();
    }
        catch(HibernateException e)
        {
            e.printStackTrace();
        }
        finally
        {
            HibernateUtil.closeSession(session);
        }

}


public static void delStu(String id)
{
    try{
        session =HibernateUtil.currentSession();
        Transaction tx = session.beginTransaction();
        Student stu =(Student) session.get(Student.class,id);
        session.delete(stu);
        tx.commit();


    }
    catch (HibernateException e)
    {
        e.printStackTrace();

    }   finally
    {
        HibernateUtil.closeSession(session);
    }
}

public static void mdfStu(Student stu)
{
    try {

        session=HibernateUtil.currentSession();
        Transaction tx = session.beginTransaction();
        session.update(stu);
        tx.commit();
    }
    catch(HibernateException e)

    {
        e.printStackTrace();
    }
    finally {
        HibernateUtil.closeSession(session);
    }
}

 

/*取得所有学生的列表-----
 */


public static List getALLStu()
{
    List list = null;
    try {
        session = HibernateUtil.currentSession();
        Transaction tx =session.beginTransaction();
        Query q= session.createQuery("from Student");
        list=q.list();
        tx.commit();
    } catch(HibernateException e)
    {
          e.printStackTrace();
    }
    finally
    {
        HibernateUtil.closeSession(session);
    }
    return list;
}
}

 

 

(4)定义service 类。调用功能类进行查看功能实现情况。

 

package business;

import org.edayup.model.Student;
import org.edayup.persistence.StudentDAOImp;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 2009-6-10
 * Time: 15:30:29
 * To change this template use File | Settings | File Templates.
 */
public class StudentManager
{

    public static void main(String[] args){
        StudentManager sm = new StudentManager();
      //  sm.create();
        //sm.update();
        sm.delete();
    }


    private void create()
    {
        Student stu = new Student();
        stu.setAge(28);
        stu.setCardId("20090610");
        stu.setName("tom");

        StudentDAOImp.createStu(stu);

    }
    private void update()
    {
        List list =StudentDAOImp.getALLStu();
        Student stu =(Student)list.get(0);
        stu.setName("jack");
        stu.setCardId("123456");
        if(stu.getAge()<18)
        {
            stu.setAge(18);
        }
        StudentDAOImp.mdfStu(stu);
    }

    private void delete()
    {
        List list=StudentDAOImp.getALLStu();
        Student stu=(Student)list.get(0) ;
        StudentDAOImp.delStu(stu.getId());
    }

}

 

(5)进行hibernate 配置文件设置和连接数据库的文件设置。

其中hibernate.cfg.xml的内容如下:

 

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="connection.url">jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=SAMPLEDB</property>
        <property name="connection.driver_class">com.microsoft.jdbc.sqlserver.SQLServerDriver</property>
        <property name="connection.username">sa</property>
        <property name="connection.password">123456</property>

        <mapping resource="org/edayup/model/Student.hbm.xml"/>

        <!-- DB schema will be updated if needed -->
        <!-- <property name="hbm2ddl.auto">update</property> -->
    </session-factory>
</hibernate-configuration>

 

 

 

 

 

hibernate.properties文件的内容为:

 

 


hibernate.dialect=org.hibernate.dialect.SQLServerDialect
hibernate.connection.driver_class=com.microsoft.jdbc.sqlserver.SQLServerDriver
hibernate.connection.url=jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=SAMPLEDB
hibernate.connection.username=sa
hibernate.connection.password=123456
hibernate.show_sql=true

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值