idea中新建springboot項目,引入spring-boot-starter-data-jpa依賴
application.yml中配置數據庫連接,示例如下:
spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver username: root password: 123 url: jdbc:mysql://localhost/shell?characterEncoding=utf-8&userSSL=false jpa: show-sql: true
數據表如下:
create table `product_category` ( `category_id` int not null auto_increment, `category_name` varchar(64) not null comment '類目名字', `category_type` int not null comment '類目編號', `create_time` timestamp not null default current_timestamp comment '創建時間', `update_time` timestamp not null default current_timestamp on update current_timestamp comment '修改時間', primary key (`category_id`), unique key `uqe_category_type` (`category_type`) ) comment '類目表';
新建實體類ProductCategory,類上添加@Entity注解(javax.persistence-api依賴下)
新建接口ProductCategoryRepository,繼承JpaRepository接口(spring-data-jpa依賴下),指定泛型,如下:
public interface ProductCategoryRepository extends JpaRepository<ProductCategory, Integer> { }
新建測試類ProductCategoryRepositoryTest,注入productCategoryRepository,測試查詢方法,如下:
package com.example.shell.repository; import com.example.shell.dataobject.ProductCategory; 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.test.context.junit4.SpringRunner; import java.util.List; import java.util.Optional; import static org.junit.Assert.*; @RunWith(SpringRunner.class) @SpringBootTest public class ProductCategoryRepositoryTest { @Autowired private ProductCategoryRepository productCategoryRepository; @Test public void findOneTest(){ Optional<ProductCategory> productCategory = productCategoryRepository.findById(1); System.out.println(productCategory); } }
報錯 No identifier specified for entity:
錯誤處理:實體類ProductCategory的categoryId字段上添加@Id注解(javax-persistence-api依賴下)
測試插入方法:
@Test public void saveTest(){ ProductCategory productCategory = new ProductCategory(); productCategory.setCategoryName("男生最愛"); productCategory.setCategoryType(3); productCategoryRepository.save(productCategory); }
報錯ids for this class must be manually assigned before calling save():
錯誤處理:實體類ProductCategory的categoryId字段上添加@GeneratedValue(strategy = GenerationType.IDENTITY) 在javax-persistence-api依賴下
實體類ProductCategory字段及注解如下
@Entity @DynamicUpdate public class ProductCategory { /** 類目id. */ @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer categoryId; /** 類目名字. */ private String categoryName; /** 類目編號. */ private Integer categoryType; private Date createTime; private Date updateTime;
再次測試插入方法:
@Test public void saveTest(){ Optional<ProductCategory> productCategory = productCategoryRepository.findById(2); productCategory.get().setCategoryType(11); // ProductCategory productCategory = new ProductCategory(); // productCategory.setCategoryId(2); // productCategory.setCategoryName("男生最愛"); // productCategory.setCategoryType(3); productCategoryRepository.save(productCategory.get()); }
若發現更新時間並沒有改變,應在實體類ProductCategory上添加@DynamicUpdate注解(hibernate-core依賴下)
其他,實體類中的屬性是與數據表中的字段相對應的,若在實體類中添加了額外的屬性,可以在屬性上加@Transient注解