SSH自动生成表结构

来源:互联网 发布:mac 再次登录时 编辑:程序博客网 时间:2024/05/22 11:32

Hibernate与Spring配合生成表结构。


首先,要将Spring的信息配置的web.xml,配置Spring用于初始化容器对象的监听器。

web.xml

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">  
  3.   <display-name>oa_01</display-name>  
  4.     
  5.   <!-- 配置Spring用于初始化容器对象的监听器 -->  
  6.   <listener>  
  7.     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  8.   </listener>  
  9.   <context-param>  
  10.     <param-name>contextConfigLocation</param-name>  
  11.     <param-value>classpath:applicationContext*.xml</param-value>  
  12.   </context-param>  
  13.     
  14.   <welcome-file-list>  
  15.     <welcome-file>index.jsp</welcome-file>  
  16.   </welcome-file-list>  
  17. </web-app>  


然后,将Hibernate的信息配置到Spring的配置文件中,将Hibernate交由Spring来管理。

applicationContext.xml

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"  
  4.     xmlns:tx="http://www.springframework.org/schema/tx"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  6.                 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd  
  7.                 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">  
  8.   
  9.     <!-- 自动扫描与装配bean -->  
  10.     <context:component-scan base-package="com.tgb.oa"></context:component-scan>  
  11.       
  12.     <!-- 导入外部的properties文件 -->  
  13.     <context:property-placeholder location="classpath:jdbc.properties"/>  
  14.       
  15.     <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
  16.         <!-- 指定hibernate配置文件的位置 -->  
  17.         <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>  
  18.         <!-- 配置c3p0数据库连接池 -->  
  19.         <property name="dataSource">  
  20.             <bean class="com.mchange.v2.c3p0.ComboPooledDataSource">  
  21.                 <!-- 数据连接信息 -->  
  22.                 <property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3307/myoa"></property>  
  23.                 <property name="driverClass" value="com.mysql.jdbc.Driver"></property>  
  24.                 <property name="user" value="root"></property>  
  25.                 <property name="password" value="123456"></property>  
  26.                   
  27.                 <!-- 初始化时获取三个连接(取值应在minPoolSize与maxPoolSize之间。默认值: 3) -->  
  28.                 <property name="initialPoolSize" value="3"></property>  
  29.                 <!-- 连接池中保留的最小连接数,默认值:3 -->  
  30.                 <property name="minPoolSize" value="3"></property>  
  31.                 <!-- 连接池中保留的最大连接数,默认值:15 -->  
  32.                 <property name="maxPoolSize" value="5"></property>  
  33.                 <!-- 当连接池中的连接数耗尽的时候,c3p0一次同时获取的连接数,默认值:3 -->  
  34.                 <property name="acquireIncrement" value="3"></property>  
  35.                 <!-- 控制数据源内加载的PreparedStatements数量。如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0 -->  
  36.                 <property name="maxStatements" value="8"></property>  
  37.                 <!--maxStatementsPerConnection定义了连接池内单个连接所拥有的最大缓存statements数。Default: 0 -->  
  38.                 <property name="maxStatementsPerConnection" value="5"></property>  
  39.                 <!--最大空闲时间,1800秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->  
  40.                 <property name="maxIdleTime" value="1800"></property>  
  41.             </bean>  
  42.         </property>         
  43.     </bean>  
  44.       
  45.       
  46. </beans>  


这里我将数据库连接信息以及连接池都配置到了Spring中,当然你也可以将数据库连接信息写到Hibernate的配置文件里,Hibernate会有自己默认的连接池配置,但是它没有Spring的好用。具体写到哪看你需要吧。


接下来就是Hibernate的配置了,里面包括数据库方言、是否显示sql语句、更新方式以及实体映射文件。当然也可按上面说的将数据库连接信息写到里面。

hibernate.cfg.xml

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE hibernate-configuration PUBLIC  
  3.     "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
  4.     "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">  
  5.   
  6. <hibernate-configuration>  
  7.     <session-factory>  
  8.           
  9.         <property name="dialect">  
  10.             org.hibernate.dialect.MySQL5InnoDBDialect  
  11.         </property>  
  12.           
  13.         <property name="show_sql">true</property>  
  14.         <property name="hbm2ddl.auto">update</property>  
  15.           
  16.         <mapping resource="com/tgb/oa/domain/User.hbm.xml"/>  
  17.           
  18.     </session-factory>  
  19. </hibernate-configuration>  


实体映射文件,不做过多解释。

User.hbm.xml

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" <span style="font-family: Arial, Helvetica, sans-serif;">encoding="UTF-8"?</span>>  
  2. <!DOCTYPE hibernate-mapping PUBLIC  
  3.         "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
  4.         "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  
  5.   
  6. <hibernate-mapping package="com.tgb.oa.domain">  
  7.   
  8.     <class name="User" table="T_User">  
  9.         <id name="id">  
  10.             <generator class="native"/>  
  11.         </id>  
  12.         <property name="name" />  
  13.     </class>  
  14.       
  15. </hibernate-mapping>  


实体类

User.java
[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package com.tgb.oa.domain;  
  2.   
  3. public class User {  
  4.   
  5.     private String name;  
  6.     private  Long id;  
  7.       
  8.     public String getName() {  
  9.         return name;  
  10.     }  
  11.     public void setName(String name) {  
  12.         this.name = name;  
  13.     }  
  14.     public Long getId() {  
  15.         return id;  
  16.     }  
  17.     public void setId(Long id) {  
  18.         this.id = id;  
  19.     }  
  20.       
  21.       
  22. }  


当tomcat启动的时候,会先找到web.xml,然后根据web.xml的配置,会找到spring中的applicationContext.xml的配置文件,在applicationContext.xml中有相应的SessionFactory的配置,里面有Hibernate的相关信息,接着就会找到Hibernate-cfg.xml,读取该文件,并找到实体映射文件User.hbm.xml,最后根据User.hbm.xml的配置映射成相应的表结构。


Tomcat启动以后,表结构也跟着生成了,生成表结构后的效果:



两种生成表结构的方式其实也没有哪种好,哪种不好之说。用工具类生成的方式不需要Spring的参与,但是需要一个工具类来支持;与Spring配合的方式不需要多余的东西,但是需要与Spring配合才能用。如果你只需要Hibernate那就用第一种,如果正好是配合Spring来使用那毫无疑问就用第二种。具体看情况吧。

0 0
原创粉丝点击