SpringBoot集成Jpa

SpringBoot集成Jpa

SpringBoot版本: 2.2.4

简单demo

在pom文件中添加依赖:

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

配置数据库连接

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/note?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=

添加实体

@Entity(name = "notebook")
@ToString
@Getter
@Setter
public class Note implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
    @Column(name = "title")
    private String title;
    @Column(name = "content")
    private String content;
}

添加repository

public interface NoteRepository extends JpaRepository<Note,Integer> {
}

添加service

public interface NoteService {

    Note getNote(Integer id);
}

service实现

@Service
public class NoteServiceImpl implements NoteService {

    @Autowired
    private NoteRepository noteRepository;

    @Override
    public Note getNote(Integer id) {
        return noteRepository.findById(id).get();
    }
}

添加Controller

@RestController
@RequestMapping("/note")
public class NoteController {

    @Autowired
    private NoteService noteService;

    @RequestMapping("/get")
    public Note getNote(Integer id){
        return noteService.getNote(id);
    }
}

问题

NoteServiceImplnoteRepository.findById(id).get();如果换成noteRepository.getOne(id)就会报下面的错

com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: com.springboot.jpa.model.Note$HibernateProxy$LH4iQSqZ["hibernateLazyInitializer"])

本篇文章由一文多发平台ArtiPub自动发布

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容