sh_Spring整合Hibernate
分别介绍了Sping和Hibernate,以下是将它们整合到一块去了。
一、Hibernate内容
1.创建PO类。
package cn.tgb.domain; //User实体
public class User {
private Integer id;
private String username;
private String password; public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
2.编写User.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping>
<class name="cn.tgb.domain.User" table="t_user">
<id name="id">
<generator class="native"></generator>
</id>
<property name="username"></property>
<property name="password"></property>
</class>
</hibernate-mapping>
3.编写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>
<!-- #1核心四项 -->
<property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<property name="hibernate.connection.url">jdbc:sqlserver://localhost:1433; DatabaseName=hibernate02</property>
<property name="hibernate.connection.username">sa</property>
<property name="hibernate.connection.password">123456</property> <!-- #2设置方言 -->
<property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property> <!-- #3 sql -->
<!-- * 输出sql-->
<property name="hibernate.show_sql">true</property>
<!-- * 格式化sql -->
<property name="hibernate.format_sql">true</property>
<!-- * 是否创建表 -->
<property name="hibernate.hbm2ddl.auto">update</property> <!-- #4整合c3p0 -->
<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property> <!-- #5 web项目6.0 取消bean校验 -->
<property name="javax.persistence.validation.mode">none</property> <!-- #6加入映射文件 -->
<mapping resource="cn/tgb/domain/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
二、Spring内容
1.Dao实现类:使用HibernateTemplate对PO类进行操作。
package cn.tgb.dao.impl; import java.util.List; import org.springframework.orm.hibernate3.HibernateTemplate; import cn.tgb.dao.UserDao;
import cn.tgb.domain.User; public class UserDaoImpl implements UserDao{ //使用hibernateTemplate对PO类进行操作
private HibernateTemplate hibernateTemplate;
//提供set方法
public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
this.hibernateTemplate = hibernateTemplate;
} @Override
public void save(User user) {
this.hibernateTemplate.save(user);
} @Override
public void update(User user) {
this.hibernateTemplate.update(user);
} @Override
public void delete(User user) {
this.hibernateTemplate.delete(user);
} @Override
public User findById(Integer uid) {
return this.hibernateTemplate.get(User.class, uid);
} @Override
public List<User> findAllUser() {
return this.hibernateTemplate.find("from User");
} }
2.Service实现类:直接使用dao层,通过spring tx管理事务
package cn.tgb.service.impl; import java.util.List; import cn.tgb.dao.UserDao;
import cn.tgb.domain.User;
import cn.tgb.service.UserService; public class UserServiceImpl implements UserService{
//引入UserDao
private UserDao userDao;
//提供set方法
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
} @Override
public void addUser(User user) {
this.userDao.save(user);
} @Override
public void updateUser(User user) {
this.userDao.update(user);
} @Override
public void deleteUser(User user) {
this.userDao.delete(user);
} @Override
public User findUserById(Integer uid) {
return this.userDao.findById(uid);
} @Override
public List<User> findAllUser() {
return this.userDao.findAllUser();
} }
3.配置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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- #1配置sessionFactory,
通过LocalSessionFactoryBean载入配置获得SessionFactory
--> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- #1.1确定配置文件的位置 -->
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
</bean>
<!-- #2 hibernateTemplate -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<!-- 须要使用sessionFactory获得session,操作PO对象 -->
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- #3 dao -->
<bean id="userDao" class="cn.tgb.dao.impl.UserDaoImpl">
<property name="hibernateTemplate" ref="hibernateTemplate"></property>
</bean>
<!-- #4 service -->
<bean id="userService" class="cn.tgb.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao"></property>
</bean> <!-- #5 事务管理 -->
<!-- #5.1事务管理器 -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<!-- 必须确定sessionFactory。 从而获得session,再获得事务 -->
<property name="sessionFactory" ref="sessionFactory"></property>
</bean> <!-- #5.2事务通知(增强),对指定目标方法进行增强 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="add*"/>
<tx:method name="update*"/>
<tx:method name="delete*"/>
<tx:method name="find*" read-only="true"/>
</tx:attributes>
</tx:advice> <!-- #5.3使用aop生成代理,进行增强 -->
<aop:config>
<!-- 确定切入点 -->
<aop:pointcut expression="execution(* cn.tgb.service..*.*(..))" id="txPointCut"/>
<!-- 声明切面 -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/>
</aop:config>
</beans>
至此,Spring和Hibernate就算整合好了。以下编写一个測试类測试下。
package cn.tgb.test; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.tgb.domain.User;
import cn.tgb.service.UserService; public class TestApp {
public static void main(String[] args) { //1.创建建用户
User user = new User();
user.setUsername("jiangxiao");
user.setPassword("123"); //2.获取Sping配置文件
String xmlPathString="applicationContext.xml";
ApplicationContext applicationContext= new ClassPathXmlApplicationContext(xmlPathString); //3.通过Ioc获得UserService
UserService userService = (UserService) applicationContext.getBean("userService"); //4.运行加入用户操作
userService.addUser(user);
}
}
分析总结:
这张思维导图大概介绍了一下配置文件的配置内容和载入过程,当中蓝色的云朵部分是Spring整合Hibernate的内容。
程序执行时。通过载入配置文件,便将程序中全部的配置信息都载入了。假设有个扩展改动什么的也方便。
日后会继续介绍它们两个与Struts的整合。
sh_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。。。。
环境搭建,在eclipse中导入spring和hibernate框架的插件,和导入所有使用到的架包 首先,hibernate的创建: 建立两个封装类,其中封装了数据库中表的属性,这儿只写属性,gett ...
- 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使 ...
- Spring学习7-Spring整合Hibernate
一.Springl为什么要整合Hibernate 二者的整合主要是把hibernate中核心的一些类型交给spring管理,这些类型主要包括sessionFactory. transactionM ...
随机推荐
- canvas 动画库 CreateJs 之 EaselJS(上篇)
本文来自网易云社区 作者:田亚楠 须知 本文主要是根据 createjs 中的 EaselJS 在 github 上的 tutorials 目录下的文章整理而来 (原文链接),同时也包含了很多本人的理 ...
- C++ STL 的初步认知
学无止境!!! 尊重他人劳动,尊重出处:http://www.cnblogs.com/shiyangxt/archive/2008/09/11/1289493.html 我已经做了4年的MFC ...
- day04_07 while循环01
while循环结构: #while 条件: print("any") print("any") 死循环案例 num = 1 while num<=10 : ...
- postgres 用户管理
首次安装完成 pg 数据库后,会默认自带一个用户, 用户名: postgres 密码: postgres 可以使用命令 \du 查看数据库用户 创建新用户: create user dev with ...
- [转]how to inserting multiple rows in one step
To insert multiple rows in the table use executemany() method of cursor object. Syntax: cursor_objec ...
- 开发者选择短视频SDK,为何青睐七牛云?
从文字到图片再到视频的互联网内容媒介发展途径,随着 5g 技术的逐渐落地愈发清晰.短视频市场中的角力也随着诸多资本和创业者的涌入,进入到白热化阶段.这样的情况下,选择合适的短视频SDK产品就显得尤为重 ...
- 【bzoj3170】[Tjoi 2013]松鼠聚会 旋转坐标系
题目描述 有N个小松鼠,它们的家用一个点x,y表示,两个点的距离定义为:点(x,y)和它周围的8个点即上下左右四个点和对角的四个点,距离为1.现在N个松鼠要走到一个松鼠家去,求走过的最短距离. 输入 ...
- 刷题总结——拆网线(noip模拟 贪心)
题目: 给定一颗树··在保证有k个点与其它点连接的情况下问最少保留多少条边···· 树的节点树n和k均小于100000: 题解: 很容易看出来我们要尽量保留那种一条边连两个节点的情况···· 然后考试 ...
- 归并排序,时间复杂度nlogn
思路: /* 考点: 1. 快慢指针:2. 归并排序. 此题经典,需要消化吸收. 复杂度分析: T(n) 拆分 n/2, 归并 n/2 ...
- Python基础教程总结(一)
引言: 一直都听说Python很强大,以前只是浏览了一些博客,发现有点像数学建模时使用的Matlab,就没有深入去了解了.如今Python使用的地方越来越多,最近又在学习机器学习方面的知识,因此想系统 ...