springboot框架访问的是微信云数据库用户信息集合test001代码实现 ControllerService DAO的连接将数据展示在layui网页上
首先,你需要在Spring Boot项目中引入相关依赖,包括Spring Web、Spring Data MongoDB和Lombok等,可以在pom.xml中添加如下依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
接下来,创建用户信息实体类User.java:
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
@Data
@Document(collection = "test001")
public class User {
@Id
private String id;
private String name;
private String email;
// 其他字段...
}
然后,创建用户信息的DAO层接口UserRepository.java:
import org.springframework.data.mongodb.repository.MongoRepository;
public interface UserRepository extends MongoRepository<User, String> {
}
接着,创建用户信息的Service层UserService.java:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserService {
private final UserRepository userRepository;
@Autowired
public UserService(UserRepository userRepository) {
this.userRepository = userRepository;
}
public List<User> getAllUsers() {
return userRepository.findAll();
}
}
最后,创建用户信息的Controller层UserController.java:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/users")
public class UserController {
private final UserService userService;
@Autowired
public UserController(UserService userService) {
this.userService = userService;
}
@GetMapping
public List<User> getAllUsers() {
return userService.getAllUsers();
}
}
以上是一个简单的示例,通过UserController中的/users接口可以获取所有用户信息。你可以根据需要进一步完善代码,比如增加分页查询、增加新增用户、修改用户、删除用户等操作,并在Layui网页中通过Ajax请求获取数据并展示
原文地址: https://www.cveoy.top/t/topic/iFZW 著作权归作者所有。请勿转载和采集!