springboot结合jpa
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注解

浙公网安备 33010602011771号