springboot jpaRepository为何一定要对Entity序列化

 更新时间:2021年12月06日 14:55:43   作者:Interesting_Talent  
这篇文章主要介绍了springboot jpaRepository为何一定要对Entity序列化,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

springboot jpaRepository对Entity序列化

1. 问题

一开始,我没有对实体类Inventory序列化,导致在使用内嵌数据库H2的JPA时,它直接安装字母序列把表Inventory的字段生成。

举例,原来我按照

inventory(id, name, quantity, type, comment)

顺序写的数据库导入表,但是因为没有序列化,导致表结构变成

inventory(id, comment,name, quantity, type )

所以后面JPA处理失败。

2. 写个基本的JpaRepository的使用

顺便记录一下写spring cloud 基于和H2 database的jpa简单restful 程序。

实体类Inventory

package com.example.demo; 
import java.io.Serializable; 
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
 
@Entity
public class Inventory implements Serializable{ 
    private static final long serialVersionUID = 1L; 
    @Id
    @SequenceGenerator(name="inventory_generator", sequenceName="inventory_sequence", initialValue = 2)
    @GeneratedValue(generator = "inventory_generator")
    private Integer id;
    @Column(nullable = false)
    private String name;
    @Column(nullable = false)
    private Integer quantity;
    @Column(nullable = false)
    private Integer type;
    @Column(nullable = false)
    private String comment;
    public Inventory(Integer id, String name, Integer quantity, Integer type, String comment) {
        super();
        this.id = id;
        this.name = name;
        this.quantity = quantity;
        this.type = type;
        this.comment = comment;
    }
    public Inventory() {
        super();
    }
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getQuantity() {
        return quantity;
    }
    public void setQuantity(Integer quantity) {
        this.quantity = quantity;
    }
    public Integer getType() {
        return type;
    }
    public void setType(Integer type) {
        this.type = type;
    }
    public String getComment() {
        return comment;
    }
    public void setComment(String comment) {
        this.comment = comment;
    }
    @Override
    public String toString() {
        return "Inventory [id=" + id + ", name=" + name + ", quantity=" + quantity + ", type=" + type + ", comment="
                + comment + "]";
    }
}

下面使用JpaRepository简化开发流程,非常舒服地定义简单的service 接口即可,会自动实现,大赞。

package com.example.demo; 
import org.springframework.data.jpa.repository.JpaRepository; 
public interface InventoryRepository extends JpaRepository<Inventory, Integer> { 
    Inventory findById(Integer id); 
}

我把controller方法放到了springboot启动类里面,这又是一个大问题,因为我的项目只有放在这才能被dispatcher转发,简直了。

这里的@EnableDiscoveryClient 是因为我在做spring cloud的eureka 服务发现,需要这个注解让注册中心发现这个服务。

package com.example.demo; 
import java.time.LocalTime; 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
 
@SpringBootApplication
@EnableDiscoveryClient
@RestController
public class InventoryApplication { 
    public static void main(String[] args) {
        SpringApplication.run(InventoryApplication.class, args);
    }
    @Autowired
    private InventoryRepository inventoryRepository;
 
    @Value("${server.port}")
    private Integer port; 
    @RequestMapping("/info")
    public String info(){
        inventoryRepository.save(new Inventory(1, "火锅底料", 10000, 1, "你吃火锅,我吃底料"));
        inventoryRepository.save(new Inventory(2, "微服务架构", 100, 2, "微服务还是要考虑 一波"));
        return "{Inventory[port:"+port+", info:库存微服务"+"]}";
    }
 
    @GetMapping("/get/{id}")
    @ResponseBody
    @Transactional
    public String getById(@PathVariable("id")Integer id){
        return inventoryRepository.findById(id).toString();
    }
 
    @GetMapping("/")
    @ResponseBody
    @Transactional
    public String re(){
        return inventoryRepository.findAll().toString();
    }
 
    @GetMapping("/delete/{id}")
    @ResponseBody
    @Transactional
    public String delete(@PathVariable("id")Integer id){
        inventoryRepository.delete(id);
        return "delete successfully";
    }
 
    @GetMapping("/save/id={id}&name={name}&quantity={quantity}&type={type}&comment={comment}")
/*  @ResponseBody
    @Transactional*/
    public String save(@PathVariable("id")Integer id,@PathVariable("name")String name,
            @PathVariable("quantity")Integer quantity,@PathVariable("type")Integer type,
            @PathVariable("comment")String comment){
        inventoryRepository.save(new Inventory(id,name,quantity,type,comment));
        System.out.println(new Inventory(id,name,quantity,type,comment));
        //强调一下identity和auto
        return "save successfully";
    }
 
    @GetMapping("/update/id={id}&name={name}&quantity={quantity}&type={type}&comment={comment}")
    @ResponseBody
    @Transactional
    public String update(@PathVariable("id")Integer id,@PathVariable("name")String name,
            @PathVariable("quantity")Integer quantity,@PathVariable("type")Integer type,
            @PathVariable("comment")String comment){
        Inventory inventory=inventoryRepository.findById(id);
        if(inventory.getComment().length()<LocalTime.now().toString().length()){
            inventory.setComment(inventory.getComment()+LocalTime.now());
        }else{
            inventory.setComment(inventory.getComment().substring(0,inventory.getComment().length()-
                    LocalTime.now().toString().length())+LocalTime.now());
        }
        inventoryRepository.save(inventory);
        inventoryRepository.flush();
        return "update successfully";
    }
}

application.properties的配置很关键,搜了不少教程。

