hibernate框架搭建
选用hibernate版本为3.5.6 Final。
1、在项目中导入hibernate的依赖库:
hibernate3.jar
required jar:
antlr-2.7.6.jar
commons-collections-3.1.jar
dom4j-1.6.1.jar
javassist-3.9.0.GA.jar
jta-1.1.jar
slf4j-api-1.5.8.jar
为引入CGLIB导入以下jar包:
asm.jar
cglib-2.2.jar
同时依赖以下官方未提供的jar包:
log4j-1.2.16.jar
slf4j-log4j12-1.6.1.jar
2、在src包下配置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.bytecode.use_reflection_optimizer">false</property><property name="hibernate.cache.provider_class">org.hibernate.cache.NoCacheProvider</property><property name="hibernate.connection.pool_size">50</property><!-- MySQL --><property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property><property name="hibernate.connection.password">admin</property><property name="hibernate.connection.url">jdbc:mysql://127.0.0.1:3306/ys_app?useUnicode=true&characterEncoding=utf-8</property><property name="hibernate.connection.username">root</property><property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property><!-- MySQL end --><property name="hibernate.current_session_context_class">managed</property><property name="hibernate.format_sql">true</property><property name="hibernate.query.substitutions">true 1, false 0</property><property name="hibernate.show_sql">false</property><property name="hibernate.use_sql_comments">true</property><mapping resource="com/ys/entity/User.hbm.xml" /></session-factory></hibernate-configuration>
##Tue Nov 5 11:08:28 CST 2010log4j.rootLogger=info,A1,R log4j.appender.A1=org.apache.log4j.ConsoleAppender log4j.appender.A1.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss} [%c]-[%p] %m%n log4j.appender.A1.layout=org.apache.log4j.PatternLayout log4j.appender.R.File=C:/YsApp.loglog4j.appender.R.MaxFileSize=100KB log4j.appender.R.layout=org.apache.log4j.PatternLayout log4j.appender.R.MaxBackupIndex=8 log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%nlog4j.appender.R=org.apache.log4j.RollingFileAppender
package com.ys.entity;// Generated 2010-11-5 13:43:31 by Hibernate Tools 3.2.4.GA/** * Users generated by hbm2java */public class User extends Entity {private String name;public String getName() {return this.name;}public void setName(String name) {this.name = name;}}
<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><!-- Generated 2010-11-5 13:43:31 by Hibernate Tools 3.2.4.GA --><hibernate-mapping> <class name="com.ys.entity.User" table="users" catalog="ys_app"> <id name="id" type="java.lang.Integer"> <column name="id" /> <generator /> </id> <property name="name" type="string"> <column name="name" not-null="true" /> </property> </class></hibernate-mapping>
public class UserTest extends TestCase {@Overridepublic void setUp() {HibernateSessionFactory.init();HibernateSessionFactory.openSession();}public void testUserNameList() {User user = new User();List users = user.findAll();assertEquals(2, users.size());assertEquals("sunny", ((User) users.get(0)).getName());assertEquals("jimmy", ((User) users.get(1)).getName());}@Overridepublic void tearDown() {HibernateSessionFactory.closeSession();HibernateSessionFactory.doneFactory();}}