配置Hibernate环境

来源:互联网 发布:淘宝贷款139邮箱 编辑:程序博客网 时间:2024/06/05 09:35

1:

在lib环境下添加Hibernate中的所有JAR包:

2:

对应数据库做一个持久类(也可由工具自动生成),例如:

public class student
{
     public student()
     {
     }

     private String name;
     private String sex;
     private int age;
     private int id;

     public String getName()
     {
          return name;
     }

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

     public String getSex()
     {
          return sex;
     }

     public void setSex(String sex)
     {
          this.sex = sex;
     }

     public int getAge()
     {
          return age;
     }

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

     public int getId()
     {
          return id;
     }

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

}

3:将hibernate.cfg.xml放到CLASS目录下,内容如下(对应自己的信息稍加修改)

<?xml version='1.0' encoding='GB2312'?>
<!DOCTYPE hibernate-configuration PUBLIC
 "-//Hibernate/Hibernate Configuration DTD//EN"
 "http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">

<hibernate-configuration>

<session-factory>

<property name="dialect">net.sf.hibernate.dialect.SQLServerDialect</property>
<property name="connection.driver_class">
                  com.microsoft.jdbc.sqlserver.SQLServerDriver</property>
<property name="connection.url">
                  jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=MyDB</property>
<property name="connection.username">sa</property>
<property name="connection.password"></property>
<property name="hibernate.connection.pool.size">10</property>
<property name="hibernate.show_sql">true</property>
<property name="jdbc.fetch_size">50</property>
<property name="jdbc.batch_size">25</property>
<property name="jdbc.use_scrollable_resultset">false</property>

<mapping resource="student.hbm.xml"/>

</session-factory>

</hibernate-configuration>

4:将数据库对应的student.hbm.xml放到CLASS目录下,内容如下(可由工具自动生成)

<?xml version='1.0' encoding='GB2312'?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

    <hibernate-mapping>
     <class name="student" table="student">
  <id name="name" column="name" type="int">
       <generator class="increment"/>
       
      </id>
  <property name="sex" type="int"/>
  <property name="age" type="int"/>
     </class>
    </hibernate-mapping>

5:编写对持久类(数据库)操作的类例如(研究中。。。)

import net.sf.hibernate.*;
import net.sf.hibernate.cfg.*;
import java.util.*;

public class Select {
 public static void main(String[] args) {
  try{
   SessionFactory sf=new Configuration().configure().buildSessionFactory();
   Session session=sf.openSession();
   Query query=session.createQuery("select t from student t");
   List list=query.list();
   Iterator it=list.iterator();

   while(it.hasNext()){
    student st=(student)it.next();
    System.out.println("id=="+st.getName()+"   "+"name="+st.getSex());
   }
   session.close();
  }catch(HibernateException e){
   e.printStackTrace();
  }
 }
}
补充修改中!!!