利用spring2.0+hibernate3.2來做個hbm2ddl(hibernate配置文件直接幫你建表)簡單示例
運行環境: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;
}
}
<?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等等
posted on 2008-03-26 15:22 ybc 閱讀(463) 評論(0) 編輯 收藏 所屬分類: SSH