model:
package com.lzj.www.model; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import org.hibernate.annotations.GenericGenerator; @Entity public class User { private Integer id; private String name; @Id @GenericGenerator(name="generator", strategy="increment") @GeneratedValue(generator="generator") 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; } }
service:
package com.lzj.www.service.impl; import org.springframework.orm.hibernate4.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); } }
xml文件:spring配置
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:cache="http://www.springframework.org/schema/cache" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.0.xsd"> <!-- 引入properties文件 --> <context:property-placeholder location="classpath*:/appConfig.properties" /> <!-- 定义数据库连接池数据源bean destroy-method="close"的作用是当数据库连接不使用的时候,就把该连接重新放到数据池中,方便下次使用调用 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <!-- 设置JDBC驱动名称 --> <property name="driverClass" value="${driver}" /> <!-- 设置JDBC连接URL --> <property name="jdbcUrl" value="${url}" /> <!-- 设置数据库用户名 --> <property name="user" value="${user}" /> <!-- 设置数据库密码 --> <property name="password" value="${password}" /> <!-- 设置连接池初始值 --> <property name="initialPoolSize" value="5" /> </bean> <!-- 配置sessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <!-- 数据源 --> <property name="dataSource" ref="dataSource" /> <!-- hibernate的相关属性配置 --> <property name="hibernateProperties"> <value> <!-- 设置数据库方言 --> hibernate.dialect=org.hibernate.dialect.MySQLDialect <!-- 设置自动创建|更新|验证数据库表结构 --> hibernate.hbm2ddl.auto=update <!-- 是否在控制台显示sql --> hibernate.show_sql=true <!-- 是否格式化sql,优化显示 --> hibernate.format_sql=true <!-- 是否开启二级缓存 --> hibernate.cache.use_second_level_cache=false <!-- 是否开启查询缓存 --> hibernate.cache.use_query_cache=false <!-- 数据库批量查询最大数 --> hibernate.jdbc.fetch_size=50 <!-- 数据库批量更新、添加、删除操作最大数 --> hibernate.jdbc.batch_size=50 <!-- 是否自动提交事务 --> hibernate.connection.autocommit=true <!-- 指定hibernate在何时释放JDBC连接 --> hibernate.connection.release_mode=auto <!-- 创建session方式 hibernate4.x 的方式 --> hibernate.current_session_context_class=org.springframework.orm.hibernate4.SpringSessionContext <!-- javax.persistence.validation.mode默认情况下是auto的,就是说如果不设置的话它是会自动去你的classpath下面找一个bean-validation**包 所以把它设置为none即可 --> javax.persistence.validation.mode=none </value> </property> <!-- 自动扫描实体对象 com.lzj.www.model包结构中存放实体类 --> <property name="packagesToScan" value="com.lzj.www.model" /> </bean> <!-- 定义事务管理 --> <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="create*" propagation="REQUIRED" /> <tx:method name="save*" propagation="REQUIRED" /> <tx:method name="add*" propagation="REQUIRED" /> <tx:method name="update*" propagation="REQUIRED" /> <tx:method name="remove*" propagation="REQUIRED" /> <tx:method name="del*" propagation="REQUIRED" /> <tx:method name="import*" propagation="REQUIRED" /> <!-- read-only="true" 表示只读 --> <tx:method name="*" propagation="NOT_SUPPORTED" read-only="true" /> </tx:attributes> </tx:advice> <!-- 定义切面,执行有关的hibernate session的事务操作 --> <aop:config> <aop:pointcut id="serviceOperation" expression="execution(* com.lzj.www.service.impl.*.*(..))" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation" /> </aop:config> <!-- 扫描有注解的文件 base-package 包路径 --> <context:component-scan base-package="com.lzj.www.model.User"/> <bean id="userService" class="com.lzj.www.service.impl.UserServiceImpl"> <property name="sessionFactory" ref="sessionFactory"/> </bean> </beans>
test:
package com.lzj.www.test; import org.junit.Test; import org.springframework.beans.factory.BeanFactory; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.lzj.www.model.User; import com.lzj.www.service.UserService; public class test { @Test public void test01(){ BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml"); UserService userService = (UserService)factory.getBean("userService"); User user = new User(); user.setName("name"); userService.addUser(user); } //用main的方法测试方便查错 public static void main(String[] args) { BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml"); UserService userService = (UserService)factory.getBean("userService"); User user = new User(); user.setName("name"); userService.addUser(user); } }
相关推荐
Spring4 MVC Hibernate4集成,使用Annotation,封装dao层和service层。 环境: spring 4.0.3.RELEASE hibernate 4.3.5.Final mysql 5.1.29
平台依赖度低的解决方案 ...测试环境:equinox3.5.2。其它用到的Bundle包括hibernate、hibernate-annotation、hsqldb、 spring、spring-osgi等。请到http://www.springsource.com/repository/下载。
基于 spring2.5 hibernate3.3 jpa 的annotation注解来减少配置文件的大小 通过jersey实现restful形式请求
jsf1.2+spring3.0+hibernate3.3+ajax annotation集成,自定义转换器,自定义验证器,国际化,验证码等包含users表的登录增删改查操作,dao层设计抽象范型统一的父类,非常有参考价值。 欢迎交流q 38732796
Spring3Annotation+Struts2Annotation+Hibernate3Annotation 全注解集成开发,操作Emp表
内置一个基于数据库的代码生成器rapid-generator,极易进行二次开发 struts1,struts2的零配置 spring集成及加强,自动搜索hibernate的entity annotation class 集成动态构造sql的工具:rapid-xsqlbuilder 集成...
JQuery1.4.2+Struts2.1.8+JSON0.34+Spring2.5.6+Hibernate3.5+XFire1.2.6整合实例(已上传) 1、JSON0.34使用的是struts2附带的struts2-json-plugin-2.1.8.1.jar 2、db是mysql,名字为test,用户名root,密码空 3、...
现在Annotation越来越流行,最近一段时间也学了一些,EJB3.0、Hibernate、Spring等都很好地支持Annotation,而且熟悉了Annotation的话整个项目开发时间会缩短,代码封装比较好,但是封装带来的就是代码阅读就比较...
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <!-- JTA环境的事务配置 <bean id="transactionManager" class="org.springframework....
基于SpringMVC+Hibernate4的考勤管理系统+.zip 项目环境 系统:Windows7 X64位系统 IDE:Intellij IDEA 14.0 后端技术选型 J D K 版 本:JDK 1.8 数 据 库:Mysql 5.7 WEB容器:Tomcat 7.0 视图框架:SpringMVC 4.3.6....
6.7. 以编程方式创建@AspectJ代理 6.8. 在Spring应用中使用AspectJ 6.8.1. 在Spring中使用AspectJ进行domain object的依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 ...
16.4.3 集成Hibernate 16.4.4 集成Dbunit 16.4.5 自定义扩展模块 16.5 使用Unitils测试DAO层 16.5.1 数据库测试的难点 16.5.2 扩展Dbunit用Excel准备数据 16.5.3 测试实战 16.6 使用unitils测试Service层 16.7 测试...
6.7. 以编程方式创建@AspectJ代理 6.8. 在Spring应用中使用AspectJ 6.8.1. 在Spring中使用AspectJ进行domain object的依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 ...
16.4.3 集成Hibernate 16.4.4 集成Dbunit 16.4.5 自定义扩展模块 16.5 使用Unitils测试DAO层 16.5.1 数据库测试的难点 16.5.2 扩展Dbunit用Excel准备数据 16.5.3 测试实战 16.6 使用unitils测试Service层 16.7 测试...
mybatis实战教程mybatis in action之五与spring3集成附源码 mybatis实战教程mybatis in action之六与Spring MVC 的集成 mybatis实战教程mybatis in action之七实现mybatis分页源码下载 mybatis实战教程mybatis in ...
6.4.1 spring 2.5集成orm中间件hibernate 3.2 404 6.4.2 spring 2.5集成mvc框架struts 2 411 6.4.3 ssh 2组合框架的基本开发步骤—eportal启程 414 6.5 小结 423 第2篇 实践篇 第7章 ssh 2热身——构建新闻发布...
一,集成 Spring 与 Hibernate 1,配置SessionFactory 1,配置 ---------------------- applicationContext.xml ------------------------ <!-- 配置SessionFactory(整合Hibernate) --> <context:...
Spring4GWT GWT Spring 使得在 Spring 框架下构造 GWT 应用变得很简单,提供一个易于理解的依赖注入和RPC机制。 Java扫雷游戏 JVMine JVMine用Applets开发的扫雷游戏,可在线玩。 public class JVMine extends java...
在工程目录下创建libs/hibernate/ ,放入 hibernate-tools-3.2.0.ga.jar jtidy-4aug2000r7-dev.jar build.properties project.name=sw project.version=1.0 basedir=. build.dir =${basedir}/build web.dir = ${...