Spring Data JPA

2020-04-14  本文已影响0人  低调的灬攻城狮

一、Spring Data JPA概述

二、Spring Data JPA正向工程

  <?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:tx="http://www.springframework.org/schema/tx"
         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/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">
      <context:property-placeholder location="classpath:db.properties" />
      <context:component-scan base-package="com.qfedu.service" />
      <context:component-scan base-package="com.qfedu.dao" />
      <bean id="ds" class="com.alibaba.druid.pool.DruidDataSource">
          <property name="url" value="${url}" />
          <property name="driverClassName" value="${driver}" />
          <property name="username" value="${aaa}" />
          <property name="password" value="${bbb}" />
      </bean>
      <!-- 配置 HibernateJpaVendorAdapter,用来分别设置数据库的方言和是否显示sql语句 -->
      <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" id="adapter">
          <!--<property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect" />-->
          <property name="databasePlatform" value="org.hibernate.dialect.MySQL57InnoDBDialect" />
          <property name="showSql" value="true" />
      </bean>
      <!-- 配置EntityManagerFactoryBean -->
      <bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="emf">
          <property name="dataSource" ref="ds" />
          <property name="packagesToScan" value="com.qfedu.entity" />
          <property name="jpaVendorAdapter" ref="adapter" />
          <property name="jpaProperties">
              <props>
                  <prop key="hibernate.format_sql">true</prop>
                  <!--<prop key="hibernate.dialect.storage_engine">innodb</prop>-->
                  <!-- 使用hibernate.hbm2ddl.auto属性来根据需要动态创建数据库的表结构
                      create:表示启动的时候先drop,再create
                      create-drop: 也表示创建,只不过再系统关闭前执行一下drop
                      update: 这个操作启动的时候会去检查schema是否一致,如果不一致会做scheme更新
                      validate: 启动时验证现有schema与你配置的hibernate是否一致,如果不一致就抛出异常,并不做更新 -->
                  <prop key="hibernate.hbm2ddl.auto">update</prop>
              </props>
          </property>
      </bean>
      <!-- 配置jpa的事务管理器 -->
      <bean class="org.springframework.orm.jpa.JpaTransactionManager" id="jtx">
          <property name="entityManagerFactory" ref="emf" />
      </bean>
      <!-- 配置事务的注解驱动 -->
      <tx:annotation-driven proxy-target-class="false" transaction-manager="jtx" />
      <jpa:repositories base-package="com.qfedu.dao" entity-manager-factory-ref="emf" transaction-manager-ref="jtx" />
  </beans>

三、条件查询

上一篇 下一篇

猜你喜欢

热点阅读