我看过了尚硅谷nio视频教程的hibernate教程 一开始就不停的报错

尚硅谷Spring整合Hibernate基于xml配置
时间: 15:14:51
&&&& 阅读:188
&&&& 评论:
&&&& 收藏:0
标签:&&&&&&&&&描述:这是一个最简单网上书城demo.
下载地址:
1. Spring 整合 Hibernate 整合什么 ?
1). 有 IOC 容器来管理 Hibernate 的 SessionFactory
2). 让 Hibernate 使用上 Spring 的声明式事务
2. 整合步骤:
1). 加入 hibernate
①. jar 包
②. 添加 hibernate 的配置文件: hibernate.cfg.xml
③. 编写了持久化类对应的 .hbm.xml 文件。
2). 加入 Spring
①. jar 包
②. 加入 Spring 的配置文件
3. 编写代码
项目结构:
BookShopDao代码:
package com.atguigu.spring.hibernate.
public interface BookShopDao {
//根据书号获取书的单价
public int findBookPriceByIsbn(String isbn);
//更新数的库存. 使书号对应的库存 - 1
public void updateBookStock(String isbn);
//更新用户的账户余额: 使 username 的 balance - price
public void updateUserAccount(String username, int price);
BookShopDaoImpl代码:
package com.atguigu.spring.hibernate.dao.
import org.hibernate.Q
import org.hibernate.S
import org.hibernate.SessionF
import org.springframework.beans.factory.annotation.A
import org.springframework.stereotype.R
import com.atguigu.spring.hibernate.dao.BookShopD
import com.atguigu.spring.hibernate.exceptions.BookStockE
import com.atguigu.spring.hibernate.exceptions.UserAccountE
@Repository
public class BookShopDaoImpl implements BookShopDao {
@Autowired
private SessionFactory sessionF
//不推荐使用 HibernateTemplate 和 HibernateDaoSupport
//因为这样会导致 Dao 和 Spring 的 API 进行耦合
//可以移植性变差
//private HibernateTemplate hibernateT
//获取和当前线程绑定的 Session.
private Session getSession(){
return sessionFactory.getCurrentSession();
public int findBookPriceByIsbn(String isbn) {
String hql = &SELECT b.price FROM Book b WHERE b.isbn = ?&;
Query query = getSession().createQuery(hql).setString(0, isbn);
return (Integer)query.uniqueResult();
public void updateBookStock(String isbn) {
//验证书的库存是否充足.
String hql2 = &SELECT b.stock FROM Book b WHERE b.isbn = ?&;
int stock = (int) getSession().createQuery(hql2).setString(0, isbn).uniqueResult();
if(stock == 0){
throw new BookStockException(&库存不足!&);
String hql = &UPDATE Book b SET b.stock = b.stock - 1 WHERE b.isbn = ?&;
getSession().createQuery(hql).setString(0, isbn).executeUpdate();
public void updateUserAccount(String username, int price) {
//验证余额是否足够
String hql2 = &SELECT a.balance FROM Account a WHERE a.username = ?&;
int balance = (int) getSession().createQuery(hql2).setString(0, username).uniqueResult();
if(balance & price){
throw new UserAccountException(&余额不足!&);
String hql = &UPDATE Account a SET a.balance = a.balance - ? WHERE a.username = ?&;
getSession().createQuery(hql).setInteger(0, price).setString(1, username).executeUpdate();
单元测试类SpringHibernateTest代码:
package com.atguigu.spring.hibernate.
import java.sql.SQLE
import java.util.A
import javax.sql.DataS
import org.junit.T
import org.springframework.context.ApplicationC
import org.springframework.context.support.ClassPathXmlApplicationC
import com.atguigu.spring.hibernate.service.BookShopS
import com.atguigu.spring.hibernate.service.C
public class SpringHibernateTest {
private ApplicationContext ctx =
private BookShopService bookShopService =
private Cashier cashier =
ctx = new ClassPathXmlApplicationContext(&applicationContext.xml&);
bookShopService = ctx.getBean(BookShopService.class);
cashier = ctx.getBean(Cashier.class);
public void testCashier(){
cashier.checkout(&aa&, Arrays.asList(&1001&,&1002&));
public void testBookShopService(){
bookShopService.purchase(&aa&, &1001&);
public void testDataSource() throws SQLException {
DataSource dataSource = ctx.getBean(DataSource.class);
System.out.println(dataSource.getConnection());
数据库配置db.properties代码:
jdbc.user=root
jdbc.password=1230
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql:///spring7
jdbc.initPoolSize=5
jdbc.maxPoolSize=10
hibernate.cfg.xml文件代码:
&?xml version=&1.0& encoding=&UTF-8&?&
&!DOCTYPE hibernate-configuration PUBLIC
&-//Hibernate/Hibernate Configuration DTD 3.0//EN&
&http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd&&
&hibernate-configuration&
&session-factory&
&!-- 配置 hibernate 的基本属性 --&
&!-- 1. 数据源需配置到 IOC 容器中, 所以在此处不再需要配置数据源 --&
&!-- 2. 关联的 .hbm.xml 也在 IOC 容器配置 SessionFactory 实例时在进行配置 --&
&!-- 3. 配置 hibernate 的基本属性: 方言, SQL 显示及格式化, 生成数据表的策略以及二级缓存等. --&
&property name=&hibernate.dialect&&org.hibernate.dialect.MySQL5InnoDBDialect&/property&
&property name=&hibernate.show_sql&&true&/property&
&property name=&hibernate.format_sql&&true&/property&
&property name=&hibernate.hbm2ddl.auto&&update&/property&
&!-- 配置 hibernate 二级缓存相关的属性. --&
&/session-factory&
&/hibernate-configuration&
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&
xmlns:aop=&http://www.springframework.org/schema/aop&
xsi:schemaLocation=&http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd&&
&!-- 配置自动扫描的包 --&
&context:component-scan base-package=&com.atguigu.spring.hibernate&&&/context:component-scan&
&!-- 导入资源文件 --&
&context:property-placeholder location=&classpath:db.properties&/&
&!-- 配置数据源 --&
&bean id=&dataSource& class=&com.mchange.v2.c3p0.ComboPooledDataSource&&
&property name=&user& value=&${jdbc.user}&&&/property&
&property name=&password& value=&${jdbc.password}&&&/property&
&property name=&driverClass& value=&${jdbc.driverClass}&&&/property&
&property name=&jdbcUrl& value=&${jdbc.jdbcUrl}&&&/property&
&property name=&initialPoolSize& value=&${jdbc.initPoolSize}&&&/property&
&property name=&maxPoolSize& value=&${jdbc.maxPoolSize}&&&/property&
&!-- 配置 Hibernate 的 SessionFactory 实例: 通过 Spring 提供的 LocalSessionFactoryBean 进行配置 --&
&bean id=&sessionFactory& class=&org.springframework.orm.hibernate4.LocalSessionFactoryBean&&
&!-- 配置数据源属性 --&
&property name=&dataSource& ref=&dataSource&&&/property&
&!-- 配置 hibernate 配置文件的位置及名称 --&
&property name=&configLocation& value=&classpath:hibernate.cfg.xml&&&/property&
&!-- 使用 hibernateProperties 属相来配置 Hibernate 原生的属性 --&
&property name=&hibernateProperties&&
&prop key=&hibernate.dialect&&org.hibernate.dialect.MySQL5InnoDBDialect&/prop&
&prop key=&hibernate.show_sql&&true&/prop&
&prop key=&hibernate.format_sql&&true&/prop&
&prop key=&hibernate.hbm2ddl.auto&&update&/prop&
&/property&
&!-- 配置 hibernate 映射文件的位置及名称, 可以使用通配符 --&
&property name=&mappingLocations&
value=&classpath:com/atguigu/spring/hibernate/entities/*.hbm.xml&&&/property&
&!-- 配置 Spring 的声明式事务 --&
&!-- 1. 配置事务管理器 --&
&bean id=&transactionManager& class=&org.springframework.orm.hibernate4.HibernateTransactionManager&&
&property name=&sessionFactory& ref=&sessionFactory&&&/property&
&!-- 2. 配置事务属性, 需要事务管理器 --&
&tx:advice id=&txAdvice& transaction-manager=&transactionManager&&
&tx:attributes&
&tx:method name=&get*& read-only=&true&/&
&tx:method name=&purchase& propagation=&REQUIRES_NEW&/&
&tx:method name=&*&/&
&/tx:attributes&
&/tx:advice&
&!-- 3. 配置事务切点, 并把切点和事务属性关联起来 --&
&aop:config&
&aop:pointcut expression=&execution(* com.atguigu.spring.hibernate.service.*.*(..))&
id=&txPointcut&/&
&aop:advisor advice-ref=&txAdvice& pointcut-ref=&txPointcut&/&
&/aop:config&
标签:&&&&&&&&&原文:http://blog.csdn.net/u/article/details/
教程昨日排行
&&国之画&&&& &&&&&&
&& &&&&&&&&&&&&&&
鲁ICP备号-4
打开技术之扣,分享程序人生!他的最新文章
他的热门文章
您举报文章:
举报原因:
原文地址:
原因补充:
(最多只允许输入30个字)只需一步, 快速开始
只需一步,快速开始
后使用快捷导航没有帐号?
& & & & 尚硅谷Hibernate
今日: 0|主题: 25|排名: 10&
Powered by轻松一扫,精彩不停
扫描下载APP
正在学习:
感谢你的评价,我们会继续努力!
恭喜,你已学完本节课程!
感谢你的评价,我们会继续努力!
写点什么吧,你的感受对其他同学有很大帮助噢
字数限250字以内
Java视频_Hibernate4 视频教程
1084人已学
Java视频_Struts2 视频教程
1031人已学
Java视频_SSH整合&综合案例
购买后可以学习整个课程
&课程信息&
课程价格:0.00元
购买人数:2398人
学生满意度:98.55%
直播倒计时:09小时52分15秒
上课时间:
线下课倒计时:09小时52分15秒
扫码下载APP
1. 尚硅谷_佟刚_Spring_HelloWorld
有一定编程基础的人员
Spring4.0是 Spring 推出的一个重大版本升级,进一步加强了 Spring 作为 Java 领域第一开源平台的地位。Spring4.0 引入了众多 Java 开发者期盼的新特性,如泛型依赖注入、SpEL、校验及格式化框架、Rest风格的 WEB 编程模型等。这些新功能实用性强、易用性高,可大幅降低 JavaEE 开发的难度,同时有效提升应用开发的优雅性。
  资深项目经理、技术讲师、SUN SCJP、SCWCD、原工信部移动云计算教育培训中心教学总监。十年软件开发经验: 参与完成辽宁某高校远程教学管理系统、慧文信息门户系统、日本麦卡尔超市管理系统、崎玉市外来人口登记系统、深海视频会议管理系统、仙台市宫城县日常事务系统等项目的设计和开发。
Spring4.0是 Spring 推出的一个重大版本升级,进一步加强了 Spring 作为 Java 领域第一开源平台的地位。Spring4.0 引入了众多 Java 开发者期盼的新特性,如泛型依赖注入、SpEL、校验及格式化框架、Rest风格的 WEB 编程模型等。这些新功能实用性强、易用性高,可大幅降低 JavaEE 开发的难度,同时有效提升应用开发的优雅性。
第1章本章的标题
1. 尚硅谷_佟刚_Spring_HelloWorld
2. 尚硅谷_佟刚_Spring_IOC&DI概述
3. 尚硅谷_佟刚_Spring_配置 Bean
4. 尚硅谷_佟刚_Spring_属性配置细节
5. 尚硅谷_佟刚_Spring_自动装配
6. 尚硅谷_佟刚_Spring_Bean 之间的关系
7. 尚硅谷_佟刚_Spring_Bean 的作用域
8. 尚硅谷_佟刚_Spring_使用外部属性文件
9. 尚硅谷_佟刚_Spring_SpEL
10. 尚硅谷_佟刚_Spring_管理 Bean 的生命周期
11. 尚硅谷_佟刚_Spring_通过工厂方法配置 Bean
12. 尚硅谷_佟刚_Spring_通过 FactoryBean 配置 Bean
13. 尚硅谷_佟刚_Spring_通过注解配置 Bean(1)
14. 尚硅谷_佟刚_Spring_通过注解配置 Bean(2)
15. 尚硅谷_佟刚_Spring_泛型依赖注入
16. 尚硅谷_佟刚_Spring_AOP 基础
17. 尚硅谷_佟刚_Spring_前置通知
18. 尚硅谷_佟刚_Spring_后置通知
19. 尚硅谷_佟刚_Spring_返回通知&异常通知&环绕通知
20. 尚硅谷_佟刚_Spring_切面的优先级
21. 尚硅谷_佟刚_Spring_重用切点表达式
22. 尚硅谷_佟刚_Spring_基于配置文件的方式来配置 AOP
23. 尚硅谷_佟刚_Spring_使用 JdbcTemplate和JdbcDaoSupport
24. 尚硅谷_佟刚_Spring_使用 NamedParameterJdbcTemplate
25. 尚硅谷_佟刚_Spring_事务准备
26. 尚硅谷_佟刚_Spring_声明式事务
27. 尚硅谷_佟刚_Spring_事务的传播行为
28. 尚硅谷_佟刚_Spring_事务其他属性(隔离级别&回滚&只读&过期)
29. 尚硅谷_佟刚_Spring_使用XML文件的方式配置事务
30. 尚硅谷_佟刚_Spring_整合 Hibernate 准备
31. 尚硅谷_佟刚_Spring_整合 Hibernate 配置
32. 尚硅谷_佟刚_Spring_整合 Hibernate 代码测试
33. 尚硅谷_整合 Hibernate 不适用 Hibernate 配置文件
34. 尚硅谷_佟刚_Spring_在 WEB 应用中使用 Spring 的基本思路
35. 尚硅谷_佟刚_Spring_在 WEB 应用中使用 Spring
36. 尚硅谷_佟刚_Spring_集成 Struts2
课程暂无资料
jackxo1981
字数限400字符内
字数限400字符内
字数限400字符内
BoboTreeXDS
字数限400字符内
萤火之光6613
字数限400字符内
字数限400字符内
阳明门下蝼蚁2
字数限400字符内
字数限400字符内
字数限400字符内
该机构的同类课程
1119 人已学
1084 人已学
1031 人已学
964 人已学
该机构的热门课程
2074 人已学
2030 人已学
1338 人已学
1119 人已学02.Spring整合Hibernate和生成数据表【尚硅谷SSH整合案例视频教程(基于Spring4,Struts2,Hibernate4)】
- 爱酷学习网,免费高清视频教程在线观看
02.Spring整合Hibernate和生成数据表
距离广告结束还有
SSH整合是学习JavaEE最基本的知识点,只有你整合了SSH才算是对JavaEE有了入门。当然,现在还流行着另外一个版本即SSM(Spring + SpringMVC + Mybatis),有兴趣的童鞋可以多多关注下SSM的整合,爱酷学习网所用到的基本技术也是基于SSM而成的。本视频所涉及框架版本有:Spring 4.0Struts 2.3.15Hibernate 4.2.4jQuery 1.9.1本视频涉并非简单的对SSH进行整合,而是涉及到了诸多开发时的细节:ModelDrivenPreparable 拦截器编写自定义的类型转换器Struts2 处理 AjaxOpenSessionInViewFilter迫切左外连接Spring 声明式事务Spring IOC 管理各个组件
Album Info
请输入正确的邮件(格式:xxx@xxx.xx)
必须填本条信息!

我要回帖

更多关于 尚硅谷jquery视频教程 的文章

 

随机推荐