springdatajpa

dao层
public interface IUserDao extends CrudRepository<User, Long>,JpaRepository<User, Long>,PagingAndSortingRepository<User,Long>{

  
}

persistence.xml

<?xml version="1.0"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
version="1.0">
<persistence-unit name="springdatajpa" transaction-type="RESOURCE_LOCAL">
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
<property name="hibernate.hbm2ddl.auto" value="update" />
    <property name="javax.persistence.validation.mode" value="none"/>
<property name="hibernate.show_sql" value="true" />
  </properties>
</persistence-unit>
</persistence>

applicationContext.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:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa-1.2.xsd
">

<context:annotation-config/>
<context:component-scan base-package="cn.itcast.springdatajpa01.service"/>

<!-- 引入外部资源文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>

<!-- c3p0连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
    <property name="driverClass" value="${db.driverClassName}"></property>
    <property name="user" value="${db.username}"></property>
    <property name="password" value="${db.password}"></property>
    <property name="jdbcUrl" value="${db.url}"></property>
</bean>

<!-- 定义实体管理器工厂 -->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
  <property name="dataSource" ref="dataSource"></property>
    <property name="persistenceXmlLocation" value="classpath:persistence.xml"/>
      <property name="persistenceProvider">
            <bean class="org.hibernate.ejb.HibernatePersistence"/>
        </property>
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="database" value="MYSQL"/>
            </bean>
        </property>
</bean>
<!-- 事务管理器 -->
  <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
   <property name="entityManagerFactory" ref="entityManagerFactory"></property>
  </bean>
  <tx:annotation-driven />
    <!-- 开启 spring data jpa扫描目录 -->
    <jpa:repositories base-package="cn.itcast.springdatajpa01.dao"/>

</beans>


你可能感兴趣的:(springdatajpa)