springboot教程第二课_JPA、Mysql实现增删改查

目录

配置DataSource

添加Model

添加Repository

添加Service

添加Restfull接口

启动测试


注:使用docker-compose一键安装mysql安装链接


去掉排除校验数据源的注解,在dev对应的yml文件中设置开发环境的数据源信息:

server:
  servlet:
    context-path: /demo
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/bootdemo
    username: root
    password:
  jpa:
    hibernate:
      ddl-auto: update

添加Order对应的model

/**
 * Copyright 2020 michelin.com.cn All right reserved. This software is the
 * confidential and proprietary information of michelin.com.cn ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with michelin.com.cn.
 */
package org.demo.springboot.model;

import lombok.Data;
import org.hibernate.annotations.GenericGenerator;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

/**
 * 描    述:class description.
 * <p>
 * Copyright: Copyright (c) 2020
 *
 * @author 葛东升
 * @version 1.0    6/30/21
 * @see HISTORY 6/30/21 葛东升 创建文件
 */
@Entity
@Table(name = "t_order")
@Data
public class Order {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public Long id;
    public String name;
    public Double price;
}

添加jpa对应的Repository

package org.demo.springboot.repository;

import org.demo.springboot.model.Order;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.CrudRepository;

/**
 * 描    述:class description.
 * <p>
 * Copyright: Copyright (c) 2020
 *
 * @author 葛东升
 * @version 1.0    6/30/21
 * @see HISTORY 6/30/21 葛东升 创建文件
 */
public interface OrderRepository extends JpaRepository<Order,Long>, CrudRepository<Order,Long> {
}

添加Service(可选)

添加restfull接口

package org.demo.springboot.order;

import org.demo.springboot.model.Order;
import org.demo.springboot.repository.OrderRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 描    述:class description.
 * <p>
 * Copyright: Copyright (c) 2020
 *
 * @author 葛东升
 * @version 1.0    6/29/21
 * @see HISTORY 6/29/21 葛东升 创建文件
 */
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderRepository orderRepository;

    @GetMapping("/queryAll")
    public List<Order> queryAll(){
//        List<HashMap<String, String>> res = new ArrayList<>();
//        HashMap<String, String> r = new HashMap<>();
//        r.put("1","hello");
//        res.add(r);
        List<Order> res = orderRepository.findAll();

        return res;
    }

    @GetMapping("/addOne")
    public void addOne(@RequestParam("name") String name,@RequestParam("price") Double price){

        Order order = new Order();
        order.setName(name);
        order.setPrice(price);
        orderRepository.saveAndFlush(order);

    }
}

启动测试

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值