
Spring结合Hibernate的配置方法之一:直接引用hibernate文件
发布日期:2021-05-09 01:07:19
浏览次数:22
分类:原创文章
本文共 3665 字,大约阅读时间需要 12 分钟。
Spring结合Hibernate主要有集中配置方式,分别是
1、直接引用hibernate的*.cfg.xml配置文件
2、
3、
一、直接饮用hibernate的*.cfg.xml配置文件
项目的包结构如下:
此时Spring配置文件applicationContext.xml配置如下:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans 3 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 4 http://www.springframework.org/schema/context 5 http://www.springframework.org/schema/context/spring-context-2.5.xsd 6 http://www.springframework.org/schema/aop 7 http://www.springframework.org/schema/aop/spring-aop-2.0.xsd 8 http://www.springframework.org/schema/tx 9 http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">10 11 <!-- Spring整合Hibernate:直接饮用hibernate配置文件--> 12 <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">13 <property name="configLocation" value="classpath:chanjiayi.cfg.xml"></property>14 </bean>15 16 <!-- 使用HibernateTemplate必须将sessionFactory注入DAO类中 -->17 <bean id="userDAO" class="com.chanjiayi.dao.impl.UserDAOImpl">18 <property name="sessionFactory" ref="sessionFactory"/>19 </bean>20 21 <bean id="userService" class="com.chanjiayi.service.impl.UserServiceImpl">22 <property name="dao" ref="userDAO"/>23 </bean>24 25 </beans>
hibenate的配置文件chanjiayi.cfg.xml配置:
1 <?xml version='1.0' encoding='UTF-8'?> 2 <!DOCTYPE hibernate-configuration PUBLIC 3 "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 4 "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 5 6 <!-- Generated by MyEclipse Hibernate Tools. --> 7 <hibernate-configuration> 8 9 <session-factory>10 <!-- 配置JDBC连接属性 -->11 <property name="myeclipse.connection.profile">12 com.mysql.jdbc.Driver13 </property>14 <property name="connection.url">15 jdbc:mysql://localhost:3306/chanjiayi16 </property>17 <property name="connection.username">root</property>18 <property name="connection.password">sa</property>19 <property name="connection.driver_class">20 com.mysql.jdbc.Driver21 </property>22 <property name="dialect">23 org.hibernate.dialect.MySQLDialect24 </property>25 26 <!-- 配置C3P0连接池及连接池属性 -->27 <property name="hibernate.connection.provider_class">28 org.hibernate.connection.C3P0ConnectionProvider29 </property>30 <property name="hibernate.c3p0.timeout">120</property>31 <!--连接池中保留的最小连接数。-->32 <property name="minPoolSize">5</property>33 <!--连接池中保留的最大连接数。Default: 15 -->34 <property name="maxPoolSize">15</property>35 <!-- 最大连接数 -->36 <property name="hibernate.c3p0.max_size">10</property>37 <!-- 最小连接数 -->38 <property name="hibernate.c3p0.min_size">5</property>39 40 <!-- 自动建表 -->41 <property name="hbm2ddl.auto">update</property>42 43 <property name="show_sql">true</property>44 45 <property name="connection.autocommit">true</property>46 <mapping class="com.chanjiayi.pojo.Order" />47 <mapping class="com.chanjiayi.pojo.Product" />48 <mapping class="com.chanjiayi.pojo.User" />49 <mapping class="com.chanjiayi.pojo.UserInfo" />50 51 </session-factory>53 </hibernate-configuration>
使用了C3P0连接池,我使用的Jar包是:c3p0-0.9.0.2.jar。点击下载。
发表评论
最新留言
表示我来过!
[***.240.166.169]2025年05月01日 22时21分22秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
MFC中Socket网络通讯
2025-04-13
MFC之处理消息映射的步骤...
2025-04-13
MFC之设置ListCtrl控件的风格
2025-04-14
MFC使用Qt类库
2025-04-14
MFC函数之BitBlt
2025-04-14
MFC对话框 菜单项 禁用与启用
2025-04-14
MFC对话框屏幕居中
2025-04-14
mfc小工具开发之定时闹钟之---多线程急线程同步
2025-04-14
mfc属性页
2025-04-14
MFC工作笔记0001---认识MFC
2025-04-14
MFC工作笔记0002---MFC HelloWorld程序
2025-04-14
MFC工作笔记0003---WindowsAPI与MFC的关系
2025-04-14
MFC工作笔记0005---::在vc++中是什么意思
2025-04-14
MFC工作笔记0007---消息映射处理
2025-04-14
MFC工作笔记0010---PeekMessage 详解
2025-04-14
MFC工作笔记0011---atoi的用法
2025-04-14