Springboot jpa 本人亲测 附有代码

代码

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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当在Java Spring Boot项目中使用JPA时,你可以通过使用Spring Cache来增加和删除缓存。下面是一些示例代码来演示如何在Spring Boot项目中实现缓存的增加和删除: 首先,在你的实体类上使用`@Cacheable`注解来启用缓存,指定缓存的名称: ```java @Entity @Cacheable("usersCache") // 指定缓存名称为"usersCache" public class User { // 实体类的属性和方法 } ``` 接下来,在你的服务类(Service)中使用`@CachePut`注解来增加或更新缓存数据: ```java @Service public class UserService { @Autowired private UserRepository userRepository; @CachePut(value = "usersCache", key = "#user.id") // 将新增或更新的数据放入缓存 public User saveUser(User user) { // 保存用户到数据库 return userRepository.save(user); } } ``` 在上述示例中,`saveUser`方法使用了`@CachePut`注解,它会将新增或更新的数据放入指定名称为"usersCache"的缓存中。`key`属性指定了缓存键的表达式,这里使用了`user.id`作为键。 最后,如果需要删除缓存中的数据,可以使用`@CacheEvict`注解: ```java @Service public class UserService { @Autowired private UserRepository userRepository; @CacheEvict(value = "usersCache", key = "#id") // 根据ID删除缓存中的数据 public void deleteUser(Long id) { // 从数据库中删除用户 userRepository.deleteById(id); } } ``` 在上述示例中,`deleteUser`方法使用了`@CacheEvict`注解,它会根据指定的缓存名称和键删除缓存中的数据。 需要注意的是,使用`@CachePut`和`@CacheEvict`注解时,要确保方法执行成功,否则缓存的状态可能与数据库不一致。 这只是一个简单的示例,你可以根据自己的需求和业务逻辑进行更复杂的缓存操作。希望对你有所帮助!如果还有其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值