目录
注:使用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);
}
}