spring整合jpa优化

本篇是针对上一篇《spring整合jpa》文章进行优化

1.1.  使用接口代替dao层

1.1.1.   删除IpersonDao和PersonDaoImpl

1.1.2.   新建PersonDao.java


PersonDao.java


package com.morris.dao;

import org.springframework.data.repository.Repository;

import com.morris.entity.Person;

public interface PersonDao extends Repository<Person, Integer>{

void save(Person person);

}

1.1.3.   测试结果


后台打印sql语句:


Hibernate: insert into Person (age, name) values (?, ?)

1.2.  去除persistence.xml

1.2.1.   删除META-INF和persistence.xml文件

1.2.2.   配置spring.xml


spring.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"

xmlns:p="http://www.springframework.org/schema/p"xmlns:cache="http://www.springframework.org/schema/cache"

xmlns:jpa="http://www.springframework.org/schema/data/jpa"

xsi:schemaLocation="http://www.springframework.org/schema/beans  

          http://www.springframework.org/schema/beans/spring-beans-3.1.xsd   

          http://www.springframework.org/schema/context  

          http://www.springframework.org/schema/context/spring-context-3.1.xsd  

          http://www.springframework.org/schema/aop  

          http://www.springframework.org/schema/aop/spring-aop-3.1.xsd  

          http://www.springframework.org/schema/tx   

          http://www.springframework.org/schema/tx/spring-tx-3.1.xsd

          http://www.springframework.org/schema/cache

          http://www.springframework.org/schema/cache/spring-cache-3.1.xsd

          http://www.springframework.org/schema/data/jpa

          http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">

<!-- 数据源 -->

<bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource">

<property name="driverClassName" value="com.mysql.jdbc.Driver" />

<property name="url" value="jdbc:mysql://localhost:3306/mysql" />

<property name="username" value="root" />

<property name="password" value="root" />

<property name="initialSize" value="5" />

<property name="minIdle" value="5" />

<property name="maxIdle" value="30" />

<property name="maxActive" value="100" />

<property name="maxWait" value="1000" />

</bean>

<!-- 实体管理工厂 -->

<bean id="entityManagerFactory" name="mysql"

class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">

<property name="dataSource" ref="dataSource" />

<property name="packagesToScan" value="com.morris.entity" />

<property name="jpaDialect">

<bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />

</property>

<property name="persistenceProvider">

<bean class="org.hibernate.jpa.HibernatePersistenceProvider" />

</property>

<property name="jpaProperties">

<props>

<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>

<prop key="hibernate.show_sql">true</prop>

<prop key="hibernate.max_fetch_depth">3</prop>

<prop key="hibernate.jdbc.fetch_size">18</prop>

<prop key="hibernate.jdbc.batch_size">10</prop>

<prop key="hibernate.hbm2ddl.auto">update</prop>

</props>

</property>

</bean>

<!-- 定义扫描根路径,不使用默认的扫描方式 -->

<context:component-scan base-package="com.morris"

use-default-filters="false">

<!-- 扫描符合@Service @Repository的类 -->

<context:include-filter type="annotation"

expression="org.springframework.stereotype.Service" />

<context:include-filter type="annotation"

expression="org.springframework.stereotype.Repository" />

</context:component-scan>

<tx:annotation-driven transaction-manager="transactionManager" />

<bean id="transactionManager"class="org.springframework.orm.jpa.JpaTransactionManager">

<property name="entityManagerFactory" ref="entityManagerFactory" />

</bean>

<jpa:repositories base-package="com.morris.dao"

entity-manager-factory-ref="entityManagerFactory"

transaction-manager-ref="transactionManager" />

</beans>

1.2.3.   测试结果


TestPerson运行后台打印sql:


Hibernate: insert into Person (age, name) values (?, ?)

时间: 2024-07-30 10:53:42

spring整合jpa优化的相关文章

spring整合jpa

