spring+hibernate:整合

步骤1:引入类包

如下图:这里是所有的类包,为后面的struts整合考虑

步骤2:修改web.xml

在web.xml中加入下面的配置

  <context-param><param-name>contextConfigLocation</param-name><param-value>classpath:beans.xml</param-value>//这个beans.xml是在src目录下建立的文件,具体下面会陈述</context-param><!-- 对Spring容器进行实例化 --><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><filter><filter-name>OpenSessionInViewFilter</filter-name><filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class></filter><filter-mapping><filter-name>OpenSessionInViewFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping>

步骤3:建立实体类并建立hibernate的配置文件

package com.test.model;public class user {private int id;private String username;private String password;private String age;public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public String getAge() {return age;}public void setAge(String age) {this.age = age;}public void setUsername(String username) {this.username = username;}public String getUsername() {return username;}public void setId(int id) {this.id = id;}public int getId() {return id;}
}

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC"-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.test.model"><class name="user" table="[User]"><cache usage="read-write" region="com.test.model.user"/><id name="id"><generator class="identity"/></id><property name="username"  column="username" not-null="true"/><property name="password"   column="password"  not-null="true"/><property name="age"        column="age" not-null="false"/></class>
</hibernate-mapping>

步骤4:建立beans.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/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"><context:annotation-config/><bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"><property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/><property name="url" value="jdbc:sqlserver://127.0.0.1:1433;databaseName=Hibernate"/>   数据库链接<property name="username" value="sa"/><property name="password" value="123456"/><!-- 连接池启动时的初始值 --><property name="initialSize" value="1"/><!-- 连接池的最大值 --><property name="maxActive" value="500"/><!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 --><property name="maxIdle" value="2"/><!--  最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 --><property name="minIdle" value="1"/></bean><bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"><property name="dataSource" ref="dataSource"/><property name="mappingResources"><list><value>com/test/model/user.hbm.xml</value>    //这个是实体类的hibernate配置文件</list></property><property name="hibernateProperties"><value>hibernate.dialect=org.hibernate.dialect.SQLServerDialecthibernate.hbm2ddl.auto=updatehibernate.show_sql=falsehibernate.format_sql=falsehibernate.cache.use_second_level_cache=truehibernate.cache.use_query_cache=falsehibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider</value></property></bean><bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"><property name="sessionFactory" ref="sessionFactory"/></bean><tx:annotation-driven transaction-manager="txManager"/><bean id="userService" class="com.test.implement.userServiceBean"/>   /</beans>

步骤5:定义一个userService接口,并实现它。

package com.test.Interface;import java.util.List;import com.test.model.user;public interface userService {public    List<user> getAll();}

package com.test.implement;import java.util.List;import javax.annotation.Resource;import org.hibernate.SessionFactory;
import org.springframework.transaction.annotation.Transactional;import com.test.Interface.userService;
import com.test.model.user;public class userServiceBean implements userService{@Resource private SessionFactory sessionFactory;@Transactional    public List<user> getAll() {return sessionFactory.getCurrentSession().createQuery("from user").list();}}

步骤6:单元测试

package com.test.test;import java.util.List;import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;import com.test.Interface.userService;
import com.test.model.user;import junit.framework.TestCase;public class userTest {private static userService userservice;@BeforeClasspublic static void setUpBeforeClass() throws Exception {ApplicationContext ac=new ClassPathXmlApplicationContext("beans.xml");userservice=(userService) ac.getBean("userService");}@Testpublic void testGetAll(){List<user>    users=userservice.getAll();for (user item : users) {System.out.print("Id:"+item.getId()+"\t");System.out.print("username:"+item.getUsername()+"\t");}}}

错误1:

org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here

原因:在实现的地方,没有加入事务

解决办法:在对应方法上面加@Transactional

到此,spring和hibernate整合成功

转载于:https://www.cnblogs.com/shenghaishiweini/p/3331593.html

