spring mvc之jpa的配置

时间:2021-06-11 20:33:54

上面已经讲了spring mvc各个方面的配置,还差缓存和验证码的方面,这篇我讲一下jpa的配置,话不多说,贴代码。

spring-data-commons-core-1.4.0.RC1.jar和spring-data-jpa-1.2.0.RC1是必须的,你也可以自己挑选别的。

头部声明

xmlns:jpa="http://www.springframework.org/schema/data/jpa"
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/data/repository
http://www.springframework.org/schema/data/repository/spring-repository-1.6.xsd
全部配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/data/repository
http://www.springframework.org/schema/data/repository/spring-repository-1.6.xsd">

<context:annotation-config />

<!-- 自动扫描并注解 -->
<context:component-scan base-package="com.spring" />
<!-- 加载hibenate.properties -->
<context:property-placeholder location="classpath:/hibernate.properties" />

<!-- 配置数据源 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<!-- 基本属性 url、user、password -->
<property name="url" value="${dataSource.url}" />
<property name="username" value="${dataSource.username}" />
<property name="password" value="${dataSource.password}" />
<property name="driverClassName" value="${dataSource.driverClassName}" />
</bean>

<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="persistenceProvider" ref="persistenceProvider" />
<property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
<property name="jpaDialect" ref="jpaDialect" />

<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">${dataSource.dialect}</prop>
<prop key="hibernate.hbm2ddl.auto">${dataSource.hbm2ddl.auto}</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>

<property name="packagesToScan">
<list>
<value>com.spring.entity</value>
</list>
</property>
</bean>

<bean id="persistenceProvider"
class="org.hibernate.ejb.HibernatePersistence" />

<bean id="jpaVendorAdapter"
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="database" value="MYSQL" />
</bean>

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

<jpa:repositories base-package="com.spring.dao"
entity-manager-factory-ref="entityManagerFactory"
transaction-manager-ref="txManager" />

<bean id="txManager"
class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory"
ref="entityManagerFactory" />
</bean>

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

<aop:config>
<aop:pointcut expression="execution(public * com.spring.service.impl.*.*(..))"
id="businessService" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="businessService" />

<aop:aspect id="aspect" ref="aspectBean">
<aop:pointcut expression="execution(public * com.spring.service.impl.*.*(..))" id="businessService1"/>
<aop:before pointcut-ref="businessService1" method="doBefore"/>
<aop:after-throwing pointcut-ref="businessService1" throwing="e" method="doAfterThrow" />
<aop:after pointcut-ref="businessService1" method="doAfter"/>
<aop:around pointcut-ref="businessService1" method="doAround"/>
</aop:aspect>

</aop:config>

<bean id="aspectBean" class="com.spring.util.Aspect" />

<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>

<tx:method name="find*" read-only="true" propagation="NOT_SUPPORTED" />
<tx:method name="*" />
<tx:method name="get*" propagation="REQUIRED" read-only="true" />
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="add*" propagation="REQUIRED"/>

</tx:attributes>
</tx:advice>
</beans>
jpa的使用

import java.util.List;

import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.Repository;

import com.spring.entity.User;

public interface LoginDao extends Repository<User, Integer>{

@Query("select u from User u where u.name = ?1")
public User findByName(String loginName);

@Modifying
@Query("update User u set u.name=?1 where u.id=1")
public int update(String name);

public User findById(int id);

}
这个是我的学习里面的,大家如果不明白,看看我的前几篇。