菜鸟学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 ...
随机推荐
- JS数组追加数组採用push.apply的坑
JS数组追加数组没有现成的函数,这么多年我已经习惯了a.push.apply(a, b);这样的自以为非常酷的,不须要写for循环的写法,一直也没遇到什么问题,直到今天我要append的b是个非常大的 ...
- Cocos2d-x Tiled地图编辑器(一)基本使用
Tiled地图编辑器支持普通视角地图和45度角地图, 它生成的地图数据文件cocos2d-x完美的支持,Tiled地图编辑器是一个以普通使用为目标地图编辑器,它使用简单而且能够轻松地在不同的游戏引擎中 ...
- Eclipse中的SVN的冲突解决方案详解
版本冲突原因: 假设A.B两个用户都在版本号为100的时候,更新了kingtuns.txt这个文件,A用户在修改完成之后提交kingtuns.txt到服务器,这个时候提交成功,这个时候kingtuns ...
- oracle看到用户的所有表名、表睐、字段名称、现场的目光、是空的、字段类型
--oracle看到用户的所有表名.表睐.字段名称.现场的目光.是空的.字段类型 select distinct TABLE_COLUMN.*, TABLE_NALLABLE.DATA_TYPE, T ...
- 【LeetCode with Python】 Rotate Image
博客域名:http://www.xnerv.wang 原标题页:https://oj.leetcode.com/problems/rotate-image/ 题目类型:下标计算 难度评价:★★★ 本文 ...
- 为应用程序池“XX”提供服务的进程在与 Windows Process Activation Service 通信时出现严重错误
场景 WCF应用程序部署在IIS7中,使用net.tcp协议对外给几百台客户端提供服务,应用程序池不断崩溃重启. 分析过程 在事件查看器中看到的错误信息类似于 为应用程序池“XX”提供服务的进程在与 ...
- 【从翻译mos文章】在OGG (Oracle GoldenGate) 正在使用SCHEMATRANDATA如果,需要额外的db patch
在OGG (Oracle GoldenGate) 正在使用SCHEMATRANDATA如果.需要额外的db patch 参考原始: Patches needed to support SCHEMATR ...
- 编译gRPC
编译gRPC 目录 一.概述 二.编译gRPC 三.C#中使用gRPC 四.C++中使用gRPC 无论通过哪种语言调用gRPC,都必须要编译gRPC,因为生成proto访问类时,除了产生标准的数据定义 ...
- C语言 cgi(3)
1cs3157 – Advanced ProgrammingSummer 2014, Project 1, 150 pointsJune 17, 2014Follow these step-by-st ...
- 理解RESTful架构(转)
越来越多的人开始意识到,网站即软件,而且是一种新型的软件. 这种"互联网软件"采用客户端/服务器模式,建立在分布式体系上,通过互联网通信,具有高延时(high latency).高 ...