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自動(dòng)讀取指定位置的配置為簡(jiǎn)到spring中 -->
<context:property-placeholder location="classpath*:/application.properties"/>
<context:component-scan base-package="com.shiroweb">
<!-- 掃描com.shiroweb包下除去@Controller以外注解的類(lèi) -->
<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 配置 關(guān)聯(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í)體類(lèi)對(duì)應(yīng)數(shù)據(jù)庫(kù)沒(méi)有表 就生成一個(gè)表
<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關(guān)聯(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 事務(wù)配置 -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<!-- 使用annotation定義事務(wù) -->
<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>
這里有個(gè)屬性為
<prop key="hibernate.hbm2ddl.auto">update</prop>
這是一個(gè)有用的設(shè)置
其實(shí)這個(gè)hibernate.hbm2ddl.auto參數(shù)的作用主要用于:自動(dòng)創(chuàng)建|更新|驗(yàn)證數(shù)據(jù)庫(kù)表結(jié)構(gòu)。如果不是此方面的需求建議set value="none"。
create:每次加載hibernate時(shí)都會(huì)刪除上一次的生成的表,然后根據(jù)你的model類(lèi)再重新來(lái)生成新表,哪怕兩次沒(méi)有任何改變也要這樣執(zhí)行,這就是導(dǎo)致數(shù)據(jù)庫(kù)表數(shù)據(jù)丟失的一個(gè)重要原因。create-drop:每次加載hibernate時(shí)根據(jù)model類(lèi)生成表,但是sessionFactory一關(guān)閉,表就自動(dòng)刪除。update:最常用的屬性,第一次加載hibernate時(shí)根據(jù)model類(lèi)會(huì)自動(dòng)建立起表的結(jié)構(gòu)(前提是先建立好數(shù)據(jù)庫(kù)),以后加載hibernate時(shí)根據(jù) model類(lèi)自動(dòng)更新表結(jié)構(gòu),即使表結(jié)構(gòu)改變了但表中的行仍然存在不會(huì)刪除以前的行。要注意的是當(dāng)部署到服務(wù)器后,表結(jié)構(gòu)是不會(huì)被馬上建立起來(lái)的,是要等 應(yīng)用第一次運(yùn)行起來(lái)后才會(huì)。validate:每次加載hibernate時(shí),驗(yàn)證創(chuàng)建數(shù)據(jù)庫(kù)表結(jié)構(gòu),只會(huì)和數(shù)據(jù)庫(kù)中的表進(jìn)行比較,不會(huì)創(chuàng)建新表,但是會(huì)插入新值。
Sping Data Jpa配置問(wèn)題
spring.jpa.properties.hibernate.hbm2ddl.auto=update
在配置spring data jpa時(shí),如果spring.jpa.properties.hibernate.hbm2ddl.auto設(shè)置為update,會(huì)自動(dòng)更新數(shù)據(jù)表結(jié)構(gòu),比如Entity中增加成員變量,數(shù)據(jù)表中也會(huì)增加相應(yīng)的字段,但是需要注意的是,如果刪除一個(gè)成員變量,這時(shí)數(shù)據(jù)表中不會(huì)自動(dòng)刪除對(duì)應(yīng)的字段,如果刪除的那個(gè)成員變量在數(shù)據(jù)表中被設(shè)置為not null,當(dāng)再次運(yùn)行時(shí)就會(huì)報(bào)錯(cuò),如下面的例子
新建一個(gè)實(shí)體類(lèi)
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í)可以在數(shù)據(jù)庫(kù)中看到已經(jīng)自動(dòng)生成數(shù)據(jù)表car,并且有對(duì)應(yīng)的字段

這時(shí)我們刪掉Car中的price,重新運(yùn)行,我們?cè)俅尾榭磾?shù)據(jù)庫(kù)

發(fā)現(xiàn)還是存在price字段
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
java并發(fā)包JUC誕生及詳細(xì)內(nèi)容
這篇文章主要為大家介紹了java并發(fā)包JUC的誕生及JUC增加的內(nèi)容詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-02-02
關(guān)于Java利用反射實(shí)現(xiàn)動(dòng)態(tài)運(yùn)行一行或多行代碼
這篇文章主要介紹了關(guān)于Java利用反射實(shí)現(xiàn)動(dòng)態(tài)運(yùn)行一行或多行代碼,借鑒了別人的方法和書(shū)上的內(nèi)容,最后將題目完成了,和大家一起分享以下解決方法,需要的朋友可以參考下2023-04-04
解決springboot報(bào)錯(cuò)找不到自動(dòng)注入的service問(wèn)題
這篇文章主要介紹了解決springboot報(bào)錯(cuò)找不到自動(dòng)注入的service問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08
Java中常用的數(shù)據(jù)庫(kù)連接池_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
數(shù)據(jù)庫(kù)連接池負(fù)責(zé)分配、管理和釋放數(shù)據(jù)庫(kù)連接,它允許應(yīng)用程序重復(fù)使用一個(gè)現(xiàn)有的數(shù)據(jù)庫(kù)連接,而不是再重新建立一個(gè);釋放空閑時(shí)間超過(guò)最大空閑時(shí)間的數(shù)據(jù)庫(kù)連接來(lái)避免因?yàn)闆](méi)有釋放數(shù)據(jù)庫(kù)連接而引起的數(shù)據(jù)庫(kù)連接遺漏2017-08-08
SpringBoot注解@ConditionalOnClass底層源碼實(shí)現(xiàn)
這篇文章主要為大家介紹了SpringBoot注解@ConditionalOnClass底層源碼實(shí)現(xiàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02
MyBatisPlus3.4.3版自動(dòng)生成代碼的使用過(guò)程
這篇文章主要介紹了MyBatisPlus3.4.3版自動(dòng)生成代碼的使用,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-04-04
java實(shí)現(xiàn)自動(dòng)回復(fù)聊天機(jī)器人
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)自動(dòng)回復(fù)聊天機(jī)器人,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08

