1、在pom.xml中需要添加jpa的依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
2、application.yml中配置數據庫連接和jpa的相關屬性
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://192.168.1.18/db_sell?charactorEncoding=utf-8&useSSL=false
username: root
password: 123456
jpa:
show-sql: true
3、實體類的使用(ProductCategory類)
import lombok.Data;
import lombok.RequiredArgsConstructor;
import javax.persistence.*;
/**
* 這是商品類目的實體類
*/
@Entity
@Data
public class ProductCategory {
/**
* 類別id
*/
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer categoryId;
/**
* 類別名稱
*/
@Column(name = "category_name")
private String categoryName;
/**
* 類別編號
*/
@Column(name = "category_type")
private Integer categoryType;
public ProductCategory() {
}
public ProductCategory(String categoryName, Integer categoryType) {
this.categoryName = categoryName;
this.categoryType = categoryType;
}
}
@Entity註解代表說明這是一個實體類
@GeneratedValue 主鍵自增策略,有sequence,table,identity和auto四種策略,默認是AUTO,但是我在使用jpa連接mysql的時候一直報增長策略的錯誤,建議直接設置strategy爲對應數據庫的增長策略,例如mysql爲identity
@Data是使用的lombok插件 它的作用是在編譯階段動態生成成員變量的getset方法和toString方法
@Setter@Getter同時用也能生成gettersetter方法 這兩個註解也是lombok的註解
@NoArgsConstructor 動態創建無參構造方法 lombok的註解
4、dao層jpa的基本使用
import com.xaq.entity.ProductCategory;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;
import java.util.Optional;
/**
*
*/
public interface ProductCategoryRepository extends JpaRepository<ProductCategory,Integer> {
/**
* 通過類別編號查詢
* @param categoryType
* @return
*/
ProductCategory findByCategoryType(Integer categoryType);
/**
* 通過類別id查詢
* @param categoryId
* @return
*/
ProductCategory findByCategoryId(Integer categoryId);
/**
* 通過類別編號範圍得到列表
* @param categoryTypes
* @return
*/
List<ProductCategory> findByCategoryTypeIn(List<Integer> categoryTypes);
}
需要繼承JpaRepository接口,泛型的第一個參數爲綁定的實體類,第二個參數爲主鍵id的類型
默認就可以使用該接口提供的方法,需要動態通過某個參數進行查詢的話,可以定義爲findBy參數名字,裏面的api方法衆多,至於排序分頁查詢jpa接口文檔
5、lombok插件安裝,這裏本機已經安裝了,未安裝應該顯示爲install,點擊安裝
6、添加依賴到pom.xml中
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
7、springboot測試(測試ProductCategoryRepository接口)
package com.xaq.dao;
import com.xaq.entity.ProductCategory;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import static org.junit.Assert.*;
/**
* 商品類別dao測試
*/
@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
public class ProductCategoryRepositoryTest {
@Autowired
private ProductCategoryRepository repository;
/**
* 測試保存實體
* @throws Exception
*/
@Test
public void testSave () throws Exception {
ProductCategory productCategory = new ProductCategory("我的天",4);
repository.save(productCategory);
}
/**
* 測試查找所有的類別實體
* @throws Exception
*/
@Test
public void testFindAll() throws Exception {
List<ProductCategory> list = repository.findAll();
for (ProductCategory productCategory : list) {
System.out.println(productCategory);
}
}
/**
* 測試通過類別編號查詢
* @throws Exception
*/
@Test
public void testFindByCategoryType() throws Exception {
ProductCategory productCategory = repository.findByCategoryType(4);
log.info(productCategory.toString());
}
/**
* 測試通過類別id查詢
* @throws Exception
*/
@Test
public void testFindByCategoryId() throws Exception {
ProductCategory productCategory = repository.findByCategoryId(1);
ProductCategory productCategory1 = repository.findById(1).get();
log.info(productCategory.toString());
log.info(productCategory1.toString());
}
/**
* 測試通過一個返回的類別編號查詢
* @throws Exception
*/
@Test
public void testFindByCategoryTypeIn() throws Exception {
List<Integer> list = Arrays.asList(1,2,3,4);
List<ProductCategory> resultList = repository.findByCategoryTypeIn(list);
for(ProductCategory productCategory : resultList){
log.info(productCategory.toString());
}
}
}
@Slf4J 是lombok的註解,它的性質相當於
private static final Logger log= LoggerFactory.getLogger(this.getClass())
使用了這個註解直接可以使用log.info() 或者其它級別的方法