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);
}
}
问题
在 NoteServiceImpl
中 noteRepository.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自动发布
网友评论