在Spring Boot项目中,List
、Set
和Map
这些集合类的使用场景非常广泛。以下是一些具体的例子,展示了在Spring Boot应用中这些集合类型的使用情况:
1. List
List
在Spring Boot中经常用于:
-
存储有序集合:比如在从数据库检索记录时,通常会将它们存储在
List
中以保持顺序。例如,将查询结果封装到List<Entity>
中。@Service public class UserService { public List<User> findAllUsers() { return userMapper.findAll(); } }
-
配置属性:在
application.properties
或application.yml
中定义一系列的值,Spring Boot可以自动将它们映射到一个List
属性中。app: cors: allowed-origins: - http://localhost:3000 - http://localhost:4200
@Component @ConfigurationProperties(prefix = "app.cors") public class CorsProperties {