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

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

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

1.導(dǎo)入版本管理依賴(lài) 到父項(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 依賴(lài) 在子模塊

  <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í)體類(lèi)

package com.kuang.pojo;
import javax.persistence.*;
@Entity//作為 hibernate實(shí)體類(lèi)
@Table(name = "tb_customer")//映射的表名
public class Customer {
    /**
     * @Id: 聲明主鍵的配置
     * @GeneratedValue: 配置主鍵的生成策略
     *        strategy :
     *            1. GenerationType.IDENTITY :自增 mysql
     *               底層數(shù)據(jù)庫(kù)必須支持自動(dòng)增長(zhǎng) (底層數(shù)據(jù)庫(kù)支持的自動(dòng)增長(zhǎng)方式,對(duì)id自增)
     *            2. GenerationType.SEQUENCE : 序列 ,oracle
     *               底層書(shū)庫(kù)必須支持序列
     *            3. GenerationType.TABLE : jpa 提供的一種機(jī)制, 通過(guò)一張數(shù)據(jù)庫(kù)表的形式幫助我們完成主鍵的配置
     *            4. GenerationType.AUTO : 由程序自動(dòng)的幫助我們選擇主鍵生成策略
     *   @Column(name = "cust_id") 配置屬性和字段的映射關(guān)系
     *       name: 數(shù)據(jù)庫(kù)表中字段的名稱(chēng)
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "cust_id")
    private Long custId;//客戶(hù)的主鍵
    @Column(name = "cust_name")
    private String custName;//客戶(hù)的名稱(chēng)
    @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í)體類(lèi)的包  來(lái)決定哪些實(shí)體類(lèi)做 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>
<!-- 啟動(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è)試通過(guò)主鍵查詢(xún)

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)文章

  • 一篇文章帶你詳解Spring的AOP

    一篇文章帶你詳解Spring的AOP

    這篇文章主要為大家介紹了Spring的AOP,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助
    2022-01-01
  • java面向?qū)ο笤O(shè)計(jì)原則之接口隔離原則示例詳解

    java面向?qū)ο笤O(shè)計(jì)原則之接口隔離原則示例詳解

    這篇文章主要為大家介紹了java面向?qū)ο笤O(shè)計(jì)原則之接口隔離原則的示例詳解,有需要的朋友可以借鑒參考下希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪
    2021-10-10
  • SpringBoot中@Valid對(duì)List校驗(yàn)失效問(wèn)題的有效解決方法

    SpringBoot中@Valid對(duì)List校驗(yàn)失效問(wèn)題的有效解決方法

    在Spring Boot應(yīng)用開(kāi)發(fā)中,我們經(jīng)常需要對(duì)傳入的請(qǐng)求參數(shù)進(jìn)行校驗(yàn),以確保數(shù)據(jù)的合法性和安全性,然而,當(dāng)我們嘗試對(duì)列表(List)類(lèi)型的參數(shù)進(jìn)行校驗(yàn)時(shí),可能會(huì)遇到校驗(yàn)失效的問(wèn)題,本文將詳細(xì)探討這一問(wèn)題的失效原因,并提供有效的解決方法,需要的朋友可以參考下
    2025-07-07
  • java 排序算法之歸并排序

    java 排序算法之歸并排序

    本文主要講解了排序算法中的歸并排序,文中運(yùn)用大量的圖片和代碼講解的非常詳細(xì),感興趣的朋友可以學(xué)習(xí)一下這篇文章,相信可以幫助到你
    2021-09-09
  • Java 注解的使用實(shí)例詳解

    Java 注解的使用實(shí)例詳解

    這篇文章主要介紹了Java 注解的使用實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下
    2016-12-12
  • spring定義和裝配bean詳解

    spring定義和裝配bean詳解

    這篇文章主要介紹了spring定義和裝配bean詳解,具有一定參考價(jià)值,需要的朋友可以了解下。
    2017-12-12
  • Mybatis 入門(mén)示例代碼之 Association

    Mybatis 入門(mén)示例代碼之 Association

    這篇文章主要介紹了Mybatis 入門(mén)示例代碼之 Association,需要的的朋友參考下
    2017-02-02
  • JAVA?從完整的文件路徑中分別截取文件名和文件路徑的實(shí)現(xiàn)

    JAVA?從完整的文件路徑中分別截取文件名和文件路徑的實(shí)現(xiàn)

    在Java編程中,經(jīng)常會(huì)遇到需要截取文件名的場(chǎng)景,本文主要介紹了JAVA?從完整的文件路徑中分別截取文件名和文件路徑的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-04-04
  • springboot集成redis哨兵集群的實(shí)現(xiàn)示例

    springboot集成redis哨兵集群的實(shí)現(xiàn)示例

    本文主要介紹了springboot集成redis哨兵集群的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-08-08
  • Spring?IOC注入的兩種方式詳解以及代碼示例

    Spring?IOC注入的兩種方式詳解以及代碼示例

    在Spring框架中,依賴(lài)注入(Dependency?Injection,DI)是通過(guò)控制反轉(zhuǎn)(Inversion?of?Control,IOC)實(shí)現(xiàn)的,Spring提供了多種方式來(lái)實(shí)現(xiàn)IOC注入,本文就給大家介紹兩種注入的方式:基于XML和基于注解,文中有詳細(xì)的代碼示例,需要的朋友可以參考下
    2023-08-08

最新評(píng)論

上思县| 临湘市| 军事| 吴桥县| 涞源县| 恩平市| 盐池县| 诸城市| 河曲县| 昂仁县| 德格县| 滨海县| 和平县| 富锦市| 达尔| 湖口县| 焦作市| 濮阳县| 云南省| 邯郸市| 将乐县| 普定县| 云龙县| 泾川县| 离岛区| 张掖市| 锡林浩特市| 塘沽区| 松原市| 孙吴县| 邯郸市| 柯坪县| 罗江县| 罗源县| 陆良县| 巴林右旗| 德化县| 绥滨县| 土默特左旗| 南宫市| 嘉黎县|