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"?> < ...
随机推荐
- 第 14 篇:交流的桥梁“评论功能”——HelloDjango 系列教程
截止到目前为止我们的 django blog 文章展示部分,已经实现的"八九不离十"了.你以为本系列文章就要结束了吗?不能够!新的征程才刚刚开始,HelloDjango 系列文章刚 ...
- 2018 Multi-University Training Contest 2(部分题解)
Game Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submi ...
- 手工释放服务器的swap分区缓存
时间 恢复时间 状态 信息 主机 问题 • 严重性 持续时间 确认 动作 2019-03-21 20:29:30 09:51:30 -ai-代理 Lack of free sw ...
- Python作业本——前言
大四毕业了,9月才开始研究生生涯,导师也没有严格要求我暑假留校做项目,也没提具体的学习要求.这两三个月比较闲,所以就打算学学Python.学习过程中肯定会有些心得体会,以及一些小练习.学习编程不同于传 ...
- 013 turtle程序语法元素分析
目录 一.概述 二.库引用与import 2.1 库引用 2.2 使用from和import保留字共同完成库引用 2.3 两种库引用方法比较 2.4 使用import和as保留字共同完成库引用 三.t ...
- (1)jsoncpp库的使用
本节主要介绍 json是什么以及jsoncpp库的使用. (1)JSON是什么 json 是一种轻量级的文本数据交换格式: json 独立于语言.平台,使用java script语法来描述对象 ...
- 关于git远程被覆盖的问题
有同事A和B,git远程版本为A0,两个人的本地项目已经跟远程同步.同事A先向git提交了3次,A1.A2.A3.git远程版本为A0.A1.A2.A3.同事B也向git提交了1次B1,但是同事B提交 ...
- 计时器Chronometer
布局文件很简单 <Chronometer android:id="@+id/test" android:layout_width="wrap_content&quo ...
- redis的几个知识点
Redis的全称是Remote Dictionary Server,即远程字典服务,通常用作服务器缓存服务. 这里通过Redis的几个知识点来了解Redis. Redis的通讯协议 Redis的通讯协 ...
- Integer对象大小比较问题
一.问题 先来看一看例子 public class IntegerTest { public static void main(String[] args) throws Exception { In ...