Springboot jpa 本人亲测 附有代码

简介: 版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_34173549/article/details/80887488 代码...
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_34173549/article/details/80887488

代码

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的方法

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();
    }

}

相关文章
|
1月前
|
NoSQL Java Redis
SpringBoot集成Redis解决表单重复提交接口幂等(亲测可用)
SpringBoot集成Redis解决表单重复提交接口幂等(亲测可用)
376 0
|
7月前
|
Java 数据库连接
27SpringBoot之JDBC(完整代码)
27SpringBoot之JDBC(完整代码)
53 0
|
7月前
|
SQL Java 数据库连接
26SpringBoot之JDBC(关键代码)
26SpringBoot之JDBC(关键代码)
49 0
|
8月前
|
Java 应用服务中间件 Maven
解析Spring Boot中的Profile:配置文件与代码的双重掌控
解析Spring Boot中的Profile:配置文件与代码的双重掌控
|
6月前
|
Java 关系型数据库 MySQL
基于springboot的问卷调查管理系统(核心代码文档)。Javaee项目,springboot项目。
基于springboot的问卷调查管理系统(核心代码文档)。Javaee项目,springboot项目。
|
1月前
|
数据库
Springboot+mybatis-plus逆向工程生成代码器
Springboot+mybatis-plus逆向工程生成代码器
|
1月前
|
存储 前端开发 Java
基于SpringBoot实现文件上传和下载(详细讲解And附完整代码)
基于SpringBoot实现文件上传和下载(详细讲解And附完整代码)
|
2月前
|
存储 JavaScript 前端开发
基于SpringBoot的医护人员排班系统(代码+数据库+文档)
基于SpringBoot的医护人员排班系统(代码+数据库+文档)
|
2月前
|
前端开发 Java 数据库连接
基于SpringBoot宠物领养系统的设计与实现(代码+数据库+文档)
基于SpringBoot宠物领养系统的设计与实现(代码+数据库+文档)
|
2月前
|
存储 Java 数据库
基于springboot的医院信息管理系统(程序+代码+文档)
基于springboot的医院信息管理系统(程序+代码+文档)