相关pom依赖:

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

  application.yml配置

spring:
  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true

自动建表相关代码

package com.lingerqi.springboot03.entity;

import lombok.Data;

import javax.persistence.*;

/**
 * @author xyls
 * @blog name & blog address 027@0030
 * @create  2019-11-30 16:42
 */
@Data
@Entity
@Table(name = "t_springboot_book_1")
public class Book {
    @Id
    @GeneratedValue
    private Integer bid;
    @Column(length = 100)
    private String bname;
    @Column
    private float price;

}

jpa值增删改查

 * 只要继承JpaRepository,通常所用的增删查改方法都有

 *  第一个参数:操作的实体类

 *  第二个参数:实体类对应数据表的主键

 */

public interface JpaDao extends JpaRepository<Book, Integer> {

}

controller

@Autowired
    private JpaDao jpaDao;

    @RequestMapping("/add")
    public String add(Book book){
        jpaDao.save(book);
        return "success";
    }

    @RequestMapping("/edit")
    public String edit(Book book){
        jpaDao.save(book);
        return "success";
    }

    @RequestMapping("/del")
    public String del(Book book){
        jpaDao.delete(book);
        return "success";
    }

    @RequestMapping("/getOne")
    public Book getOne(Integer bid){
//        会出现懒加载问题:org.hibernate.LazyInitializationException: could not initialize proxy - no Session
//        return jpaDao.getOne(bid);
        return jpaDao.findById(bid).get();
    }

    @RequestMapping("/getAll")
    public List<Book> getAll(){
        return jpaDao.findAll();
    }

  

 *  要使用高级查询必须继承
 * org.springframework.data.jpa.repository.JpaSpecificationExecutor<T>接口
 */
public interface JpaDao extends JpaRepository<Book, Integer>, JpaSpecificationExecutor<Book> {
}

  

 新增:

01-06 13:50