idea中新建springboot项目,引入spring-boot-starter-data-jpa依赖

application.yml中配置数据库连接,示例如下:

  1. spring:
  2. datasource:
  3. driver-class-name: com.mysql.cj.jdbc.Driver
  4. username: root
  5. password: 123
  6. url: jdbc:mysql://localhost/shell?characterEncoding=utf-8&userSSL=false
  7. jpa:
  8. show-sql: true

数据表如下:

  1. create table `product_category` (
  2. `category_id` int not null auto_increment,
  3. `category_name` varchar(64) not null comment '类目名字',
  4. `category_type` int not null comment '类目编号',
  5. `create_time` timestamp not null default current_timestamp comment '创建时间',
  6. `update_time` timestamp not null default current_timestamp on update current_timestamp comment '修改时间',
  7. primary key (`category_id`),
  8. unique key `uqe_category_type` (`category_type`)
  9. ) comment '类目表';

新建实体类ProductCategory,类上添加@Entity注解(javax.persistence-api依赖下)

新建接口ProductCategoryRepository,继承JpaRepository接口(spring-data-jpa依赖下),指定泛型,如下:

  1. public interface ProductCategoryRepository extends JpaRepository<ProductCategory, Integer> {
  2. }

新建测试类ProductCategoryRepositoryTest,注入productCategoryRepository,测试查询方法,如下:

  1. package com.example.shell.repository;
  2.  
  3. import com.example.shell.dataobject.ProductCategory;
  4. import org.junit.Test;
  5. import org.junit.runner.RunWith;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.boot.test.context.SpringBootTest;
  8. import org.springframework.test.context.junit4.SpringRunner;
  9.  
  10. import java.util.List;
  11. import java.util.Optional;
  12.  
  13. import static org.junit.Assert.*;
  14.  
  15. @RunWith(SpringRunner.class)
  16. @SpringBootTest
  17. public class ProductCategoryRepositoryTest {
  18.  
  19. @Autowired
  20. private ProductCategoryRepository productCategoryRepository;
  21.  
  22. @Test
  23. public void findOneTest(){
  24. Optional<ProductCategory> productCategory = productCategoryRepository.findById(1);
  25. System.out.println(productCategory);
  26. }
  27.  
  28. }

报错 No identifier specified for entity:

错误处理:实体类ProductCategory的categoryId字段上添加@Id注解(javax-persistence-api依赖下)

测试插入方法:

  1. @Test
  2. public void saveTest(){
  3. ProductCategory productCategory = new ProductCategory();
  4. productCategory.setCategoryName("男生最爱");
  5. productCategory.setCategoryType(3);
  6. productCategoryRepository.save(productCategory);
  7. }

报错ids for this class must be manually assigned before calling save():

错误处理:实体类ProductCategory的categoryId字段上添加@GeneratedValue(strategy = GenerationType.IDENTITY) 在javax-persistence-api依赖下

实体类ProductCategory字段及注解如下

  1. @Entity
  2. @DynamicUpdate
  3. public class ProductCategory {
  4.  
  5. /** 类目id. */
  6. @Id
  7. @GeneratedValue(strategy = GenerationType.IDENTITY)
  8. private Integer categoryId;
  9.  
  10. /** 类目名字. */
  11. private String categoryName;
  12.  
  13. /** 类目编号. */
  14. private Integer categoryType;
  15.  
  16. private Date createTime;
  17.  
  18. private Date updateTime;

再次测试插入方法:

  1.   @Test
  2. public void saveTest(){
  3. Optional<ProductCategory> productCategory = productCategoryRepository.findById(2);
  4. productCategory.get().setCategoryType(11);
  5.  
  6. // ProductCategory productCategory = new ProductCategory();
  7. // productCategory.setCategoryId(2);
  8. // productCategory.setCategoryName("男生最爱");
  9. // productCategory.setCategoryType(3);
  10. productCategoryRepository.save(productCategory.get());
  11. }

若发现更新时间并没有改变,应在实体类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. codeforces 762 D. Maximum path(dp)

    题目链接:http://codeforces.com/problemset/problem/762/D 题意:给出一个3*n的矩阵然后问从左上角到右下角最大权值是多少,而且每一个点可以走上下左右,但是 ...

  2. codeforces 766 D. Mahmoud and a Dictionary(种类并查集+stl)

    题目链接:http://codeforces.com/contest/766/problem/D 题意:给你n个单词,m个关系(两个单词是反义词还是同义词),然后问你所给的关系里面有没有错的,最后再给 ...

  3. CSU 1803 2016 湖南省2016省赛

    1803: 2016 Submit Page   Summary   Time Limit: 5 Sec     Memory Limit: 128 Mb     Submitted: 1416    ...

  4. Redis缓存穿透、缓存雪崩、并发问题分析与解决方案

    (一)缓存和数据库间数据一致性问题 分布式环境下(单机就不用说了)非常容易出现缓存和数据库间的数据一致性问题,针对这一点的话,只能说,如果你的项目对缓存的要求是强一致性的,那么请不要使用缓存.我们只能 ...

  5. springboot中动态修改log4j2日志级别

    springboot中动态修改log4j2日志级别 在spring boot中使用log4j2日志时,项目运行中,想要修改日志级别. 1.pom.xml依赖: <dependency> & ...

  6. 纯css制作电闪雷鸣的天气图标

    效果 效果图如下 ​ 实现思路 使用box-shadow属性写几个圆,将这些圆错落的组合在一起,形成云朵图案 after伪元素写下面的投影样式 before伪元素写黄色闪电的样式 dom结构 用两个嵌 ...

  7. jquery多级树形下拉菜单

    效果图: 使用方法 (1)引入 jQuery 包,下载地址 (2)引入 zTree 包,下载地址 (3)引入 tree-select.js (4)$("#id").treeSele ...

  8. Java8之熟透Lambda表达式

    一.Lambda简述 1.1.Lambda概述 ​ Lambda 表达式可以理解为简洁地表示可传递的匿名函数的一种方式:它没有名称,但它有参数列表.函数主体.返回类型,可能还有一个可以抛出的异常列表. ...

  9. Python—字符串和常用数据结构

    目录 1. 字符串 2. 列表 2.1 列表的增删改查 2.2 列表的切片和排序 2.3 生成式语法 3. 元组 4.集合 5. 字典 5.1 字典的增删改查 5.2 字典的常见操作 序言:这一章我们 ...

  10. 浅谈ViewPager与TabLayout的简单用法

      今天介绍一下ViewPager与TabLayout的简单用法 1.准备   在一切开始之前,你懂得,先导库,老方法,在build.gradle直接添加下面这一句   implementation ...