Java 显示数据库MySQL中保存的消息
我创建了一个关于spring boot的聊天。几个人可以通信的地方。因此,我创建了一个数据库,并在那里存储来自用户的所有消息。所以我想,如果一个新用户进入聊天室,那么他应该只看到最后10条消息。问题是程序没有从数据库获取最后10条消息,而是从服务器获取,这是不正确的。我要他从数据库中提取最后10条信息 我的代码 休息控制器Java 显示数据库MySQL中保存的消息,java,mysql,spring,spring-boot,vaadin,Java,Mysql,Spring,Spring Boot,Vaadin,我创建了一个关于spring boot的聊天。几个人可以通信的地方。因此,我创建了一个数据库,并在那里存储来自用户的所有消息。所以我想,如果一个新用户进入聊天室,那么他应该只看到最后10条消息。问题是程序没有从数据库获取最后10条消息,而是从服务器获取,这是不正确的。我要他从数据库中提取最后10条信息 我的代码 休息控制器 @SpringComponent @org.springframework.web.bind.annotation.RestController public class R
@SpringComponent
@org.springframework.web.bind.annotation.RestController
public class RestController {
private List<Message> store;
public RestController() {
store = new ArrayList<>();
}
@PutMapping("/api/save")
public void saveMessage(@RequestBody String chatMessage) {
store.add(new Gson().fromJson(chatMessage, Message.class));
if (store.size() > 10)
store.remove(0);
}
@GetMapping("/api/last")
public String getLasts() {
return new Gson().toJson(store);
}
}
server.port=8080
# This is a workaround for https://github.com/vaadin/spring/issues/381
spring.servlet.multipart.enabled = false
spring.datasource.url=jdbc:mysql://localhost:3306/chat?createDatabaseIfNotExist=true&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=123456
spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
消息存储库
@Repository
public interface MessageRepository extends JpaRepository<Message, Long> {
}
应用程序控制器
@SpringComponent
@org.springframework.web.bind.annotation.RestController
public class RestController {
private List<Message> store;
public RestController() {
store = new ArrayList<>();
}
@PutMapping("/api/save")
public void saveMessage(@RequestBody String chatMessage) {
store.add(new Gson().fromJson(chatMessage, Message.class));
if (store.size() > 10)
store.remove(0);
}
@GetMapping("/api/last")
public String getLasts() {
return new Gson().toJson(store);
}
}
server.port=8080
# This is a workaround for https://github.com/vaadin/spring/issues/381
spring.servlet.multipart.enabled = false
spring.datasource.url=jdbc:mysql://localhost:3306/chat?createDatabaseIfNotExist=true&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=123456
spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
坦率地说,我不理解在控制器中存储对象的目的。如果要查询最后10条消息,只需在存储库中实现一个方法并在控制器中调用它即可 MessageRepository
@Repository
public interface MessageRepository extends JpaRepository<Message, Long> {
List<Message> findTop10ByOrderByTimeDesc();
}
@存储库
公共接口MessageRepository扩展了JpaRepository{
列出findTop10ByOrderByTimeDesc();
}
尝试在RestController.getLasts()中获取最后10条消息,而不是整个存储对象。您可以携带代码。您可以使用Page和Pageable,因此添加api@ApiParam Pageable Pageable,您的存储库应该是公共接口MessageRepository extensed JpaRepository,JpaSpecificationExecutor{}只需在rest页面Page=pkoCategoryRequirementRepository.findAll(可分页)中给他打电话;getLasts(){return new Gson().toJson(store.subList(Math.max(store.size()-10,0),store.size());}这是不正确的((
server.port=8080
# This is a workaround for https://github.com/vaadin/spring/issues/381
spring.servlet.multipart.enabled = false
spring.datasource.url=jdbc:mysql://localhost:3306/chat?createDatabaseIfNotExist=true&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=123456
spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
@Repository
public interface MessageRepository extends JpaRepository<Message, Long> {
List<Message> findTop10ByOrderByTimeDesc();
}