一个简单的Hibernate例子!

使用Eclipse3.0+MyEclipse3.8+Hibernate2.0+MySQL5.0+JUnit3.8

1. 创建hibernate.cfg.xml:

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 2.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="connection.username">root</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/mydb</property>
        <property name="dialect">net.sf.hibernate.dialect.MySQLDialect</property>
        <property name="connection.password">root</property>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <mapping resource="bxf/com/Student.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

2. 创建Student类:

package bxf.com;
import java.io.Serializable;
public class Customers implements Serializable {
 private Integer stuId;
 private String stuName;
 private int stuAge;
 public Customers() {}
 public int getStuAge() {return stuAge;}
 public void setStuAge(int stuAge) {this.stuAge = stuAge;}
 public Integer getStuId() {return stuId;}
 public void setStuId(Integer stuId) {this.stuId = stuId;}
 public String getStuName() {return stuName;}
 public void setStuName(String StuName) {this.stuName = stuName;}
}

3. 创建Student表:

 CREATE TABLE Student(stuId bigint(20) not null primary key,stuName varchar(15),stuAge int(11));

4. 创建Student.hbm.xml:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "hibernate-mapping-2.0.dtd" >
<hibernate-mapping>
 <class name="bxf.com.Student" table="Student">
  <id name="stuId" type="java.lang.Integer" column="stuId"><generator class="assigned" /></id>
  <property name="stuName" type="java.lang.String" column="stuName" length="11"/>
  <property name="stuAge" type="int" column="stuAge" length="11"/>
 </class>
</hibernate-mapping>

5. 创建测试案例TestHibernate.java

package bxf.test;
import bxf.com.Student;
import junit.framework.TestCase;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.cfg.Configuration;
public class TestHibernate extends TestCase {
 Session session = null;
 protected void setUp() throws Exception {
  try {
    Configuration config = new Configuration().configure();
    SessionFactory sessionFactory = config.buildSessionFactory();
    session = sessionFactory.openSession();
   } catch (HibernateException e) {e.printStackTrace();}
 }
 protected void tearDown() throws Exception {
  try {session.close();} catch (HibernateException e) {e.printStackTrace();}
 }
 public void testSaveCustomers() {
  Student stu = new Student();
  stu.setStuId(new Integer(330121));
  stu.setStuName("huhpreal");
  stu.setStuAge(24);
  try {session.save(stu);} catch (Exception e) {e.printStackTrace();}
 }
}

6. 运行JUnit案例测试,如无错误,在MySQL表Student中应该添加了一条记录!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值