ybc

 

利用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;
 }

}

第三步:配置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等等

posted on 2008-03-26 15:22 ybc 阅读(451) 评论(0)  编辑  收藏 所属分类: SSH


只有注册用户登录后才能发表评论。


网站导航:
 

导航

统计

常用链接

留言簿(1)

随笔档案

文章分类

文章档案

搜索

最新评论

阅读排行榜

评论排行榜