一个简单的hibernate程序


1.首先创建项目并导入JAR包
我的项目如下(其中处理Hibernate需要的包外,还需要MySQL的驱动包)。
2.然后在src目录上编写核心配置文件 hibernate.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">

<hibernate-configuration>

    <session-factory>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">123456</property>

        <!--指定数据库方言 -->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.format_sql">true</property>
        <property name="hibernate.hbm2ddl.auto">update</property>

        <!--引入映射文件 -->
        <mapping resource="demo/entity/User.hbm.xml" />

    </session-factory>

</hibernate-configuration>

3.在实体包内创建用户群体User.java文件(一个JavaBean)。
package HT.entity;

public class User
{
       private String name;    
       
       private String password;
       
       private int id;

    public String getName()
    {
        return name;
    }

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

    public String getPassword()
    {
        return password;
    }

    public void setPassword(String password)
    {
        this.password=password;
    }

    public int getId()
    {
        return id;
    }

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

    
}
4.在同一个包内配置User类映射文件的User.hbm.xml。
<?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="domain.User1 table="t_user">
    <id name="id" column="ID"  type=“int">
      	<generator class=“increment"/>
    </id>
    <property name="name" column=“NAME" type=“String"/>
    <property name=“password" column=“PASSWORD" type=“String"/>
  </class>
</hibernate-mapping>
5.最后写一个测试类

package com.DAO;
import org.hibernate.HibernateException;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;
import com.domainModel.User;
public class testSaveUser {
 /**  * @param args  */ public static void main(String[] args) {  // TODO Auto-generated method stub  Configuration cfg = null;  SessionFactory sf = null;    Session s = null;  Transaction ts = null;  User user = new User();  user.setUsername("老范");  user.setPassword("laofan");  user.setAge(21);  try {   cfg = new Configuration().configure();   sf = cfg.buildSessionFactory();      s = sf.openSession();   ts = s.beginTransaction();   s.save(user);   ts.commit();  } catch (HibernateException e) {   // TODO Auto-generated catch block   if(ts != null)   {    ts.rollback();   }   e.printStackTrace();  } finally{   if(s != null){    s.close();   }  } }
}


</hr>

更新


目前,本博客所有内容均为“为应付本人所在高校的某老师审查”为理由所编写的。本人不对本博客目前的任何内容做出保证。(包括但不限于内容准确性、内容时效性、以及内容是否有价值及其他一系列问题。)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值