最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

JPA配置詳解之jpaProperties用法

 更新時間:2021年11月22日 10:28:17   作者:LQW_home  
這篇文章主要介紹了JPA配置詳解之jpaProperties用法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

JPA配置之jpaProperties

<?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:jpa="http://www.springframework.org/schema/data/jpa"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.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/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
	
	<!-- spring自動讀取指定位置的配置為簡到spring中 -->
	<context:property-placeholder location="classpath*:/application.properties"/>
	
	<context:component-scan base-package="com.shiroweb">
		<!-- 掃描com.shiroweb包下除去@Controller以外注解的類 -->
		<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
		<context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
	</context:component-scan>
	
	<!-- c3p0數(shù)據(jù)源配置 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="${jdbc.driver}"/>
		<property name="jdbcUrl" value="${jdbc.url}" />
		<property name="user" value="${jdbc.username}"/>
		<property name="password" value="${jdbc.password}"/>
	</bean>
	
	<!-- Jpa Entity Manager 配置 關聯(lián)hibernateJpaVendorAdapter -->
	<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 
		<property name="dataSource" ref="dataSource"/>
		<property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter"/>
		<property name="packagesToScan" value="com.shiroweb"/>
		<!-- <property name="jpaProperties">
			<props>
				命名規(guī)則 My_NAME->MyName
				<prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop>
				實體類對應數(shù)據(jù)庫沒有表 就生成一個表
				<prop key="hibernate.hbm2ddl.auto">update</prop>
			</props>
		</property> -->
		
		<!-- 指定JPA屬性;如Hibernate中指定是否顯示SQL的是否顯示、方言等 -->
		<property name="jpaProperties">
           <props>
               <!-- <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop> -->
               <prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop>
               <!-- <prop key="hibernate.cache.provider_class">org.hibernate.cache.NoCacheProvider</prop> -->
               <prop key="hibernate.show_sql">true</prop>
               <prop key="hibernate.format_sql">true</prop>
               <!-- <prop key="hibernate.hbm2ddl.auto">validate</prop> -->
               <prop key="hibernate.hbm2ddl.auto">update</prop>
           </props>
       </property>
	</bean>
	
	<!-- 配置hibernateJpaVendorAdapter關聯(lián)數(shù)據(jù)源 -->
	<bean id="hibernateJpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
		<property name="database" value="MYSQL" />  
        <property name="showSql" value="true" /> 
	</bean>
	
	<!-- Spring Data Jpa配置 -->
 	<jpa:repositories base-package="com.shiroweb"  transaction-manager-ref="transactionManager" entity-manager-factory-ref="entityManagerFactory"/>
   
	<!-- Jpa 事務配置 -->
	<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
		<property name="entityManagerFactory" ref="entityManagerFactory"/>
	</bean>
 
	<!-- 使用annotation定義事務 -->
	<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />
</beans>

其中jpaProperties是這是jpa的一些屬性的

<!-- 指定JPA屬性;如Hibernate中指定是否顯示SQL的是否顯示、方言等 -->
 <property name="jpaProperties">
           <props>
               <!-- <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop> -->
               <prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop>
               <!-- <prop key="hibernate.cache.provider_class">org.hibernate.cache.NoCacheProvider</prop> -->
               <prop key="hibernate.show_sql">true</prop>
               <prop key="hibernate.format_sql">true</prop>
               <!-- <prop key="hibernate.hbm2ddl.auto">validate</prop> -->
               <prop key="hibernate.hbm2ddl.auto">update</prop>
           </props>
       </property>

這里有個屬性為

<prop key="hibernate.hbm2ddl.auto">update</prop>

這是一個有用的設置

其實這個hibernate.hbm2ddl.auto參數(shù)的作用主要用于:自動創(chuàng)建|更新|驗證數(shù)據(jù)庫表結構。如果不是此方面的需求建議set value="none"。

  • create:每次加載hibernate時都會刪除上一次的生成的表,然后根據(jù)你的model類再重新來生成新表,哪怕兩次沒有任何改變也要這樣執(zhí)行,這就是導致數(shù)據(jù)庫表數(shù)據(jù)丟失的一個重要原因。
  • create-drop:每次加載hibernate時根據(jù)model類生成表,但是sessionFactory一關閉,表就自動刪除。
  • update:最常用的屬性,第一次加載hibernate時根據(jù)model類會自動建立起表的結構(前提是先建立好數(shù)據(jù)庫),以后加載hibernate時根據(jù) model類自動更新表結構,即使表結構改變了但表中的行仍然存在不會刪除以前的行。要注意的是當部署到服務器后,表結構是不會被馬上建立起來的,是要等 應用第一次運行起來后才會。
  • validate:每次加載hibernate時,驗證創(chuàng)建數(shù)據(jù)庫表結構,只會和數(shù)據(jù)庫中的表進行比較,不會創(chuàng)建新表,但是會插入新值。

Sping Data Jpa配置問題

spring.jpa.properties.hibernate.hbm2ddl.auto=update