spring.jpa.show-sql=true
logging.pattern.level=trace
server.port=8765
spring.application.name=inventory
server.tomcat.max-threads=1000
eureka.instance.leaseRenewalIntervalInSeconds= 10
eureka.client.registryFetchIntervalSeconds= 5
eureka.client.serviceUrl.defaultZone=http://localhost:8889/eureka
#eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${eureka.instance.server.port}/eureka
 
#spring.thymeleaf.prefix=classpath:/templates/  
#spring.thymeleaf.suffix=.html  
#spring.thymeleaf.mode=HTML5  
#spring.thymeleaf.encoding=UTF-8  
# ;charset=<encoding> is added  
#spring.thymeleaf.content-type=text/html  
# set to false for hot refresh  
spring.h2.console.enabled=true
spring.thymeleaf.cache=false  
spring.jpa.hibernate.ddl-auto=update
#这里是把h2持久化到本地文件夹,这可以保持数据
spring.datasource.url=jdbc:h2:file:C\:/h2/h2cache;AUTO_SERVER=TRUE;DB_CLOSE_DELAY=-1
logging.file=c\:/h2/logging.log
logging.level.org.hibernate=debug
#spring.datasource.data=classpath:import.sql

数据库是自动导入的,只要命名方式是import.sql, 放在src/main/resources下面就可以

insert into inventory(id, name, quantity, type, comment) values (1, "火锅底料", 10000, 1, "你吃火锅,我吃底料")
insert into inventory(id, name, quantity, type, comment) values (2, "微服务架构", 100, 2, "微服务还是要考虑 一波")

最后一个简单的测试

junit测试一波

package com.example.demo; 
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner; 
@RunWith(SpringRunner.class)
@SpringBootTest
public class InventoryApplicationTests {
 
    @Autowired
    private InventoryRepository inventoriRepository;
 
    @Test
    public void test2() {
        System.out.println(inventoriRepository.findAll());
    } 
}

这里写图片描述

上图是项目结构图

springboot 使用JpaRepository

在对数据库操作时使用 MissionInfoRepository,对应的实体类必须用下面两个注解修饰

@Entity
@Table(name = "mission_info")

主键用下面修饰

 @Id
 @GeneratedValue(strategy = IDENTITY)
 @Column(name = "id", nullable = false)

在这里插入图片描述

属性命名如果使用驼峰,例如missionId,表中字段对应mission_id,否则对数据库操作的时候会报错,所以在表字段定义的时候,方便代码里使用JPA的话,表字段定义多个词之间用“_”隔开,而不是驼峰定义表字段。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • SpringCache快速使用及入门案例

    SpringCache快速使用及入门案例

    Spring Cache 是Spring 提供的一整套的缓存解决方案,它不是具体的缓存实现,本文主要介绍了SpringCache快速使用及入门案例,感兴趣的可以了解一下
    2023-08-08
  • Servlet第一个项目的发布(入门)

    Servlet第一个项目的发布(入门)

    这篇文章主要介绍了Servlet第一个项目的发布,下面是用servlet实现的一个简单的web项目,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2021-04-04
  • 深入解析@InitBinder注解的功能与应用

    深入解析@InitBinder注解的功能与应用

    这篇文章主要介绍了深入解析@InitBinder注解的功能与应用,从字面意思可以看出这个的作用是给Binder做初始化的,被此注解的方法可以对WebDataBinder初始化,webDataBinder是用于表单到方法的数据绑定的,需要的朋友可以参考下
    2023-10-10
  • Java如何实现验证码验证功能

    Java如何实现验证码验证功能

    这篇文章主要教大家如何实现Java验证码验证功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-02-02
  • Java对象序列化操作详解

    Java对象序列化操作详解

    这篇文章主要介绍了Java对象序列化操作,简单描述了Java序列化相关概念、原理并结合实例形式总结分析了常见序列化操作相关定于与使用技巧,需要的朋友可以参考下
    2018-09-09
  • 30分钟入门Java8之方法引用学习

    30分钟入门Java8之方法引用学习

    在Java8中,我们可以直接通过方法引用来简写lambda表达式中已经存在的方法,这篇文章主要介绍了30分钟入门Java8之方法引用学习,有兴趣可以了解一下。
    2017-04-04
  • SpringBoot MongoDB详细使用教程

    SpringBoot MongoDB详细使用教程

    这篇文章主要介绍了SpringBoot整合Mongodb实现简单的增删查改,MongoDB是一个以分布式数据库为核心的数据库,因此高可用性、横向扩展和地理分布是内置的,并且易于使用。况且,MongoDB是免费的,开源的,感兴趣的朋友跟随小编一起看看吧
    2022-10-10
  • Java分形绘制山脉模型

    Java分形绘制山脉模型

    这篇文章主要为大家详细介绍了Java分形绘制山脉模型,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-01-01
  • SpringBoot整合Mybatis-plus关键词模糊查询结果为空

    SpringBoot整合Mybatis-plus关键词模糊查询结果为空

    SpringBoot整合Mybatis-plus使用关键词模糊查询的时候,数据库中有数据,但是无法查找出来,本文就来介绍一下SpringBoot整合Mybatis-plus关键词模糊查询结果为空的解决方法
    2025-04-04
  • J2SE基础之下载eclipse并创建项目

    J2SE基础之下载eclipse并创建项目

    本文给大家介绍的是最流行的java 集成开发环境IDE eclipse的使用方法,非常的简单,有需要的小伙伴可以参考下
    2016-05-05

最新评论