model:
package com.lzj.www.model; public class User { private Integer id; private String name; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
model.hbm.xml:
<?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.lzj.www.model"> <class name="User" table="user"> <id name="id"> <generator class="increment"/> </id> <property name="name"/> </class> </hibernate-mapping>
service:
package com.lzj.www.service.impl; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import com.lzj.www.model.User; import com.lzj.www.service.UserService; public class UserServiceImpl extends HibernateDaoSupport implements UserService{ @Override public void addUser(User user) { this.getHibernateTemplate().save(user); } }
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> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost:3306/Spring</property> <property name="connection.username">root</property> <property name="connection.password">java_5937</property> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.hbm2ddl.auto">update</property> <property name="show_sql">true</property> <mapping resource="com/lzj/www/model/User.hbm.xml"/> </session-factory> </hibernate-configuration>
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: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/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="configLocation"> <value>classpath:hibernate.cfg.xml</value> </property> </bean> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory"> <ref bean="sessionFactory"/> </property> </bean> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="add*" propagation="REQUIRED"/> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut expression="execution(* com.lzj.www.service.impl.*.*(..))" id="pointcut"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"/> </aop:config> <!-- nullException --> <bean id="userService" class="com.lzj.www.service.impl.UserServiceImpl"> <property name="sessionFactory" ref="sessionFactory"/> </bean> </beans>
可能出现的错误:
1、nullpointException this.getHibernateTemplate()为null,原因是使用Template的对象是直接new出来的,spring并没有 对其进行注入,因此该对象是不具有sessionFactory的,也没有绑定相关的事务 所以要把这个对象配置到ioc容器中 2、java.lang.IllegalArgumentException Pointcut is not well-formed: expecting '(' at character position 0 在配置aop的advisor时,配置的是pointcut-ref而不是pointcut,用快捷键的时候小心点就是了
相关推荐
传播智客,为Spring集成的Hibernate配置二级缓存、cache.xml
1、 同时使用了Struts2、Spring4、Hibernate4、Quartz、JUnit4、Log4j、SLF4J、Ehcache、Gson等库或框架,搭建一个最基本的项目原型。 2、 Spring配置提供了多种可选整合方案:详见applicationContext.xml的配置 (1...
由于spring对hibernate配置文件hibernate.cfg.xml的集成相当好,可以在项目中放弃hibernate.cfg.xml文件,介绍mappingLocations、mappingDirectoryLocations与mappingJarLocations的用法和区别
该文档详细描述了SSH2三大主流框架的整合对于初学SSH开发的学友有一定的帮助希望大家喜欢
1.通过google ehcache-spring-annotatios.jar自动注解方式实现整合Spring+Ehcache。 2.Action里通过struts2-spring-plugin.jar插件自动根据名字注入。 3.Ajax无刷新异步调用Struts2,返回Json数据,以用户注册为例。...
//applicationContext.xml文件中添加 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi=...
使用了SpringMVC框架,Spring4的xml配置和注解配置。 5. 前台展示页面使用了JQuery EasyUI的布局、列表和消息组件。 6. 系统实现了较为完善的用户模块,对用户数据实现了增、删、改、查、分页等的功能。
Spring集成hibernate3和4有一定的区别,目前基本都在使用3,所以此处内容以3为基础; 1.导入hibernate的包和Spring的包 2.创建beans.xml ... 详情查看博客:http://blog.csdn.net/omsvip
spring+springMVC+hibernate集成,包括配置文件applicationContext.xml、web.xml、springMVC-servlet.xml
springmvc5+hibernate5+junit4+log4j2整合实例 包含 代码实例 数据库建表sql 运行环境 myeclipes2015 jdk 1.8 tomcat 8.0
由于spring3.x,基于asm的某些特征,而这些asm还没有用jdk8编译,所以采用Spring 3+JDK8就会报错,提示错误信息( java.lang.IllegalArgumentException),具体解决方案有:1、Spring 3+JDK7及以下版本 2、Spring 4+JDK8...
本系统使用Struts2.0 + Spring2.0 +Hibernate 3.2 (即:SSH)及以上版本架构整合模式开发 本系统作为学生学习SSH很有参考价值,实现了图书信息的添加,修改,删除,查询! 查询结果可以导出到excel文件! 这里是...
spring-oxm.jar:Spring对于object/xml映射的支持,可以让JAVA与XML之间来回切换 spring-messaging.jar: spring-test.jar:对JUNIT等测试框架的简单封装 spring-tx.jar:为JDBC、Hibernate、JDO、JPA等提供的一致的...
spring-expression-3.0.7.RELEASE.jar,spring-hibernate3.jar,spring-jms-3.0.7.RELEASE.jar,spring-tx-3.0.7.RELEASE.jar,spring-web-3.0.7.RELEASE.jar,sqljdbc4.jar,stax2-api-3.1.1.jar,velocity-1.7.jar,WHICH_...
简单的JSF+Hibernate+Spring集成项目 This project is very easy to understand JSF,Spring and Hibernate integration.项目结构 如果你想使用这个项目。你几乎不需要改变。 1.Go to WEB-INF/applicationContext.xml...
25个经典的Spring面试问答,Spring面试常见的问题与回答。 在准备Spring框架的面试时,应掌握的核心主题和概念包括...Spring与其它技术集成:了解Spring与其它技术的集成,例如与JPA、Hibernate、Thymeleaf等的集成。 S
Spring_IoC集成原理的应用设计.pdf Sturts2与以其它Web应用框架整合的研究.pdf 图书缺藏网站解决方案.pdf 基于Ajax技术即时通讯系统的设计与实现.pdf 基于Ajax技术的实时数据监测系统的研究与应用.pdf 基于DWR与JST...
SSH 为 struts2.4+spring3.1+hibernate4.1的一个集成框架,是目前较流行的一种Web应用程序开源框架。 集成SSH框架的系统从职责上分为四层:表示层、业务逻辑层、数据持久层和域模块层,以帮助开发人员在短期内搭建...
第12章:讲解了如何在Spring中集成Hibernate、myBatis等数据访问框架,同时,读者还将学习到ORM框架的混用和DAO层设计的知识。 第13章:本章重点对在Spring中如何使用Quartz进行任务调度进行了讲解,同时还涉及...