- 浏览: 1089849 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
skyesx:
这是2PC实现,更常用的是一个柔性事务的实现,可以参考http ...
Spring分布式事务实现 -
ddbird:
这第一句就不严谨“分布式事务是指操作多个数据库之间的事务”,显 ...
Spring分布式事务实现 -
呵呵6666:
基于互联网支付系统的微服务架构分布式事务解决方案http:// ...
Spring分布式事务实现 -
小黄牛:
写得不错,交流群:472213887
Spring分布式事务实现 -
jiaoqf321456:
这明明是用的apache的压缩,给ant.jar有半毛钱关系吗 ...
使用ant.jar进行文件zip压缩
<1>persistence.xml放到类路径下的META-INF下面
<2>applicationContext.xml配置
<3>web.xml文件
<4>泛型dao定义
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0"> <!--事务类型:local的还是global(JTA)的事务 --> <persistence-unit name="jpa_test" transaction-type="RESOURCE_LOCAL"> </persistence-unit> </persistence>
<2>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: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-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd"> <!-- 自动装配注解Bean后置处理器 --> <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/> <!-- JPA注解Bean后置处理器 --> <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" /> <!-- 利用Spring的实体管理器工厂来创建JPA实体管理器 --> <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> <property name="database" value="MYSQL" /> <property name="showSql" value="true" /> <!-- <property name="generateDdl" value="true" /> --> </bean> </property> </bean> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=UTF-8" /> <property name="username" value="root" /> <property name="password" value="root" /> </bean> <!-- <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName"> <value>java:comp/env/jdbc/jpa_ds</value> </property> </bean> --> <!-- 声明一个Spring提供的JPA事务管理器,传入的参数是Spring中的实体管理器工厂 --> <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="entityManagerFactory" /> </bean> <!-- 开启Spring提供的基于注解的声明式事务管理 --> <tx:annotation-driven transaction-manager="transactionManager" /> <!-- <bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor"> <property name="transactionManager"> <ref local="transactionManager"/> </property> <property name="transactionAttributes"> <props> <prop key="insert*">PROPAGATION_REQUIRED</prop> <prop key="update*">PROPAGATION_REQUIRED</prop> <prop key="save*">PROPAGATION_REQUIRED</prop> <prop key="add*">PROPAGATION_REQUIRED</prop> <prop key="update*">PROPAGATION_REQUIRED</prop> <prop key="remove*">PROPAGATION_REQUIRED</prop> <prop key="delete*">PROPAGATION_REQUIRED</prop> <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop> <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop> <prop key="load*">PROPAGATION_REQUIRED,readOnly</prop> <prop key="change*">PROPAGATION_REQUIRED</prop> <prop key="*">PROPAGATION_REQUIRED,readOnly</prop> </props> </property> </bean> <bean id="beanNameAutoProxyCreator" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"> <property name="beanNames"> <list> <value>*Service</value> <value>*Dao</value> </list> </property> <property name="interceptorNames"> <list> <value>transactionInterceptor</value> </list> </property> </bean> --> <!-- 直接使用Spring的 JpaTemplate --> <bean id="jpaTemplate" class="org.springframework.orm.jpa.JpaTemplate"> <property name="entityManagerFactory" ref="entityManagerFactory" /> </bean> <bean id="personDao" class="quickstart.dao.impl.PersonDaoImpl" autowire="byName"/> <bean id="peopleService" class="quickstart.service.impl.PeopleServiceImpl"/> </beans>
<3>web.xml文件
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>jpa_test</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:applicationContext.xml </param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <!-- 配置字符编码过滤器 --> <filter> <filter-name>encoding</filter-name> <filter-class> org.springframework.web.filter.CharacterEncodingFilter </filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
<4>泛型dao定义
public interface GenericDao<T, ID extends Serializable> { public T save(T entity); public T update(T entity); public Integer updateBySql(final String sql); public void delete(T entity); public T findById(ID id); public List findByJPQL(String jpql); public List findBySQL(String sql); public List findAll(); public int findRowCount(); }
public class GenericDaoImpl<T, ID extends Serializable> extends JpaDaoSupport implements GenericDao<T, ID> { private Class persistentClass; public GenericDaoImpl() { this.persistentClass = (Class) ((ParameterizedType) getClass() .getGenericSuperclass()).getActualTypeArguments()[0]; } public Class getPersistentClass() { return persistentClass; } public T save(Object entity) { getJpaTemplate().persist(entity); return (T)entity; } public T update(Object entity) { getJpaTemplate().merge(entity); return (T)entity; } public Integer updateBySql(final String sql){ return (Integer)getJpaTemplate().execute(new JpaCallback(){ public Integer doInJpa(EntityManager em) throws PersistenceException{ return em.createNativeQuery(sql).executeUpdate(); } }); } public void delete(Object entity) { getJpaTemplate().remove(entity); } public T findById(Serializable id) { return (T)getJpaTemplate().find(this.getPersistentClass(), id); } //要立即抓取时用"JOIN FETCH" public List findByJPQL(String jpql) { return getJpaTemplate().find(jpql); } /** * 返回:list中装入的是对象数组(Object[]) */ public List findBySQL(final String sql) { return getJpaTemplate().executeFind(new JpaCallback(){ public List doInJpa(EntityManager em) throws PersistenceException{ return em.createNativeQuery(sql).getResultList(); } }); } public List findAll() { return getJpaTemplate().executeFind(new JpaCallback() { public Object doInJpa(EntityManager em) throws PersistenceException { StringBuffer jpql = new StringBuffer("from "); jpql.append(getPersistentClass().getName()); jpql.append(" obj"); return em.createQuery(jpql.toString()).getResultList(); } }); } public int findRowCount() { return ((Long) getJpaTemplate().execute(new JpaCallback() { public Object doInJpa(EntityManager em) throws PersistenceException { StringBuffer strBuff = new StringBuffer("select count(*) from "); strBuff.append(getPersistentClass().getName()); return em.createQuery(strBuff.toString()).getResultList().get(0); } })).intValue(); } }
- lib.rar (9.7 MB)
- 描述: 库文件
- 下载次数: 610
- jpa_test.rar (13.8 KB)
- 描述: 例子
- 下载次数: 712
发表评论
-
SpringBoot开发WebService之Axis
2019-07-14 23:56 4663一、服务器端发布WebService服务 1、POM.xml文 ... -
SpringBoot开发WebService之CXF
2019-07-14 23:56 1222一、在服务器端的WebSerivce服务发布 1、POM.xm ... -
SpringBoot项目非web方式启动
2019-07-03 17:02 47071、springboot 1.x中以非web方式启动 @S ... -
SpringBoot使用Druid数据库密码加密
2019-03-06 23:28 14441、生成公钥、私钥和密码加密串 java -cp drui ... -
Spring Annotation
2010-12-02 17:14 0Spring2.x引入了组件自动扫描机制,可以在类路径底 ... -
Spring分布式事务实现
2010-11-10 14:28 83145分布式事务是指操作多个数据库之间的事务,spring的 ... -
Spring3 Annotation + Hibernate3-jpa2.0 + CGLIB + 多数据源
2010-08-19 09:30 10484一、定义一个测试用Entity。 @Entity pub ... -
使用iBatis2.0
2010-05-26 10:20 0一、NULL问题 ibatis操作oracle数据库时, ... -
使用AspectJ LTW(Load Time Weaving)
2010-01-04 14:25 10727在Java 语言中,从 ... -
Spring2.0 AOP AspectJ 注释实现
2010-01-04 14:24 5549一、AOP基本概念 切面(Aspect): 一个关注点的模块 ... -
配置spring数据源
2009-11-06 16:47 1216配置一个数据源 Spring在第三方依赖包中包含了两 ... -
hibernate的dialect
2009-07-23 10:04 5410一、hibernate的dialect RDBM ... -
spring ibatis入门
2009-04-20 14:16 3848一、applicationContext.xml <?x ... -
Hibernate缓存配置/批量处理
2009-03-25 21:50 10915Hibernate除了自动对Se ... -
Hibernate的一级与二级缓存
2009-03-25 21:24 1677缓存是介于应用程序和物理数据源之间,其作用是为了降低应用 ... -
spring jdbcTemplate使用
2008-07-15 17:17 78168一、使用示例 (1)springJdbcContext.xml ... -
Spring2.X以AspectJ 式AOP 配置事务
2008-07-10 13:23 2041(1)配置: Spring的事务管理是通过AOP代理实 ... -
spring 事务管理
2008-07-08 16:35 11927声明式的事务管理(Declarative transactio ... -
Hibernate中one-to-many/many-to-one和many-to-many
2008-06-28 17:03 3939<1>one-to-many/many-to-on ... -
Hibernate中的对象one-to-one关系
2008-06-26 22:55 2448(1) 通过主健参考,限制2个数据表中的主健使用相同的值 c ...
相关推荐
Maven整合Spring+SpringMVC+Hibernate+SpringDataJPA
spring4.2+spring mvc +spring data+jpa+hibernate的程序构架
JSF+Spring+JPA(Hibernate实现)的环境搭建.doc JSF+Spring+JPA(Hibernate实现)的环境搭建.doc
spring4.0.1 springMVC jpa2.0 hibernate4.3.7 框架整合
spring+springmvc+hibernate+jpa环境搭建
Struts2+Spring+JPA(Hibernate)项目所需要的jar包
JSF+Spring+JPA_Hibernate实现_的环境搭建.pdf
免费的Spring++JPA+Hibernate搭建,手把手教你如何搭建SpringMVC+Hibernate框架
Spring+SpringMVC+SpringData+JPA+hibernate+shiro完美整合,你需要的包都在这了
SpringMvc+Spring+JPA+Hibernate实现的增删改查 采用idea和eclipse两种方式
包含构建spring+springmvc+jpa(hibernate)架构框架的所需文件
该源码包含Spring整合Jersey,接管Jpa的实现,能够实现与前端页面的交互,可以实现Jersey单元测试。
JSF+Spring+JPA以我个人看来,应该说是Struts2+Spring+Hibernate的替代解决方案。 引入JPA去取代或者说包装或者说是升级Hibernate是为了符合JAVA EE的规范,达到ORM统一的结果。下次项目用EJB也好、用TOPLINK也好、...
NULL 博文链接:https://prowl.iteye.com/blog/519618
这是jsf+spring2.5+jpa(hibernate)的jar包,很多人为了jsj环境而配置半天,在此提供jar包共享。注:除了ajax4jsf和tomahawk-1.1.3.jar,因为csdn只让我上传20mb,大家自己可以下一下自己试试。
基础框架,可满足开发需要
实现了spring3+springmvc+jpa2.0+hibernate之间的集成,框架搭建。项目可以直接在tomcat中运行