SpringData JPA 搭建 xml的 配置方式

?

1.導入版本管理依賴 到父項目里

<dependencyManagement><dependencies><dependency><groupId>org.springframework.data</groupId><artifactId>spring-data-bom</artifactId><version>2021.1.10</version><scope>import</scope><type>pom</type></dependency></dependencies>
</dependencyManagement>

2.導入spring-data-jpa 依賴 在子模塊

  <dependencies><!--    Junit    --><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope></dependency><!--   hibernate --><dependency><groupId>org.hibernate</groupId><artifactId>hibernate-entitymanager</artifactId><version>5.4.32.Final</version></dependency><!--        mysql  --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.47</version></dependency><!--        jpa  --><dependency><groupId>org.springframework.data</groupId><artifactId>spring-data-jpa</artifactId></dependency><!--     連接池   --><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.2.8</version></dependency><!--     spring -  test    --><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>5.3.10</version><scope>test</scope></dependency></dependencies>

3.創建實體類

package com.kuang.pojo;import javax.persistence.*;@Entity//作為 hibernate實體類
@Table(name = "tb_customer")//映射的表名
public class Customer {/*** @Id: 聲明主鍵的配置* @GeneratedValue: 配置主鍵的生成策略*        strategy :*            1. GenerationType.IDENTITY :自增 mysql*               底層數據庫必須支持自動增長 (底層數據庫支持的自動增長方式,對id自增)*            2. GenerationType.SEQUENCE : 序列 ,oracle*               底層書庫必須支持序列*            3. GenerationType.TABLE : jpa 提供的一種機制, 通過一張數據庫表的形式幫助我們完成主鍵的配置*            4. GenerationType.AUTO : 由程序自動的幫助我們選擇主鍵生成策略*   @Column(name = "cust_id") 配置屬性和字段的映射關系*       name: 數據庫表中字段的名稱*/@Id@GeneratedValue(strategy = GenerationType.IDENTITY)@Column(name = "cust_id")private Long custId;//客戶的主鍵@Column(name = "cust_name")private String custName;//客戶的名稱@Column(name = "cust_address")private String custAddress;public Long getCustId() {return custId;}public void setCustId(Long custId) {this.custId = custId;}public String getCustName() {return custName;}public void setCustName(String custName) {this.custName = custName;}public String getCustAddress() {return custAddress;}public void setCustAddress(String custAddress) {this.custAddress = custAddress;}@Overridepublic String toString() {return "Customer{" +"custId=" + custId +", custName='" + custName + '\'' +", custAddress='" + custAddress + '\'' +'}';}
}

4.創建spring配置文件

<?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:jpa="http://www.springframework.org/schema/data/jpa" xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beanshttps://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/data/jpahttps://www.springframework.org/schema/data/jpa/spring-jpa.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"><!-- 用于整合 jpa  相當于 @EnableJpaRepositories       --><jpa:repositories base-package="com.kuang.repositories"entity-manager-factory-ref="entityManagerFactory"transaction-manager-ref="transactionManager"/><!-- 配置 bean  EntityManagerFactory    --><bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"><property name="jpaVendorAdapter"><!--         Hibernate 實現   --><bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"><!--            是否自動的表的生成  true 相當于之前的 update  false 相當于 none  --><property name="generateDdl" value="true"/><!--             是否顯示sql   --><property name="showSql" value="true"/></bean></property><!--        掃描實體類的包  來決定哪些實體類做 ORM映射  --><property name="packagesToScan" value="com.kuang.pojo"></property>
<!--    數據源   druid --><property name="dataSource" ref="dataSource"/></bean><!--    數據源--><bean class="com.alibaba.druid.pool.DruidDataSource" id="dataSource"><property name="driverClassName" value="com.mysql.jdbc.Driver"/><property name="url" value="jdbc:mysql://localhost:3306/springdata_jpa?useUnicode=true&amp;useSSL=false&amp;characterEncoding=UTF-8"/><property name="username" value="root"/><property name="password" value="2001"/></bean><!--    聲明式事務  --><bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager"><property name="entityManagerFactory" ref="entityManagerFactory"/></bean>
<!-- 啟動注解方式的聲明式事務--><tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
</beans>

5.創建Repository接口

package com.kuang.repositories;import com.kuang.pojo.Customer;
import org.springframework.data.repository.CrudRepository;public interface CustomerRepository extends CrudRepository<Customer,Long> {}

6.測試通過主鍵查詢

package com.kuang.test;import com.kuang.pojo.Customer;
import com.kuang.repositories.CustomerRepository;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import java.util.Optional;@ContextConfiguration("/spring.xml")
@RunWith(SpringJUnit4ClassRunner.class)
public class SpringDataJpaTest {@Autowiredprivate CustomerRepository customerRepository;@Testpublic void select() {Optional<Customer> byId = customerRepository.findById(1L);Customer customer = byId.get();System.out.println(customer);}
}