1.1.  Spring整合jpa 1.1.1.  新建工程 1.1.2.  引入jar包 1.        所使用hibernat版本:hibernate-release-4.3.7.Final,Lib\jpa目录和lib\required目录下所有的jar包, 2.        mysql驱动包 3.        spring版本spring-framework-4.1.3.RELEASE,libs下所有的jar包 4.        tomcat jdbc连接池 tomcat-jdb

JPA学习笔记-Spring整合JPA

Spring 整合 JPA 三种整合方式: -LocalEntityManagerFactoryBean:适用于那些仅使用 JPA 进行数据访问的项目,该 FactoryBean 将根据JPA PersistenceProvider 自动检测配置文件进行工作,一般从"META-INF/persistence.xml"读取配置信息,这种方式最简单,但不能设置 Spring 中定义的DataSource,且不支持 Spring 管理的全局事务 -从JNDI中获取:用于从 Java EE 服

Spring整合JPA改进办法

在标准JPA中,持久化单元默认被定义在META-INF/persistence.xml文件中,并且通过@Entity注解搜索获得.但是大多数情况下,持久化单元不会仅仅存在一个,并且数据源也不会是一个,基于这个原因,Spring提供了一个可选方案,即通过LocalEntityManagerFactoryBean和LocalContainerEntityManagerFactoryBean进行管理.localEntityManagerFactoryBean扩展功能太少,这个不说,我们以LocalCo

spring整合JPA(hibernate)以及jdbctemple

下面的基本的项目部署信息: [c-sharp] view plaincopy <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" /> <!-- jpa  entityManagerFactory--> <bean id="entityManagerFactory" class="org.springf

Spring整合JPA时,为实体类添加@Entity注解时提示The type MultipartEntity is deprecated

这个情况是由于导入错了Entity包所导致的. 按住Alt+T时,会有两个关于@Entity的提示 org.hibernate.annotations.Entity 和 javax.persistence.Entity,我们应该使用javax.persistence.Entity.此时就不会出现过时的提示. @org.hibernate.annotations.Entity 和 @javax.persistence.Entity 的区别: JPA的Entity类和Hibernate的Entity

Spring笔记(五): spring 整合jdbc、hibernate、jpa

一.简介 (一)需要的jar包 1.需要的jar包:spring.hibernate.mysql.xml.apache-commons等等的jar包.        2.以上jar包,如spring.xml和commos都是冗余的. (二)分析 1.涉及到的实体.service.dao接口.jdbc配置文件以及service实现类都可以通用,只需要实现不同的dao实现类.配置.测试类. 2.通用的源码如下: 1)实体: <span style="font-size:18px;"&

spring data jpa学习笔记一:helloworld

在学习 JPA Spring Data之前,我们有必要了解一下JPA和Spring Data. JPA JPA全称Java Persistence API.即java持久化规范.JPA通过注解或XML描述对象-关系表的映射关系,并将运行期的实体对象持久化到数据库中. Spring Data Spring Data是Spring框架的一个子项目,就像Spring MVC是Spring的一部分一样.使得数据库访问变得方便和快捷.Spring Data 支持JPA. JPA Spring Data J

spring data jpa执行update和delete语句时报错处理

之前项目中使用spring data jpa时,遇到删除记录的需求时,主要利用spring data中自带的delete()方法处理,最近在dao层使用delete sql语句时报错,代码如下: [java] view plain copy @Query(value = "delete parcel,parcel_file,ms_files,t_order,route " + "from parcel left join route on parcel.route_id = 

java框架整合例子(spring、spring mvc、spring data jpa、hibernate)

这是自己参考springside开源项目整合的框架,主要整合了spring.spring mvc.spring data jpa.hibernate这几个框架,对于这几个框架其中感觉比较舒服的还是spring data jpa这个框架,这个框架在写dao类的时候,只需要写一个接口声明,spring data jpa会自动的实现其实现类,使用起来比较方便,至于详细的使用方法还请自己百度吧,因为我也不清楚.个人感觉还有一个比较不错的地方就是能够打印sql语句,都知道hibernate打印的sql语句