亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

 更新時間:2023年12月18日 09:34:41   作者:葒色海灣  
這篇文章主要介紹了SpringData JPA 如何搭建 xml的配置方式,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧

1.導(dǎo)入版本管理依賴 到父項目里

<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)建實體類

package com.kuang.pojo;
import javax.persistence.*;
@Entity//作為 hibernate實體類
@Table(name = "tb_customer")//映射的表名
public class Customer {
    /**
     * @Id: 聲明主鍵的配置
     * @GeneratedValue: 配置主鍵的生成策略
     *        strategy :
     *            1. GenerationType.IDENTITY :自增 mysql
     *               底層數(shù)據(jù)庫必須支持自動增長 (底層數(shù)據(jù)庫支持的自動增長方式,對id自增)
     *            2. GenerationType.SEQUENCE : 序列 ,oracle
     *               底層書庫必須支持序列
     *            3. GenerationType.TABLE : jpa 提供的一種機制, 通過一張數(shù)據(jù)庫表的形式幫助我們完成主鍵的配置
     *            4. GenerationType.AUTO : 由程序自動的幫助我們選擇主鍵生成策略
     *   @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 實現(xiàn)   -->
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <!--            是否自動的表的生成  true 相當(dāng)于之前的 update  false 相當(dāng)于 none  -->
                <property name="generateDdl" value="true"/>
                <!--             是否顯示sql   -->
                <property name="showSql" value="true"/>
            </bean>
        </property>
        <!--        掃描實體類的包  來決定哪些實體類做 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&amp;useSSL=false&amp;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>
<!-- 啟動注解方式的聲明式事務(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.測試通過主鍵查詢

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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 深度優(yōu)先與廣度優(yōu)先Java實現(xiàn)代碼示例

    深度優(yōu)先與廣度優(yōu)先Java實現(xiàn)代碼示例

    這篇文章主要介紹了深度優(yōu)先與廣度優(yōu)先Java實現(xiàn)代碼示例,具有一定借鑒價值,需要的朋友可以參考下。
    2017-12-12
  • Kotlin 與 Java基本語法對比

    Kotlin 與 Java基本語法對比

    這篇文章主要介紹了Kotlin 與 Java基本語法對比的相關(guān)資料,需要的朋友可以參考下
    2017-05-05
  • java遞歸設(shè)置層級菜單的實現(xiàn)

    java遞歸設(shè)置層級菜單的實現(xiàn)

    本文主要介紹了java遞歸設(shè)置層級菜單的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-08-08
  • Java保留兩位小數(shù)的實現(xiàn)方法

    Java保留兩位小數(shù)的實現(xiàn)方法

    這篇文章主要介紹了 Java保留兩位小數(shù)的實現(xiàn)方法的相關(guān)資料,需要的朋友可以參考下
    2017-06-06
  • idea配置gradle全過程

    idea配置gradle全過程

    安裝Gradle首先需要解壓安裝包到指定目錄,隨后配置環(huán)境變量GRDLE_HOME和GRADLE_USER_HOME,這里的GRADLE_USER_HOME是指文件下載的路徑,安裝后,通過命令行輸入gradle -v來測試是否安裝成功,對于Idea的配置,需要通過File->Setting->Gradle進行
    2024-10-10
  • Spring Security代碼實現(xiàn)JWT接口權(quán)限授予與校驗功能

    Spring Security代碼實現(xiàn)JWT接口權(quán)限授予與校驗功能

    本文給大家介紹Spring Security代碼實現(xiàn)JWT接口權(quán)限授予與校驗功能,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友參考下吧
    2019-12-12
  • SpringBoot整合Gson 整合Fastjson的實例詳解

    SpringBoot整合Gson 整合Fastjson的實例詳解

    這篇文章主要介紹了SpringBoot整合Gson 整合Fastjson的實例詳解,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-11-11
  • 關(guān)于Spring @Bean 相同加載順序不同結(jié)果不同的問題記錄

    關(guān)于Spring @Bean 相同加載順序不同結(jié)果不同的問題記錄

    本文主要探討了在Spring 5.1.3.RELEASE版本下,當(dāng)有兩個全注解類定義相同類型的Bean時,由于加載順序不同,最終生成的Bean實例也會不同,文章通過分析ConfigurationClassPostProcessor的執(zhí)行過程,解釋了BeanDefinition的加載和覆蓋機制,感興趣的朋友一起看看吧
    2025-02-02
  • 淺談MyBatis-plus入門使用

    淺談MyBatis-plus入門使用

    這幾天本人了解到了MyBatis-plus,一個 Mybatis 增強工具包.經(jīng)過一番研究,發(fā)現(xiàn)這玩意真的好用,不用寫任何 xml ,內(nèi)置通用的 Mapper,而且完全是面向?qū)ο缶幊?文檔給的示例代碼,跟之前用過的 sequelize (Node.js 的 ORM)非常像,因此本人也嘗試了一把, 需要的朋友可以參考下
    2021-05-05
  • Maven之pom.xml文件中的Build配置解析

    Maven之pom.xml文件中的Build配置解析

    這篇文章主要介紹了Maven之pom.xml文件中的Build配置解析,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-12-12

最新評論