使用spring data JPA进行类目增删改查测试
注意两点
- 增加和修改都是用save方法来实现的
- 在使用更新方法的时候,时间并没有自动更新,需要在ProductCategory类上加@DynamicUpdate注解,实现自动更新
另:在测试方法上加上@Transactional注解,在测试方法执行完之后,会自动回滚。
因为会回滚,那我么怎么知道是否成功了呢,我们可以加上断言Assert.assertNotNull(result);
如果result不为null,则通过,如果为null,则抛出异常。
ProductCategory
package com.lbl.dataObject;import lombok.Data;
import org.hibernate.annotations.DynamicUpdate;import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import java.util.Date;/*** 类目* Created by 李柏霖* 2020-10-09 20:41* product_category*/
@Entity
@DynamicUpdate
@Data
public class ProductCategory {/** 类目id. */@Id@GeneratedValueprivate Integer categoryId;/** 类目名字. */private String categoryName;/** 类目编号. */private Integer categoryType;private Date createTime;private Date updateTime;public ProductCategory() {}public ProductCategory(String categoryName, Integer categoryType) {this.categoryName = categoryName;this.categoryType = categoryType;}
}
ProductCategoryRepository
package com.lbl.repository;import com.lbl.dataObject.ProductCategory;
import org.springframework.data.jpa.repository.JpaRepository;/*** Created by 李柏霖* 2020-10-09 21:34*/
public interface ProductCategoryRepository extends JpaRepository<ProductCategory, Integer> {}
ProductCategoryRepositoryTest
package com.lbl.repository;import com.lbl.dataObject.ProductCategory;
import org.junit.Assert;
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 org.springframework.transaction.annotation.Transactional;/*** Created by 李柏霖* 2020-10-09 21:35*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class ProductCategoryRepositoryTest {@Autowiredprivate ProductCategoryRepository repository;@Testpublic void findOneTest() {ProductCategory productCategory = repository.findOne(1);System.out.println(productCategory.toString());}@Test@Transactionalpublic void addCategoryTest() {ProductCategory productCategory = new ProductCategory("女生最爱",2);ProductCategory result = repository.save(productCategory);Assert.assertNotNull(result);System.out.println(result.toString());}@Testpublic void updateCategoryTest() {ProductCategory productCategory = repository.findOne(2);productCategory.setCategoryType(10);ProductCategory save = repository.save(productCategory);System.out.println(save.toString());}@Testpublic void deleteCategoryTest() {repository.delete(3);}}
使用spring data JPA进行类目增删改查测试相关推荐
- ORM框架之Spring Data JPA(二)spring data jpa方式的基础增删改查
上一篇主要在介绍hibernate实现jpa规范,如何实现数据增删改查,这一篇将会着重spring data jpa 一.Spring Data JPA 1.1 Spring Data JPA介绍: ...
- 【Spring Data JPA】基于 JpaRepository 增删改查
文章目录 创建实体类 定义JPA查询接口 增.删.改.查 增加.修改 删除 删除[根据实体类删除] 删除[根据实体类主键删除] 查询 简单查询 查询单个信息[findBy] 查询多个信息[findAl ...
- Spring Data JDBC自动生成的增删改查CRUD分页、排序SQL语句非常简洁没有多余的SQL
通过在application.properties文件中设置记录SQL日志 logging.level.org.springframework.jdbc.core.JdbcTemplate=DEBUG ...
- 数据操作类:增删改查
一.数据连接类 using System; using System.Collections.Generic; using System.Linq; using System.Text; using ...
- java 怎么快速找到实现类_JAVA懒开发:FreeMarker快速实现类的增删改查接口
太懒,不多说看图,mybatis-generator工具执行后的项目结构 这个时候没得额service接口类,和service实现类,也没的controller类 执行引擎工具类BeanGenerat ...
- DBUtils工具类实现增删改查
DBUtils工具类实现增删改查 一.数据库连接池Druid工具类 二.DBUtils实现增删改 三.DBUtils实现查询单条数据 四.DBUtils实现查询批量数据 五.DBUtils实现按键值对 ...
- 增删改查测试场景(菜市场的大妈都能看懂)
前言 1.相同点 2.不同点 2.1.增加 2.2.删除 2.3.查询 2.4.修改 前言 1.本想在网上找一下资料,质量很差.要不然就是一些没太大实际作用的测试点(实际测试过程中,没人给你勇气提啊啊 ...
- Spring Data Jpa 实体类自动创建数据库表失败解决
先说一下我遇到的这个问题,首先我是通过maven创建了一个spring boot的工程,引入了Spring data jpa,结果实体类创建好之后,运行工程却没有在数据库中自动创建数据表. 找了半天发 ...
- 基于 Spring Boot 的 Restful 风格实现增删改查
前言 在去年的时候,在各种渠道中略微的了解了SpringBoot,在开发web项目的时候是如何的方便.快捷.但是当时并没有认真的去学习下,毕竟感觉自己在Struts和SpringMVC都用得不太熟练. ...
- springboot整合JPA+MYSQL+queryDSL数据增删改查
Spring Boot Jpa 是 Spring 基于 ORM 框架.Jpa 规范的基础上封装的一套 Jpa 应用框架,可使开发者用极简的代码即可实现对数据的访问和操作.它提供了包括增删改查等在内的常 ...
最新文章
- 特征工程包含那些步骤?如何进行特征的迭代?
- numpy 点乘_Numpy入门指南
- 项目分享三:页面之间的传值
- [UE4]把工程升级到最新版本
- c# winform笔记
- 03、MySQL—数据表操作
- 全局搜索快捷键_Windows 自带的聚合搜索来了,与 Mac 的 Spotlight 相比体验如何?...
- C# 10 新特性 —— 命名空间的变化
- Web应用中request获取各种获取path或URI,URL的方法
- 赋能零售成长型企业营销增长,云徙「数盈·新营销中台」发布
- 递推算法之滚动数组思维方式
- MySQL 创建用户
- 信息学奥赛一本通(2068:【例2.6】鸡兔同笼)
- RabbitMQ-AMQP术语介绍
- python gridsearch_Python超参数自动搜索模块GridSearchCV上手
- echarts grid的样式位置_CSS Grid 系列(下)-使用Grid布局构建网站首页
- 【Netty】Netty之Bootstrapping
- XMLDictionary 解析的使用
- miui android mmi,小米5miui9稳定版自改官包方法
- 中国所有省市区的ip经纬度接口