SPRING+JPA+Hibernate配置方法

1.applicationContext.xml

 1 <?xml version="1.0"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
 4     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
 5     xsi:schemaLocation="http://www.springframework.org/schema/beans
 6                         http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
 7                         http://www.springframework.org/schema/context
 8                         http://www.springframework.org/schema/context/spring-context-4.1.xsd
10                         http://www.springframework.org/schema/tx
11                         http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">
12
13     <!-- 扫描目录 -->
14     <context:component-scan base-package="xxx"></context:component-scan>
15     <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
16     <context:property-placeholder location="classpath:jdbc.properties"  ignore-unresolvable="true"/>
17
18     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
19         destroy-method="close">
20         <property name="driverClass" value="${jdbc.driver}" />
21         <property name="jdbcUrl" value="${jdbc.url}" />
22         <property name="user" value="${jdbc.username}" />
23         <property name="password" value="${jdbc.password}" />
24         <property name="initialPoolSize" value="${connection_pools.initial_pool_size}" />
25         <property name="minPoolSize" value="${connection_pools.min_pool_size}" />
26         <property name="maxPoolSize" value="${connection_pools.max_pool_size}" />
27         <property name="maxIdleTime" value="${connection_pools.max_idle_time}" />
28         <property name="acquireIncrement" value="${connection_pools.acquire_increment}" />
29         <property name="checkoutTimeout" value="${connection_pools.checkout_timeout}" />
30     </bean>
31
32     <bean id="entityManagerFactory"
33         class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
34         <property name="dataSource" ref="dataSource" />
35         <property name="packagesToScan" value="xxx.entity" />
36         <property name="persistenceProvider">
37             <bean class="org.hibernate.jpa.HibernatePersistenceProvider" />
38         </property>
39         <property name="jpaVendorAdapter">
40             <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
41                 <property name="generateDdl" value="false" />
42             </bean>
43         </property>
44         <property name="jpaProperties">
45             <props>
46                 <prop key="hibernate.dialect">${hibernate.dialect}</prop>
47                 <prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop>
48                 <prop key="hibernate.cache.use_second_level_cache">${hibernate.cache.use_second_level_cache}</prop>
49                 <prop key="hibernate.cache.region.factory_class">${hibernate.cache.region.factory_class}</prop>
50                 <prop key="hibernate.cache.use_query_cache">${hibernate.cache.use_query_cache}</prop>
51                 <prop key="hibernate.jdbc.fetch_size">${hibernate.jdbc.fetch_size}</prop>
52                 <prop key="hibernate.jdbc.batch_size">${hibernate.jdbc.batch_size}</prop>
53                 <prop key="hibernate.hbm2ddl.auto">update</prop>
54                 <prop key="hibernate.show_sql">true</prop>
55                 <prop key="hibernate.format_sql">false</prop>
56                 <prop key="hibernate.use_sql_comments">false</prop>
57                 <prop key="hibernate.connection.isolation">2</prop>
58                 <prop key="javax.persistence.validation.mode">none</prop>
59             </props>
60         </property>
61     </bean>
62
63     <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
64         <property name="entityManagerFactory" ref="entityManagerFactory" />
65     </bean>
66
67
68     <!-- 配置 Annotation 驱动,定义事务 -->
69     <tx:annotation-driven transaction-manager="transactionManager"
70         proxy-target-class="false" />
71
72     <!-- 配置事物自动提交 -->
73
74 </beans>

2. BaseDao

1 public interface BaseDao<T, ID extends Serializable> {
2 /**
3 省略
4 */
5 }

3. BaseDaoImpl

 1 import java.io.Serializable;
 2 import java.lang.reflect.ParameterizedType;
 3 import java.lang.reflect.Type;
 4
 5 import javax.persistence.EntityManager;
 6 import javax.persistence.FlushModeType;
 7 import javax.persistence.LockModeType;
 8 import javax.persistence.PersistenceContext;
 9