spring+hibernate+struts整合(1)相关推荐

  1. 小结spring和struts整合的三类方式

    小结spring和struts整合的三类方式 整合spring和hibernate的三种方式,小结之. 1) 在struts中使用webapplicationcontext调用spring     声 ...

  2. Struts2+Spring+Hibernate的整合

    整体程序结构 1.maven依赖 <!--实现Struts2+Spring+Hibernate的整合 --><dependencies><!--Spring --> ...

  3. hibernate版本_基于jsp+mysql+Spring+hibernate+Struts 2的SSH在线蛋糕销售网站平台管理系统...

    运行环境: 最好是java jdk 1.8,我们在这个平台上运行的.其他版本理论上也可以.IDE环境: Eclipse,Myeclipse,IDEA都可以tomcat环境: Tomcat 7.x,8. ...

  4. 在MyEclipse中如何查看Spring/Hibernate/Struts/JDK等源码的方法

    在MyEclipse中开发,习惯于点击类名,按Ctrl键查看源码 但是,如果是Spring/Hibernate/Struts/JDK这些开源jar的源码该如何看呢? 一般,我们导入的只有jar文件,所 ...

  5. Spring、Struts整合

    正式整合Spring.Struts2之前,咱们先来聊聊看看它俩框架有哪些差异,差异肯定就是决定解决手段的,所以有必要了解它们俩的差异: 众所周知,Struts2可以看作是MVC设计模式中C角色一个比较 ...

  6. struts2+spring+hibernate框架整合与项目

    嗯,其实一两周前都写好了,可一直懒得发,今天终于不懒一会,发一下.内容很清楚,主要是搭建框架的过程还有我写项目中遇到的许多问题.鉴于太多了,所以懒惰的我直接发的我参考的那些作者的链接,大家可以看一看. ...

  7. Spring+hibernate+struts

    一.Spring 主要功能:解耦和(对象之间可配置,依赖注入的) 1.概念: 容器:容器可以装载对象,实例化对象,配置对象之间的依赖关系. IOC/DI IOC:Inversion of Contro ...

  8. Spring+Hibernate+Struts2整合所需要的Jar包

    struts2.1.6 支持jar包 xwork-2.1.2.jar struts2-core-2.1.6.jar commons-logging-1.0.4.jar freemarker-2.3.1 ...

  9. spring+hibernate+Struts2 整合(全注解及注意事项)

    最近帮同学做毕设,一个物流管理系统,一个点餐系统,用注解开发起来还是很快的,就是刚开始搭环境费了点事,今天把物流管理系统的一部分跟环境都贴出来,有什么不足的,请大神不吝赐教. 1.结构如下 2.jar ...

最新文章

  1. 算法笔记_218:花朵数(Java)
  2. win11 WSL Ubuntu更换为清华源
  3. Syntax error on tokens, delete these tokens
  4. 一加8 Lite或将搭载联发科天玑1000:支持SA、NSA双模5G
  5. 50-20-010-kafka 配置-Listeners
  6. Spring Data Jpa的@DynamicInsert注解和@DynamicUpdate注解
  7. # Please enter the commit message for your changes. Lines starting # with ‘#‘ will be ignored
  8. SQL Server 和 Oracle 的常用函数对比
  9. 阿里云 cGPU 容器技术白皮书
  10. SetupParameter(mil)
  11. Django实现web端tailf日志文件
  12. 软件测试书单/书籍推荐(整理更新中)
  13. 通过微软官方工具卸载office
  14. 如何确定硕士毕业论文选题?
  15. vuex获取php数据,vue+vuex+axio从后台获取数据存入vuex实现组件之间共享数据
  16. 硬件中控一键开关机设计方案
  17. 【债券量化策略研究系列】债券风险测度指标:久期(Duration)与凸度(Convexity)
  18. i78565U和i710510U的区别 i78565U和i710510U参数配置对比哪个好
  19. 查找相交链表相交节点
  20. 20个免费下载PSD设计网站

热门文章

  1. 远程桌面上的文件复制到本地
  2. SQLyog连接虚拟机中mysql8.0详解,2003、1130、2058错误码解决
  3. java set path_Java Path.setEffect方法代码示例
  4. graphpad图片怎么导出矢量图_CorelDRAW导出的图片颜色失真怎么办
  5. jsp页面其本质就是一个servlet
  6. 如何快速学会嵌入式?
  7. 算术运算符、数学函数Math、数据类型转换、自增自减运算符、关系逻辑运算符、位运算符、括号及运算符级别
  8. 操作系统原理: 操作系统概述
  9. OS / 5 种 IO 模型
  10. 求最大素数的c语言,for语句计算输出10000以内最大素数怎么搞最简单??各位大神们...