在配置spring data jpa時,如果spring.jpa.properties.hibernate.hbm2ddl.auto設置為update,會自動更新數(shù)據(jù)表結構,比如Entity中增加成員變量,數(shù)據(jù)表中也會增加相應的字段,但是需要注意的是,如果刪除一個成員變量,這時數(shù)據(jù)表中不會自動刪除對應的字段,如果刪除的那個成員變量在數(shù)據(jù)表中被設置為not null,當再次運行時就會報錯,如下面的例子

新建一個實體類

import lombok.Data;
import javax.persistence.*; 
@Entity
@Data
public class Car{ 
    @Id
    @Column(name="id", nullable = false)
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
 
    @Column(nullable = false)
    private String price;
 
    @Column
    private String color;
 
    @Column
    private String brand;
}

這時可以在數(shù)據(jù)庫中看到已經(jīng)自動生成數(shù)據(jù)表car,并且有對應的字段

這時我們刪掉Car中的price,重新運行,我們再次查看數(shù)據(jù)庫

發(fā)現(xiàn)還是存在price字段

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

  • java枚舉轉(zhuǎn)list通用類過程

    java枚舉轉(zhuǎn)list通用類過程

    文章介紹了如何將Java枚舉類型轉(zhuǎn)換為List<Map<String, Object>>的通用類,該類可以實現(xiàn)枚舉到列表的轉(zhuǎn)換,并且示例了使用方法
    2025-03-03
  • Java中map和flatMap的區(qū)別舉例詳解

    Java中map和flatMap的區(qū)別舉例詳解

    這篇文章主要給大家介紹了關于Java中map和flatMap區(qū)別的相關資料,在Java中Stream接口有map()和flatmap()方法,兩者都有中間流操作,并返回另一個流作為方法輸出,需要的朋友可以參考下
    2023-10-10
  • Spring AOP使用@Aspect注解 面向切面實現(xiàn)日志橫切的操作

    Spring AOP使用@Aspect注解 面向切面實現(xiàn)日志橫切的操作

    這篇文章主要介紹了Spring AOP使用@Aspect注解 面向切面實現(xiàn)日志橫切的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • Java深入學習圖形用戶界面GUI之創(chuàng)建窗體

    Java深入學習圖形用戶界面GUI之創(chuàng)建窗體

    圖形編程中,窗口是一個重要的概念,窗口其實是一個矩形框,應用程序可以使用其從而達到輸出結果和接受用戶輸入的效果,學習了GUI就讓我們用它來創(chuàng)建一個窗體
    2022-05-05
  • java正則表達式用法大全(深度好文)

    java正則表達式用法大全(深度好文)

    這篇文章主要給大家介紹了關于java正則表達式用法大全的相關資料,正則表達式在處理字符串時非常有用,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2023-10-10
  • Spring Boot 中使用 JSON Schema 校驗復雜JSON數(shù)據(jù)的過程

    Spring Boot 中使用 JSON Schema 校驗復雜JSO

    在數(shù)據(jù)交換領域,JSON Schema 以其強大的標準化能力,為定義和規(guī)范 JSON 數(shù)據(jù)的結構與規(guī)則提供了有力支持,下面給大家介紹Spring Boot 中使用 JSON Schema 校驗復雜JSON數(shù)據(jù)的過程,感興趣的朋友跟隨小編一起看看吧
    2024-08-08
  • spring注解 @PropertySource配置數(shù)據(jù)源全流程

    spring注解 @PropertySource配置數(shù)據(jù)源全流程

    這篇文章主要介紹了spring注解 @PropertySource配置數(shù)據(jù)源全流程,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • springCloud集成nacos啟動時報錯原因排查

    springCloud集成nacos啟動時報錯原因排查

    這篇文章主要介紹了springCloud集成nacos啟動時報錯原因排查,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-04-04
  • 淺談Java并發(fā)中ReentrantLock鎖應該怎么用

    淺談Java并發(fā)中ReentrantLock鎖應該怎么用

    本文主要介紹了ava并發(fā)中ReentrantLock鎖的具體使用,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • 深入淺析springsecurity入門登錄授權

    深入淺析springsecurity入門登錄授權

    SpringSecurity為我們提供了基于注解的權限控制方案,這也是我們項目中主要采用的方式,我們可以使用注解去指定訪問對應的資源所需的權限,這篇文章主要介紹了springsecurity入門登錄授權,需要的朋友可以參考下
    2024-05-05

最新評論

康马县| 南昌县| 安丘市| 青铜峡市| 喀喇沁旗| 阳曲县| 阿巴嘎旗| 湟源县| 永昌县| 班玛县| 治多县| 弋阳县| 海丰县| 上高县| 白山市| 河东区| 德庆县| 思南县| 姚安县| 宝山区| 濮阳县| 凭祥市| 谢通门县| 尉氏县| 灯塔市| 建湖县| 修文县| 西和县| 乐亭县| 北碚区| 临沂市| 济南市| 长宁县| 炎陵县| 珲春市| 库车县| 夏河县| 当涂县| 沾益县| 镇江市| 砀山县|