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 (?, ?)

 

评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

morris131

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值