SpringData JPA 如何搭建 xml的配置方式

1.導(dǎo)入版本管理依賴 到父項(xiàng)目里
<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.導(dǎo)入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.創(chuàng)建實(shí)體類
package com.kuang.pojo;
import javax.persistence.*;
@Entity//作為 hibernate實(shí)體類
@Table(name = "tb_customer")//映射的表名
public class Customer {
/**
* @Id: 聲明主鍵的配置
* @GeneratedValue: 配置主鍵的生成策略
* strategy :
* 1. GenerationType.IDENTITY :自增 mysql
* 底層數(shù)據(jù)庫必須支持自動(dòng)增長(zhǎng) (底層數(shù)據(jù)庫支持的自動(dòng)增長(zhǎng)方式,對(duì)id自增)
* 2. GenerationType.SEQUENCE : 序列 ,oracle
* 底層書庫必須支持序列
* 3. GenerationType.TABLE : jpa 提供的一種機(jī)制, 通過一張數(shù)據(jù)庫表的形式幫助我們完成主鍵的配置
* 4. GenerationType.AUTO : 由程序自動(dòng)的幫助我們選擇主鍵生成策略
* @Column(name = "cust_id") 配置屬性和字段的映射關(guān)系
* name: 數(shù)據(jù)庫表中字段的名稱
*/
@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;
}
@Override
public String toString() {
return "Customer{" +
"custId=" + custId +
", custName='" + custName + '\'' +
", custAddress='" + custAddress + '\'' +
'}';
}
}4.創(chuàng)建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/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/data/jpa
https://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 相當(dāng)于 @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 實(shí)現(xiàn) -->
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<!-- 是否自動(dòng)的表的生成 true 相當(dāng)于之前的 update false 相當(dāng)于 none -->
<property name="generateDdl" value="true"/>
<!-- 是否顯示sql -->
<property name="showSql" value="true"/>
</bean>
</property>
<!-- 掃描實(shí)體類的包 來決定哪些實(shí)體類做 ORM映射 -->
<property name="packagesToScan" value="com.kuang.pojo"></property>
<!-- 數(shù)據(jù)源 druid -->
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 數(shù)據(jù)源-->
<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&useSSL=false&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="2001"/>
</bean>
<!-- 聲明式事務(wù) -->
<bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<!-- 啟動(dòng)注解方式的聲明式事務(wù)-->
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
</beans>5.創(chuàng)建Repository接口
package com.kuang.repositories;
import com.kuang.pojo.Customer;
import org.springframework.data.repository.CrudRepository;
public interface CustomerRepository extends CrudRepository<Customer,Long> {
}6.測(cè)試通過主鍵查詢
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 {
@Autowired
private CustomerRepository customerRepository;
@Test
public 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 {
@Autowired
private CustomerRepository customerRepository;
@Test
public void select() {
Optional<Customer> byId = customerRepository.findById(1L);
Customer customer = byId.get();
System.out.println(customer);
}
@Test
public void insert() {
Customer customer = new Customer();
customer.setCustAddress("南環(huán)路");
customer.setCustName("豪哥");
Customer save = customerRepository.save(customer);
save.setCustAddress("劉備");
System.out.println(customer);
}
@Test
public void update() {
Customer customer = new Customer();
customer.setCustId(1L);
customer.setCustAddress("棲霞路");
customer.setCustName("張飛");
Customer save = customerRepository.save(customer);
}
@Test
public void remove() {
Customer customer = new Customer();
customer.setCustId(1L);
customerRepository.delete(customer);
}
}到此這篇關(guān)于SpringData JPA 搭建 xml的 配置方式的文章就介紹到這了,更多相關(guān)SpringData JPA 搭建 xml內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
深度優(yōu)先與廣度優(yōu)先Java實(shí)現(xiàn)代碼示例
這篇文章主要介紹了深度優(yōu)先與廣度優(yōu)先Java實(shí)現(xiàn)代碼示例,具有一定借鑒價(jià)值,需要的朋友可以參考下。2017-12-12
java遞歸設(shè)置層級(jí)菜單的實(shí)現(xiàn)
本文主要介紹了java遞歸設(shè)置層級(jí)菜單的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08
Java保留兩位小數(shù)的實(shí)現(xiàn)方法
這篇文章主要介紹了 Java保留兩位小數(shù)的實(shí)現(xiàn)方法的相關(guān)資料,需要的朋友可以參考下2017-06-06
Spring Security代碼實(shí)現(xiàn)JWT接口權(quán)限授予與校驗(yàn)功能
本文給大家介紹Spring Security代碼實(shí)現(xiàn)JWT接口權(quán)限授予與校驗(yàn)功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2019-12-12
SpringBoot整合Gson 整合Fastjson的實(shí)例詳解
這篇文章主要介紹了SpringBoot整合Gson 整合Fastjson的實(shí)例詳解,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11
關(guān)于Spring @Bean 相同加載順序不同結(jié)果不同的問題記錄
本文主要探討了在Spring 5.1.3.RELEASE版本下,當(dāng)有兩個(gè)全注解類定義相同類型的Bean時(shí),由于加載順序不同,最終生成的Bean實(shí)例也會(huì)不同,文章通過分析ConfigurationClassPostProcessor的執(zhí)行過程,解釋了BeanDefinition的加載和覆蓋機(jī)制,感興趣的朋友一起看看吧2025-02-02