package com.kuang.test;import com.kuang.pojo.Customer;
import com.kuang.repositories.CustomerRepository;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import java.util.Optional;@ContextConfiguration("/spring.xml")
@RunWith(SpringJUnit4ClassRunner.class)
public class SpringDataJpaTest {@Autowiredprivate CustomerRepository customerRepository;@Testpublic void select() {Optional<Customer> byId = customerRepository.findById(1L);Customer customer = byId.get();System.out.println(customer);}@Testpublic void insert() {Customer customer = new Customer();customer.setCustAddress("南環路");customer.setCustName("豪哥");Customer save = customerRepository.save(customer);save.setCustAddress("劉備");System.out.println(customer);}@Testpublic void update() {Customer customer = new Customer();customer.setCustId(1L);customer.setCustAddress("棲霞路");customer.setCustName("張飛");Customer save = customerRepository.save(customer);}@Testpublic void remove() {Customer customer = new Customer();customer.setCustId(1L);customerRepository.delete(customer);}
}

?

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/214490.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/214490.shtml
英文地址,請注明出處:http://en.pswp.cn/news/214490.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

【力扣100】238.除自身以外數組的乘積

添加鏈接描述 class Solution:def productExceptSelf(self, nums: List[int]) -> List[int]:# 構造第i個數的左右數組n len(nums)left,right,res [1]*n,[1]*n,[1]*nfor i in range(1,n):left[i] nums[i-1]*left[i-1]for i in range(n-2,-1,-1):right[i] nums[i1]*right…

STM32Cube高效開發教程<基礎篇>(十二)----ADC

聲明:本人水平有限,博客可能存在部分錯誤的地方,請廣大讀者諒解并向本人反饋錯誤。 ?? 本專欄博客參考《STM32Cube高效開發教程(基礎篇)》,有意向的讀者可以購買正版書籍輔助學習,本書籍由王維波老師、鄢志丹老師、王釗老師傾力打造,書籍內容干貨滿滿。 一、功能概述 …

【C++11】lambda表達式及包裝器

一.lambda表達式 1.可調用對象 可調用對象即可以像函數一樣被調用的對象&#xff0c;有以下三種&#xff1a; 函數(指針)仿函數對象lambda表達式 tips&#xff1a;調用函數時&#xff0c;既可以用函數名&#xff0c;也可以用函數地址&#xff0c;因為函數名和函數地址是一回事…

Python從入門到精通五:Python數據容器

數據容器入門 為什么學習數據容器 思考一個問題&#xff1a;如果我想要在程序中&#xff0c;記錄5名學生的信息&#xff0c;如姓名。 如何做呢&#xff1f; 學習數據容器&#xff0c;就是為了批量存儲或批量使用多份數據 Python中的數據容器&#xff1a; 一種可以容納多份…

Kalman濾波、擴展Kalman濾波、無跡Kalman濾波和異步濾波的原理及其Matlab代碼

目錄 引言Kalman濾波代碼及其結果展示 擴展Kalman濾波代碼及其結果展示 無跡Kalman濾波無跡變換無跡Kalman濾波代碼及其結果展示 異步無跡Kalman濾波原理代碼及其結果展示 引言 本文給出了Kalman Filter&#xff08;卡爾曼濾波&#xff09;、Extended Kalman Filter&#xff0…

leetcode 98. 驗證二叉搜索樹

leetcode 98. 驗證二叉搜索樹 題目 給你一個二叉樹的根節點 root &#xff0c;判斷其是否是一個有效的二叉搜索樹。 有效 二叉搜索樹定義如下&#xff1a; 節點的左子樹只包含 小于 當前節點的數。 節點的右子樹只包含 大于 當前節點的數。 所有左子樹和右子樹自身必須也是…

vue3 引入 markdown編輯器

參考文檔 安裝依賴 pnpm install mavon-editor // "mavon-editor": "3.0.1",markdown 編輯器 <mavon-editor></mavon-editor>新增文本 <mavon-editor ref"editorRef" v-model"articleModel.text" codeStyle"…

Adams與Abaqus沖突問題

隨著工程仿真軟件的廣泛應用&#xff0c;Adams和Abaqus已成為眾多工程師的首選工具。然而&#xff0c;在使用過程中&#xff0c;一些用戶可能會遇到這兩個軟件之間的沖突問題&#xff0c;導致無法正常進行仿真分析。為了幫助大家解決這一難題&#xff0c;我們推出了一篇關于Ada…

Softmax回歸

一、Softmax回歸關鍵思想 1、回歸問題和分類問題的區別 Softmax回歸雖然叫“回歸”&#xff0c;但是它本質是一個分類問題。回歸是估計一個連續值&#xff0c;而分類是預測一個離散類別。 2、Softmax回歸模型 Softmax回歸跟線性回歸一樣將輸入特征與權重做線性疊加。與線性回歸…

