利用spring2.0+hibernate3.2来做个hbm2ddl的简单示例

利用spring2.0+hibernate3.2来做个hbm2ddl的简单示例

运行环境:eclipse3.2+myeclispe5.1
数据库:mysql4.1
服务期:tomcat5.5
框架:spring2.0+hibernate3.2
1。第一步:把环境搭起来,建个webproject项目
2。第二步:建个简单的类

例如:
package vo;

public class Student {
private int id;
private String name;
public Student(int id, String name) {
super();
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}

}


第三步:配置hbm文件

<?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 package="vo">

<!-- 每个 class 元素映射一个持久化类 -->

<class name="Student" table="student_table">

<!-- 映射标识属性 -->

<id name="id">

<!-- 指定主键生成器策略 -->

<generator class="identity"/>

</id>

<!-- 映射 name -->

<property name="name"/>


</class>

</hibernate-mapping>



第四步:配置applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">

<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/school"/>

<property name="driverClass" value="com.mysql.jdbc.Driver"/>

<property name="user" value="root"/>

<property name="password" value="1234"/>

<property name="maxPoolSize" value="40"/>

<property name="minPoolSize" value="1"/>

<property name="initialPoolSize" value="1"/>

<property name="maxIdleTime" value="20"/>

</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

<property name="dataSource" ref="dataSource"/>

<property name="mappingResources">

<list>

<value>vo/student.hbm.xml</value>

</list>

</property>

<property name="hibernateProperties">

<props>

<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>

<prop key="show_sql">true</prop>

<prop key="hibernate.hbm2ddl.auto">update</prop>

<prop key="hibernate.jdbc.batch_size">20</prop>

</props>

</property>

</bean>

</beans>



第五步:配置web.xml (注:主要是为了加载applicationContext.xml而用的)

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<context-param>
<param-name>contextConfigLocation</param-name>

<param-value>/WEB-INF/applicationContext.xml</param-value>

</context-param>

<listener>

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

</web-app>
第六步:就可以发布项目和运行tomcat,然后在数据库里就可以看到建成的student_table表了,是不是很简单啊,呵呵这只是个简单的入门。

注意:1.在数据库中要先建好database,我这里是school
2.一些包不要忘记添加:如:antlr.jar,asm.jar,c3p0.jar,cglib.jar,commoons-beanutils.jar,commons-collection,jar
hibernate3.2.jar,jta.jar,mysql-connector.jar
spring-2.0.jar等等

