代码

Springboot + jpa  上面是代码

配置

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/demo
    username: root
    password: root
    driver-class-name: com.mysql.jdbc.Driver
  jpa: # jpa 配置
    database: mysql
    show-sql: true
    hibernate:
      ddl-auto: update
  devtools: # 热部署配置
    restart:
      enabled: true

实体类

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

/**
 *
 * @author rainyday
 * @createTime 2018/7/2.
 */
@Entity
public class Cat {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;

    private String catName;

    private int catAge;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getCatName() {
        return catName;
    }

    public void setCatName(String catName) {
        this.catName = catName;
    }

    public int getCatAge() {
        return catAge;
    }

    public void setCatAge(int catAge) {
        this.catAge = catAge;
    }
}

继承

CrudRepository

可以直接调用 jpa的方法

Springboot jpa 本人亲测 附有代码

import org.springframework.data.repository.CrudRepository;
import springboot.bean.Cat;


public interface CatRepository extends CrudRepository<Cat, Integer>{
}

Service层

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import springboot.bean.Cat;
import springboot.repository.CatRepository;

/**
 * @author rainyday
 * @createTime 2018/7/2.
 */
@Service
public class CatService {

    @Autowired
    private CatRepository catRepository;

    @Transactional
    public void save(Cat cat) {
        catRepository.save(cat);
    }

    @Transactional
    public void delete(int id) {
        catRepository.deleteById(id);
    }


    @Cacheable("getAll")
    public Iterable<Cat> getAll() {
        System.out.println("no use cache");
        return catRepository.findAll();
    }
}

控制层

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import springboot.bean.Cat;
import springboot.service.CatService;

/**
 * @author rainyday
 * @createTime 2018/7/2.
 */
@RestController
@RequestMapping("/cat")
public class CatController {

    @Autowired
    private CatService catService;

    @PostMapping("/save")
    public void save(@RequestParam(required = false) Cat  cat) {
        if (StringUtils.isEmpty(cat)) {
            cat = new Cat();
            cat.setCatName("jack");
        }
        cat.setCatAge(3);
        catService.save(cat);
    }

    @RequestMapping("delete")
    public String delete() {
        catService.delete(1);
        return "delete ok!";
    }

    public Iterable<Cat> getAll() {
        return catService.getAll();
    }

}

相关文章:

  • 2021-06-30
  • 2021-05-14
  • 2021-08-25
  • 2022-02-26
  • 2021-07-09
  • 2021-10-03
  • 2021-04-28
  • 2022-12-23
猜你喜欢
  • 2021-05-11
  • 2021-12-27
  • 2021-09-05
  • 2021-07-24
  • 2021-07-06
  • 2022-12-23
  • 2021-10-07
相关资源
相似解决方案