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. 隐藏input的三种方法和区别

    一.<input type="hidden" />二.<input type="text" style="display:none& ...

  2. 《UNIX环境高级编程》(APUE) 笔记第一章 - UNIX基础知识

    1 - UNIX基础知识 Github 地址 1. 操作系统 可将操作系统定义为一种软件,它控制计算机硬件资源,提供程序运行环境.通常将这种软件称为 内核 (kernel) .( Linux 是 GN ...

  3. java.sql.SQLException: The server time zone value '�й���׼ʱ��' is unrecognized

    idea数据库连接字符串需要添加一些参数: ?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai ...

  4. 控制shell终端提示符格式和颜色

    字体颜色值 (ASCII) 背景颜色值 (ASCII) 显示颜色 30 40 黑色 31 41 红色 32 42 绿色 33 43 黄色 34 44 蓝色 35 45 紫红色 36 46 青蓝色 37 ...

  5. WSL配置高翔vslam环境配置流水账

    1 安装参考博文链接:https://www.cnblogs.com/dalaska/p/12802384.html 2 Ubuntu 16.04地址:https://www.microsoft.co ...

  6. Spring设置启动时执行方法

    @PostConstruct方法 在实现类和方法上加注解,类上加bean注解,方法上加@PostConstruct注解. @PostConstruct//启动执行public void refresh ...

  7. (私人收藏)2019WER积木教育机器人赛(普及赛)解决方案-(全套)获取能源核心

    2019WER积木教育机器人赛(普及赛)解决方案-(全套)获取能源核心 含地图,解决程序,详细规则,搭建方案EV3;乐高;机器人比赛;能力风暴;WER https://pan.baidu.com/s/ ...

  8. Spring Cloud系列教程第九篇-Eureka自我保护机制

    Spring Cloud系列教程第九篇-Eureka自我保护机制 本文主要内容: 1:自我保护介绍 2:导致原因分析 3:怎么禁止自我保护 本文是由凯哥(凯哥Java:kagejava)发布的< ...

  9. Deno 学习笔记(1)安装及简单的request

    Deno下载和安装 PowerShell iwr https://deno.land/x/install/install.ps1 -useb | iex Shell curl -fsSL https: ...

  10. Redis批量查询模板

    场景 在开发的时候经常会遇到批量取缓存的问题,例如查询商品信息 传入一个商品Id列表,查询Redis数据存在则放入返回列表 不存在的数据查找数据库,并放入Redis 上面两步数据整合返回 伪代码为 l ...