配置Hibernate环境

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();
  }
 }
}
补充修改中!!!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值