下载最新hibernate的资源:http://www.hibernate.org/6.html  本文使用最新的 3.3.0 SP1 版本

所需jar包列表如下:

antlr-2.7.6.jar
commons-collections-3.1.jar
commons-logging-1.1.jar
dom4j-1.6.1.jar
ehcache-1.2.3.jar
ejb3-persistence.jar
hibernate3.jar
hibernate-annotations.jar
hibernate-commons-annotations.jar
hibernate-validator.jar
javassist-3.4.GA.jar
jta-1.1.jar
log4j.jar
mysql-connector-java-5.1.6-bin.jar
slf4j-api-1.4.2.jar
slf4j-log4j12.jar

hibernate.cfg.xml配置文件:

  1. <?xml version='1.0' encoding='UTF-8'?>
  2. <!DOCTYPE hibernate-configuration PUBLIC
  3.     "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  4.     "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

  5. <hibernate-configuration>

  6.     <session-factory>
  7.         <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
  8.         <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hb3demo</property>
  9.         <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
  10.         <property name="connection.username">root</property>
  11.         <property name="connection.password">root</property>
  12.         <property name="jdbc.fetch_size">50</property>
  13.         <property name="jdbc.batch_size">30</property>
  14.         <property name="show_sql">true</property>
  15.         <property name="use_outer_join">true</property>
  16.         <property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
  17.         <property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
  18.         <property name="hibernate.cache.use_query_cache">true</property>
  19.         
  20.         <mapping resource="com/hb3/pack_01/model/User.hbm.xml" />

  21.     </session-factory>

  22. </hibernate-configuration>

log4j.properties

  1. log4j.rootLogger=warn, stdout

  2. log4j.appender.stdout=org.apache.log4j.ConsoleAppender
  3. log4j.appender.stdout.Target=System.out
  4. log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
  5. log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

User.java

  1. package com.hb3.pack_01.model;

  2. public class User {
  3.     
  4.     private Integer id;
  5.     private String name;
  6.     private Integer age;

  7.     public Integer getId() {
  8.         return id;
  9.     }
  10.     public void setId(Integer id) {
  11.         this.id = id;
  12.     }
  13.     public String getName() {
  14.         return name;
  15.     }
  16.     public void setName(String name) {
  17.         this.name = name;
  18.     }
  19.     public Integer getAge() {
  20.         return age;
  21.     }
  22.     public void setAge(Integer age) {
  23.         this.age = age;
  24.     }
  25. }

User.hbm.xml

  1. <?xml version="1.0" encoding="UTF-8"?>

  2. <!DOCTYPE hibernate-mapping PUBLIC
  3.     "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
  4.     "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

  5. <hibernate-mapping>
  6.     <class name="com.hb3.pack_01.model.User" table="user">
  7.         <id name="id" column="id" type="java.lang.Integer">
  8.             <generator class="native" />
  9.         </id>
  10.         
  11.         <property name="name" column="name" type="java.lang.String" />
  12.         
  13.         <property name="age" column="age" type="java.lang.Integer" />
  14.     </class>
  15. </hibernate-mapping>

BusinessService.java

  1. package com.hb3.pack_01;

  2. import org.hibernate.Session;
  3. import org.hibernate.SessionFactory;
  4. import org.hibernate.Transaction;
  5. import org.hibernate.cfg.Configuration;

  6. import com.hb3.pack_01.model.User;

  7. public class BusinessService {

  8.     public static void main(String[] args) {
  9.         
  10.         Configuration config = new Configuration().configure();
  11.         SessionFactory sessionFactory = config.buildSessionFactory();      
  12.         Session session = sessionFactory.openSession();
  13.         
  14.         User user = new User();
  15.         user.setName("shenbin");
  16.         user.setAge(new Integer(28));       
  17.         
  18.         Transaction tx = session.beginTransaction(); 
  19.         session.save(user);          
  20.         tx.commit();
  21.         
  22.         session.close();
  23.         sessionFactory.close();
  24.     }
  25. }

在MySQL数据库中新建数据库hb3demo,并执行如下DDL

  1. CREATE TABLE user (
  2.     id INT(11) NOT NULL auto_increment PRIMARY KEY,
  3.     name VARCHAR(100) NOT NULL default '',
  4.     age INT
  5. );

这样就可以使用hibernate向数据库插入数据了。



ExtJS教程- Hibernate教程-Struts2 教程-Lucene教程