Linux安裝Nginx并部署Vue項目

今天部署了一個Vue項目到阿里云的云服務器上&#xff0c;現記錄該過程。 1. 修改Vue項目配置 我們去項目中發送axios請求的文件里更改一下后端的接口路由&#xff1a; 2. 執行命令打包 npm run build ### 或者 yarn build 打包成功之后&#xff0c;我們會看到一個dist包&a…

[MySQL]SQL優化之索引的使用規則

&#x1f308;鍵盤敲爛&#xff0c;年薪30萬&#x1f308; 目錄 一、索引失效 &#x1f4d5;最左前綴法則 &#x1f4d5;范圍查詢> &#x1f4d5;索引列運算&#xff0c;索引失效 &#x1f4d5;前模糊匹配 &#x1f4d5;or連接的條件 &#x1f4d5;字符串類型不加 …

110. 平衡二叉樹(Java)

給定一個二叉樹&#xff0c;判斷它是否是高度平衡的二叉樹。 本題中&#xff0c;一棵高度平衡二叉樹定義為&#xff1a; 一個二叉樹每個節點 的左右兩個子樹的高度差的絕對值不超過 1 。 示例 1&#xff1a; 輸入&#xff1a;root [3,9,20,null,null,15,7] 輸出&#xff1a;t…

如何通過SPI控制Peregrine的數控衰減器

概要 Peregrine的數控衰減器PE4312是6位射頻數字步進衰減器(DSA,Digital Step Attenuator)工作頻率覆蓋1MHz~4GHz,插入損耗2dB左右,衰減步進0.5dB,最大衰減量為31.5dB,高達59dBm的IIP3提供了良好的動態性能,切換時間0.5微秒,供電電源2.3V~5.5V,邏輯控制兼容1.8V,20…

?如何使用https://www.krea.ai/來實現文生圖,圖生圖,

網址&#xff1a;https://www.krea.ai/apps/image/realtime Krea.ai 是一個強大的人工智能藝術生成器&#xff0c;可用于創建各種創意內容。它可以用來生成文本描述的圖像、將圖像轉換為其他圖像&#xff0c;甚至寫博客文章。 文本描述生成圖像 要使用 Krea.ai 生成文本描述…

設計模式——建造者模式(Java示例)

引言 生成器是一種創建型設計模式&#xff0c; 使你能夠分步驟創建復雜對象。 與其他創建型模式不同&#xff0c; 生成器不要求產品擁有通用接口。 這使得用相同的創建過程生成不同的產品成為可能。 復雜度&#xff1a; 中等 流行度&#xff1a; 流行 使用示例&#xff1a…

【conda】利用Conda創建虛擬環境,Pytorch各版本安裝教程(Ubuntu)

TOC conda 系列&#xff1a; 1. conda指令教程 2. 利用Conda創建虛擬環境&#xff0c;安裝Pytorch各版本教程(Ubuntu) 1. 利用Conda創建虛擬環境 nolonolo:~/sun/SplaTAM$ conda create -n splatam python3.10查看結果&#xff1a; (splatam) nolonolo:~/sun/SplaTAM$ cond…

Java 中的 Deque 接口及其用途

文章目錄 Deque 介紹Deque 使用雙端隊列普通隊列棧 總結 在 Java 中&#xff0c;Deque 接口是一個雙端隊列&#xff08;double-ended queue&#xff09;的數據結構&#xff0c;它支持在兩端插入和移除元素。Deque 是 “Double Ended Queue” 的縮寫&#xff0c;而且它可以同時充…

Linux系統編程(一):基本概念

參考引用 Unix和Linux操作系統有什么區別&#xff1f;一文帶你徹底搞懂posix Linux系統編程&#xff08;文章鏈接匯總&#xff09; 1. Unix 和 Linux 1.1 Unix Unix 操作系統誕生于 1969 年&#xff0c;貝爾實驗室發布了一個用 C 語言編寫的名為「Unix」的操作系統&#xff0…

【基于LSTM的電商評論情感分析:Flask與Sklearn的完美結合】

基于LSTM的電商評論情感分析&#xff1a;Flask與Sklearn的完美結合 引言數據集與爬取數據處理與可視化情感分析模型構建Flask應用搭建詞云展示創新點結論 引言 在當今數字化時代&#xff0c;電商平臺上涌現出大量的用戶評論數據。了解和分析這些評論對于企業改進產品、服務以及…

?expect命令運用于bash?

目錄 ?expect命令運用于bash? expect使用原理 expet使用場景 常用的expect命令選項 Expect腳本的結尾 常用的expect命令選參數 Expect執行方式 單一分支語法 多分支模式語法第一種 多分支模式語法第二種 在shell 中嵌套expect Shell Here Document&#xff08;內…