一个简单的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中应该添加了一条记录!

阅读更多
个人分类: JAVA LANGUAGE
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