本篇是针对上一篇《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 (?, ?) |