菜鸟学SSH(十二)——Hibernate与Spring配合生成表结构
前几天向大家介绍了一种用工具类生成数据表的方法,只是之前的方法须要使用一个跟项目关系不大的工具类。不免让人认为有些多余,所以呢。今天再向大家介绍一种方法。即Hibernate与Spring配合生成表结构。
首先。要将Spring的信息配置的web.xml。配置Spring用于初始化容器对象的监听器。
web.xml
<? xml version="1.0" encoding="UTF-8"?>
<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">
<display-name>oa_01</display-name> <!-- 配置Spring用于初始化容器对象的监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext*.xml</param-value>
</context-param> <welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
然后,将Hibernate的信息配置到Spring的配置文件中。将Hibernate交由Spring来管理。
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <!-- 自己主动扫描与装配bean -->
<context:component-scan base-package="com.tgb.oa"></context:component-scan> <!-- 导入外部的properties文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- 指定hibernate配置文件的位置 -->
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
<!-- 配置c3p0数据库连接池 -->
<property name="dataSource">
<bean class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 数据连接信息 -->
<property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3307/myoa"></property>
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="user" value="root"></property>
<property name="password" value="123456"></property> <!-- 初始化时获取三个连接(取值应在minPoolSize与maxPoolSize之间。默认值: 3) -->
<property name="initialPoolSize" value="3"></property>
<!-- 连接池中保留的最小连接数,默认值:3 -->
<property name="minPoolSize" value="3"></property>
<!-- 连接池中保留的最大连接数,默认值:15 -->
<property name="maxPoolSize" value="5"></property>
<!-- 当连接池中的连接数耗尽的时候,c3p0一次同一时候获取的连接数,默认值:3 -->
<property name="acquireIncrement" value="3"></property>
<!-- 控制数据源内载入的PreparedStatements数量。假设maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。 Default: 0 -->
<property name="maxStatements" value="8"></property>
<!--maxStatementsPerConnection定义了连接池内单个连接所拥有的最大缓存statements数。Default: 0 -->
<property name="maxStatementsPerConnection" value="5"></property>
<!--最大空暇时间,1800秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
<property name="maxIdleTime" value="1800"></property>
</bean>
</property>
</bean> </beans>
这里我将数据库连接信息以及连接池都配置到了Spring中。当然你也能够将数据库连接信息写到Hibernate的配置文件中。Hibernate会有自己默认的连接池配置,可是它没有Spring的好用。详细写到哪看你须要吧。
接下来就是Hibernate的配置了,里面包含数据库方言、是否显示sql语句、更新方式以及实体映射文件。当然也可按上面说的将数据库连接信息写到里面。
hibernate.cfg.xml
<? xml version="1.0" encoding="UTF-8"? >
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration>
<session-factory> <property name="dialect">
org.hibernate.dialect.MySQL5InnoDBDialect
</property> <property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property> <mapping resource="com/tgb/oa/domain/User.hbm.xml"/> </session-factory>
</hibernate-configuration>
实体映射文件,不做过多解释。
User.hbm.xml
<? 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="com.tgb.oa.domain"> <class name="User" table="T_User">
<id name="id">
<generator class="native"/>
</id>
<property name="name" />
</class> </hibernate-mapping>
实体类
User.java
package com.tgb.oa.domain;
public class User {
private String name;
private Long id;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
当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来使用那毫无疑问就用另外一种。详细看情况吧。
菜鸟学SSH(十二)——Hibernate与Spring配合生成表结构的更多相关文章
- 菜鸟学SSH(十一)——Hibernate之SchemaExport+配置文件生成表结构
今天说点基础的东西,说说怎样通过SchemaExport跟Hibernate的配置文件生成表结构.事实上方法很easy,仅仅须要两个配置文件,两个Java类就能够完毕. 首先要生成表,得先有实体类,以 ...
- Hibernate之SchemaExport+配置文件生成表结构
首先要生成表,得先有实体类,以Person.java为例: /** * * @author Administrator * @hibernate.class table="T_Person& ...
- 菜鸟学SSH(二)——Struts2国际化手动切换版
国际化(internationalization,i18n)和本地化(localization,l10n)指让产品(出版物,软件,硬件等)能够适应非本地环境,特别是其他的语言和文化.程序在不修改内部代 ...
- hibernate.hbm2ddl.auto=update不能自动生成表结构
在写上篇文章<spring整合springmvc和hibernate>的时候,曾遇到一个问题 INFO: Server startup in 8102 ms Hibernate: inse ...
- Hibernate使用自定义脚本替换注解或者xml文件中的自动生成表结构
本文作者:苏生米沿 本文地址:http://blog.csdn.net/sushengmiyan/article/details/50534361 我们都清楚,可以使用hibernate的metada ...
- 跟我学SpringCloud | 第十二篇:Spring Cloud Gateway初探
SpringCloud系列教程 | 第十二篇:Spring Cloud Gateway初探 Springboot: 2.1.6.RELEASE SpringCloud: Greenwich.SR1 如 ...
- 从头开始学JavaScript (十二)——Array类型
原文:从头开始学JavaScript (十二)--Array类型 一.数组的创建 注:ECMAscript数组的每一项都可以保存任何类型的数据 1.1Array构造函数 var colors = ne ...
- MINA、Netty、Twisted一起学(十二):HTTPS
由于HTTPS协议是由HTTP协议加上SSL/TLS协议组合而成,在阅读本文前可以先阅读一下HTTP服务器和SSL/TLS两篇博文,本文中的代码也是由这两篇博文中的代码组合而成. HTTPS介绍 上一 ...
- 菜鸟学SSH(三)——Struts2国际化自动检测浏览器语言版
前几天发了一篇Struts国际化的博客——<菜鸟学习SSH(二)——Struts2国际化手动切换版>,有网友提了一个意见,见下图: 于是就有了下面修改的版本: web.xml <?x ...
随机推荐
- iOS得知1_初体验
UIView:父类的所有控件,所有的UIView它是一个容器.可容纳其他UIView UIController:用于控制UIView,责创建/销毁自己的UIView,显示/隐藏UIView.处理UIV ...
- 服务器编程入门(4)Linux网络编程基础API
问题聚焦: 这节介绍的不仅是网络编程的几个API 更重要的是,探讨了Linux网络编程基础API与内核中TCP/IP协议族之间的关系. 这节主要介绍三个方面的内容:套接字( ...
- Tkinter隐藏窗口再让他显示出来的例子
隐藏主要是 : withdraw()函数. 重新显示出来主要是: update()和deiconify()函数. 来源:http://www.blog.pythonlibrary.org/2012/0 ...
- ASP.NET Web Service应用发布到IIs怎么做
首先把你写的webservice Publish 到 一个文件夹 D:\MyWebService 下,在IIS下的website里面new一个虚拟目录,别名(Alias)随便输一个(这个别名是用于别的 ...
- HUST 1017(DLX)
题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=65998#problem/A 题意:求01矩阵的精确覆盖. DLX学习资料:ht ...
- IL来理解属性
IL来理解属性 阅读目录 概述: C#中如何定义一个属性 Student类 属性Name Main方法 实现get,set方法 性能 访问权限 回到最开始提出的问题 参考资料 .Net底层剖析目录 ...
- uva 11722 - Joining with Friend(概率)
题目连接:uva 11722 - Joining with Friend 题目大意:你和朋友乘火车,而且都会路过A市.给定两人可能到达A市的时段,火车会停w.问说两人能够见面的概率. 解题思路:y = ...
- HDU-2647拓扑排序
这道题不能用矩阵表示,因为1w*1w绝对超内存,分析数据,前一个a的钱要多于后一个b,所以我们要把b作为出度,a为入度,如果不明白这个地方,举例:b——>a——>c——>d ,b为8 ...
- in与exist , not in与not exist 的区别(转)
in和exists in 是把外表和内表作hash 连接,而exists是对外表作loop循环,每次loop循环再对内表进行查询.一直以来认为exists比in效率高的说法是不准确的. 如果查询的 ...
- 正則表達式 取出img标签 保存于指定路径
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...