10 import javax.transaction.Transactional;
11
12 import org.apache.commons.lang.StringUtils;
13 import org.springframework.stereotype.Repository;
14 import org.springframework.util.Assert;
15
16 @Transactional
17 @Repository
18 public abstract class BaseDaoImpl<T, ID extends Serializable> implements BaseDao<T, ID> {
19
20     /** 实体类类型 */
21     private Class<T> entityClass;
22
23
24     @PersistenceContext
25     protected EntityManager entityManager;
26
27
28     @SuppressWarnings("unchecked")
29     public BaseDaoImpl() {
30         Type type = getClass().getGenericSuperclass();
31         Type[] parameterizedType = ((ParameterizedType) type).getActualTypeArguments();
32         entityClass = (Class<T>) parameterizedType[0];
33     }
34
35     public T find(ID id) {
36         if (id != null) {
37             return entityManager.find(entityClass, id);
38         }
39         return null;
40     }
41
42
43 public List<T> findList(Integer first, Integer count, List<Filter> filters, List<Order> orders) {
44         CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
45         CriteriaQuery<T> criteriaQuery = criteriaBuilder.createQuery(entityClass);
46         criteriaQuery.select(criteriaQuery.from(entityClass));
47         return findList(criteriaQuery, first, count, filters, orders);
48     }
49
50     public Page<T> findPage(Pageable pageable) {
51         CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
52         CriteriaQuery<T> criteriaQuery = criteriaBuilder.createQuery(entityClass);
53         criteriaQuery.select(criteriaQuery.from(entityClass));
54         return findPage(criteriaQuery, pageable);
55     }
56
57
58 public void persist(T entity) {
59         Assert.notNull(entity);
60
61         entityManager.persist(entity);
62     }
63
64     public T merge(T entity) {
65         Assert.notNull(entity);
66         return entityManager.merge(entity);
67     }
68
69     public void remove(T entity) {
70         if (entity != null) {
71             entityManager.remove(entity);
72         }
73     }
74
75     public void refresh(T entity) {
76         if (entity != null) {
77             entityManager.refresh(entity);
78         }
79     }
80 /**81 省略
82 */83 }

4 Common Dao

1 import org.springframework.stereotype.Repository;
2
3
4 @Repository
5 public class EmployeeDAO extends BaseDaoImpl<EmployeeEntity, Long>  {
6  public void save(EmployeeEntity entity){
7      persist(entity);
8  }
9 }

5 Entity

 1 import javax.persistence.Column;
 2 import javax.persistence.Entity;
 3 import javax.persistence.Table;
 4 import javax.persistence.GeneratedValue;
 5 import javax.persistence.Id;
 6
 7 @Entity
 8 @Table(name = "employee")
 9 public class EmployeeEntity  {
10
11 @Id
12 @GeneratedValue(strategy = GenerationType.AUTO)
13 @Column(name="staffid")
14 private Long id;
15
16 /**
17 省略
18 */
19 }
时间: 2024-10-30 21:56:16

SPRING+JPA+Hibernate配置方法的相关文章

【JPA】两种不同的实现jpa的配置方法

两种不同的实现jpa的配置方法 第一种: com.mchange.v2.c3p0.ComboPooledDataSource datasource.connection.driver_class=com.mysql.jdbc.Driver <!-- Where to find repositories --> <jpa:repositories base-package="org.springframework.data.jpa.example.repository.simpl

springmvc+spring+jpa(hibernate)+redis+maven配置

废话不多少 项目结构 pom.xml配置例如以下 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd

spring + springMVC +hibernate 配置2

这种方式比较精简 配置项不多 spring采用自动扫描配置 ,分spring_springMVC.xml  . hibernate_config.xml 两个文件 web.xml配置如下 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.o

Spring+Hibernate整合配置 --- 比较完整的spring、hibernate 配置

Spring+Hibernate整合配置 分类: J2EE2010-11-25 17:21 16667人阅读 评论(1) 收藏 举报 springhibernateclassactionservletmysql 在公司一直没有什么机会直接折腾SSH“原生态”的SSH当今比较流行的轻量级的框架,用着公司的框架也是郁闷异常,今天没事整整原来用过的一个项目的配置,发现就算是自己曾经用过的东西,如果较长时间不返过去重新学习,许多你半熟不熟的知识就是异常陌生.下面贴上我的一些配置,暂且权当备份吧. web

使用Spring为Hibernate配置声明式事物

从AOP的角度看,事物跟日志一样,都是跟业务逻辑无关的东西,这两个东西通过切入方式,放到系统中,是非常合适的.下面,将Hibernate中的事物配置到Spring中,使我们在编程的时候,无需考虑事物的存在,专心的放到系统的逻辑实现上. 在Spring的配置文件中加入如下配置: <!-- 配置sessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate

Spring 集成mybatis 配置方法

<?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/sch

Spring整合Hibernate配置

<?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:p="http://www.springframework.org/schema/p&

Spring AOP—注解配置方法的使用

Spring除了支持Schema方式配置AOP,还支持注解方式:使用@AspectJ风格的切面声明. 1 启用对@AspectJ的支持 Spring默认不支持@AspectJ风格的切面声明,为了支持需要使用如下配置: 这样Spring就能发现@AspectJ风格的切面并且将切面应用到目标对象. 2 声明切面 @AspectJ风格的声明切面非常简单,使用@Aspect注解进行声明: 然后将该切面在配置文件中声明为Bean后,Spring就能自动识别并进行AOP方面的配置: 该切面就是一个POJO,

Spring中Hibernate配置笔记

ApplicatonContext.xml中的配置: <jee:jndi-lookup id="dataSource" jndi-name="java:comp/env/global/ds" /> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <proper