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注解
springboot结合jpa的更多相关文章
- Springboot+Atomikos+Jpa+Mysql实现JTA分布式事务
1 前言 之前整理了一个spring+jotm实现的分布式事务实现,但是听说spring3.X后不再支持jotm了,jotm也有好几年没更新了,所以今天整理springboot+Atomikos+jp ...
- 【极简版】SpringBoot+SpringData JPA 管理系统
前言 只有光头才能变强. 文本已收录至我的GitHub仓库,欢迎Star:https://github.com/ZhongFuCheng3y/3y 在上一篇中已经讲解了如何从零搭建一个SpringBo ...
- 带你搭一个SpringBoot+SpringData JPA的环境
前言 只有光头才能变强. 文本已收录至我的GitHub仓库,欢迎Star:https://github.com/ZhongFuCheng3y/3y 不知道大家对SpringBoot和Spring Da ...
- 二、springboot使用jpa
花了几天时间,好好看了看springboot的jpa部分,总结了常用的形式. 1.通过STS工具添加jpa的依赖项 要连mysql,测试的时候需要web,顺便添加了lombok不写set和get方法了 ...
- Springboot+MyBatis+JPA集成
1.前言 Springboot最近可谓是非常的火,本人也在项目中尝到了甜头.之前一直使用Springboot+JPA,用了一段时间发现JPA不是太灵活,也有可能是我不精通JPA,总之为了多学学Sp ...
- 第11章—使用对象关系映射持久化数据—SpringBoot+SpringData+Jpa进行查询修改数据库
SpringBoot+SpringData+Jpa进行查询修改数据库 JPA由EJB 3.0软件专家组开发,作为JSR-220实现的一部分.但它又不限于EJB 3.0,你可以在Web应用.甚至桌面应用 ...
- 集成Springboot+MyBatis+JPA
1.前言 Springboot最近可谓是非常的火,本人也在项目中尝到了甜头.之前一直使用Springboot+JPA,用了一段时间发现JPA不是太灵活,也有可能是我不精通JPA,总之为了多学学Spri ...
- SpringBoot Data JPA 关联表查询的方法
SpringBoot Data JPA实现 一对多.多对一关联表查询 开发环境 IDEA 2017.1 Java1.8 SpringBoot 2.0 MySQL 5.X 功能需求 通过关联关系查询商店 ...
- 用SpringBoot+MySql+JPA实现对数据库的增删改查和分页
使用SpringBoot+Mysql+JPA实现对数据库的增删改查和分页 JPA是Java Persistence API的简称,中文名Java持久层API,是JDK 5.0注解或XML描述 ...
- springboot使用Jpa连接数据库
springboot使用Jpa连接数据库 1.pom.xml: <?xml version="1.0" encoding="UTF-8"?> < ...
随机推荐
- codeforces 762 D. Maximum path(dp)
题目链接:http://codeforces.com/problemset/problem/762/D 题意:给出一个3*n的矩阵然后问从左上角到右下角最大权值是多少,而且每一个点可以走上下左右,但是 ...
- codeforces 766 D. Mahmoud and a Dictionary(种类并查集+stl)
题目链接:http://codeforces.com/contest/766/problem/D 题意:给你n个单词,m个关系(两个单词是反义词还是同义词),然后问你所给的关系里面有没有错的,最后再给 ...
- CSU 1803 2016 湖南省2016省赛
1803: 2016 Submit Page Summary Time Limit: 5 Sec Memory Limit: 128 Mb Submitted: 1416 ...
- Redis缓存穿透、缓存雪崩、并发问题分析与解决方案
(一)缓存和数据库间数据一致性问题 分布式环境下(单机就不用说了)非常容易出现缓存和数据库间的数据一致性问题,针对这一点的话,只能说,如果你的项目对缓存的要求是强一致性的,那么请不要使用缓存.我们只能 ...
- springboot中动态修改log4j2日志级别
springboot中动态修改log4j2日志级别 在spring boot中使用log4j2日志时,项目运行中,想要修改日志级别. 1.pom.xml依赖: <dependency> & ...
- 纯css制作电闪雷鸣的天气图标
效果 效果图如下 实现思路 使用box-shadow属性写几个圆,将这些圆错落的组合在一起,形成云朵图案 after伪元素写下面的投影样式 before伪元素写黄色闪电的样式 dom结构 用两个嵌 ...
- jquery多级树形下拉菜单
效果图: 使用方法 (1)引入 jQuery 包,下载地址 (2)引入 zTree 包,下载地址 (3)引入 tree-select.js (4)$("#id").treeSele ...
- Java8之熟透Lambda表达式
一.Lambda简述 1.1.Lambda概述 Lambda 表达式可以理解为简洁地表示可传递的匿名函数的一种方式:它没有名称,但它有参数列表.函数主体.返回类型,可能还有一个可以抛出的异常列表. ...
- Python—字符串和常用数据结构
目录 1. 字符串 2. 列表 2.1 列表的增删改查 2.2 列表的切片和排序 2.3 生成式语法 3. 元组 4.集合 5. 字典 5.1 字典的增删改查 5.2 字典的常见操作 序言:这一章我们 ...
- 浅谈ViewPager与TabLayout的简单用法
今天介绍一下ViewPager与TabLayout的简单用法 1.准备 在一切开始之前,你懂得,先导库,老方法,在build.gradle直接添加下面这一句 implementation ...