Hibernate 注解时 hibernate.hbm.xml的配置方法 以及与SSH整合里的配置方式
①纯Hibernate开发:
当你在Bean中写入注解后,需要告诉hibernate哪些类使用了注解。
方法是在hibernate.hbm.xml文件中配置
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <!-- 这些信息都可以在官方目录下的properties文件中找到 -->
<hibernate-configuration>
<!-- 告诉hibernate怎么连接数据库,底层还是用jdbc访问的 -->
<session-factory>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="connection.url">
jdbc:mysql:///hibernatetest
</property>
<property name="connection.username">root</property>
<property name="connection.password">123</property> <property name="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</property>
<!-- hibernate启动的时候将已有的表重新覆盖,使用create的话 -->
<property name="hbm2ddl.auto">create</property>
<!-- 当hibernate运行时产生sql语句 -->
<property name="show_sql">true</property> <mapping class="com.hyy.hibernate.one_to_many.domain.Department"/>
<mapping class="com.hyy.hibernate.one_to_many.domain.Employee"/>
</session-factory>
</hibernate-configuration>
即上述xml文件中带下划线部分。
②如果是SSH整合框架中:
<?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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 扫描 -->
<context:component-scan base-package="com.hlcg"></context:component-scan> <!-- 配置数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="org.gjt.mm.mysql.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/hlcg?useUnicode=true&characterEncoding=UTF-8"/>
<property name="user" value="root"/>
<property name="password" value="123"/>
<!--初始化时获取的连接数,取值应该在最大和最小之间。默认:3-->
<property name="initialPoolSize" value="15"/>
<property name="minPoolSize" value="10"/>
<!-- 连接池中保留的最大连接数,默认:15 -->
<property name="maxPoolSize" value="80"/>
<!-- 最大空闲时间,60秒内未使用则连接被丢弃。若0则永远不丢弃,默认:0 -->
<property name="maxIdleTime" value="30"/>
<!-- 当连接池的连接耗尽的时候,c3p0一次同时获取的连接数,默认:3 -->
<property name="acquireIncrement" value="5"/>
<!-- 每60秒检查连接池中所有空闲连接,默认:0 -->
<property name="idleConnectionTestPeriod" value="30"/>
</bean> <!-- 配置sessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mappingResources">
<list>
<!--这里是存放以XML形式映射实体的xml文件路径-->
</list>
</property> <property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
hibernate.hbm2ddl.auto=update
show_sql=true
format_sql=true
cache.use_second_level_cache=true
hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.EhCacheRegionFactory
hibernate.cache.use_query_cache=true
</value>
</property> <property name="packagesToScan" value="com.hlcg.main.bean"/>
</bean>
</beans>
以上下划线部分配置Hibernate注解。
Hibernate 注解时 hibernate.hbm.xml的配置方法 以及与SSH整合里的配置方式的更多相关文章
- 使用oracle数据库和MySQL数据库时hibernate的映射文件.hbm.xml的不同
假设是使用oracle数据库.那么hibernate的映射文件.hbm.xml例如以下: <id name="xuehao" column="xuehao" ...
- 通过MyEclipse生成Hibernate类文件和hbm.xml文件,或者annotation文件
1. 前言 很多人都在使用myEclipse,很多公司也都使用hibernate框架,老版本的hibernate中,由于没有annotation,我们需要写两个文件来维护表与对象的关系,写一个类, ...
- 错误/异常:org.hibernate.InvalidMappingException: Could not parse mapping document from resource com/shore/model/Husband.hbm.xml 的解决方法
1.错误/异常视图 错误/异常描述:无效的映射异常,无法从xxxxx资源中解析映射文档 2.解决方法 出现这个异常,一般情况下是包名写错了.改回来即可. 看报错/异常的第一行,最后面,会提示你 ...
- Spring,Hibernate 集成解决多hbm.xml文件繁多的方案
开发一个大一点的项目有很多的hbm.xml文件,有时候上百个也不稀奇,如果用 <property name="mappingLocations"> <list&g ...
- springboot整合mybatis时无法读取xml文件解决方法(必读)
转 http://baijiahao.baidu.com/s?id=1588136004120071836&wfr=spider&for=pc 在springboot整合myba ...
- vs2017 项目生成时不产生xml文件的方法
在项目.csproj文件 <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' & ...
- centos下网络配置方法(网关、dns、ip地址配置)
本文介绍了centos网络配置的方法,centos网络配置主要包括dns.网关.IP地址: 1.IP地址配置: /etc/sysconfig/network-scripts/ifcfg-eth0 2. ...
- hibernate注解的简单应用
注解代替了我们用的*.hbm.xml文件.简少了我们的代码量:应用简单. @Override 用途:重写父类的同名方法 单元测试注解 @Test 用途:用于测试 @Before 用途:单测方法走之前执 ...
- 关于hibernate注解的简单应用
@Override 用途:重写父类的同名方法 单元测试注解 @Test 用途:用于测试 @Before 用途:单测方法走之前执行 @After 用途:单测方法走之后执行 注解的目标:替换小配置.替换h ...
随机推荐
- 通用的 makefile 小工具分享 - Easymake 使用说明
Easymake 使用说明 介绍 Easymake 是一个在linux系统中 C/C++ 开发的通用 makefile.在一个简单的 C/C++ 程序中使用 easymake,你甚至可以不写一行 ma ...
- Socket.io各个发送消息的含义
// send to current request socket client socket.emit('message', "this is a test"); // send ...
- MySQL的链接,查看数据库,使用数据库,查看表
MySQL的链接,查看数据库,使用数据库,查看表 mysql> show databases; +--------------------+ | Database | +------------ ...
- jqery选择器
根据可见性 属性 匹配元素 <!doctype html> <html lang="en"> <head> <meta charset=& ...
- Double跟double
Double 是类 double是基础数据类型.Double类型是double的包装类,在JDK1.5以后,二者可以直接相互赋值,称为自动拆箱和自动装箱.看你的提示,我推测你的jdk版本在1.5以前. ...
- java Spring bean作用域
1. Singleton作用域 当一个bean的作用域为singleton, 那么Spring IoC容器中只会存在一个共享的bean实例,并且所有对bean的请求,只要id与该bean定义相匹配,则 ...
- 怎么用js代码改变单选框的选中状态
今天突然有一个需求要用到,使用js代码改变单选框的选中状态.当时想也不想直接 function doGender(gender) { if (gender == "男") { ge ...
- 使用ICSharpZipLib将文件夹压缩为zip文件
序言: 在我接触Git和SVN之前,我最常用的保存数据的办法就是把文件夹压缩成一个zip文件,添加上时间戳.下面是我在学习C#的文件操作之后做的一个练习,使用开源的ICSharpZipLib来 ...
- C++实现RTMP协议发送H.264编码及AAC编码的音视频
http://www.cnblogs.com/haibindev/archive/2011/12/29/2305712.html C++实现RTMP协议发送H.264编码及AAC编码的音视频 RTMP ...
- CAS Server 单点登录开源项目
https://www.apereo.org/cas/download This project contains code that can extent an existing ASP.NET w ...