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注解

springboot结合jpa的更多相关文章

  1. Springboot+Atomikos+Jpa+Mysql实现JTA分布式事务

    1 前言 之前整理了一个spring+jotm实现的分布式事务实现,但是听说spring3.X后不再支持jotm了,jotm也有好几年没更新了,所以今天整理springboot+Atomikos+jp ...

  2. 【极简版】SpringBoot+SpringData JPA 管理系统

    前言 只有光头才能变强. 文本已收录至我的GitHub仓库,欢迎Star:https://github.com/ZhongFuCheng3y/3y 在上一篇中已经讲解了如何从零搭建一个SpringBo ...

  3. 带你搭一个SpringBoot+SpringData JPA的环境

    前言 只有光头才能变强. 文本已收录至我的GitHub仓库,欢迎Star:https://github.com/ZhongFuCheng3y/3y 不知道大家对SpringBoot和Spring Da ...

  4. 二、springboot使用jpa

    花了几天时间,好好看了看springboot的jpa部分,总结了常用的形式. 1.通过STS工具添加jpa的依赖项 要连mysql,测试的时候需要web,顺便添加了lombok不写set和get方法了 ...

  5. Springboot+MyBatis+JPA集成

      1.前言 Springboot最近可谓是非常的火,本人也在项目中尝到了甜头.之前一直使用Springboot+JPA,用了一段时间发现JPA不是太灵活,也有可能是我不精通JPA,总之为了多学学Sp ...

  6. 第11章—使用对象关系映射持久化数据—SpringBoot+SpringData+Jpa进行查询修改数据库

    SpringBoot+SpringData+Jpa进行查询修改数据库 JPA由EJB 3.0软件专家组开发,作为JSR-220实现的一部分.但它又不限于EJB 3.0,你可以在Web应用.甚至桌面应用 ...

  7. 集成Springboot+MyBatis+JPA

    1.前言 Springboot最近可谓是非常的火,本人也在项目中尝到了甜头.之前一直使用Springboot+JPA,用了一段时间发现JPA不是太灵活,也有可能是我不精通JPA,总之为了多学学Spri ...

  8. SpringBoot Data JPA 关联表查询的方法

    SpringBoot Data JPA实现 一对多.多对一关联表查询 开发环境 IDEA 2017.1 Java1.8 SpringBoot 2.0 MySQL 5.X 功能需求 通过关联关系查询商店 ...

  9. 用SpringBoot+MySql+JPA实现对数据库的增删改查和分页

    使用SpringBoot+Mysql+JPA实现对数据库的增删改查和分页      JPA是Java Persistence API的简称,中文名Java持久层API,是JDK 5.0注解或XML描述 ...

  10. springboot使用Jpa连接数据库

    springboot使用Jpa连接数据库 1.pom.xml: <?xml version="1.0" encoding="UTF-8"?> < ...

随机推荐

  1. Python 之父的解析器系列之五:左递归 PEG 语法

    原题 | Left-recursive PEG grammars 作者 | Guido van Rossum(Python之父) 译者 | 豌豆花下猫("Python猫"公众号作者 ...

  2. List集合的排序

    最近在写需求时,将某张表中的短信信息拿出,sql写完后,取出来后的结构是List<Map>,在进行到某一步时需要将这个List<Map>进行逆序排序, 当时第一想法便是写一个f ...

  3. 工作中遇到的99%SQL优化,这里都能给你解决方案(三)

    -- 示例表 CREATE TABLE `employees` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(24) NOT NULL ...

  4. MySql(二)_NHibernateHelper管理会话工厂

    1.定义接口的好处: (1) 清楚的看到里面有哪些方法: ( 2 )  可以更换实现类:Nhibernate实现件可以更换: Manger文件夹(另外两个是Model.Mappings文件夹) 首先M ...

  5. C#中Path类的常用方法

    场景 打开VS,输入Path,我们可以看到其定义. 都是静态方法,所以我们可以使用类名直接调用. 实现 新建命令行程序,编码如下: string str = @"C:\Users\Admin ...

  6. Java-HashSet集合中的几种遍历方式

    //我们先创建一个set集合 public static void main(String[] args) { Set<Integer> sets = new HashSet<> ...

  7. Java连载32-对象、类及其关系与定义

    一.采用面向对象的方式开发一个软件,生命周期之中: (1)面向对象的分析:OOA (2)面向对象的设计:OOD (3)面向对象的编程:OOP 二.类 定义:类在现实世界世界之中是不存在的,是一个模板, ...

  8. Linux 笔记 - 第十七章 Linux LVM 逻辑卷管理器

    一.前言 在实际生产中,有时会遇到磁盘分区空间不足的情况,这时候就需要对磁盘进行扩容,普通情况下需要新加一块磁盘,重分区.格式化.数据复制.卸载旧分区.挂载新分区等繁琐的步骤,而且有可能造成数据的丢失 ...

  9. Linux 笔记 - 第十三章 Linux 系统日常管理之(一)系统状态监控

    博客地址:http://www.moonxy.com 一.前言 如果你是一名 Linux 运维人员,最主要的工作是优化系统配置,使应用在系统上以最优的状态运行.系统运行状态主要包括:系统负载.内存状态 ...

  10. validator 自动化校验

    温馨提示 请收藏再看.此文篇幅太长,你短时间看不完:此文干货太多,错过太可惜. 示例代码可以关注逸飞兮(公众号)回复jy获取. 收获 讲解详细:能让你掌握使用 hibernate-validator ...