下载最新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配置文件:
- <?xml version='1.0' encoding='UTF-8'?>
- <!DOCTYPE hibernate-configuration PUBLIC
- "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
- "http://hibernate.sourceforge.net/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/hb3demo</property>
- <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
- <property name="connection.username">root</property>
- <property name="connection.password">root</property>
- <property name="jdbc.fetch_size">50</property>
- <property name="jdbc.batch_size">30</property>
- <property name="show_sql">true</property>
- <property name="use_outer_join">true</property>
- <property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
- <property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
- <property name="hibernate.cache.use_query_cache">true</property>
-
- <mapping resource="com/hb3/pack_01/model/User.hbm.xml" />
- </session-factory>
- </hibernate-configuration>
log4j.properties
- log4j.rootLogger=warn, stdout
- log4j.appender.stdout=org.apache.log4j.ConsoleAppender
- log4j.appender.stdout.Target=System.out
- log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
- log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
User.java
- package com.hb3.pack_01.model;
- public class User {
-
- private Integer id;
- private String name;
- private Integer age;
- public Integer getId() {
- return id;
- }
- public void setId(Integer id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public Integer getAge() {
- return age;
- }
- public void setAge(Integer age) {
- this.age = age;
- }
- }
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="com.hb3.pack_01.model.User" table="user">
- <id name="id" column="id" type="java.lang.Integer">
- <generator class="native" />
- </id>
-
- <property name="name" column="name" type="java.lang.String" />
-
- <property name="age" column="age" type="java.lang.Integer" />
- </class>
- </hibernate-mapping>
BusinessService.java
- package com.hb3.pack_01;
- import org.hibernate.Session;
- import org.hibernate.SessionFactory;
- import org.hibernate.Transaction;
- import org.hibernate.cfg.Configuration;
- import com.hb3.pack_01.model.User;
- public class BusinessService {
- public static void main(String[] args) {
-
- Configuration config = new Configuration().configure();
- SessionFactory sessionFactory = config.buildSessionFactory();
- Session session = sessionFactory.openSession();
-
- User user = new User();
- user.setName("shenbin");
- user.setAge(new Integer(28));
-
- Transaction tx = session.beginTransaction();
- session.save(user);
- tx.commit();
-
- session.close();
- sessionFactory.close();
- }
- }
在MySQL数据库中新建数据库hb3demo,并执行如下DDL
- CREATE TABLE user (
- id INT(11) NOT NULL auto_increment PRIMARY KEY,
- name VARCHAR(100) NOT NULL default '',
- age INT
- );
这样就可以使用hibernate向数据库插入数据了。
ExtJS教程-
Hibernate教程-
Struts2 教程-
Lucene教程