Spring Date JPA实现增删改查
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实现增删改查的更多相关文章
- Spring Boot(十五):spring boot+jpa+thymeleaf增删改查示例
Spring Boot(十五):spring boot+jpa+thymeleaf增删改查示例 一.快速上手 1,配置文件 (1)pom包配置 pom包里面添加jpa和thymeleaf的相关包引用 ...
- (转)Spring Boot (十五): Spring Boot + Jpa + Thymeleaf 增删改查示例
http://www.ityouknow.com/springboot/2017/09/23/spring-boot-jpa-thymeleaf-curd.html 这篇文章介绍如何使用 Jpa 和 ...
- spring boot2+jpa+thymeleaf增删改查例子
参考这遍文章做了一个例子,稍微不同之处,原文是spring boot.mysql,这里改成了spring boot 2.Oracle. 一.pom.xml引入相关模块web.jpa.thymeleaf ...
- Spring Boot + Jpa + Thymeleaf 增删改查示例
快速上手 配置文件 pom 包配置 pom 包里面添加 Jpa 和 Thymeleaf 的相关包引用 <dependency> <groupId>org.springframe ...
- Spring Boot (十五): Spring Boot + Jpa + Thymeleaf 增删改查示例
这篇文章介绍如何使用 Jpa 和 Thymeleaf 做一个增删改查的示例. 先和大家聊聊我为什么喜欢写这种脚手架的项目,在我学习一门新技术的时候,总是想快速的搭建起一个 Demo 来试试它的效果,越 ...
- Spring Data JPA基本增删改查和JPQL查询(含完整代码和视频连接)
问题:SpringDataJPA怎么使用? 一.考察目标 主要考核SpringDataJPA的用法 二.题目分析 spring data jpa 的使用步骤(下面有具体实现细节) 1.创建maven工 ...
- Spring JPA实现增删改查
1. 创建一个Spring工程 2.配置application文件 spring.datasource.driver-class-name= com.mysql.cj.jdbc.Driver spri ...
- SpringBoot JPA + H2增删改查示例
下面的例子是基于SpringBoot JPA以及H2数据库来实现的,下面就开始搭建项目吧. 首先看下项目的整体结构: 具体操作步骤: 打开IDEA,创建一个新的Spring Initializr项目, ...
- SpringBoot JPA实现增删改查、分页、排序、事务操作等功能
今天给大家介绍一下SpringBoot中JPA的一些常用操作,例如:增删改查.分页.排序.事务操作等功能.下面先来介绍一下JPA中一些常用的查询操作: //And --- 等价于 SQL 中的 and ...
随机推荐
- Azure Monitor(二)Log Analytics
一,引言( 前情回顾) Azure Monitor 包括 Log Analytics 和 Application Insights,其提供的高级工具适用于收集和分析遥测数据,以便最大程度地提高云和本地 ...
- div嵌套引起的内层margin-top对外层div起作用
嵌套div中margin-top转移问题的解决办法在这两个浏览器中,有两个嵌套关系的div,如果外层div的父元素padding值为0,那么内层div的margin-top或者margin-botto ...
- JavaScript基础关于JSON(011)
JSON意即JavaScript Object Notation,是JavaScript里数据表示的通用格式,JSON数据格式很象JavaScript里的对象: {"name": ...
- linux下 解释 终端命令 ls -al或者ls -li 输出的信息
$ ls -al drwxr-xr-x. wjshan0808 wjshan0808 Sep : .cache $ ls -li ...
- docker容器化python服务部署(supervisor-gunicorn-flask)
docker容器化python服务部署(supervisor-gunicorn-flask) 本文系作者原创,转载请注明出处: https://www.cnblogs.com/further-furt ...
- Drozer简单使用
1.Drozer简介 Drozer是一款针对Android系统的安全测试框架.Drozer可以通过与dalivik VM交互.与其他应用程序的IPC端点交互.与底层操作系统的交互,来避免正处于开发 ...
- Spring Security 实战干货:图解Spring Security中的Servlet过滤器体系
1. 前言 我在Spring Security 实战干货:内置 Filter 全解析对Spring Security的内置过滤器进行了罗列,但是Spring Security真正的过滤器体系才是我们了 ...
- Spark 两种方法计算分组取Top N
Spark 分组取Top N运算 大数据处理中,对数据分组后,取TopN是非常常见的运算. 下面我们以一个例子来展示spark如何进行分组取Top的运算. 1.RDD方法分组取TopN from py ...
- 状压DP之集合选数
题目 [HNOI2012]集合选数 <集合论与图论>这门课程有一道作业题,要求同学们求出{1, 2, 3, 4, 5}的所有满足以 下条件的子集:若 x 在该子集中,则 2x 和 3x 不 ...
- day14 参数
目录 一.参数介绍 二.形参与实参的具体使用 2.1位置参数 2.2关键字参数 2.3关键字实参和位置实参混合使用时 2.4默认参数 2.5位置形参和默认形参混用 2.6 可变长度的参数(*与**用法 ...