载最新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配置文件:
<?xmlversion='1.0'encoding='UTF-8'?>
<!DOCTYPEhibernate-configurationPUBLIC
"-//Hibernate/HibernateConfigurationDTD3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<propertyname="hibernate.connection.driver_">com.mysql.jdbc.Driver</property>
<propertyname="hibernate.connection.url">jdbc:mysql://localhost:3306/hb3demo</property>
<propertyname="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<propertyname="connection.username">root</property>
<propertyname="connection.password">root</property>
<propertyname="jdbc.fetch_size">50</property>
<propertyname="jdbc.batch_size">30</property>
<propertyname="show_sql">true</property>
<propertyname="use_outer_join">true</property>
<propertyname="transaction.factory_">org.hibernate.transaction.JDBCTransactionFactory</property>
<propertyname="hibernate.cache.provider_">org.hibernate.cache.EhCacheProvider</property>
<propertyname="hibernate.cache.use_query_cache">true</property>
<mappingresource="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=.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE}%5p%c{1}:%L-%m%n
User.java
packagecom.hb3.pack_01.model;
publicUser{
privateIntegerid;
privateStringname;
privateIntegerage;
publicIntegergetId{
id;
}
publicvoidId(Integerid){
this.id=id;
}
publicStringgetName{
name;
}
publicvoidName(Stringname){
this.name=name;
}
publicIntegergetAge{
age;
}
publicvoidAge(Integerage){
this.age=age;
}
}
User.hbm.xml
<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEhibernate-mappingPUBLIC
"-//Hibernate/HibernateMappingDTD3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<name="com.hb3.pack_01.model.User"table="user">
<idname="id"column="id"type="java.lang.Integer">
<generator="native"/>
</id>
<propertyname="name"column="name"type="java.lang.String"/>
<propertyname="age"column="age"type="java.lang.Integer"/>
</>
</hibernate-mapping>
BusinessService.java
packagecom.hb3.pack_01;
importorg.hibernate.Session;
importorg.hibernate.SessionFactory;
importorg.hibernate.Transaction;
importorg.hibernate.cfg.Configuration;
importcom.hb3.pack_01.model.User;
publicBusinessService{
publicvoid(Stringargs){
Configurationconfig=Configuration.configure;
SessionFactorysessionFactory=config.buildSessionFactory;
Sessionsession=sessionFactory.openSession;
Useruser=User;
user.Name("shenbin");
user.Age(Integer(28));
Transactiontx=session.beginTransaction;
session.save(user);
tx.commit;
session.close;
sessionFactory.close;
}
}
在MySQL数据库中新建数据库hb3demo并执行如下DDL
CREATETABLEuser(
idINT(11)NOTNULLauto_incrementPRIMARYKEY,
nameVARCHAR(100)NOTNULLdefault'',
ageINT
);
这样就可以使用hibernate向数据库插入数据了