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"?> < ...
随机推荐
- Unity3D 客户端编程
Photon Server 和 Unity3D 数据交互: Photon Server 服务端编程 Unity3D 客户端编程. VS2017 之 MYSQL实体数据模 1:打开unity新建新项目, ...
- PHP. 02®. Ajax异步处理、常见的响应状态、XMLHttpRequest对象及API、ajax的get/post方法、
异步对象 a)创建异步对象 b)设置请求的url等参数 c) 发送请求 d)注册时间 e)在注册的事件中获取返回的内容并修改页面显示的内容 布尔类型不能直接用echo输出 常见的响应状态 Ajax概 ...
- 讲解开源项目:用 Python 生成有“灵魂”的二维码
本文作者:HelloGitHub-LITTLECHIEH 这是 HelloGitHub 推出的<讲解开源项目>系列,今天给大家推荐一个 Python 开源生成二维码的项目--qrcode ...
- 详细的漏洞复现:Shellshock CVE-2014-6271 CVE-2014-7169
目录 前言 漏洞原理 利用方式 复现过程 1. 环境准备 (1) 为容器配置固定IP地址 (2) 查看bash版本 2. 本地验证:测试镜像系统是否存在漏洞 3. 远程模拟验证(原理验证) (1) 查 ...
- Unity - 2D中的物理关节
本文概述: 分析Unity中几个2D物理关节组件的基本功能.使用方法.运用场景等 开发环境:Unity2019.3.0a2 / VS2017 项目资源包: 2D Joints Starter 说明: ...
- .Net基础篇_学习笔记_第七天_随机数的产生
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- zookeeper伪集群
Zookeeper运行需要java环境,需要安装jdk,建议本地下载好需要的安装包然后上传到服务器上面,服务器上面下载速度太慢. 上传 [root@192 ~]# java -version java ...
- [LeetCode] 由 “找零钱" 所想
Ref: [Optimization] Dynamic programming[寻找子问题] Ref: [Optimization] Advanced Dynamic programming[优于re ...
- Windows(Win7)搭建RabbitMQ服务器
首先安装Erlang环境,RabbitMQ的运行依赖于Erlang.可以在官网链接http://www.erlang.org/downloads 页面找到对应的开发环境安装包.例如64位Windows ...
- AWS加入.NET Foundation企业赞助商计划
.NET 走向开源,MIT许可协议. 微软为了推动.NET开源社区的发展,2014年联合社区成立了.NET基金会. .NET基金会是一个独立的组织,支持.NET社区和开源,旨在拓宽和加强.NET生态系 ...