下面是log4j产生的运行信息:
2008-03-26 14:49:35,796 INFO [main] context.ContextLoader (ContextLoader.java:174) - Root WebApplicationContext: initialization started
2008-03-26 14:49:35,796 INFO [main] core.ApplicationContext (ApplicationContext.java:646) - Loading Spring root WebApplicationContext
2008-03-26 14:49:36,156 INFO [main] core.CollectionFactory (CollectionFactory.java:76) - JDK 1.4+ collections available
2008-03-26 14:49:36,187 INFO [main] core.CollectionFactory (CollectionFactory.java:80) - Commons Collections 3.x available
2008-03-26 14:49:36,390 INFO [main] xml.XmlBeanDefinitionReader (XmlBeanDefinitionReader.java:347) - Loading XML bean definitions from ServletContext resource [/WEB-INF/applicationContext.xml]
2008-03-26 14:49:36,734 INFO [main] support.AbstractRefreshableApplicationContext (AbstractRefreshableApplicationContext.java:100) - Bean factory for application context [Root WebApplicationContext]: org.springframework.beans.factory.support.DefaultListableBeanFactory defining beans [dataSource,sessionFactory]; root of BeanFactory hierarchy
2008-03-26 14:49:36,812 INFO [main] support.AbstractApplicationContext (AbstractApplicationContext.java:324) - 2 beans defined in application context [Root WebApplicationContext]
2008-03-26 14:49:36,937 INFO [main] support.AbstractApplicationContext (AbstractApplicationContext.java:475) - Unable to locate MessageSource with name 'messageSource': using default [org.springframework.context.support.DelegatingMessageSource@105fa26]
2008-03-26 14:49:36,953 INFO [main] support.AbstractApplicationContext (AbstractApplicationContext.java:497) - Unable to locate ApplicationEventMulticaster with name 'applicationEventMulticaster': using default [org.springframework.context.event.SimpleApplicationEventMulticaster@f90137]
2008-03-26 14:49:37,078 INFO [main] support.UiApplicationContextUtils (UiApplicationContextUtils.java:83) - Unable to locate ThemeSource with name 'themeSource': using default [org.springframework.ui.context.support.ResourceBundleThemeSource@e38fca]
2008-03-26 14:49:37,093 INFO [main] support.DefaultListableBeanFactory (DefaultListableBeanFactory.java:278) - Pre-instantiating singletons in factory [org.springframework.beans.factory.support.DefaultListableBeanFactory defining beans [dataSource,sessionFactory]; root of BeanFactory hierarchy]
2008-03-26 14:49:37,218 INFO [main] log.MLog (MLog.java:80) - MLog clients using log4j logging.
2008-03-26 14:49:37,546 INFO [main] c3p0.C3P0Registry (C3P0Registry.java:204) - Initializing c3p0-0.9.1.2 [built 21-May-2007 15:04:56; debug? true; trace: 10]
2008-03-26 14:49:38,296 INFO [main] cfg.Environment (Environment.java:500) - Hibernate 3.2.0
2008-03-26 14:49:38,343 INFO [main] cfg.Environment (Environment.java:533) - hibernate.properties not found
2008-03-26 14:49:38,343 INFO [main] cfg.Environment (Environment.java:667) - Bytecode provider name : cglib
2008-03-26 14:49:38,359 INFO [main] cfg.Environment (Environment.java:584) - using JDK 1.4 java.sql.Timestamp handling
2008-03-26 14:49:39,390 INFO [main] cfg.HbmBinder (HbmBinder.java:300) - Mapping class: vo.Student -> student_table
2008-03-26 14:49:39,562 INFO [main] hibernate3.LocalSessionFactoryBean (LocalSessionFactoryBean.java:738) - Building new Hibernate SessionFactory
2008-03-26 14:49:39,937 INFO [main] connection.ConnectionProviderFactory (ConnectionProviderFactory.java:72) - Initializing connection provider: org.springframework.orm.hibernate3.LocalDataSourceConnectionProvider
2008-03-26 14:49:40,203 INFO [main] impl.AbstractPoolBackedDataSource (AbstractPoolBackedDataSource.java:462) - Initializing c3p0 pool... com.mchange.v2.c3p0.ComboPooledDataSource [ acquireIncrement -> 3, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 0, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, dataSourceName -> 1hge1277s1svuo9rknctg8|856d3b, debugUnreturnedConnectionStackTraces -> false, description -> null, driverClass -> com.mysql.jdbc.Driver, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, identityToken -> 1hge1277s1svuo9rknctg8|856d3b, idleConnectionTestPeriod -> 0, initialPoolSize -> 1, jdbcUrl -> jdbc:mysql://localhost:3306/school, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 20, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 40, maxStatements -> 0, maxStatementsPerConnection -> 0, minPoolSize -> 1, numHelperThreads -> 3, numThreadsAwaitingCheckoutDefaultUser -> 0, preferredTestQuery -> null, properties -> {user=******, password=******}, propertyCycle -> 0, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, unreturnedConnectionTimeout -> 0, usesTraditionalReflectiveProxies -> false ]
2008-03-26 14:49:40,781 INFO [main] cfg.SettingsFactory (SettingsFactory.java:81) - RDBMS: MySQL, version: 4.1.7-nt
2008-03-26 14:49:40,781 INFO [main] cfg.SettingsFactory (SettingsFactory.java:82) - JDBC driver: MySQL-AB JDBC Driver, version: mysql-connector-java-3.1.13 ( $Date: 2005-11-17 15:53:48 +0100 (Thu, 17 Nov 2005) $, $Revision$ )
2008-03-26 14:49:40,906 INFO [main] dialect.Dialect (Dialect.java:141) - Using dialect: org.hibernate.dialect.MySQLDialect
2008-03-26 14:49:40,937 INFO [main] transaction.TransactionFactoryFactory (TransactionFactoryFactory.java:31) - Using default transaction strategy (direct JDBC transactions)
2008-03-26 14:49:40,953 INFO [main] transaction.TransactionManagerLookupFactory (TransactionManagerLookupFactory.java:33) - No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
2008-03-26 14:49:40,953 INFO [main] cfg.SettingsFactory (SettingsFactory.java:134) - Automatic flush during beforeCompletion(): disabled
2008-03-26 14:49:40,953 INFO [main] cfg.SettingsFactory (SettingsFactory.java:138) - Automatic session close at end of transaction: disabled
2008-03-26 14:49:40,953 INFO [main] cfg.SettingsFactory (SettingsFactory.java:145) - JDBC batch size: 20
2008-03-26 14:49:40,953 INFO [main] cfg.SettingsFactory (SettingsFactory.java:148) - JDBC batch updates for versioned data: disabled
2008-03-26 14:49:40,968 INFO [main] cfg.SettingsFactory (SettingsFactory.java:153) - Scrollable result sets: enabled
2008-03-26 14:49:40,968 INFO [main] cfg.SettingsFactory (SettingsFactory.java:161) - JDBC3 getGeneratedKeys(): enabled
2008-03-26 14:49:40,968 INFO [main] cfg.SettingsFactory (SettingsFactory.java:169) - Connection release mode: on_close
2008-03-26 14:49:40,968 INFO [main] cfg.SettingsFactory (SettingsFactory.java:193) - Maximum outer join fetch depth: 2
2008-03-26 14:49:40,968 INFO [main] cfg.SettingsFactory (SettingsFactory.java:196) - Default batch fetch size: 1
2008-03-26 14:49:40,968 INFO [main] cfg.SettingsFactory (SettingsFactory.java:200) - Generate SQL with comments: disabled
2008-03-26 14:49:40,968 INFO [main] cfg.SettingsFactory (SettingsFactory.java:204) - Order SQL updates by primary key: disabled
2008-03-26 14:49:40,968 INFO [main] cfg.SettingsFactory (SettingsFactory.java:369) - Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
2008-03-26 14:49:41,000 INFO [main] ast.ASTQueryTranslatorFactory (ASTQueryTranslatorFactory.java:24) - Using ASTQueryTranslatorFactory
2008-03-26 14:49:41,000 INFO [main] cfg.SettingsFactory (SettingsFactory.java:212) - Query language substitutions: {}
2008-03-26 14:49:41,000 INFO [main] cfg.SettingsFactory (SettingsFactory.java:217) - JPA-QL strict compliance: disabled
2008-03-26 14:49:41,000 INFO [main] cfg.SettingsFactory (SettingsFactory.java:222) - Second-level cache: enabled
2008-03-26 14:49:41,000 INFO [main] cfg.SettingsFactory (SettingsFactory.java:226) - Query cache: disabled
2008-03-26 14:49:41,000 INFO [main] cfg.SettingsFactory (SettingsFactory.java:356) - Cache provider: org.hibernate.cache.NoCacheProvider
2008-03-26 14:49:41,000 INFO [main] cfg.SettingsFactory (SettingsFactory.java:241) - Optimize cache for minimal puts: disabled
2008-03-26 14:49:41,000 INFO [main] cfg.SettingsFactory (SettingsFactory.java:250) - Structured second-level cache entries: disabled
2008-03-26 14:49:41,062 INFO [main] cfg.SettingsFactory (SettingsFactory.java:277) - Statistics: disabled
2008-03-26 14:49:41,062 INFO [main] cfg.SettingsFactory (SettingsFactory.java:281) - Deleted entity synthetic identifier rollback: disabled
2008-03-26 14:49:41,062 INFO [main] cfg.SettingsFactory (SettingsFactory.java:296) - Default entity-mode: pojo
2008-03-26 14:49:41,203 INFO [main] impl.SessionFactoryImpl (SessionFactoryImpl.java:161) - building session factory
2008-03-26 14:49:41,781 INFO [main] tuple.PojoInstantiator (PojoInstantiator.java:61) - no default (no-argument) constructor for class: vo.Student (class must be instantiated by Interceptor)
2008-03-26 14:49:42,937 INFO [main] impl.SessionFactoryObjectFactory (SessionFactoryObjectFactory.java:82) - Not binding factory to JNDI, no JNDI name configured
2008-03-26 14:49:42,968 INFO [main] hbm2ddl.SchemaUpdate (SchemaUpdate.java:115) - Running hbm2ddl schema update
2008-03-26 14:49:42,968 INFO [main] hbm2ddl.SchemaUpdate (SchemaUpdate.java:126) - fetching database metadata
2008-03-26 14:49:43,000 INFO [main] hbm2ddl.SchemaUpdate (SchemaUpdate.java:138) - updating schema
2008-03-26 14:49:43,031 INFO [main] hbm2ddl.DatabaseMetadata (DatabaseMetadata.java:91) - table not found: student_table
2008-03-26 14:49:43,031 INFO [main] hbm2ddl.DatabaseMetadata (DatabaseMetadata.java:91) - table not found: student_table
2008-03-26 14:49:43,421 INFO [main] hbm2ddl.SchemaUpdate (SchemaUpdate.java:160) - schema update complete
2008-03-26 14:49:43,546 INFO [main] context.ContextLoader (ContextLoader.java:189) - Using context class [org.springframework.web.context.support.XmlWebApplicationContext] for root WebApplicationContext
2008-03-26 14:49:43,578 INFO [main] context.ContextLoader (ContextLoader.java:199) - Root WebApplicationContext: initialization completed in 7782 ms

你可能感兴趣的:(利用spring2.0+hibernate3.2来做个hbm2ddl的简单示例)