Hibernate(十)--spring整合hibernate
结构:

Spring和Hibernate整合借助于HibernateTemplate
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:aop="http://www.springframework.org/schema/aop"
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/aop
http://www.springframework.org/schema/aop/spring-aop-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"> <bean name="c" class="com.lc.pojo.Category">
<property name="name" value="yyy" />
</bean> <bean name="dao" class="com.lc.dao.CategoryDAO">
<property name="sessionFactory" ref="sf" />
</bean> <bean name="sf"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="ds" />
<property name="mappingResources">
<list>
<value>com/lc/pojo/Category.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.show_sql=true
hbm2ddl.auto=update
</value>
</property>
</bean> <!-- 数据源-->
<bean name="ds"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/test?characterEncoding=GBK" />
<property name="username" value="root" />
<property name="password" value="123456" />
</bean> </beans>
测试:
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
CategoryDAO bean =(CategoryDAO) context.getBean("dao");
DetachedCriteria criteria = DetachedCriteria.forClass(Category.class);
// List<Category> list = bean.findByCriteria(criteria, 0, 5); //分页--取0-5个返回
// System.out.println(list);

Category.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.lc.pojo"> <class name="Category" table="category_">
<id name="id" column="id">
<generator class="native">
</generator>
</id>
<property name="name" />
</class> </hibernate-mapping>
result:

Hibernate(十)--spring整合hibernate的更多相关文章
- 【Java EE 学习 53】【Spring学习第五天】【Spring整合Hibernate】【Spring整合Hibernate、Struts2】【问题:整合hibernate之后事务不能回滚】
一.Spring整合Hibernate 1.如果一个DAO 类继承了HibernateDaoSupport,只需要在spring配置文件中注入SessionFactory就可以了:如果一个DAO类没有 ...
- spring整合hibernate的详细步骤
Spring整合hibernate需要整合些什么? 由IOC容器来生成hibernate的sessionFactory. 让hibernate使用spring的声明式事务 整合步骤: 加入hibern ...
- spring整合hibernate
spring整合hibernate包括三部分:hibernate的配置.hibernate核心对象交给spring管理.事务由AOP控制 好处: 由java代码进行配置,摆脱硬编码,连接数据库等信息更 ...
- spring 整合hibernate
1. Spring 整合 Hibernate 整合什么 ? 1). 有 IOC 容器来管理 Hibernate 的 SessionFactory2). 让 Hibernate 使用上 Spring 的 ...
- Spring 整合 Hibernate
Spring 整合 Hibernate •Spring 支持大多数流行的 ORM 框架, 包括 Hibernate JDO, TopLink, Ibatis 和 JPA. •Spring 对这些 OR ...
- 使用Spring整合Hibernate,并实现对数据表的增、删、改、查的功能
1.1 问题 使用Spring整合Hibernate,并实现资费表的增.删.改.查. 1.2 方案 Spring整合Hibernate的步骤: 1.3 步骤 实现此案例需要按照如下步骤进行. 采用的环 ...
- Spring整合Hibernate详细步骤
阅读目录 一.概述 二.整合步骤 回到顶部 一.概述 Spring整合Hibernate有什么好处? 1.由IOC容器来管理Hibernate的SessionFactory 2.让Hibernate使 ...
- SSH整合之spring整合hibernate
SSH整合要导入的jar包: MySQL中创建数据库 create database ssh_db; ssh_db 一.spring整合hibernate带有配置文件hibernate.cfg.xml ...
- 【Spring】Spring系列6之Spring整合Hibernate
6.Spring整合Hibernate 6.1.准备工作 6.2.示例 com.xcloud.entities.book com.xcloud.dao.book com.xcloud.service. ...
随机推荐
- Balanced Lineup -POJ3264
题意: 告诉你n头奶牛的高度,然后给你一个区间,你需要求出这个区间最高的奶牛与最矮的奶牛之间相差多少 链接:http://poj.org/problem?id=3264 思路: 线段树区间查询,用两个 ...
- 吴裕雄--天生自然PythonDjangoWeb企业开发:学员管理系统后台
需求 提供一个学员管理系统,一个前台页面,展示现有学员,并供新学员提交申请,一个后台,能够处理申请. pip install django==1.11.2 创建项目 使用控制台进入到一个目录下,具体是 ...
- SpringMvc 返回json的三种方式
参考:https://blog.csdn.net/fupengyao/article/details/62220886
- Gamma 矫正
参考如下链接: https://www.zhihu.com/question/27467127
- Flask 教程 第二十三章:应用程序编程接口(API)
本文翻译自The Flask Mega-Tutorial Part XXIII: Application Programming Interfaces (APIs) 我为此应用程序构建的所有功能都只适 ...
- MyEclipse JSP页面中文字符不能保存
问题: 就是写HTML+JSP代码时有些中文,保存时提示sava could not be completed. Reason: some characters cannot be mapped ...
- MacBook OSX VMWare Fusion 11安装 Tools For Windows
需要加载对应客户机系统的安装文件,可以在/Applications/VMware\ Fusion.app/Contents/Library/isoimages文件夹下找到: 设置虚拟机的光驱: 在虚拟 ...
- Lesson 12 banks and their customers
Why is there no risk to the customer when a bank prints the customer's name on his cheques? When any ...
- 生成资源文件时候,可以动态替换为maven属性
1.maven管理的文件或者是maven插件处理的文件中 可以引用maven属性,在编译输出时候,可以替换 ${project.build.testOutputDirectory} 在资源 ...
- SpringTest
Spring Test 1.对junit的一个扩展 必须先导入junit jar包 2.简化获取bean的步骤 它的底层也是IOC容器 3.IOC的全部 junit的全部 junit的版本必须是 ...