1.新建一个Cart类

package com.entity;

public class Cart {
private int id;
private int userId;
private int medicineId;
private int count;
private int price;
private String medicineName;
private String medicineSize;
private int status;
}

2.添加注解

@Entity //表示是一个实体类
@Table(name = "tbl_cart") //指定数据库中对应的表
public class Cart { @Id //指定主键
@GeneratedValue(strategy = GenerationType.IDENTITY) //自增
private int id;
private int userId;
private int medicineId;
private int count;
private int price;
private String medicineName;
private String medicineSize;
private int status;
}

后面添加get、set方法和toString方法

3.创建实体类的CartRepository接口

package com.Dao;

import com.entity.Cart;
import org.springframework.data.jpa.repository.JpaRepository; public interface CartRepository extends JpaRepository<Cart,Integer> {
}

接口继承JpaRepository<操作的实体类,主键类型>

接口默认实现了增删改查等一些简单的操作,其他操作可以参考Spring Data JPA根据属性名查询在接口类中直接创建。

4.创建CartService类

创建CartService类然后添加service层注解,然后在类中实现各种方法。

package com.service;

import com.Dao.CartRepository;
import com.entity.Cart;
import org.springframework.stereotype.Service; import javax.annotation.Resource;
import java.util.List; @Service
public class CartService { @Resource
CartRepository cartRepository; //查询全部
public List<Cart> findAll(){
return this.cartRepository.findAll();
}
}

5.创建CartController

RestController只能返回String类型的数据

package com.controller;

import com.entity.Cart;
import com.google.gson.Gson;
import com.service.CartService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource;
import java.util.List; @RestController
@RequestMapping("/cart")
public class CartController { @Resource
CartService cartService;
Gson gson = new Gson(); //获取所有购物车信息
@RequestMapping(value = "/list")
public String list()
{
List<Cart> cartList = this.cartService.findAll();
String result = gson.toJson(cartList);
return result;
}
}

6.通过post或者get请求访问服务器

http://localhost:8070/onepill/cart/list

参考:JPA概述

Spring Date JPA实现增删改查的更多相关文章

  1. Spring Boot(十五):spring boot+jpa+thymeleaf增删改查示例

    Spring Boot(十五):spring boot+jpa+thymeleaf增删改查示例 一.快速上手 1,配置文件 (1)pom包配置 pom包里面添加jpa和thymeleaf的相关包引用 ...

  2. (转)Spring Boot (十五): Spring Boot + Jpa + Thymeleaf 增删改查示例

    http://www.ityouknow.com/springboot/2017/09/23/spring-boot-jpa-thymeleaf-curd.html 这篇文章介绍如何使用 Jpa 和 ...

  3. spring boot2+jpa+thymeleaf增删改查例子

    参考这遍文章做了一个例子,稍微不同之处,原文是spring boot.mysql,这里改成了spring boot 2.Oracle. 一.pom.xml引入相关模块web.jpa.thymeleaf ...

  4. Spring Boot + Jpa + Thymeleaf 增删改查示例

    快速上手 配置文件 pom 包配置 pom 包里面添加 Jpa 和 Thymeleaf 的相关包引用 <dependency> <groupId>org.springframe ...

  5. Spring Boot (十五): Spring Boot + Jpa + Thymeleaf 增删改查示例

    这篇文章介绍如何使用 Jpa 和 Thymeleaf 做一个增删改查的示例. 先和大家聊聊我为什么喜欢写这种脚手架的项目,在我学习一门新技术的时候,总是想快速的搭建起一个 Demo 来试试它的效果,越 ...

  6. Spring Data JPA基本增删改查和JPQL查询(含完整代码和视频连接)

    问题:SpringDataJPA怎么使用? 一.考察目标 主要考核SpringDataJPA的用法 二.题目分析 spring data jpa 的使用步骤(下面有具体实现细节) 1.创建maven工 ...

  7. Spring JPA实现增删改查

    1. 创建一个Spring工程 2.配置application文件 spring.datasource.driver-class-name= com.mysql.cj.jdbc.Driver spri ...

  8. SpringBoot JPA + H2增删改查示例

    下面的例子是基于SpringBoot JPA以及H2数据库来实现的,下面就开始搭建项目吧. 首先看下项目的整体结构: 具体操作步骤: 打开IDEA,创建一个新的Spring Initializr项目, ...

  9. SpringBoot JPA实现增删改查、分页、排序、事务操作等功能

    今天给大家介绍一下SpringBoot中JPA的一些常用操作,例如:增删改查.分页.排序.事务操作等功能.下面先来介绍一下JPA中一些常用的查询操作: //And --- 等价于 SQL 中的 and ...

随机推荐

  1. JavaScript基础函数的声明(014)

    1.函数的重要地位 函数(functions)在JavaScript里有着重要的地位,其原因有二: 它们是一种特殊的对象 它们提供作用域 说函数在JavaScript里是特殊的对象,因为: 程序的执行 ...

  2. 高速缓存一致性协议MESI与内存屏障

    一.CPU高速缓存简单介绍 CPU高速缓存机制的引入,主要是为了解决CPU越来越快的运行速度与相对较慢的主存访问速度的矛盾.CPU中的寄存器数量有限,在执行内存寻址指令时,经常需要从内存中读取指令所需 ...

  3. tkinter的三种几何布局管理类

    1.pack() 主要采用块的方式组织子组件 如下: import tkinter root=tkinter.Tk() #创建窗口对象 label=tkinter.Label(root,text='h ...

  4. SpringCloud系列之集成分布式事务Seata应用篇

    目录 前言 项目版本 项目说明 Seata服务端部署 Seata客户端集成 cloud-web module-order module-cart module-goods module-wallet ...

  5. HttpContext, HttpContextBase, HttpContextWrapper之间关系

    HttpContext是最原始的ASP.NET Context. MVC的目的之一是能够单元测试. HttpContextBase, 是用来在MVC中替代HttpContext.但是这是一个abstr ...

  6. 使用Splunk监控SAP Dump

    最近在尝试使用Splunk对SAP系统进行监控,以Dump监控为例,总结了一点相关信息,记录在这里. 本文链接:https://www.cnblogs.com/hhelibeb/p/13260385. ...

  7. PE文件动态加载执行过程

    主要步骤: 1.将要加载的文件读取到内存中(简称为文内),检查文件格式无误后,根据可选PE头(简称op头)的SizeOfImage,申请出一块空间用于存储该文件加载到内存后展开的数据(简称为内内).记 ...

  8. scala 数据结构(十):折叠、扫描、拉链(合并)、迭代器

    1 折叠 fold函数将上一步返回的值作为函数的第一个参数继续传递参与运算,直到list中的所有元素被遍历. 1)可以把reduceLeft看做简化版的foldLeft. 如何理解: def redu ...

  9. 数据可视化之PowerQuery篇(一)空值(null)运算的的解决思路

    https://zhuanlan.zhihu.com/p/81535007 星友们在知识星球(PowerBI星球)提出的问题中,关于空值的运算经常被提及.平时接触到的源数据常常有空值,比如Excel数 ...

  10. C#文件说明

    Bin -- 用来存放编译的结果,是默认的输出路径,项目属性—>配置属性—>输出路径. obj -- 用于存放编译过程中生成的中间临时文件.增量编译:项目属性—>配置属性—>高 ...