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"?> < ...
随机推荐
- Dokit支持iOS本地crash查看功能
一.前言 在日常开发中或者测试过程中,我们的应用可能会出现Crash的问题.对于这类问题我们要抱着零容忍的态度,因为如果线上出现了这类问题,将会严重影响用户的体验. 如果Crash出现的时候恰好是在开 ...
- 前端-HTML-web服务本质-HTTP协议-请求-标签-01(待完善)
目录 前端 什么是前端 什么是后端 学习流程 前端三剑客的形容 web服务的本质 测试--浏览器作为客户端向服务器发起请求 浏览器输入网址回车发生了几件事 ***** HTTP协议(超文本传输协议) ...
- centos7安装mongodb以及使用
https://blog.csdn.net/sun007700/article/details/100671570
- 错误:java.lang.Exception: No tests found matching Method testPrePage1(egou_manager_web.TestEBrand) from org.junit.internal.requests.ClassRequest@4f3cc73c
今天测试分页时出现以下错误: java.lang.Exception: No tests found matching Method testPrePage1(egou_manager_web.Tes ...
- Redis小白入门系列
一.从NoSQL说起 NoSQL 是 Not only SQL 的缩写,大意为"不只是SQL",说明这项技术是传统关系型数据库的补充而非替代.在整个NoSQL技术栈中 MemCac ...
- cobbler高可用方案
一.环境准备 主网IP 私网IP 主机名 角色 VIP 10.203.178.125 192.168.10.2 cnsz22VLK12919 主 10.203.178.137,192.168.10.1 ...
- UGUI_创建旋转物体,使用Slider控制小球旋转速度
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Player : ...
- Winform中怎样根据Name获取同窗体的控件
场景 在同一个Winform窗体中,点击一个Button按钮时, 获取同窗体的其他控件的属性. 首先需要对要获取的控件赋予Name属性,然后就可以通过Name进行获取. 实现 在Button的点击事件 ...
- 通过js获取tinymce4.x的值
问题的引出: 在使用过程中,用传统的js的方法判断tinymce所选textarea(下面直接称textarea)的值是会出现这样的问题的: 在已有输入内容时,首次提交的时候,依然会弹出js写的警告提 ...
- JAVA面试问题与解答(1-15)
Q1.内部类和子类之间有什么区别? Ans:Inner类是嵌套在另一个类中的类.内类具有嵌套它的类的访问权限,并且它可以访问外部类中定义的所有变量和方法. 子类是从另一个名为super class的类 ...