?
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&useSSL=false&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);}
}
?